From ab5a770346733a9f2acbdc36acae996886521591 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 19 Jul 2022 17:23:34 +0300 Subject: [PATCH] updated tests --- apis/admin_test.go | 4 ++++ apis/user_test.go | 8 ++++++++ tests/api.go | 16 +++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/apis/admin_test.go b/apis/admin_test.go index 6ebcb8b0..482e586e 100644 --- a/apis/admin_test.go +++ b/apis/admin_test.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" "testing" + "time" "github.com/labstack/echo/v5" "github.com/pocketbase/dbx" @@ -94,6 +95,7 @@ func TestAdminRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/admins/request-password-reset", Body: strings.NewReader(`{"email":"missing@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, }, { @@ -101,6 +103,7 @@ func TestAdminRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/admins/request-password-reset", Body: strings.NewReader(`{"email":"test@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, ExpectedEvents: map[string]int{ "OnModelBeforeUpdate": 1, @@ -114,6 +117,7 @@ func TestAdminRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/admins/request-password-reset", Body: strings.NewReader(`{"email":"test@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { // simulate recent password request diff --git a/apis/user_test.go b/apis/user_test.go index f382236f..66532d0a 100644 --- a/apis/user_test.go +++ b/apis/user_test.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" "testing" + "time" "github.com/labstack/echo/v5" "github.com/pocketbase/pocketbase/daos" @@ -134,6 +135,7 @@ func TestUserRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-password-reset", Body: strings.NewReader(`{"email":"missing@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, }, { @@ -141,6 +143,7 @@ func TestUserRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-password-reset", Body: strings.NewReader(`{"email":"test@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, ExpectedEvents: map[string]int{ "OnModelBeforeUpdate": 1, @@ -154,6 +157,7 @@ func TestUserRequestPasswordReset(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-password-reset", Body: strings.NewReader(`{"email":"test@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { // simulate recent password request @@ -249,6 +253,7 @@ func TestUserRequestVerification(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-verification", Body: strings.NewReader(`{"email":"missing@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, }, { @@ -256,6 +261,7 @@ func TestUserRequestVerification(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-verification", Body: strings.NewReader(`{"email":"test@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, }, { @@ -263,6 +269,7 @@ func TestUserRequestVerification(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-verification", Body: strings.NewReader(`{"email":"test2@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, ExpectedEvents: map[string]int{ "OnModelBeforeUpdate": 1, @@ -276,6 +283,7 @@ func TestUserRequestVerification(t *testing.T) { Method: http.MethodPost, Url: "/api/users/request-verification", Body: strings.NewReader(`{"email":"test2@example.com"}`), + Delay: 100 * time.Millisecond, ExpectedStatus: 204, BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { // simulate recent verification sent diff --git a/tests/api.go b/tests/api.go index 66c467d2..8aacc17d 100644 --- a/tests/api.go +++ b/tests/api.go @@ -22,12 +22,20 @@ type ApiScenario struct { Url string Body io.Reader RequestHeaders map[string]string + + // Delay adds a delay before checking the expectations usually + // to ensure that all fired non-awaited go routines have finished + Delay time.Duration + // expectations + // --- ExpectedStatus int ExpectedContent []string NotExpectedContent []string ExpectedEvents map[string]int + // test hooks + // --- BeforeFunc func(t *testing.T, app *TestApp, e *echo.Echo) AfterFunc func(t *testing.T, app *TestApp, e *echo.Echo) } @@ -81,11 +89,9 @@ func (scenario *ApiScenario) Test(t *testing.T) { t.Errorf("[%s] Expected status code %d, got %d", prefix, scenario.ExpectedStatus, res.StatusCode) } - // @todo consider replacing with sync.WaitGroup - // - // apply a small delay before checking the expectations to ensure - // that all fired go routines have complicated before cleaning up the app instance - time.Sleep(5 * time.Millisecond) + if scenario.Delay > 0 { + time.Sleep(scenario.Delay) + } if len(scenario.ExpectedContent) == 0 && len(scenario.NotExpectedContent) == 0 { if len(recorder.Body.Bytes()) != 0 {