From 311bc74b7e971bdf88cdc0deece009f146d529e3 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 25 Aug 2023 22:14:04 +0300 Subject: [PATCH] [#3025] updated tests.ApiScenario fields --- CHANGELOG.md | 22 ++++++++++++++++++++++ apis/backup_test.go | 24 ++++++++++++------------ apis/collection_test.go | 20 ++++++++++---------- apis/realtime_test.go | 16 ++++++++-------- apis/record_auth_test.go | 4 ++-- apis/record_crud_test.go | 10 +++++----- apis/settings_test.go | 6 +++--- tests/api.go | 23 ++++++++++++----------- 8 files changed, 74 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5329775f..1b850c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,27 @@ $security.sha512(text) ``` +- ⚠️ Changes to `tests.ApiScenario` struct: + + - The `ApiScenario.AfterTestFunc` now receive as 3rd argument `*http.Response` pointer instead of `*echo.Echo` as the latter is not really useful in this context. + ```go + // old + AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) + + // new + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) + ``` + + - The `ApiScenario.TestAppFactory` now accept the test instance as argument and no longer expect an error as return result ([#3025](https://github.com/pocketbase/pocketbase/discussions/3025#discussioncomment-6592272)). + ```go + // old + TestAppFactory: func() (*tests.TestApp, error) + + // new + TestAppFactory: func(t *testing.T) *tests.TestApp + ``` + _Returning a `nil` app instance from the factory results in test failure._ + - Fill the `LastVerificationSentAt` and `LastResetSentAt` fields only after a successfull email send ([#3121](https://github.com/pocketbase/pocketbase/issues/3121)). - Skip API `fields` json transformations for non 20x responses ([#3176](https://github.com/pocketbase/pocketbase/issues/3176)). @@ -79,6 +100,7 @@ - Reflected the latest JS SDK changes in the Admin UI. + ## v0.17.7 - Fixed the autogenerated `down` migrations to properly revert the old collection rules in case a change was made in `up` ([#3192](https://github.com/pocketbase/pocketbase/pull/3192); thanks @impact-merlinmarek). diff --git a/apis/backup_test.go b/apis/backup_test.go index 6758e71e..d631cd5f 100644 --- a/apis/backup_test.go +++ b/apis/backup_test.go @@ -85,7 +85,7 @@ func TestBackupsCreate(t *testing.T) { Name: "unauthorized", Method: http.MethodPost, Url: "/api/backups", - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureNoBackups(t, app) }, ExpectedStatus: 401, @@ -98,7 +98,7 @@ func TestBackupsCreate(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoUmVjb3JkIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyMjA4OTg1MjYxfQ.UwD8JvkbQtXpymT09d7J6fdA0aP9g4FJ1GPh_ggEkzc", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureNoBackups(t, app) }, ExpectedStatus: 401, @@ -114,7 +114,7 @@ func TestBackupsCreate(t *testing.T) { BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { app.Cache().Set(core.CacheKeyActiveBackup, "") }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureNoBackups(t, app) }, ExpectedStatus: 400, @@ -127,7 +127,7 @@ func TestBackupsCreate(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { files, err := getBackupFiles(app) if err != nil { t.Fatal(err) @@ -152,7 +152,7 @@ func TestBackupsCreate(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureNoBackups(t, app) }, ExpectedStatus: 400, @@ -169,7 +169,7 @@ func TestBackupsCreate(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { files, err := getBackupFiles(app) if err != nil { t.Fatal(err) @@ -371,7 +371,7 @@ func TestBackupsDelete(t *testing.T) { t.Fatal(err) } }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { noTestBackupFilesChanges(t, app) }, ExpectedStatus: 401, @@ -389,7 +389,7 @@ func TestBackupsDelete(t *testing.T) { t.Fatal(err) } }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { noTestBackupFilesChanges(t, app) }, ExpectedStatus: 401, @@ -407,7 +407,7 @@ func TestBackupsDelete(t *testing.T) { t.Fatal(err) } }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { noTestBackupFilesChanges(t, app) }, ExpectedStatus: 400, @@ -428,7 +428,7 @@ func TestBackupsDelete(t *testing.T) { // mock active backup with the same name to delete app.Cache().Set(core.CacheKeyActiveBackup, "test1.zip") }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { noTestBackupFilesChanges(t, app) }, ExpectedStatus: 400, @@ -449,7 +449,7 @@ func TestBackupsDelete(t *testing.T) { // mock active backup with different name app.Cache().Set(core.CacheKeyActiveBackup, "new.zip") }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { files, err := getBackupFiles(app) if err != nil { t.Fatal(err) @@ -481,7 +481,7 @@ func TestBackupsDelete(t *testing.T) { t.Fatal(err) } }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { files, err := getBackupFiles(app) if err != nil { t.Fatal(err) diff --git a/apis/collection_test.go b/apis/collection_test.go index 936f29be..4b8777a1 100644 --- a/apis/collection_test.go +++ b/apis/collection_test.go @@ -245,7 +245,7 @@ func TestCollectionDelete(t *testing.T) { "OnCollectionBeforeDeleteRequest": 1, "OnCollectionAfterDeleteRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureDeletedFiles(app, "9n89pl5vkct6330") }, }, @@ -264,7 +264,7 @@ func TestCollectionDelete(t *testing.T) { "OnCollectionBeforeDeleteRequest": 1, "OnCollectionAfterDeleteRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureDeletedFiles(app, "9n89pl5vkct6330") }, }, @@ -693,7 +693,7 @@ func TestCollectionCreate(t *testing.T) { "OnCollectionBeforeCreateRequest": 1, "OnCollectionAfterCreateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { indexes, err := app.Dao().TableIndexes("new") if err != nil { t.Fatal(err) @@ -823,7 +823,7 @@ func TestCollectionUpdate(t *testing.T) { "OnCollectionBeforeUpdateRequest": 1, "OnCollectionAfterUpdateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { // check if the record table was renamed if !app.Dao().HasTable("new") { t.Fatal("Couldn't find record table 'new'.") @@ -1060,7 +1060,7 @@ func TestCollectionUpdate(t *testing.T) { "OnCollectionBeforeUpdateRequest": 1, "OnCollectionAfterUpdateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { indexes, err := app.Dao().TableIndexes("new") if err != nil { t.Fatal(err) @@ -1115,7 +1115,7 @@ func TestCollectionsImport(t *testing.T) { `"data":{`, `"collections":{"code":"validation_required"`, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { collections := []*models.Collection{} if err := app.Dao().CollectionQuery().All(&collections); err != nil { t.Fatal(err) @@ -1143,7 +1143,7 @@ func TestCollectionsImport(t *testing.T) { "OnCollectionsBeforeImportRequest": 1, "OnModelBeforeDelete": 4, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { collections := []*models.Collection{} if err := app.Dao().CollectionQuery().All(&collections); err != nil { t.Fatal(err) @@ -1185,7 +1185,7 @@ func TestCollectionsImport(t *testing.T) { "OnCollectionsBeforeImportRequest": 1, "OnModelBeforeCreate": 2, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { collections := []*models.Collection{} if err := app.Dao().CollectionQuery().All(&collections); err != nil { t.Fatal(err) @@ -1241,7 +1241,7 @@ func TestCollectionsImport(t *testing.T) { "OnModelBeforeCreate": 3, "OnModelAfterCreate": 3, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { collections := []*models.Collection{} if err := app.Dao().CollectionQuery().All(&collections); err != nil { t.Fatal(err) @@ -1346,7 +1346,7 @@ func TestCollectionsImport(t *testing.T) { "OnModelBeforeCreate": 1, "OnModelAfterCreate": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { collections := []*models.Collection{} if err := app.Dao().CollectionQuery().All(&collections); err != nil { t.Fatal(err) diff --git a/apis/realtime_test.go b/apis/realtime_test.go index 3a9d09e7..ef4be345 100644 --- a/apis/realtime_test.go +++ b/apis/realtime_test.go @@ -34,7 +34,7 @@ func TestRealtimeConnect(t *testing.T) { "OnRealtimeAfterMessageSend": 1, "OnRealtimeDisconnectRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if len(app.SubscriptionsBroker().Clients()) != 0 { t.Errorf("Expected the subscribers to be removed after connection close, found %d", len(app.SubscriptionsBroker().Clients())) } @@ -58,7 +58,7 @@ func TestRealtimeConnect(t *testing.T) { return nil }) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if len(app.SubscriptionsBroker().Clients()) != 0 { t.Errorf("Expected the subscribers to be removed after connection close, found %d", len(app.SubscriptionsBroker().Clients())) } @@ -79,7 +79,7 @@ func TestRealtimeConnect(t *testing.T) { return hook.StopPropagation }) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if len(app.SubscriptionsBroker().Clients()) != 0 { t.Errorf("Expected the subscribers to be removed after connection close, found %d", len(app.SubscriptionsBroker().Clients())) } @@ -124,7 +124,7 @@ func TestRealtimeSubscribe(t *testing.T) { client.Subscribe("test0") app.SubscriptionsBroker().Register(client) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if len(client.Subscriptions()) != 0 { t.Errorf("Expected no subscriptions, got %v", client.Subscriptions()) } @@ -145,7 +145,7 @@ func TestRealtimeSubscribe(t *testing.T) { client.Subscribe("test0") app.SubscriptionsBroker().Register(client) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { expectedSubs := []string{"test1", "test2"} if len(expectedSubs) != len(client.Subscriptions()) { t.Errorf("Expected subscriptions %v, got %v", expectedSubs, client.Subscriptions()) @@ -175,7 +175,7 @@ func TestRealtimeSubscribe(t *testing.T) { BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { app.SubscriptionsBroker().Register(client) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { admin, _ := client.Get(apis.ContextAdminKey).(*models.Admin) if admin == nil { t.Errorf("Expected admin auth model, got nil") @@ -199,7 +199,7 @@ func TestRealtimeSubscribe(t *testing.T) { BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { app.SubscriptionsBroker().Register(client) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { authRecord, _ := client.Get(apis.ContextAuthRecordKey).(*models.Record) if authRecord == nil { t.Errorf("Expected auth record model, got nil") @@ -224,7 +224,7 @@ func TestRealtimeSubscribe(t *testing.T) { app.SubscriptionsBroker().Register(client) }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { authRecord, _ := client.Get(apis.ContextAuthRecordKey).(*models.Record) if authRecord == nil { t.Errorf("Expected auth record model, got nil") diff --git a/apis/record_auth_test.go b/apis/record_auth_test.go index 9144480f..8cdad322 100644 --- a/apis/record_auth_test.go +++ b/apis/record_auth_test.go @@ -1188,7 +1188,7 @@ func TestRecordAuthUnlinkExternalsAuth(t *testing.T) { "OnRecordAfterUnlinkExternalAuthRequest": 1, "OnRecordBeforeUnlinkExternalAuthRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { record, err := app.Dao().FindRecordById("users", "4q1xlclmfloku33") if err != nil { t.Fatal(err) @@ -1234,7 +1234,7 @@ func TestRecordAuthUnlinkExternalsAuth(t *testing.T) { "OnRecordAfterUnlinkExternalAuthRequest": 1, "OnRecordBeforeUnlinkExternalAuthRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { record, err := app.Dao().FindRecordById("users", "4q1xlclmfloku33") if err != nil { t.Fatal(err) diff --git a/apis/record_crud_test.go b/apis/record_crud_test.go index d879475e..0abeeaf1 100644 --- a/apis/record_crud_test.go +++ b/apis/record_crud_test.go @@ -903,7 +903,7 @@ func TestRecordCrudDelete(t *testing.T) { "OnRecordAfterDeleteRequest": 1, "OnRecordBeforeDeleteRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { ensureDeletedFiles(app, "_pb_users_auth_", "4q1xlclmfloku33") // check if all the external auths records were deleted @@ -990,7 +990,7 @@ func TestRecordCrudDelete(t *testing.T) { "OnRecordBeforeDeleteRequest": 1, "OnRecordAfterDeleteRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { recId := "84nmscqy84lsi1t" rec, _ := app.Dao().FindRecordById("demo1", recId, nil) if rec != nil { @@ -2032,7 +2032,7 @@ func TestRecordCrudUpdate(t *testing.T) { "OnRecordAfterUpdateRequest": 1, "OnRecordBeforeUpdateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { record, _ := app.Dao().FindRecordById("nologin", "phhq3wr65cap535") if !record.ValidatePassword("12345678") { t.Fatal("Password update failed.") @@ -2075,7 +2075,7 @@ func TestRecordCrudUpdate(t *testing.T) { "OnRecordAfterUpdateRequest": 1, "OnRecordBeforeUpdateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { record, _ := app.Dao().FindRecordById("users", "oap640cot4yru2s") if !record.ValidatePassword("12345678") { t.Fatal("Password update failed.") @@ -2137,7 +2137,7 @@ func TestRecordCrudUpdate(t *testing.T) { "OnRecordAfterUpdateRequest": 1, "OnRecordBeforeUpdateRequest": 1, }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { record, _ := app.Dao().FindRecordById("nologin", "dc49k6jgejn40h3") if !record.ValidatePassword("123456789") { t.Fatal("Password update failed.") diff --git a/apis/settings_test.go b/apis/settings_test.go index 5ec1f012..bf0d96d2 100644 --- a/apis/settings_test.go +++ b/apis/settings_test.go @@ -400,7 +400,7 @@ func TestSettingsTestEmail(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if app.TestMailer.TotalSend != 1 { t.Fatalf("[verification] Expected 1 sent email, got %d", app.TestMailer.TotalSend) } @@ -435,7 +435,7 @@ func TestSettingsTestEmail(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if app.TestMailer.TotalSend != 1 { t.Fatalf("[password-reset] Expected 1 sent email, got %d", app.TestMailer.TotalSend) } @@ -470,7 +470,7 @@ func TestSettingsTestEmail(t *testing.T) { RequestHeaders: map[string]string{ "Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8", }, - AfterTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) { + AfterTestFunc: func(t *testing.T, app *tests.TestApp, res *http.Response) { if app.TestMailer.TotalSend != 1 { t.Fatalf("[email-change] Expected 1 sent email, got %d", app.TestMailer.TotalSend) } diff --git a/tests/api.go b/tests/api.go index a3d0dc32..057ea4c3 100644 --- a/tests/api.go +++ b/tests/api.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "net/http" "net/http/httptest" "strings" "testing" @@ -37,9 +38,9 @@ type ApiScenario struct { // test hooks // --- - TestAppFactory func() (*TestApp, error) + TestAppFactory func(t *testing.T) *TestApp BeforeTestFunc func(t *testing.T, app *TestApp, e *echo.Echo) - AfterTestFunc func(t *testing.T, app *TestApp, e *echo.Echo) + AfterTestFunc func(t *testing.T, app *TestApp, res *http.Response) } // Test executes the test scenario. @@ -54,16 +55,17 @@ func (scenario *ApiScenario) Test(t *testing.T) { func (scenario *ApiScenario) test(t *testing.T) { var testApp *TestApp - var testAppErr error if scenario.TestAppFactory != nil { - // @todo consider passing the testing instance to the factory and maybe remove the error from the declaration - // (see https://github.com/pocketbase/pocketbase/discussions/3025) - testApp, testAppErr = scenario.TestAppFactory() + testApp = scenario.TestAppFactory(t) + if testApp == nil { + t.Fatal("TestAppFactory must return a non-nill app instance") + } } else { + var testAppErr error testApp, testAppErr = NewTestApp() - } - if testAppErr != nil { - t.Fatalf("Failed to initialize the test app instance: %v", testAppErr) + if testAppErr != nil { + t.Fatalf("Failed to initialize the test app instance: %v", testAppErr) + } } defer testApp.Cleanup() @@ -172,8 +174,7 @@ func (scenario *ApiScenario) test(t *testing.T) { } } - // @todo consider adding the response body to the AfterTestFunc args if scenario.AfterTestFunc != nil { - scenario.AfterTestFunc(t, testApp, e) + scenario.AfterTestFunc(t, testApp, res) } }