1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-12-01 11:01:04 +02:00
pocketbase/tests/app.go

546 lines
17 KiB
Go
Raw Normal View History

// Package tests provides common helpers and mocks used in PocketBase application tests.
2022-07-06 23:19:05 +02:00
package tests
import (
"io"
"os"
"path"
"path/filepath"
"runtime"
"sync"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/mailer"
)
// TestApp is a wrapper app instance used for testing.
type TestApp struct {
*core.BaseApp
mux sync.Mutex
2022-07-06 23:19:05 +02:00
// EventCalls defines a map to inspect which app events
// (and how many times) were triggered.
//
// The following events are not counted because they execute always:
// - OnBeforeBootstrap
// - OnAfterBootstrap
// - OnBeforeServe
2022-07-06 23:19:05 +02:00
EventCalls map[string]int
2022-07-06 23:19:05 +02:00
TestMailer *TestMailer
}
// Cleanup resets the test application state and removes the test
// app's dataDir from the filesystem.
//
// After this call, the app instance shouldn't be used anymore.
func (t *TestApp) Cleanup() {
t.ResetEventCalls()
t.ResetBootstrapState()
if t.DataDir() != "" {
os.RemoveAll(t.DataDir())
}
}
2023-05-13 21:10:14 +02:00
// NewMailClient initializes test app mail client.
2022-07-06 23:19:05 +02:00
func (t *TestApp) NewMailClient() mailer.Mailer {
t.mux.Lock()
defer t.mux.Unlock()
2022-07-06 23:19:05 +02:00
t.TestMailer.Reset()
2022-07-06 23:19:05 +02:00
return t.TestMailer
}
// ResetEventCalls resets the EventCalls counter.
func (t *TestApp) ResetEventCalls() {
t.mux.Lock()
defer t.mux.Unlock()
2022-07-06 23:19:05 +02:00
t.EventCalls = make(map[string]int)
}
func (t *TestApp) registerEventCall(name string) error {
t.mux.Lock()
defer t.mux.Unlock()
if t.EventCalls == nil {
t.EventCalls = make(map[string]int)
}
t.EventCalls[name]++
return nil
}
2022-09-07 19:31:05 +02:00
// NewTestApp creates and initializes a test application instance.
2022-07-06 23:19:05 +02:00
//
// It is the caller's responsibility to call `app.Cleanup()`
// when the app is no longer needed.
2022-09-07 19:31:05 +02:00
func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
var testDataDir string
if len(optTestDataDir) == 0 || optTestDataDir[0] == "" {
// fallback to the default test data directory
_, currentFile, _, _ := runtime.Caller(0)
testDataDir = filepath.Join(path.Dir(currentFile), "data")
} else {
testDataDir = optTestDataDir[0]
}
tempDir, err := TempDirClone(testDataDir)
2022-07-06 23:19:05 +02:00
if err != nil {
return nil, err
}
app := core.NewBaseApp(&core.BaseAppConfig{
DataDir: tempDir,
EncryptionEnv: "pb_test_env",
IsDebug: false,
})
2022-07-06 23:19:05 +02:00
// load data dir and db connections
if err := app.Bootstrap(); err != nil {
return nil, err
}
2023-05-27 08:24:18 +02:00
// ensure that the Dao and DB configurations are properly loaded
if _, err := app.Dao().DB().NewQuery("Select 1").Execute(); err != nil {
return nil, err
}
if _, err := app.LogsDao().DB().NewQuery("Select 1").Execute(); err != nil {
return nil, err
}
2022-07-06 23:19:05 +02:00
// force disable request logs because the logs db call execute in a separate
// go routine and it is possible to panic due to earlier api test completion.
app.Settings().Logs.MaxDays = 0
t := &TestApp{
BaseApp: app,
EventCalls: make(map[string]int),
TestMailer: &TestMailer{},
}
2022-12-02 16:36:15 +02:00
t.OnBeforeApiError().Add(func(e *core.ApiErrorEvent) error {
return t.registerEventCall("OnBeforeApiError")
2022-12-02 16:36:15 +02:00
})
t.OnAfterApiError().Add(func(e *core.ApiErrorEvent) error {
return t.registerEventCall("OnAfterApiError")
2022-12-02 16:36:15 +02:00
})
2022-07-06 23:19:05 +02:00
t.OnModelBeforeCreate().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelBeforeCreate")
2022-07-06 23:19:05 +02:00
})
t.OnModelAfterCreate().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelAfterCreate")
2022-07-06 23:19:05 +02:00
})
t.OnModelBeforeUpdate().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelBeforeUpdate")
2022-07-06 23:19:05 +02:00
})
t.OnModelAfterUpdate().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelAfterUpdate")
2022-07-06 23:19:05 +02:00
})
t.OnModelBeforeDelete().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelBeforeDelete")
2022-07-06 23:19:05 +02:00
})
t.OnModelAfterDelete().Add(func(e *core.ModelEvent) error {
return t.registerEventCall("OnModelAfterDelete")
2022-07-06 23:19:05 +02:00
})
t.OnRecordsListRequest().Add(func(e *core.RecordsListEvent) error {
return t.registerEventCall("OnRecordsListRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordViewRequest().Add(func(e *core.RecordViewEvent) error {
return t.registerEventCall("OnRecordViewRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordBeforeCreateRequest().Add(func(e *core.RecordCreateEvent) error {
return t.registerEventCall("OnRecordBeforeCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
return t.registerEventCall("OnRecordAfterCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
return t.registerEventCall("OnRecordBeforeUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordAfterUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
return t.registerEventCall("OnRecordAfterUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordBeforeDeleteRequest().Add(func(e *core.RecordDeleteEvent) error {
return t.registerEventCall("OnRecordBeforeDeleteRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordAfterDeleteRequest().Add(func(e *core.RecordDeleteEvent) error {
return t.registerEventCall("OnRecordAfterDeleteRequest")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnRecordAuthRequest().Add(func(e *core.RecordAuthEvent) error {
return t.registerEventCall("OnRecordAuthRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRecordBeforeAuthWithPasswordRequest().Add(func(e *core.RecordAuthWithPasswordEvent) error {
return t.registerEventCall("OnRecordBeforeAuthWithPasswordRequest")
})
t.OnRecordAfterAuthWithPasswordRequest().Add(func(e *core.RecordAuthWithPasswordEvent) error {
return t.registerEventCall("OnRecordAfterAuthWithPasswordRequest")
})
t.OnRecordBeforeAuthWithOAuth2Request().Add(func(e *core.RecordAuthWithOAuth2Event) error {
return t.registerEventCall("OnRecordBeforeAuthWithOAuth2Request")
})
t.OnRecordAfterAuthWithOAuth2Request().Add(func(e *core.RecordAuthWithOAuth2Event) error {
return t.registerEventCall("OnRecordAfterAuthWithOAuth2Request")
})
t.OnRecordBeforeAuthRefreshRequest().Add(func(e *core.RecordAuthRefreshEvent) error {
return t.registerEventCall("OnRecordBeforeAuthRefreshRequest")
})
t.OnRecordAfterAuthRefreshRequest().Add(func(e *core.RecordAuthRefreshEvent) error {
return t.registerEventCall("OnRecordAfterAuthRefreshRequest")
})
t.OnRecordBeforeRequestPasswordResetRequest().Add(func(e *core.RecordRequestPasswordResetEvent) error {
return t.registerEventCall("OnRecordBeforeRequestPasswordResetRequest")
})
t.OnRecordAfterRequestPasswordResetRequest().Add(func(e *core.RecordRequestPasswordResetEvent) error {
return t.registerEventCall("OnRecordAfterRequestPasswordResetRequest")
})
t.OnRecordBeforeConfirmPasswordResetRequest().Add(func(e *core.RecordConfirmPasswordResetEvent) error {
return t.registerEventCall("OnRecordBeforeConfirmPasswordResetRequest")
})
t.OnRecordAfterConfirmPasswordResetRequest().Add(func(e *core.RecordConfirmPasswordResetEvent) error {
return t.registerEventCall("OnRecordAfterConfirmPasswordResetRequest")
})
t.OnRecordBeforeRequestVerificationRequest().Add(func(e *core.RecordRequestVerificationEvent) error {
return t.registerEventCall("OnRecordBeforeRequestVerificationRequest")
})
t.OnRecordAfterRequestVerificationRequest().Add(func(e *core.RecordRequestVerificationEvent) error {
return t.registerEventCall("OnRecordAfterRequestVerificationRequest")
})
t.OnRecordBeforeConfirmVerificationRequest().Add(func(e *core.RecordConfirmVerificationEvent) error {
return t.registerEventCall("OnRecordBeforeConfirmVerificationRequest")
})
t.OnRecordAfterConfirmVerificationRequest().Add(func(e *core.RecordConfirmVerificationEvent) error {
return t.registerEventCall("OnRecordAfterConfirmVerificationRequest")
})
t.OnRecordBeforeRequestEmailChangeRequest().Add(func(e *core.RecordRequestEmailChangeEvent) error {
return t.registerEventCall("OnRecordBeforeRequestEmailChangeRequest")
})
t.OnRecordAfterRequestEmailChangeRequest().Add(func(e *core.RecordRequestEmailChangeEvent) error {
return t.registerEventCall("OnRecordAfterRequestEmailChangeRequest")
})
t.OnRecordBeforeConfirmEmailChangeRequest().Add(func(e *core.RecordConfirmEmailChangeEvent) error {
return t.registerEventCall("OnRecordBeforeConfirmEmailChangeRequest")
})
t.OnRecordAfterConfirmEmailChangeRequest().Add(func(e *core.RecordConfirmEmailChangeEvent) error {
return t.registerEventCall("OnRecordAfterConfirmEmailChangeRequest")
})
t.OnRecordListExternalAuthsRequest().Add(func(e *core.RecordListExternalAuthsEvent) error {
return t.registerEventCall("OnRecordListExternalAuthsRequest")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnRecordBeforeUnlinkExternalAuthRequest().Add(func(e *core.RecordUnlinkExternalAuthEvent) error {
return t.registerEventCall("OnRecordBeforeUnlinkExternalAuthRequest")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnRecordAfterUnlinkExternalAuthRequest().Add(func(e *core.RecordUnlinkExternalAuthEvent) error {
return t.registerEventCall("OnRecordAfterUnlinkExternalAuthRequest")
2022-07-06 23:19:05 +02:00
})
t.OnMailerBeforeAdminResetPasswordSend().Add(func(e *core.MailerAdminEvent) error {
return t.registerEventCall("OnMailerBeforeAdminResetPasswordSend")
2022-07-06 23:19:05 +02:00
})
t.OnMailerAfterAdminResetPasswordSend().Add(func(e *core.MailerAdminEvent) error {
return t.registerEventCall("OnMailerAfterAdminResetPasswordSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerBeforeRecordResetPasswordSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerBeforeRecordResetPasswordSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerAfterRecordResetPasswordSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerAfterRecordResetPasswordSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerBeforeRecordVerificationSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerBeforeRecordVerificationSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerAfterRecordVerificationSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerAfterRecordVerificationSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerBeforeRecordChangeEmailSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerBeforeRecordChangeEmailSend")
2022-07-06 23:19:05 +02:00
})
2022-10-30 10:28:14 +02:00
t.OnMailerAfterRecordChangeEmailSend().Add(func(e *core.MailerRecordEvent) error {
return t.registerEventCall("OnMailerAfterRecordChangeEmailSend")
2022-07-06 23:19:05 +02:00
})
t.OnRealtimeConnectRequest().Add(func(e *core.RealtimeConnectEvent) error {
return t.registerEventCall("OnRealtimeConnectRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRealtimeDisconnectRequest().Add(func(e *core.RealtimeDisconnectEvent) error {
return t.registerEventCall("OnRealtimeDisconnectRequest")
})
t.OnRealtimeBeforeMessageSend().Add(func(e *core.RealtimeMessageEvent) error {
return t.registerEventCall("OnRealtimeBeforeMessageSend")
})
t.OnRealtimeAfterMessageSend().Add(func(e *core.RealtimeMessageEvent) error {
return t.registerEventCall("OnRealtimeAfterMessageSend")
})
2022-07-06 23:19:05 +02:00
t.OnRealtimeBeforeSubscribeRequest().Add(func(e *core.RealtimeSubscribeEvent) error {
return t.registerEventCall("OnRealtimeBeforeSubscribeRequest")
2022-07-06 23:19:05 +02:00
})
t.OnRealtimeAfterSubscribeRequest().Add(func(e *core.RealtimeSubscribeEvent) error {
return t.registerEventCall("OnRealtimeAfterSubscribeRequest")
2022-07-06 23:19:05 +02:00
})
t.OnSettingsListRequest().Add(func(e *core.SettingsListEvent) error {
return t.registerEventCall("OnSettingsListRequest")
2022-07-06 23:19:05 +02:00
})
t.OnSettingsBeforeUpdateRequest().Add(func(e *core.SettingsUpdateEvent) error {
return t.registerEventCall("OnSettingsBeforeUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnSettingsAfterUpdateRequest().Add(func(e *core.SettingsUpdateEvent) error {
return t.registerEventCall("OnSettingsAfterUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionsListRequest().Add(func(e *core.CollectionsListEvent) error {
return t.registerEventCall("OnCollectionsListRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionViewRequest().Add(func(e *core.CollectionViewEvent) error {
return t.registerEventCall("OnCollectionViewRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionBeforeCreateRequest().Add(func(e *core.CollectionCreateEvent) error {
return t.registerEventCall("OnCollectionBeforeCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionAfterCreateRequest().Add(func(e *core.CollectionCreateEvent) error {
return t.registerEventCall("OnCollectionAfterCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionBeforeUpdateRequest().Add(func(e *core.CollectionUpdateEvent) error {
return t.registerEventCall("OnCollectionBeforeUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionAfterUpdateRequest().Add(func(e *core.CollectionUpdateEvent) error {
return t.registerEventCall("OnCollectionAfterUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionBeforeDeleteRequest().Add(func(e *core.CollectionDeleteEvent) error {
return t.registerEventCall("OnCollectionBeforeDeleteRequest")
2022-07-06 23:19:05 +02:00
})
t.OnCollectionAfterDeleteRequest().Add(func(e *core.CollectionDeleteEvent) error {
return t.registerEventCall("OnCollectionAfterDeleteRequest")
2022-07-06 23:19:05 +02:00
})
2022-08-07 19:58:21 +02:00
t.OnCollectionsBeforeImportRequest().Add(func(e *core.CollectionsImportEvent) error {
return t.registerEventCall("OnCollectionsBeforeImportRequest")
2022-08-07 19:58:21 +02:00
})
t.OnCollectionsAfterImportRequest().Add(func(e *core.CollectionsImportEvent) error {
return t.registerEventCall("OnCollectionsAfterImportRequest")
2022-08-07 19:58:21 +02:00
})
2022-07-06 23:19:05 +02:00
t.OnAdminsListRequest().Add(func(e *core.AdminsListEvent) error {
return t.registerEventCall("OnAdminsListRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminViewRequest().Add(func(e *core.AdminViewEvent) error {
return t.registerEventCall("OnAdminViewRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminBeforeCreateRequest().Add(func(e *core.AdminCreateEvent) error {
return t.registerEventCall("OnAdminBeforeCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminAfterCreateRequest().Add(func(e *core.AdminCreateEvent) error {
return t.registerEventCall("OnAdminAfterCreateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminBeforeUpdateRequest().Add(func(e *core.AdminUpdateEvent) error {
return t.registerEventCall("OnAdminBeforeUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminAfterUpdateRequest().Add(func(e *core.AdminUpdateEvent) error {
return t.registerEventCall("OnAdminAfterUpdateRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminBeforeDeleteRequest().Add(func(e *core.AdminDeleteEvent) error {
return t.registerEventCall("OnAdminBeforeDeleteRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminAfterDeleteRequest().Add(func(e *core.AdminDeleteEvent) error {
return t.registerEventCall("OnAdminAfterDeleteRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminAuthRequest().Add(func(e *core.AdminAuthEvent) error {
return t.registerEventCall("OnAdminAuthRequest")
2022-07-06 23:19:05 +02:00
})
t.OnAdminBeforeAuthWithPasswordRequest().Add(func(e *core.AdminAuthWithPasswordEvent) error {
return t.registerEventCall("OnAdminBeforeAuthWithPasswordRequest")
})
t.OnAdminAfterAuthWithPasswordRequest().Add(func(e *core.AdminAuthWithPasswordEvent) error {
return t.registerEventCall("OnAdminAfterAuthWithPasswordRequest")
})
t.OnAdminBeforeAuthRefreshRequest().Add(func(e *core.AdminAuthRefreshEvent) error {
return t.registerEventCall("OnAdminBeforeAuthRefreshRequest")
})
t.OnAdminAfterAuthRefreshRequest().Add(func(e *core.AdminAuthRefreshEvent) error {
return t.registerEventCall("OnAdminAfterAuthRefreshRequest")
})
t.OnAdminBeforeRequestPasswordResetRequest().Add(func(e *core.AdminRequestPasswordResetEvent) error {
return t.registerEventCall("OnAdminBeforeRequestPasswordResetRequest")
})
t.OnAdminAfterRequestPasswordResetRequest().Add(func(e *core.AdminRequestPasswordResetEvent) error {
return t.registerEventCall("OnAdminAfterRequestPasswordResetRequest")
})
t.OnAdminBeforeConfirmPasswordResetRequest().Add(func(e *core.AdminConfirmPasswordResetEvent) error {
return t.registerEventCall("OnAdminBeforeConfirmPasswordResetRequest")
})
t.OnAdminAfterConfirmPasswordResetRequest().Add(func(e *core.AdminConfirmPasswordResetEvent) error {
return t.registerEventCall("OnAdminAfterConfirmPasswordResetRequest")
})
2022-07-06 23:19:05 +02:00
t.OnFileDownloadRequest().Add(func(e *core.FileDownloadEvent) error {
return t.registerEventCall("OnFileDownloadRequest")
2022-07-06 23:19:05 +02:00
})
t.OnFileBeforeTokenRequest().Add(func(e *core.FileTokenEvent) error {
return t.registerEventCall("OnFileBeforeTokenRequest")
})
t.OnFileAfterTokenRequest().Add(func(e *core.FileTokenEvent) error {
return t.registerEventCall("OnFileAfterTokenRequest")
})
2022-07-06 23:19:05 +02:00
return t, nil
}
// TempDirClone creates a new temporary directory copy from the
2022-09-07 19:31:05 +02:00
// provided directory path.
2022-07-06 23:19:05 +02:00
//
2022-09-07 19:31:05 +02:00
// It is the caller's responsibility to call `os.RemoveAll(tempDir)`
// when the directory is no longer needed!
func TempDirClone(dirToClone string) (string, error) {
2022-07-06 23:19:05 +02:00
tempDir, err := os.MkdirTemp("", "pb_test_*")
if err != nil {
return "", err
}
// copy everything from testDataDir to tempDir
2022-09-07 19:31:05 +02:00
if err := copyDir(dirToClone, tempDir); err != nil {
2022-07-06 23:19:05 +02:00
return "", err
}
return tempDir, nil
}
// -------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------
func copyDir(src string, dest string) error {
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
return err
}
sourceDir, err := os.Open(src)
if err != nil {
return err
}
defer sourceDir.Close()
items, err := sourceDir.Readdir(-1)
if err != nil {
return err
}
for _, item := range items {
fullSrcPath := filepath.Join(src, item.Name())
fullDestPath := filepath.Join(dest, item.Name())
var copyErr error
if item.IsDir() {
copyErr = copyDir(fullSrcPath, fullDestPath)
} else {
copyErr = copyFile(fullSrcPath, fullDestPath)
}
if copyErr != nil {
return copyErr
}
}
return nil
}
func copyFile(src string, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer destFile.Close()
if _, err := io.Copy(destFile, srcFile); err != nil {
return err
}
return nil
}