2022-07-09 16:17:41 +02:00
|
|
|
// 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"
|
2022-12-12 19:21:41 +02:00
|
|
|
"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
|
|
|
|
|
2022-12-12 19:21:41 +02:00
|
|
|
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.
|
2022-11-26 22:33:27 +02:00
|
|
|
//
|
|
|
|
// 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-11-26 22:33:27 +02:00
|
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
t.TestMailer.Reset()
|
2022-12-12 19:21:41 +02:00
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
return t.TestMailer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetEventCalls resets the EventCalls counter.
|
|
|
|
func (t *TestApp) ResetEventCalls() {
|
2022-12-12 19:21:41 +02:00
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
t.EventCalls = make(map[string]int)
|
|
|
|
}
|
|
|
|
|
2022-12-12 19:21:41 +02:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2022-09-07 19:34:18 +02:00
|
|
|
tempDir, err := TempDirClone(testDataDir)
|
2022-07-06 23:19:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-15 16:42:35 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnBeforeApiError")
|
2022-12-02 16:36:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAfterApiError().Add(func(e *core.ApiErrorEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelBeforeCreate")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnModelAfterCreate().Add(func(e *core.ModelEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelAfterCreate")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnModelBeforeUpdate().Add(func(e *core.ModelEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelBeforeUpdate")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnModelAfterUpdate().Add(func(e *core.ModelEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelAfterUpdate")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnModelBeforeDelete().Add(func(e *core.ModelEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelBeforeDelete")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnModelAfterDelete().Add(func(e *core.ModelEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnModelAfterDelete")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordsListRequest().Add(func(e *core.RecordsListEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordsListRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordViewRequest().Add(func(e *core.RecordViewEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordViewRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterUpdateRequest().Add(func(e *core.RecordUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeDeleteRequest().Add(func(e *core.RecordDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeDeleteRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterDeleteRequest().Add(func(e *core.RecordDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAuthRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2023-01-15 17:00:28 +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")
|
|
|
|
})
|
|
|
|
|
2022-12-03 14:50:02 +02:00
|
|
|
t.OnRecordBeforeRequestPasswordResetRequest().Add(func(e *core.RecordRequestPasswordResetEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeRequestPasswordResetRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterRequestPasswordResetRequest().Add(func(e *core.RecordRequestPasswordResetEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterRequestPasswordResetRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeConfirmPasswordResetRequest().Add(func(e *core.RecordConfirmPasswordResetEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeConfirmPasswordResetRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterConfirmPasswordResetRequest().Add(func(e *core.RecordConfirmPasswordResetEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterConfirmPasswordResetRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeRequestVerificationRequest().Add(func(e *core.RecordRequestVerificationEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeRequestVerificationRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterRequestVerificationRequest().Add(func(e *core.RecordRequestVerificationEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterRequestVerificationRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeConfirmVerificationRequest().Add(func(e *core.RecordConfirmVerificationEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeConfirmVerificationRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterConfirmVerificationRequest().Add(func(e *core.RecordConfirmVerificationEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterConfirmVerificationRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeRequestEmailChangeRequest().Add(func(e *core.RecordRequestEmailChangeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeRequestEmailChangeRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterRequestEmailChangeRequest().Add(func(e *core.RecordRequestEmailChangeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterRequestEmailChangeRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordBeforeConfirmEmailChangeRequest().Add(func(e *core.RecordConfirmEmailChangeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordBeforeConfirmEmailChangeRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRecordAfterConfirmEmailChangeRequest().Add(func(e *core.RecordConfirmEmailChangeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterConfirmEmailChangeRequest")
|
2022-12-03 14:50:02 +02:00
|
|
|
})
|
|
|
|
|
2022-11-17 14:17:10 +02:00
|
|
|
t.OnRecordListExternalAuthsRequest().Add(func(e *core.RecordListExternalAuthsEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRecordAfterUnlinkExternalAuthRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnMailerBeforeAdminResetPasswordSend().Add(func(e *core.MailerAdminEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnMailerBeforeAdminResetPasswordSend")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnMailerAfterAdminResetPasswordSend().Add(func(e *core.MailerAdminEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnMailerAfterRecordChangeEmailSend")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRealtimeConnectRequest().Add(func(e *core.RealtimeConnectEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeConnectRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2022-12-02 14:25:36 +02:00
|
|
|
t.OnRealtimeDisconnectRequest().Add(func(e *core.RealtimeDisconnectEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeDisconnectRequest")
|
2022-12-02 14:25:36 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRealtimeBeforeMessageSend().Add(func(e *core.RealtimeMessageEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeBeforeMessageSend")
|
2022-12-02 14:25:36 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRealtimeAfterMessageSend().Add(func(e *core.RealtimeMessageEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeAfterMessageSend")
|
2022-12-02 14:25:36 +02:00
|
|
|
})
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
t.OnRealtimeBeforeSubscribeRequest().Add(func(e *core.RealtimeSubscribeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeBeforeSubscribeRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnRealtimeAfterSubscribeRequest().Add(func(e *core.RealtimeSubscribeEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnRealtimeAfterSubscribeRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnSettingsListRequest().Add(func(e *core.SettingsListEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnSettingsListRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnSettingsBeforeUpdateRequest().Add(func(e *core.SettingsUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnSettingsBeforeUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnSettingsAfterUpdateRequest().Add(func(e *core.SettingsUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnSettingsAfterUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionsListRequest().Add(func(e *core.CollectionsListEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionsListRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionViewRequest().Add(func(e *core.CollectionViewEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionViewRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionBeforeCreateRequest().Add(func(e *core.CollectionCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionBeforeCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionAfterCreateRequest().Add(func(e *core.CollectionCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionAfterCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionBeforeUpdateRequest().Add(func(e *core.CollectionUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionBeforeUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionAfterUpdateRequest().Add(func(e *core.CollectionUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionAfterUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionBeforeDeleteRequest().Add(func(e *core.CollectionDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionBeforeDeleteRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionAfterDeleteRequest().Add(func(e *core.CollectionDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnCollectionsBeforeImportRequest")
|
2022-08-07 19:58:21 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnCollectionsAfterImportRequest().Add(func(e *core.CollectionsImportEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminsListRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminViewRequest().Add(func(e *core.AdminViewEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminViewRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminBeforeCreateRequest().Add(func(e *core.AdminCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminBeforeCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminAfterCreateRequest().Add(func(e *core.AdminCreateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminAfterCreateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminBeforeUpdateRequest().Add(func(e *core.AdminUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminBeforeUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminAfterUpdateRequest().Add(func(e *core.AdminUpdateEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminAfterUpdateRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminBeforeDeleteRequest().Add(func(e *core.AdminDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminBeforeDeleteRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminAfterDeleteRequest().Add(func(e *core.AdminDeleteEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminAfterDeleteRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.OnAdminAuthRequest().Add(func(e *core.AdminAuthEvent) error {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnAdminAuthRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2023-01-15 17:00:28 +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 {
|
2022-12-12 19:21:41 +02:00
|
|
|
return t.registerEventCall("OnFileDownloadRequest")
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2023-04-04 19:33:35 +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
|
|
|
|
}
|
|
|
|
|
2022-09-07 19:34:18 +02:00
|
|
|
// 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!
|
2022-09-07 19:34:18 +02:00
|
|
|
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
|
|
|
|
}
|