forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run added password reset and verification sent hooks tests
- Loading branch information
1 parent
383b2a1
commit f295ce9
Showing
5 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,10 @@ import ( | |
|
||
"github.com/labstack/echo/v5" | ||
"github.com/pocketbase/dbx" | ||
"github.com/pocketbase/pocketbase/daos" | ||
"github.com/pocketbase/pocketbase/models" | ||
"github.com/pocketbase/pocketbase/tests" | ||
"github.com/pocketbase/pocketbase/tools/types" | ||
) | ||
|
||
func TestAdminAuth(t *testing.T) { | ||
|
@@ -100,21 +102,31 @@ func TestAdminRequestPasswordReset(t *testing.T) { | |
Url: "/api/admins/request-password-reset", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
// usually this events are fired but since the submit is | ||
// executed in a separate go routine they are fired async | ||
// ExpectedEvents: map[string]int{ | ||
// "OnModelBeforeUpdate": 1, | ||
// "OnModelAfterUpdate": 1, | ||
// "OnMailerBeforeUserResetPasswordSend:1": 1, | ||
// "OnMailerAfterUserResetPasswordSend:1": 1, | ||
// }, | ||
ExpectedEvents: map[string]int{ | ||
"OnModelBeforeUpdate": 1, | ||
"OnModelAfterUpdate": 1, | ||
"OnMailerBeforeAdminResetPasswordSend": 1, | ||
"OnMailerAfterAdminResetPasswordSend": 1, | ||
}, | ||
}, | ||
{ | ||
Name: "existing admin (after already sent)", | ||
Method: http.MethodPost, | ||
Url: "/api/admins/request-password-reset", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { | ||
// simulate recent password request | ||
admin, err := app.Dao().FindAdminByEmail("[email protected]") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
admin.LastResetSentAt = types.NowDateTime() | ||
dao := daos.New(app.Dao().DB()) // new dao to ignore hooks | ||
if err := dao.Save(admin); err != nil { | ||
t.Fatal(err) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ import ( | |
"testing" | ||
|
||
"github.com/labstack/echo/v5" | ||
"github.com/pocketbase/pocketbase/daos" | ||
"github.com/pocketbase/pocketbase/tests" | ||
"github.com/pocketbase/pocketbase/tools/types" | ||
) | ||
|
||
func TestUsersAuthMethods(t *testing.T) { | ||
|
@@ -140,21 +142,31 @@ func TestUserRequestPasswordReset(t *testing.T) { | |
Url: "/api/users/request-password-reset", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
// usually this events are fired but since the submit is | ||
// executed in a separate go routine they are fired async | ||
// ExpectedEvents: map[string]int{ | ||
// "OnModelBeforeUpdate": 1, | ||
// "OnModelAfterUpdate": 1, | ||
// "OnMailerBeforeUserResetPasswordSend": 1, | ||
// "OnMailerAfterUserResetPasswordSend": 1, | ||
// }, | ||
ExpectedEvents: map[string]int{ | ||
"OnModelBeforeUpdate": 1, | ||
"OnModelAfterUpdate": 1, | ||
"OnMailerBeforeUserResetPasswordSend": 1, | ||
"OnMailerAfterUserResetPasswordSend": 1, | ||
}, | ||
}, | ||
{ | ||
Name: "existing user (after already sent)", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-password-reset", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { | ||
// simulate recent password request | ||
user, err := app.Dao().FindUserByEmail("[email protected]") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
user.LastResetSentAt = types.NowDateTime() | ||
dao := daos.New(app.Dao().DB()) // new dao to ignore hooks | ||
if err := dao.Save(user); err != nil { | ||
t.Fatal(err) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
|
@@ -216,50 +228,67 @@ func TestUserConfirmPasswordReset(t *testing.T) { | |
|
||
func TestUserRequestVerification(t *testing.T) { | ||
scenarios := []tests.ApiScenario{ | ||
// empty data | ||
{ | ||
Name: "empty data", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(``), | ||
ExpectedStatus: 400, | ||
ExpectedContent: []string{`"data":{"email":{"code":"validation_required","message":"Cannot be blank."}}`}, | ||
}, | ||
// invalid data | ||
{ | ||
Name: "invalid data", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(`{"email`), | ||
ExpectedStatus: 400, | ||
ExpectedContent: []string{`"data":{}`}, | ||
}, | ||
// missing user | ||
{ | ||
Name: "missing user", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
}, | ||
// existing already verified user | ||
{ | ||
Name: "existing already verified user", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
}, | ||
// existing unverified user | ||
{ | ||
Name: "existing unverified user", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
// usually this events are fired but since the submit is | ||
// executed in a separate go routine they are fired async | ||
// ExpectedEvents: map[string]int{ | ||
// "OnModelBeforeUpdate": 1, | ||
// "OnModelAfterUpdate": 1, | ||
// "OnMailerBeforeUserVerificationSend": 1, | ||
// "OnMailerAfterUserVerificationSend": 1, | ||
// }, | ||
ExpectedEvents: map[string]int{ | ||
"OnModelBeforeUpdate": 1, | ||
"OnModelAfterUpdate": 1, | ||
"OnMailerBeforeUserVerificationSend": 1, | ||
"OnMailerAfterUserVerificationSend": 1, | ||
}, | ||
}, | ||
{ | ||
Name: "existing unverified user (after already sent)", | ||
Method: http.MethodPost, | ||
Url: "/api/users/request-verification", | ||
Body: strings.NewReader(`{"email":"[email protected]"}`), | ||
ExpectedStatus: 204, | ||
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { | ||
// simulate recent verification sent | ||
user, err := app.Dao().FindUserByEmail("[email protected]") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
user.LastVerificationSentAt = types.NowDateTime() | ||
dao := daos.New(app.Dao().DB()) // new dao to ignore hooks | ||
if err := dao.Save(user); err != nil { | ||
t.Fatal(err) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters