1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-08 09:14:37 +02:00

updated tests

This commit is contained in:
Gani Georgiev 2022-07-19 17:23:34 +03:00
parent 65697add43
commit ab5a770346
3 changed files with 23 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"time"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
"github.com/pocketbase/dbx" "github.com/pocketbase/dbx"
@ -94,6 +95,7 @@ func TestAdminRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/admins/request-password-reset", Url: "/api/admins/request-password-reset",
Body: strings.NewReader(`{"email":"missing@example.com"}`), Body: strings.NewReader(`{"email":"missing@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
}, },
{ {
@ -101,6 +103,7 @@ func TestAdminRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/admins/request-password-reset", Url: "/api/admins/request-password-reset",
Body: strings.NewReader(`{"email":"test@example.com"}`), Body: strings.NewReader(`{"email":"test@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
ExpectedEvents: map[string]int{ ExpectedEvents: map[string]int{
"OnModelBeforeUpdate": 1, "OnModelBeforeUpdate": 1,
@ -114,6 +117,7 @@ func TestAdminRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/admins/request-password-reset", Url: "/api/admins/request-password-reset",
Body: strings.NewReader(`{"email":"test@example.com"}`), Body: strings.NewReader(`{"email":"test@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
// simulate recent password request // simulate recent password request

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"time"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/daos" "github.com/pocketbase/pocketbase/daos"
@ -134,6 +135,7 @@ func TestUserRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-password-reset", Url: "/api/users/request-password-reset",
Body: strings.NewReader(`{"email":"missing@example.com"}`), Body: strings.NewReader(`{"email":"missing@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
}, },
{ {
@ -141,6 +143,7 @@ func TestUserRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-password-reset", Url: "/api/users/request-password-reset",
Body: strings.NewReader(`{"email":"test@example.com"}`), Body: strings.NewReader(`{"email":"test@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
ExpectedEvents: map[string]int{ ExpectedEvents: map[string]int{
"OnModelBeforeUpdate": 1, "OnModelBeforeUpdate": 1,
@ -154,6 +157,7 @@ func TestUserRequestPasswordReset(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-password-reset", Url: "/api/users/request-password-reset",
Body: strings.NewReader(`{"email":"test@example.com"}`), Body: strings.NewReader(`{"email":"test@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
// simulate recent password request // simulate recent password request
@ -249,6 +253,7 @@ func TestUserRequestVerification(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-verification", Url: "/api/users/request-verification",
Body: strings.NewReader(`{"email":"missing@example.com"}`), Body: strings.NewReader(`{"email":"missing@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
}, },
{ {
@ -256,6 +261,7 @@ func TestUserRequestVerification(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-verification", Url: "/api/users/request-verification",
Body: strings.NewReader(`{"email":"test@example.com"}`), Body: strings.NewReader(`{"email":"test@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
}, },
{ {
@ -263,6 +269,7 @@ func TestUserRequestVerification(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-verification", Url: "/api/users/request-verification",
Body: strings.NewReader(`{"email":"test2@example.com"}`), Body: strings.NewReader(`{"email":"test2@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
ExpectedEvents: map[string]int{ ExpectedEvents: map[string]int{
"OnModelBeforeUpdate": 1, "OnModelBeforeUpdate": 1,
@ -276,6 +283,7 @@ func TestUserRequestVerification(t *testing.T) {
Method: http.MethodPost, Method: http.MethodPost,
Url: "/api/users/request-verification", Url: "/api/users/request-verification",
Body: strings.NewReader(`{"email":"test2@example.com"}`), Body: strings.NewReader(`{"email":"test2@example.com"}`),
Delay: 100 * time.Millisecond,
ExpectedStatus: 204, ExpectedStatus: 204,
BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { BeforeFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
// simulate recent verification sent // simulate recent verification sent

View File

@ -22,12 +22,20 @@ type ApiScenario struct {
Url string Url string
Body io.Reader Body io.Reader
RequestHeaders map[string]string 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 // expectations
// ---
ExpectedStatus int ExpectedStatus int
ExpectedContent []string ExpectedContent []string
NotExpectedContent []string NotExpectedContent []string
ExpectedEvents map[string]int ExpectedEvents map[string]int
// test hooks // test hooks
// ---
BeforeFunc func(t *testing.T, app *TestApp, e *echo.Echo) BeforeFunc func(t *testing.T, app *TestApp, e *echo.Echo)
AfterFunc 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) t.Errorf("[%s] Expected status code %d, got %d", prefix, scenario.ExpectedStatus, res.StatusCode)
} }
// @todo consider replacing with sync.WaitGroup if scenario.Delay > 0 {
// time.Sleep(scenario.Delay)
// 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 len(scenario.ExpectedContent) == 0 && len(scenario.NotExpectedContent) == 0 { if len(scenario.ExpectedContent) == 0 && len(scenario.NotExpectedContent) == 0 {
if len(recorder.Body.Bytes()) != 0 { if len(recorder.Body.Bytes()) != 0 {