Skip to content

Commit

Permalink
MG-2296 - Domain creation allowed with nil alias (absmach#2297)
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya authored Jun 20, 2024
1 parent 95badee commit ef1322b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
14 changes: 14 additions & 0 deletions auth/api/http/domains/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ func TestCreateDomain(t *testing.T) {
status: http.StatusBadRequest,
err: apiutil.ErrMissingName,
},
{
desc: "register a new domain with an empty alias",
domain: auth.Domain{
ID: ID,
Name: "test",
Metadata: mgclients.Metadata{"role": "domain"},
Tags: []string{"tag1", "tag2"},
Alias: "",
},
token: validToken,
contentType: contentType,
status: http.StatusBadRequest,
err: apiutil.ErrMissingAlias,
},
{
desc: "register a new domain with invalid content type",
domain: auth.Domain{
Expand Down
7 changes: 6 additions & 1 deletion auth/api/http/domains/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type createDomainReq struct {
Name string `json:"name"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Tags []string `json:"tags,omitempty"`
Alias string `json:"alias,omitempty"`
Alias string `json:"alias"`
}

func (req createDomainReq) validate() error {
Expand All @@ -36,6 +36,11 @@ func (req createDomainReq) validate() error {
if req.Name == "" {
return apiutil.ErrMissingName
}

if req.Alias == "" {
return apiutil.ErrMissingAlias
}

return nil
}

Expand Down
18 changes: 18 additions & 0 deletions auth/postgres/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ func TestSave(t *testing.T) {
},
err: nil,
},
{
desc: "add domain with empty alias",
domain: auth.Domain{
ID: testsutil.GenerateUUID(&testing.T{}),
Name: "test1",
Alias: "",
Tags: []string{"test"},
Metadata: map[string]interface{}{
"test": "test",
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedBy: userID,
UpdatedBy: userID,
Status: auth.EnabledStatus,
},
err: repoerr.ErrCreateEntity,
},
{
desc: "add domain with malformed metadata",
domain: auth.Domain{
Expand Down
6 changes: 6 additions & 0 deletions auth/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func Migration() *migrate.MemoryMigrationSource {
`DROP TABLE IF EXISTS keys`,
},
},
{
Id: "auth_2",
Up: []string{
`ALTER TABLE domains ALTER COLUMN alias SET NOT NULL`,
},
},
},
}
}
1 change: 1 addition & 0 deletions internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, errors.ErrMalformedEntity),
errors.Contains(err, apiutil.ErrMissingID),
errors.Contains(err, apiutil.ErrMissingName),
errors.Contains(err, apiutil.ErrMissingAlias),
errors.Contains(err, apiutil.ErrMissingEmail),
errors.Contains(err, apiutil.ErrMissingHost),
errors.Contains(err, apiutil.ErrInvalidResetPass),
Expand Down
3 changes: 3 additions & 0 deletions internal/apiutil/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ var (
// ErrMissingName indicates missing identity name.
ErrMissingName = errors.New("missing identity name")

// ErrMissingName indicates missing alias.
ErrMissingAlias = errors.New("missing alias")

// ErrInvalidLevel indicates an invalid group level.
ErrInvalidLevel = errors.New("invalid group level (should be between 0 and 5)")

Expand Down

0 comments on commit ef1322b

Please sign in to comment.