From 3d14addfef74360ffbba0b0a7d2be37c38381b18 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 8 Nov 2022 18:12:37 +0200 Subject: [PATCH] changed the return result of the confirm api actions --- apis/admin.go | 4 +-- apis/admin_test.go | 7 +---- apis/record_auth.go | 18 ++++------- apis/record_auth_test.go | 66 ++++++---------------------------------- 4 files changed, 18 insertions(+), 77 deletions(-) diff --git a/apis/admin.go b/apis/admin.go index 3209911b..cf8354a3 100644 --- a/apis/admin.go +++ b/apis/admin.go @@ -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 { diff --git a/apis/admin_test.go b/apis/admin_test.go index 39f69589..ce745718 100644 --- a/apis/admin_test.go +++ b/apis/admin_test.go @@ -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, }, }, } diff --git a/apis/record_auth.go b/apis/record_auth.go index 76ae9d63..d895b774 100644 --- a/apis/record_auth.go +++ b/apis/record_auth.go @@ -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 { @@ -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 { @@ -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 { diff --git a/apis/record_auth_test.go b/apis/record_auth_test.go index 97dc98a3..3ca87cca 100644 --- a/apis/record_auth_test.go +++ b/apis/record_auth_test.go @@ -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":"test@example.com"`, - `"expand":`, - `"rel":`, - `"id":"llvuca81nly1qls"`, - }, - NotExpectedContent: []string{ - `"missing":`, - }, + ExpectedStatus: 204, ExpectedEvents: map[string]int{ - "OnRecordAuthRequest": 1, "OnModelAfterUpdate": 1, "OnModelBeforeUpdate": 1, }, @@ -634,26 +621,12 @@ 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":"test@example.com"`, - `"verified":true`, - `"expand":`, - `"rel":`, - `"id":"llvuca81nly1qls"`, - }, - NotExpectedContent: []string{ - `"missing":`, - }, + ExpectedStatus: 204, ExpectedEvents: map[string]int{ - "OnRecordAuthRequest": 1, "OnModelAfterUpdate": 1, "OnModelBeforeUpdate": 1, }, @@ -661,30 +634,17 @@ func TestRecordAuthConfirmVerification(t *testing.T) { { 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":"test2@example.com"`, - `"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" }`), @@ -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":"change@example.com"`, - `"verified":true`, - }, + ExpectedStatus: 204, ExpectedEvents: map[string]int{ - "OnRecordAuthRequest": 1, "OnModelAfterUpdate": 1, "OnModelBeforeUpdate": 1, },