Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MYST-228 Permit empty identity password when creating identity #90

Merged
merged 1 commit into from Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions tequilapi/endpoints/identities.go
Expand Up @@ -21,7 +21,7 @@ type identityList struct {
}

type identityCreationDto struct {
Password string `json:"password"`
Password *string `json:"password"`
}

type identityRegistrationDto struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (endpoint *identitiesApi) Create(resp http.ResponseWriter, request *http.Re
utils.SendValidationErrorMessage(resp, errorMap)
return
}
id, err := endpoint.idm.CreateNewIdentity(createReq.Password)
id, err := endpoint.idm.CreateNewIdentity(*createReq.Password)
if err != nil {
utils.SendError(resp, err, http.StatusInternalServerError)
return
Expand Down Expand Up @@ -123,7 +123,7 @@ func validateRegistrationRequest(regReq identityRegistrationDto) (err error) {

func validateCreationRequest(createReq *identityCreationDto) (errors *validation.FieldErrorMap) {
errors = validation.NewErrorMap()
if len(createReq.Password) == 0 {
if createReq.Password == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if { "password" : "" } maybe it's better to check if len(createReq.Password) ==0 ? In this case we will handle both (not present) and (present but empty) cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit dangerous to allow making empty POST /identities, which returns you an account with empty password - api user might not realise that empty password was used!

Requiring to pass in {"password": ""} ensures that api user is aware that he is creating an account with empty password.

errors.ForField("password").AddError("required", "Field is required")
}
return
Expand Down
29 changes: 23 additions & 6 deletions tequilapi/endpoints/identities_test.go
Expand Up @@ -56,11 +56,27 @@ func TestRegisterIdentitySuccess(t *testing.T) {
assert.Equal(t, http.StatusAccepted, resp.Code)
}

func TestCreateNewIdentityEmptyPassword(t *testing.T) {
req, err := http.NewRequest(
http.MethodPost,
"/identities",
bytes.NewBufferString(`{"password": ""}`),
)

assert.Nil(t, err)

resp := httptest.NewRecorder()
handlerFunc := NewIdentitiesEndpoint(mockIdm, mystClient).Create
handlerFunc(resp, req, nil)

assert.Equal(t, http.StatusOK, resp.Code)
}

func TestCreateNewIdentityNoPassword(t *testing.T) {
req, err := http.NewRequest(
http.MethodPost,
"/identities",
bytes.NewBufferString(`{ "password": ""}`),
bytes.NewBufferString(`{}`),
)

assert.Nil(t, err)
Expand All @@ -69,14 +85,15 @@ func TestCreateNewIdentityNoPassword(t *testing.T) {
handlerFunc := NewIdentitiesEndpoint(mockIdm, mystClient).Create
handlerFunc(resp, req, nil)

assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.JSONEq(
t,
`{
"message": "validation_error",
"errors" : {
"password": [ {"code" : "required" , "message" : "Field is required" } ]
}
}`,
"message": "validation_error",
"errors" : {
"password": [ {"code" : "required" , "message" : "Field is required" } ]
}
}`,
resp.Body.String(),
)
}
Expand Down