Skip to content

Commit

Permalink
changed the return result of the confirm api actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Nov 8, 2022
1 parent bc51923 commit 3d14add
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 77 deletions.
4 changes: 2 additions & 2 deletions apis/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func (api *adminApi) confirmPasswordReset(c echo.Context) error {
return NewBadRequestError("An error occurred while loading the submitted data.", readErr)
}

admin, submitErr := form.Submit()
_, submitErr := form.Submit()
if submitErr != nil {
return NewBadRequestError("Failed to set new password.", submitErr)
}

return api.authResponse(c, admin)
return c.NoContent(http.StatusNoContent)
}

func (api *adminApi) list(c echo.Context) error {
Expand Down
7 changes: 1 addition & 6 deletions apis/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,10 @@ func TestAdminConfirmPasswordReset(t *testing.T) {
"password":"1234567891",
"passwordConfirm":"1234567891"
}`),
ExpectedStatus: 200,
ExpectedContent: []string{
`"admin":{"id":"sywbhecnh46rhm0"`,
`"token":`,
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{
"OnModelBeforeUpdate": 1,
"OnModelAfterUpdate": 1,
"OnAdminAuthRequest": 1,
},
},
}
Expand Down
18 changes: 6 additions & 12 deletions apis/record_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,12 @@ func (api *recordAuthApi) confirmPasswordReset(c echo.Context) error {
return NewBadRequestError("An error occurred while loading the submitted data.", readErr)
}

record, submitErr := form.Submit()
_, submitErr := form.Submit()
if submitErr != nil {
return NewBadRequestError("Failed to set new password.", submitErr)
}

return api.authResponse(c, record, nil)
return c.NoContent(http.StatusNoContent)
}

func (api *recordAuthApi) requestVerification(c echo.Context) error {
Expand Down Expand Up @@ -350,18 +350,12 @@ func (api *recordAuthApi) confirmVerification(c echo.Context) error {
return NewBadRequestError("An error occurred while loading the submitted data.", readErr)
}

record, submitErr := form.Submit()
_, submitErr := form.Submit()
if submitErr != nil {
return NewBadRequestError("An error occurred while submitting the form.", submitErr)
}

// don't return an auth response if the collection doesn't allow email or username authentication
authOptions := collection.AuthOptions()
if !authOptions.AllowEmailAuth && !authOptions.AllowUsernameAuth {
return c.NoContent(http.StatusNoContent)
}

return api.authResponse(c, record, nil)
return c.NoContent(http.StatusNoContent)
}

func (api *recordAuthApi) requestEmailChange(c echo.Context) error {
Expand Down Expand Up @@ -393,12 +387,12 @@ func (api *recordAuthApi) confirmEmailChange(c echo.Context) error {
return NewBadRequestError("An error occurred while loading the submitted data.", readErr)
}

record, submitErr := form.Submit()
_, submitErr := form.Submit()
if submitErr != nil {
return NewBadRequestError("Failed to confirm email change.", submitErr)
}

return api.authResponse(c, record, nil)
return c.NoContent(http.StatusNoContent)
}

func (api *recordAuthApi) listExternalAuths(c echo.Context) error {
Expand Down
66 changes: 9 additions & 57 deletions apis/record_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,27 +458,14 @@ func TestRecordAuthConfirmPasswordReset(t *testing.T) {
{
Name: "valid token and data",
Method: http.MethodPost,
Url: "/api/collections/users/confirm-password-reset?expand=rel,missing",
Url: "/api/collections/users/confirm-password-reset",
Body: strings.NewReader(`{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsImNvbGxlY3Rpb25JZCI6Il9wYl91c2Vyc19hdXRoXyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiZXhwIjoyMjA4OTg1MjYxfQ.R_4FOSUHIuJQ5Crl3PpIPCXMsoHzuTaNlccpXg_3FOg",
"password":"12345678",
"passwordConfirm":"12345678"
}`),
ExpectedStatus: 200,
ExpectedContent: []string{
`"token":`,
`"record":`,
`"id":"4q1xlclmfloku33"`,
`"email":"[email protected]"`,
`"expand":`,
`"rel":`,
`"id":"llvuca81nly1qls"`,
},
NotExpectedContent: []string{
`"missing":`,
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{
"OnRecordAuthRequest": 1,
"OnModelAfterUpdate": 1,
"OnModelBeforeUpdate": 1,
},
Expand Down Expand Up @@ -634,57 +621,30 @@ func TestRecordAuthConfirmVerification(t *testing.T) {
{
Name: "valid token",
Method: http.MethodPost,
Url: "/api/collections/users/confirm-verification?expand=rel,missing",
Url: "/api/collections/users/confirm-verification",
Body: strings.NewReader(`{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsImNvbGxlY3Rpb25JZCI6Il9wYl91c2Vyc19hdXRoXyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiZXhwIjoyMjA4OTg1MjYxfQ.hL16TVmStHFdHLc4a860bRqJ3sFfzjv0_NRNzwsvsrc"
}`),
ExpectedStatus: 200,
ExpectedContent: []string{
`"token":`,
`"record":`,
`"id":"4q1xlclmfloku33"`,
`"email":"[email protected]"`,
`"verified":true`,
`"expand":`,
`"rel":`,
`"id":"llvuca81nly1qls"`,
},
NotExpectedContent: []string{
`"missing":`,
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{
"OnRecordAuthRequest": 1,
"OnModelAfterUpdate": 1,
"OnModelBeforeUpdate": 1,
},
},
{
Name: "valid token (already verified)",
Method: http.MethodPost,
Url: "/api/collections/users/confirm-verification?expand=rel,missing",
Url: "/api/collections/users/confirm-verification",
Body: strings.NewReader(`{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Im9hcDY0MGNvdDR5cnUycyIsImVtYWlsIjoidGVzdDJAZXhhbXBsZS5jb20iLCJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJ0eXBlIjoiYXV0aFJlY29yZCIsImV4cCI6MjIwODk4NTI2MX0.PsOABmYUzGbd088g8iIBL4-pf7DUZm0W5Ju6lL5JVRg"
}`),
ExpectedStatus: 200,
ExpectedContent: []string{
`"token":`,
`"record":`,
`"id":"oap640cot4yru2s"`,
`"email":"[email protected]"`,
`"verified":true`,
},
NotExpectedContent: []string{
`"expand":`, // no rel id attached
`"missing":`,
},
ExpectedEvents: map[string]int{
"OnRecordAuthRequest": 1,
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{},
},
{
Name: "valid verification token from a collection without allowed login",
Method: http.MethodPost,
Url: "/api/collections/nologin/confirm-verification?expand=rel,missing",
Url: "/api/collections/nologin/confirm-verification",
Body: strings.NewReader(`{
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImRjNDlrNmpnZWpuNDBoMyIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsImNvbGxlY3Rpb25JZCI6ImtwdjcwOXNrMmxxYnFrOCIsInR5cGUiOiJhdXRoUmVjb3JkIiwiZXhwIjoyMjA4OTg1MjYxfQ.coREjeTDS3_Go7DP1nxHtevIX5rujwHU-_mRB6oOm3w"
}`),
Expand Down Expand Up @@ -871,16 +831,8 @@ func TestRecordAuthConfirmEmailChange(t *testing.T) {
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsImNvbGxlY3Rpb25JZCI6Il9wYl91c2Vyc19hdXRoXyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiZW1haWwiOiJ0ZXN0QGV4YW1wbGUuY29tIiwibmV3RW1haWwiOiJjaGFuZ2VAZXhhbXBsZS5jb20iLCJleHAiOjIyMDg5ODUyNjF9.1sG6cL708pRXXjiHRZhG-in0X5fnttSf5nNcadKoYRs",
"password":"1234567890"
}`),
ExpectedStatus: 200,
ExpectedContent: []string{
`"token":`,
`"record":`,
`"id":"4q1xlclmfloku33"`,
`"email":"[email protected]"`,
`"verified":true`,
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{
"OnRecordAuthRequest": 1,
"OnModelAfterUpdate": 1,
"OnModelBeforeUpdate": 1,
},
Expand Down

0 comments on commit 3d14add

Please sign in to comment.