updated tests
This commit is contained in:
@@ -7,6 +7,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestAdminLoginPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewAdminLogin(nil)
|
||||
}
|
||||
|
||||
func TestAdminLoginValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -8,6 +8,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
func TestAdminPasswordResetPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewAdminPasswordResetConfirm(nil)
|
||||
}
|
||||
|
||||
func TestAdminPasswordResetConfirmValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -7,6 +7,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestAdminPasswordResetRequestPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewAdminPasswordResetRequest(nil)
|
||||
}
|
||||
|
||||
func TestAdminPasswordResetRequestValidate(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
@@ -3,6 +3,7 @@ package forms_test
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
@@ -11,6 +12,29 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestAdminUpsertPanic1(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewAdminUpsert(nil, nil)
|
||||
}
|
||||
|
||||
func TestAdminUpsertPanic2(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewAdminUpsert(app, nil)
|
||||
}
|
||||
|
||||
func TestNewAdminUpsert(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
@@ -347,3 +371,99 @@ func TestAdminUpsertSubmitInterceptors(t *testing.T) {
|
||||
t.Fatalf("Expected the form model to be filled before calling the interceptors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminUpsertWithCustomId(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
existingAdmin, err := app.Dao().FindAdminByEmail("test@example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
scenarios := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
collection *models.Admin
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"empty data",
|
||||
"{}",
|
||||
&models.Admin{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty id",
|
||||
`{"id":""}`,
|
||||
&models.Admin{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"id < 15 chars",
|
||||
`{"id":"a23"}`,
|
||||
&models.Admin{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id > 15 chars",
|
||||
`{"id":"a234567890123456"}`,
|
||||
&models.Admin{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id = 15 chars",
|
||||
`{"id":"a23456789012345"}`,
|
||||
&models.Admin{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"changing the id of an existing item",
|
||||
`{"id":"b23456789012345"}`,
|
||||
existingAdmin,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"using the same existing item id",
|
||||
`{"id":"` + existingAdmin.Id + `"}`,
|
||||
existingAdmin,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"skipping the id for existing item",
|
||||
`{}`,
|
||||
existingAdmin,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
form := forms.NewAdminUpsert(app, scenario.collection)
|
||||
if form.Email == "" {
|
||||
form.Email = fmt.Sprintf("test_id_%d@example.com", i)
|
||||
}
|
||||
form.Password = "1234567890"
|
||||
form.PasswordConfirm = form.Password
|
||||
|
||||
// load data
|
||||
loadErr := json.Unmarshal([]byte(scenario.jsonData), form)
|
||||
if loadErr != nil {
|
||||
t.Errorf("[%s] Failed to load form data: %v", scenario.name, loadErr)
|
||||
continue
|
||||
}
|
||||
|
||||
submitErr := form.Submit()
|
||||
hasErr := submitErr != nil
|
||||
|
||||
if hasErr != scenario.expectError {
|
||||
t.Errorf("[%s] Expected hasErr to be %v, got %v (%v)", scenario.name, scenario.expectError, hasErr, submitErr)
|
||||
}
|
||||
|
||||
if !hasErr && form.Id != "" {
|
||||
_, err := app.Dao().FindAdminById(form.Id)
|
||||
if err != nil {
|
||||
t.Errorf("[%s] Expected to find record with id %s, got %v", scenario.name, form.Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,33 @@ import (
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/models/schema"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
func TestCollectionUpsertPanic1(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewCollectionUpsert(nil, nil)
|
||||
}
|
||||
|
||||
func TestCollectionUpsertPanic2(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewCollectionUpsert(app, nil)
|
||||
}
|
||||
|
||||
func TestNewCollectionUpsert(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
@@ -518,3 +542,101 @@ func TestCollectionUpsertSubmitInterceptors(t *testing.T) {
|
||||
t.Fatalf("Expected the form model to be filled before calling the interceptors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionUpsertWithCustomId(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
existingCollection, err := app.Dao().FindCollectionByNameOrId("demo3")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
newCollection := func() *models.Collection {
|
||||
return &models.Collection{
|
||||
Name: "c_" + security.RandomString(4),
|
||||
Schema: existingCollection.Schema,
|
||||
}
|
||||
}
|
||||
|
||||
scenarios := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
collection *models.Collection
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"empty data",
|
||||
"{}",
|
||||
newCollection(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty id",
|
||||
`{"id":""}`,
|
||||
newCollection(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"id < 15 chars",
|
||||
`{"id":"a23"}`,
|
||||
newCollection(),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id > 15 chars",
|
||||
`{"id":"a234567890123456"}`,
|
||||
newCollection(),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id = 15 chars",
|
||||
`{"id":"a23456789012345"}`,
|
||||
newCollection(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"changing the id of an existing item",
|
||||
`{"id":"b23456789012345"}`,
|
||||
existingCollection,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"using the same existing item id",
|
||||
`{"id":"` + existingCollection.Id + `"}`,
|
||||
existingCollection,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"skipping the id for existing item",
|
||||
`{}`,
|
||||
existingCollection,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
form := forms.NewCollectionUpsert(app, scenario.collection)
|
||||
|
||||
// load data
|
||||
loadErr := json.Unmarshal([]byte(scenario.jsonData), form)
|
||||
if loadErr != nil {
|
||||
t.Errorf("[%s] Failed to load form data: %v", scenario.name, loadErr)
|
||||
continue
|
||||
}
|
||||
|
||||
submitErr := form.Submit()
|
||||
hasErr := submitErr != nil
|
||||
|
||||
if hasErr != scenario.expectError {
|
||||
t.Errorf("[%s] Expected hasErr to be %v, got %v (%v)", scenario.name, scenario.expectError, hasErr, submitErr)
|
||||
}
|
||||
|
||||
if !hasErr && form.Id != "" {
|
||||
_, err := app.Dao().FindCollectionByNameOrId(form.Id)
|
||||
if err != nil {
|
||||
t.Errorf("[%s] Expected to find record with id %s, got %v", scenario.name, form.Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+68
-109
@@ -16,8 +16,8 @@ import (
|
||||
type CollectionsImport struct {
|
||||
config CollectionsImportConfig
|
||||
|
||||
Collections []*models.Collection `form:"collections" json:"collections"`
|
||||
DeleteOthers bool `form:"deleteOthers" json:"deleteOthers"`
|
||||
Collections []*models.Collection `form:"collections" json:"collections"`
|
||||
DeleteMissing bool `form:"deleteMissing" json:"deleteMissing"`
|
||||
}
|
||||
|
||||
// CollectionsImportConfig is the [CollectionsImport] factory initializer config.
|
||||
@@ -66,7 +66,7 @@ func (form *CollectionsImport) Validate() error {
|
||||
// - imports the form collections (create or replace)
|
||||
// - sync the collection changes with their related records table
|
||||
// - ensures the integrity of the imported structure (aka. run validations for each collection)
|
||||
// - if [form.DeleteOthers] is set, deletes all local collections that are not found in the imports list
|
||||
// - if [form.DeleteMissing] is set, deletes all local collections that are not found in the imports list
|
||||
//
|
||||
// All operations are wrapped in a single transaction that are
|
||||
// rollbacked on the first encountered error.
|
||||
@@ -78,116 +78,75 @@ func (form *CollectionsImport) Submit(interceptors ...InterceptorFunc) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return runInterceptors(form.submit, interceptors...)
|
||||
return runInterceptors(func() error {
|
||||
return form.config.TxDao.RunInTransaction(func(txDao *daos.Dao) error {
|
||||
importErr := txDao.ImportCollections(
|
||||
form.Collections,
|
||||
form.DeleteMissing,
|
||||
form.beforeRecordsSync,
|
||||
)
|
||||
if importErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validation failure
|
||||
if err, ok := importErr.(validation.Errors); ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// generic/db failure
|
||||
if form.config.App.IsDebug() {
|
||||
log.Println("Internal import failure:", importErr)
|
||||
}
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_failure",
|
||||
"Failed to import the collections configuration.",
|
||||
)}
|
||||
})
|
||||
}, interceptors...)
|
||||
}
|
||||
|
||||
func (form *CollectionsImport) submit() error {
|
||||
return form.config.TxDao.RunInTransaction(func(txDao *daos.Dao) error {
|
||||
oldCollections := []*models.Collection{}
|
||||
if err := txDao.CollectionQuery().All(&oldCollections); err != nil {
|
||||
return err
|
||||
func (form *CollectionsImport) beforeRecordsSync(txDao *daos.Dao, mappedNew, mappedOld map[string]*models.Collection) error {
|
||||
// refresh the actual persisted collections list
|
||||
refreshedCollections := []*models.Collection{}
|
||||
if err := txDao.CollectionQuery().OrderBy("created ASC").All(&refreshedCollections); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// trigger the validator for each existing collection to
|
||||
// ensure that the app is not left in a broken state
|
||||
for _, collection := range refreshedCollections {
|
||||
upsertModel := mappedOld[collection.GetId()]
|
||||
if upsertModel == nil {
|
||||
upsertModel = collection
|
||||
}
|
||||
|
||||
mappedOldCollections := make(map[string]*models.Collection, len(oldCollections))
|
||||
for _, old := range oldCollections {
|
||||
mappedOldCollections[old.GetId()] = old
|
||||
upsertForm := NewCollectionUpsertWithConfig(CollectionUpsertConfig{
|
||||
App: form.config.App,
|
||||
TxDao: txDao,
|
||||
}, upsertModel)
|
||||
|
||||
// load form fields with the refreshed collection state
|
||||
upsertForm.Id = collection.Id
|
||||
upsertForm.Name = collection.Name
|
||||
upsertForm.System = collection.System
|
||||
upsertForm.ListRule = collection.ListRule
|
||||
upsertForm.ViewRule = collection.ViewRule
|
||||
upsertForm.CreateRule = collection.CreateRule
|
||||
upsertForm.UpdateRule = collection.UpdateRule
|
||||
upsertForm.DeleteRule = collection.DeleteRule
|
||||
upsertForm.Schema = collection.Schema
|
||||
|
||||
if err := upsertForm.Validate(); err != nil {
|
||||
// serialize the validation error(s)
|
||||
serializedErr, _ := json.Marshal(err)
|
||||
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_validate_failure",
|
||||
fmt.Sprintf("Data validations failed for collection %q (%s): %s", collection.Name, collection.Id, serializedErr),
|
||||
)}
|
||||
}
|
||||
}
|
||||
|
||||
mappedFormCollections := make(map[string]*models.Collection, len(form.Collections))
|
||||
for _, collection := range form.Collections {
|
||||
mappedFormCollections[collection.GetId()] = collection
|
||||
}
|
||||
|
||||
// delete all other collections not sent with the import
|
||||
if form.DeleteOthers {
|
||||
for _, old := range oldCollections {
|
||||
if mappedFormCollections[old.GetId()] == nil {
|
||||
// delete the collection
|
||||
if err := txDao.DeleteCollection(old); err != nil {
|
||||
if form.config.App.IsDebug() {
|
||||
log.Println("[CollectionsImport] DeleteOthers failure", old.Name, err)
|
||||
}
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_collection_delete_failure",
|
||||
fmt.Sprintf("Failed to delete collection %q (%s). Make sure that the collection is not system or referenced by other collections.", old.Name, old.Id),
|
||||
)}
|
||||
}
|
||||
|
||||
delete(mappedOldCollections, old.GetId())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// raw insert/replace (aka. without any validations)
|
||||
// (required to make sure that all linked collections exists before running the validations)
|
||||
for _, collection := range form.Collections {
|
||||
if mappedOldCollections[collection.GetId()] == nil {
|
||||
collection.MarkAsNew()
|
||||
}
|
||||
|
||||
if err := txDao.Save(collection); err != nil {
|
||||
if form.config.App.IsDebug() {
|
||||
log.Println("[CollectionsImport] Save failure", collection.Name, err)
|
||||
}
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_save_failure",
|
||||
fmt.Sprintf("Integrity constraints failed - the collection %q (%s) cannot be imported.", collection.Name, collection.Id),
|
||||
)}
|
||||
}
|
||||
}
|
||||
|
||||
// refresh the actual persisted collections list
|
||||
refreshedCollections := []*models.Collection{}
|
||||
if err := txDao.CollectionQuery().All(&refreshedCollections); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// trigger the validator for each existing collection to
|
||||
// ensure that the app is not left in a broken state
|
||||
for _, collection := range refreshedCollections {
|
||||
upsertModel := mappedOldCollections[collection.GetId()]
|
||||
if upsertModel == nil {
|
||||
upsertModel = collection
|
||||
}
|
||||
upsertForm := NewCollectionUpsertWithConfig(CollectionUpsertConfig{
|
||||
App: form.config.App,
|
||||
TxDao: txDao,
|
||||
}, upsertModel)
|
||||
// load form fields with the refreshed collection state
|
||||
upsertForm.Id = collection.Id
|
||||
upsertForm.Name = collection.Name
|
||||
upsertForm.System = collection.System
|
||||
upsertForm.ListRule = collection.ListRule
|
||||
upsertForm.ViewRule = collection.ViewRule
|
||||
upsertForm.CreateRule = collection.CreateRule
|
||||
upsertForm.UpdateRule = collection.UpdateRule
|
||||
upsertForm.DeleteRule = collection.DeleteRule
|
||||
upsertForm.Schema = collection.Schema
|
||||
if err := upsertForm.Validate(); err != nil {
|
||||
// serialize the validation error(s)
|
||||
serializedErr, _ := json.Marshal(err)
|
||||
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_validate_failure",
|
||||
fmt.Sprintf("Data validations failed for collection %q (%s): %s", collection.Name, collection.Id, serializedErr),
|
||||
)}
|
||||
}
|
||||
}
|
||||
|
||||
// sync the records table for each updated collection
|
||||
for _, collection := range form.Collections {
|
||||
oldCollection := mappedOldCollections[collection.GetId()]
|
||||
if err := txDao.SyncRecordTableSchema(collection, oldCollection); err != nil {
|
||||
if form.config.App.IsDebug() {
|
||||
log.Println("[CollectionsImport] Records table sync failure", collection.Name, err)
|
||||
}
|
||||
return validation.Errors{"collections": validation.NewError(
|
||||
"collections_import_records_table_sync_failure",
|
||||
fmt.Sprintf("Failed to sync the records table changes for collection %q.", collection.Name),
|
||||
)}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
package forms_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/forms"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestCollectionsImportPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewCollectionsImport(nil)
|
||||
}
|
||||
|
||||
func TestCollectionsImportValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
form := forms.NewCollectionsImport(app)
|
||||
|
||||
scenarios := []struct {
|
||||
collections []*models.Collection
|
||||
expectError bool
|
||||
}{
|
||||
{nil, true},
|
||||
{[]*models.Collection{}, true},
|
||||
{[]*models.Collection{{}}, false},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
form.Collections = s.collections
|
||||
|
||||
err := form.Validate()
|
||||
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, s.expectError, hasErr, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionsImportSubmit(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
expectError bool
|
||||
expectCollectionsCount int
|
||||
expectEvents map[string]int
|
||||
}{
|
||||
{
|
||||
name: "empty collections",
|
||||
jsonData: `{
|
||||
"deleteMissing": true,
|
||||
"collections": []
|
||||
}`,
|
||||
expectError: true,
|
||||
expectCollectionsCount: 5,
|
||||
expectEvents: nil,
|
||||
},
|
||||
{
|
||||
name: "one of the collections has invalid data",
|
||||
jsonData: `{
|
||||
"collections": [
|
||||
{
|
||||
"name": "import1",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "import 2",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: true,
|
||||
expectCollectionsCount: 5,
|
||||
expectEvents: map[string]int{
|
||||
"OnModelBeforeCreate": 2,
|
||||
"OnModelAfterCreate": 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "all imported collections has valid data",
|
||||
jsonData: `{
|
||||
"collections": [
|
||||
{
|
||||
"name": "import1",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "import2",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: false,
|
||||
expectCollectionsCount: 7,
|
||||
expectEvents: map[string]int{
|
||||
"OnModelBeforeCreate": 2,
|
||||
"OnModelAfterCreate": 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "new collection with existing name",
|
||||
jsonData: `{
|
||||
"collections": [
|
||||
{
|
||||
"name": "demo2",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: true,
|
||||
expectCollectionsCount: 5,
|
||||
expectEvents: map[string]int{
|
||||
"OnModelBeforeCreate": 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "delete system + modified + new collection",
|
||||
jsonData: `{
|
||||
"deleteMissing": true,
|
||||
"collections": [
|
||||
{
|
||||
"id":"3f2888f8-075d-49fe-9d09-ea7e951000dc",
|
||||
"name":"demo",
|
||||
"schema":[
|
||||
{
|
||||
"id":"_2hlxbmp",
|
||||
"name":"title",
|
||||
"type":"text",
|
||||
"system":false,
|
||||
"required":true,
|
||||
"unique":false,
|
||||
"options":{
|
||||
"min":3,
|
||||
"max":null,
|
||||
"pattern":""
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "import1",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: true,
|
||||
expectCollectionsCount: 5,
|
||||
},
|
||||
{
|
||||
name: "modified + new collection",
|
||||
jsonData: `{
|
||||
"collections": [
|
||||
{
|
||||
"id":"3f2888f8-075d-49fe-9d09-ea7e951000dc",
|
||||
"name":"demo",
|
||||
"schema":[
|
||||
{
|
||||
"id":"_2hlxbmp",
|
||||
"name":"title",
|
||||
"type":"text",
|
||||
"system":false,
|
||||
"required":true,
|
||||
"unique":false,
|
||||
"options":{
|
||||
"min":3,
|
||||
"max":null,
|
||||
"pattern":""
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "import1",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "import2",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: false,
|
||||
expectCollectionsCount: 7,
|
||||
expectEvents: map[string]int{
|
||||
"OnModelBeforeUpdate": 1,
|
||||
"OnModelAfterUpdate": 1,
|
||||
"OnModelBeforeCreate": 2,
|
||||
"OnModelAfterCreate": 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "delete non-system + modified + new collection",
|
||||
jsonData: `{
|
||||
"deleteMissing": true,
|
||||
"collections": [
|
||||
{
|
||||
"id":"abe78266-fd4d-4aea-962d-8c0138ac522b",
|
||||
"name":"profiles",
|
||||
"system":true,
|
||||
"listRule":"userId = @request.user.id",
|
||||
"viewRule":"created > 'test_change'",
|
||||
"createRule":"userId = @request.user.id",
|
||||
"updateRule":"userId = @request.user.id",
|
||||
"deleteRule":"userId = @request.user.id",
|
||||
"schema":[
|
||||
{
|
||||
"id":"koih1lqx",
|
||||
"name":"userId",
|
||||
"type":"user",
|
||||
"system":true,
|
||||
"required":true,
|
||||
"unique":true,
|
||||
"options":{
|
||||
"maxSelect":1,
|
||||
"cascadeDelete":true
|
||||
}
|
||||
},
|
||||
{
|
||||
"id":"69ycbg3q",
|
||||
"name":"rel",
|
||||
"type":"relation",
|
||||
"system":false,
|
||||
"required":false,
|
||||
"unique":false,
|
||||
"options":{
|
||||
"maxSelect":2,
|
||||
"collectionId":"abe78266-fd4d-4aea-962d-8c0138ac522b",
|
||||
"cascadeDelete":false
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id":"3f2888f8-075d-49fe-9d09-ea7e951000dc",
|
||||
"name":"demo",
|
||||
"schema":[
|
||||
{
|
||||
"id":"_2hlxbmp",
|
||||
"name":"title",
|
||||
"type":"text",
|
||||
"system":false,
|
||||
"required":true,
|
||||
"unique":false,
|
||||
"options":{
|
||||
"min":3,
|
||||
"max":null,
|
||||
"pattern":""
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "test_deleted_collection_name_reuse",
|
||||
"name": "demo2",
|
||||
"schema": [
|
||||
{
|
||||
"id":"fz6iql2m",
|
||||
"name":"active",
|
||||
"type":"bool"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}`,
|
||||
expectError: false,
|
||||
expectCollectionsCount: 3,
|
||||
expectEvents: map[string]int{
|
||||
"OnModelBeforeUpdate": 2,
|
||||
"OnModelAfterUpdate": 2,
|
||||
"OnModelBeforeCreate": 1,
|
||||
"OnModelAfterCreate": 1,
|
||||
"OnModelBeforeDelete": 3,
|
||||
"OnModelAfterDelete": 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
form := forms.NewCollectionsImport(testApp)
|
||||
|
||||
// load data
|
||||
loadErr := json.Unmarshal([]byte(s.jsonData), form)
|
||||
if loadErr != nil {
|
||||
t.Errorf("[%s] Failed to load form data: %v", s.name, loadErr)
|
||||
continue
|
||||
}
|
||||
|
||||
err := form.Submit()
|
||||
|
||||
hasErr := err != nil
|
||||
if hasErr != s.expectError {
|
||||
t.Errorf("[%s] Expected hasErr to be %v, got %v (%v)", s.name, s.expectError, hasErr, err)
|
||||
}
|
||||
|
||||
// check collections count
|
||||
collections := []*models.Collection{}
|
||||
if err := testApp.Dao().CollectionQuery().All(&collections); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(collections) != s.expectCollectionsCount {
|
||||
t.Errorf("[%s] Expected %d collections, got %d", s.name, s.expectCollectionsCount, len(collections))
|
||||
}
|
||||
|
||||
// check events
|
||||
if len(testApp.EventCalls) > len(s.expectEvents) {
|
||||
t.Errorf("[%s] Expected events %v, got %v", s.name, s.expectEvents, testApp.EventCalls)
|
||||
}
|
||||
for event, expectedCalls := range s.expectEvents {
|
||||
actualCalls := testApp.EventCalls[event]
|
||||
if actualCalls != expectedCalls {
|
||||
t.Errorf("[%s] Expected event %s to be called %d, got %d", s.name, event, expectedCalls, actualCalls)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionsImportSubmitInterceptors(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
collections := []*models.Collection{}
|
||||
if err := app.Dao().CollectionQuery().All(&collections); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
form := forms.NewCollectionsImport(app)
|
||||
form.Collections = collections
|
||||
|
||||
testErr := errors.New("test_error")
|
||||
|
||||
interceptor1Called := false
|
||||
interceptor1 := func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
|
||||
return func() error {
|
||||
interceptor1Called = true
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
||||
interceptor2Called := false
|
||||
interceptor2 := func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
|
||||
return func() error {
|
||||
interceptor2Called = true
|
||||
return testErr
|
||||
}
|
||||
}
|
||||
|
||||
submitErr := form.Submit(interceptor1, interceptor2)
|
||||
if submitErr != testErr {
|
||||
t.Fatalf("Expected submitError %v, got %v", testErr, submitErr)
|
||||
}
|
||||
|
||||
if !interceptor1Called {
|
||||
t.Fatalf("Expected interceptor1 to be called")
|
||||
}
|
||||
|
||||
if !interceptor2Called {
|
||||
t.Fatalf("Expected interceptor2 to be called")
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -43,7 +43,7 @@ type RecordUpsertConfig struct {
|
||||
|
||||
// NewRecordUpsert creates a new [RecordUpsert] form with initializer
|
||||
// config created from the provided [core.App] and [models.Record] instances
|
||||
// (for create you could pass a pointer to an empty Record - `&models.Record{}`).
|
||||
// (for create you could pass a pointer to an empty Record - `models.NewRecord(collection)`).
|
||||
//
|
||||
// If you want to submit the form as part of another transaction, use
|
||||
// [NewRecordUpsertWithConfig] with explicitly set TxDao.
|
||||
@@ -55,7 +55,7 @@ func NewRecordUpsert(app core.App, record *models.Record) *RecordUpsert {
|
||||
|
||||
// NewRecordUpsertWithConfig creates a new [RecordUpsert] form
|
||||
// with the provided config and [models.Record] instance or panics on invalid configuration
|
||||
// (for create you could pass a pointer to an empty Record - `&models.Record{}`).
|
||||
// (for create you could pass a pointer to an empty Record - `models.NewRecord(collection)`).
|
||||
func NewRecordUpsertWithConfig(config RecordUpsertConfig, record *models.Record) *RecordUpsert {
|
||||
form := &RecordUpsert{
|
||||
config: config,
|
||||
@@ -302,8 +302,10 @@ func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error
|
||||
return err
|
||||
}
|
||||
|
||||
isNew := form.record.IsNew()
|
||||
|
||||
// custom insertion id can be set only on create
|
||||
if form.record.IsNew() && form.Id != "" {
|
||||
if isNew && form.Id != "" {
|
||||
form.record.MarkAsNew()
|
||||
form.record.SetId(form.Id)
|
||||
}
|
||||
@@ -319,6 +321,7 @@ func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error
|
||||
return errors.New("failed to get transaction db")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
txDao.BeforeCreateFunc = nil
|
||||
txDao.AfterCreateFunc = nil
|
||||
txDao.BeforeUpdateFunc = nil
|
||||
@@ -328,7 +331,16 @@ func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error
|
||||
return err
|
||||
}
|
||||
|
||||
return callback(txDao)
|
||||
// restore record isNew state
|
||||
if isNew {
|
||||
form.record.MarkAsNew()
|
||||
}
|
||||
|
||||
if callback != nil {
|
||||
return callback(txDao)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+136
-1
@@ -20,6 +20,29 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/list"
|
||||
)
|
||||
|
||||
func TestRecordUpsertPanic1(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewRecordUpsert(nil, nil)
|
||||
}
|
||||
|
||||
func TestRecordUpsertPanic2(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewRecordUpsert(app, nil)
|
||||
}
|
||||
|
||||
func TestNewRecordUpsert(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
@@ -32,7 +55,7 @@ func TestNewRecordUpsert(t *testing.T) {
|
||||
|
||||
val := form.Data["title"]
|
||||
if val != "test_value" {
|
||||
t.Errorf("Expected record data to be load, got %v", form.Data)
|
||||
t.Errorf("Expected record data to be loaded, got %v", form.Data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +91,7 @@ func TestRecordUpsertLoadDataJson(t *testing.T) {
|
||||
}
|
||||
|
||||
testData := map[string]any{
|
||||
"id": "test_id",
|
||||
"title": "test123",
|
||||
"unknown": "test456",
|
||||
// file fields unset/delete
|
||||
@@ -86,6 +110,10 @@ func TestRecordUpsertLoadDataJson(t *testing.T) {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
|
||||
if form.Id != "test_id" {
|
||||
t.Fatalf("Expect id field to be %q, got %q", "test_id", form.Id)
|
||||
}
|
||||
|
||||
if v, ok := form.Data["title"]; !ok || v != "test123" {
|
||||
t.Fatalf("Expect title field to be %q, got %q", "test123", v)
|
||||
}
|
||||
@@ -134,6 +162,7 @@ func TestRecordUpsertLoadDataMultipart(t *testing.T) {
|
||||
}
|
||||
|
||||
formData, mp, err := tests.MockMultipartData(map[string]string{
|
||||
"id": "test_id",
|
||||
"title": "test123",
|
||||
"unknown": "test456",
|
||||
// file fields unset/delete
|
||||
@@ -154,6 +183,10 @@ func TestRecordUpsertLoadDataMultipart(t *testing.T) {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
|
||||
if form.Id != "test_id" {
|
||||
t.Fatalf("Expect id field to be %q, got %q", "test_id", form.Id)
|
||||
}
|
||||
|
||||
if v, ok := form.Data["title"]; !ok || v != "test123" {
|
||||
t.Fatalf("Expect title field to be %q, got %q", "test123", v)
|
||||
}
|
||||
@@ -202,6 +235,7 @@ func TestRecordUpsertValidateFailure(t *testing.T) {
|
||||
|
||||
// try with invalid test data to check whether the RecordDataValidator is triggered
|
||||
formData, mp, err := tests.MockMultipartData(map[string]string{
|
||||
"id": "",
|
||||
"unknown": "test456", // should be ignored
|
||||
"title": "a",
|
||||
"onerel": "00000000-84ab-4057-a592-4604a731f78f",
|
||||
@@ -247,6 +281,7 @@ func TestRecordUpsertValidateSuccess(t *testing.T) {
|
||||
}
|
||||
|
||||
formData, mp, err := tests.MockMultipartData(map[string]string{
|
||||
"id": record.Id,
|
||||
"unknown": "test456", // should be ignored
|
||||
"title": "abc",
|
||||
"onerel": "054f9f24-0a0a-4e09-87b1-bc7ff2b336a2",
|
||||
@@ -576,3 +611,103 @@ func hasRecordFile(app core.App, record *models.Record, filename string) bool {
|
||||
|
||||
return exists
|
||||
}
|
||||
|
||||
func TestRecordUpsertWithCustomId(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
collection, _ := app.Dao().FindCollectionByNameOrId("demo3")
|
||||
existingRecord, err := app.Dao().FindFirstRecordByData(collection, "id", "2c542824-9de1-42fe-8924-e57c86267760")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
scenarios := []struct {
|
||||
name string
|
||||
data map[string]string
|
||||
record *models.Record
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"empty data",
|
||||
map[string]string{},
|
||||
models.NewRecord(collection),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty id",
|
||||
map[string]string{"id": ""},
|
||||
models.NewRecord(collection),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"id < 15 chars",
|
||||
map[string]string{"id": "a23"},
|
||||
models.NewRecord(collection),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id > 15 chars",
|
||||
map[string]string{"id": "a234567890123456"},
|
||||
models.NewRecord(collection),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id = 15 chars",
|
||||
map[string]string{"id": "a23456789012345"},
|
||||
models.NewRecord(collection),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"changing the id of an existing record",
|
||||
map[string]string{"id": "b23456789012345"},
|
||||
existingRecord,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"using the same existing record id",
|
||||
map[string]string{"id": existingRecord.Id},
|
||||
existingRecord,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"skipping the id for existing record",
|
||||
map[string]string{},
|
||||
existingRecord,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
formData, mp, err := tests.MockMultipartData(scenario.data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
form := forms.NewRecordUpsert(app, scenario.record)
|
||||
req := httptest.NewRequest(http.MethodGet, "/", formData)
|
||||
req.Header.Set(echo.HeaderContentType, mp.FormDataContentType())
|
||||
form.LoadData(req)
|
||||
|
||||
dryErr := form.DrySubmit(nil)
|
||||
hasDryErr := dryErr != nil
|
||||
|
||||
submitErr := form.Submit()
|
||||
hasSubmitErr := submitErr != nil
|
||||
|
||||
if hasDryErr != hasSubmitErr {
|
||||
t.Errorf("[%s] Expected hasDryErr and hasSubmitErr to have the same value, got %v vs %v", scenario.name, hasDryErr, hasSubmitErr)
|
||||
}
|
||||
|
||||
if hasSubmitErr != scenario.expectError {
|
||||
t.Errorf("[%s] Expected hasSubmitErr to be %v, got %v (%v)", scenario.name, scenario.expectError, hasSubmitErr, submitErr)
|
||||
}
|
||||
|
||||
if id, ok := scenario.data["id"]; ok && id != "" && !hasSubmitErr {
|
||||
_, err := app.Dao().FindRecordById(collection, id, nil)
|
||||
if err != nil {
|
||||
t.Errorf("[%s] Expected to find record with id %s, got %v", scenario.name, id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
func TestSettingsUpsertPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewSettingsUpsert(nil)
|
||||
}
|
||||
|
||||
func TestNewSettingsUpsert(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -10,6 +10,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
func TestUserEmailChangeConfirmPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserEmailChangeConfirm(nil)
|
||||
}
|
||||
|
||||
func TestUserEmailChangeConfirmValidateAndSubmit(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -44,8 +44,8 @@ func NewUserEmailChangeRequestWithConfig(config UserEmailChangeRequestConfig, us
|
||||
user: user,
|
||||
}
|
||||
|
||||
if form.config.App == nil {
|
||||
panic("Missing required config.App instance.")
|
||||
if form.config.App == nil || form.user == nil {
|
||||
panic("Invalid initializer config or nil user model.")
|
||||
}
|
||||
|
||||
if form.config.TxDao == nil {
|
||||
|
||||
@@ -9,6 +9,29 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestUserEmailChangeRequestPanic1(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserEmailChangeRequest(nil, nil)
|
||||
}
|
||||
|
||||
func TestUserEmailChangeRequestPanic2(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserEmailChangeRequest(testApp, nil)
|
||||
}
|
||||
|
||||
func TestUserEmailChangeRequestValidateAndSubmit(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestUserEmailLoginPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserEmailLogin(nil)
|
||||
}
|
||||
|
||||
func TestUserEmailLoginValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestUserOauth2LoginPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserOauth2Login(nil)
|
||||
}
|
||||
|
||||
func TestUserOauth2LoginValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -10,6 +10,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
func TestUserPasswordResetConfirmPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserPasswordResetConfirm(nil)
|
||||
}
|
||||
|
||||
func TestUserPasswordResetConfirmValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -11,6 +11,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestUserPasswordResetRequestPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserPasswordResetRequest(nil)
|
||||
}
|
||||
|
||||
func TestUserPasswordResetRequestValidate(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
@@ -3,6 +3,7 @@ package forms_test
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
@@ -11,6 +12,29 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestUserUpsertPanic1(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserUpsert(nil, nil)
|
||||
}
|
||||
|
||||
func TestUserUpsertPanic2(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserUpsert(app, nil)
|
||||
}
|
||||
|
||||
func TestNewUserUpsert(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
@@ -304,3 +328,99 @@ func TestUserUpsertSubmitInterceptors(t *testing.T) {
|
||||
t.Fatalf("Expected the form model to be filled before calling the interceptors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserUpsertWithCustomId(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
existingUser, err := app.Dao().FindUserByEmail("test@example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
scenarios := []struct {
|
||||
name string
|
||||
jsonData string
|
||||
collection *models.User
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"empty data",
|
||||
"{}",
|
||||
&models.User{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty id",
|
||||
`{"id":""}`,
|
||||
&models.User{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"id < 15 chars",
|
||||
`{"id":"a23"}`,
|
||||
&models.User{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id > 15 chars",
|
||||
`{"id":"a234567890123456"}`,
|
||||
&models.User{},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"id = 15 chars",
|
||||
`{"id":"a23456789012345"}`,
|
||||
&models.User{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"changing the id of an existing item",
|
||||
`{"id":"b23456789012345"}`,
|
||||
existingUser,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"using the same existing item id",
|
||||
`{"id":"` + existingUser.Id + `"}`,
|
||||
existingUser,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"skipping the id for existing item",
|
||||
`{}`,
|
||||
existingUser,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, scenario := range scenarios {
|
||||
form := forms.NewUserUpsert(app, scenario.collection)
|
||||
if form.Email == "" {
|
||||
form.Email = fmt.Sprintf("test_id_%d@example.com", i)
|
||||
}
|
||||
form.Password = "1234567890"
|
||||
form.PasswordConfirm = form.Password
|
||||
|
||||
// load data
|
||||
loadErr := json.Unmarshal([]byte(scenario.jsonData), form)
|
||||
if loadErr != nil {
|
||||
t.Errorf("[%s] Failed to load form data: %v", scenario.name, loadErr)
|
||||
continue
|
||||
}
|
||||
|
||||
submitErr := form.Submit()
|
||||
hasErr := submitErr != nil
|
||||
|
||||
if hasErr != scenario.expectError {
|
||||
t.Errorf("[%s] Expected hasErr to be %v, got %v (%v)", scenario.name, scenario.expectError, hasErr, submitErr)
|
||||
}
|
||||
|
||||
if !hasErr && form.Id != "" {
|
||||
_, err := app.Dao().FindUserById(form.Id)
|
||||
if err != nil {
|
||||
t.Errorf("[%s] Expected to find record with id %s, got %v", scenario.name, form.Id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
)
|
||||
|
||||
func TestUserVerificationConfirmPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserVerificationConfirm(nil)
|
||||
}
|
||||
|
||||
func TestUserVerificationConfirmValidate(t *testing.T) {
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
@@ -11,6 +11,16 @@ import (
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestUserVerificationRequestPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Fatal("The form did not panic")
|
||||
}
|
||||
}()
|
||||
|
||||
forms.NewUserVerificationRequest(nil)
|
||||
}
|
||||
|
||||
func TestUserVerificationRequestValidate(t *testing.T) {
|
||||
testApp, _ := tests.NewTestApp()
|
||||
defer testApp.Cleanup()
|
||||
|
||||
Reference in New Issue
Block a user