diff --git a/auth/api/http/domains/endpoint_test.go b/auth/api/http/domains/endpoint_test.go index 9cf676fabc..f3d596f236 100644 --- a/auth/api/http/domains/endpoint_test.go +++ b/auth/api/http/domains/endpoint_test.go @@ -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{ diff --git a/auth/api/http/domains/requests.go b/auth/api/http/domains/requests.go index 3a878eded2..88b8894db5 100644 --- a/auth/api/http/domains/requests.go +++ b/auth/api/http/domains/requests.go @@ -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 { @@ -36,6 +36,11 @@ func (req createDomainReq) validate() error { if req.Name == "" { return apiutil.ErrMissingName } + + if req.Alias == "" { + return apiutil.ErrMissingAlias + } + return nil } diff --git a/auth/postgres/domains_test.go b/auth/postgres/domains_test.go index 9a63d4653b..0116683cbd 100644 --- a/auth/postgres/domains_test.go +++ b/auth/postgres/domains_test.go @@ -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{ diff --git a/auth/postgres/init.go b/auth/postgres/init.go index bae8674b36..ae69c3a0ca 100644 --- a/auth/postgres/init.go +++ b/auth/postgres/init.go @@ -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`, + }, + }, }, } } diff --git a/internal/api/common.go b/internal/api/common.go index 5181a6842a..e14af7e559 100644 --- a/internal/api/common.go +++ b/internal/api/common.go @@ -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), diff --git a/internal/apiutil/errors.go b/internal/apiutil/errors.go index c0376ff87a..7230857ff8 100644 --- a/internal/apiutil/errors.go +++ b/internal/apiutil/errors.go @@ -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)")