1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-27 00:20:27 +02:00

added onlyVerified auth collection option

This commit is contained in:
Gani Georgiev
2023-12-06 11:57:04 +02:00
parent 865865fdeb
commit 31317df21c
43 changed files with 591 additions and 446 deletions

View File

@@ -1,6 +1,7 @@
package migratecmd_test
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -48,6 +49,7 @@ migrate((db) => {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}
});
@@ -101,6 +103,7 @@ func init() {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}
}` + "`" + `
@@ -127,63 +130,65 @@ func init() {
}
for i, s := range scenarios {
app, _ := tests.NewTestApp()
defer app.Cleanup()
t.Run(fmt.Sprintf("s%d", i), func(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
})
// @todo remove after collections cache is replaced
app.Bootstrap()
collection := &models.Collection{}
collection.Id = "new_id"
collection.Name = "new_name"
collection.Type = models.CollectionTypeAuth
collection.System = true
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.Indexes = types.JsonArray[string]{"create index test on new_name (id)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
if err := app.Dao().SaveCollection(collection); err != nil {
t.Fatalf("Failed to save collection, got %v", err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("Expected migrationsDir to be created, got: %v", err)
}
if total := len(files); total != 1 {
t.Fatalf("Expected 1 file to be generated, got %d", total)
}
expectedName := "_created_new_name." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("Expected filename to contains %q, got %q", expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("Failed to read the generated migration file: %v", err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("Expected template \n%v \ngot \n%v", s.expectedTemplate, v)
}
})
// @todo remove after collections cache is replaced
app.Bootstrap()
collection := &models.Collection{}
collection.Id = "new_id"
collection.Name = "new_name"
collection.Type = models.CollectionTypeAuth
collection.System = true
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.Indexes = types.JsonArray[string]{"create index test on new_name (id)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
if err := app.Dao().SaveCollection(collection); err != nil {
t.Fatalf("[%d] Failed to save collection, got %v", i, err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("[%d] Expected migrationsDir to be created, got: %v", i, err)
}
if total := len(files); total != 1 {
t.Fatalf("[%d] Expected 1 file to be generated, got %d", i, total)
}
expectedName := "_created_new_name." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("[%d] Expected filename to contains %q, got %q", i, expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("[%d] Failed to read the generated migration file: %v", i, err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("[%d] Expected template \n%v \ngot \n%v", i, s.expectedTemplate, v)
}
}
}
@@ -226,6 +231,7 @@ migrate((db) => {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}
});
@@ -283,6 +289,7 @@ func init() {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}
}` + "`" + `
@@ -300,69 +307,71 @@ func init() {
}
for i, s := range scenarios {
app, _ := tests.NewTestApp()
defer app.Cleanup()
t.Run(fmt.Sprintf("s%d", i), func(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
})
// create dummy collection
collection := &models.Collection{}
collection.Id = "test123"
collection.Name = "test456"
collection.Type = models.CollectionTypeAuth
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.Indexes = types.JsonArray[string]{"create index test on test456 (id)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
// use different dao to avoid triggering automigrate while saving the dummy collection
if err := daos.New(app.DB()).SaveCollection(collection); err != nil {
t.Fatalf("Failed to save dummy collection, got %v", err)
}
// @todo remove after collections cache is replaced
app.Bootstrap()
// delete the newly created dummy collection
if err := app.Dao().DeleteCollection(collection); err != nil {
t.Fatalf("Failed to delete dummy collection, got %v", err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("Expected migrationsDir to be created, got: %v", err)
}
if total := len(files); total != 1 {
t.Fatalf("Expected 1 file to be generated, got %d", total)
}
expectedName := "_deleted_test456." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("Expected filename to contains %q, got %q", expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("Failed to read the generated migration file: %v", err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("Expected template \n%v \ngot \n%v", s.expectedTemplate, v)
}
})
// create dummy collection
collection := &models.Collection{}
collection.Id = "test123"
collection.Name = "test456"
collection.Type = models.CollectionTypeAuth
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0 || 'backtick`test' = 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.Indexes = types.JsonArray[string]{"create index test on test456 (id)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
// use different dao to avoid triggering automigrate while saving the dummy collection
if err := daos.New(app.DB()).SaveCollection(collection); err != nil {
t.Fatalf("[%d] Failed to save dummy collection, got %v", i, err)
}
// @todo remove after collections cache is replaced
app.Bootstrap()
// delete the newly created dummy collection
if err := app.Dao().DeleteCollection(collection); err != nil {
t.Fatalf("[%d] Failed to delete dummy collection, got %v", i, err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("[%d] Expected migrationsDir to be created, got: %v", i, err)
}
if total := len(files); total != 1 {
t.Fatalf("[%d] Expected 1 file to be generated, got %d", i, total)
}
expectedName := "_deleted_test456." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("[%d] Expected filename to contains %q, got %q", i, expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("[%d] Failed to read the generated migration file: %v", i, err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("[%d] Expected template \n%v \ngot \n%v", i, s.expectedTemplate, v)
}
}
}
@@ -444,6 +453,7 @@ migrate((db) => {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}
collection.indexes = [
@@ -598,6 +608,7 @@ func init() {
"manageRule": "created > 0",
"minPasswordLength": 20,
"onlyEmailDomains": null,
"onlyVerified": false,
"requireEmail": false
}` + "`" + `), &options)
collection.SetOptions(options)
@@ -649,116 +660,118 @@ func init() {
}
for i, s := range scenarios {
app, _ := tests.NewTestApp()
defer app.Cleanup()
t.Run(fmt.Sprintf("s%d", i), func(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migrationsDir := filepath.Join(app.DataDir(), "_test_migrations")
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
migratecmd.MustRegister(app, nil, migratecmd.Config{
TemplateLang: s.lang,
Automigrate: true,
Dir: migrationsDir,
})
// create dummy collection
collection := &models.Collection{}
collection.Id = "test123"
collection.Name = "test456"
collection.Type = models.CollectionTypeAuth
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.UpdateRule = types.Pointer(`id = "2"`)
collection.CreateRule = nil
collection.DeleteRule = types.Pointer(`id = "3"`)
collection.Indexes = types.JsonArray[string]{"create index test1 on test456 (f1_name)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
collection.Schema.AddField(&schema.SchemaField{
Id: "f1_id",
Name: "f1_name",
Type: schema.FieldTypeText,
Required: true,
})
collection.Schema.AddField(&schema.SchemaField{
Id: "f2_id",
Name: "f2_name",
Type: schema.FieldTypeNumber,
Unique: true,
Options: &schema.NumberOptions{
Min: types.Pointer(10.0),
},
})
collection.Schema.AddField(&schema.SchemaField{
Id: "f3_id",
Name: "f3_name",
Type: schema.FieldTypeBool,
})
// use different dao to avoid triggering automigrate while saving the dummy collection
if err := daos.New(app.DB()).SaveCollection(collection); err != nil {
t.Fatalf("Failed to save dummy collection, got %v", err)
}
// @todo remove after collections cache is replaced
app.Bootstrap()
collection.Name = "test456_update"
collection.Type = models.CollectionTypeBase
collection.DeleteRule = types.Pointer(`updated > 0 && @request.auth.id != ''`)
collection.ListRule = types.Pointer("@request.auth.id != ''")
collection.ViewRule = types.Pointer(`id = "1"`) // no change
collection.UpdateRule = types.Pointer(`id = "2_update"`)
collection.CreateRule = types.Pointer(`id = "nil_update"`)
collection.DeleteRule = nil
collection.Indexes = types.JsonArray[string]{
"create index test1 on test456_update (f1_name)",
}
collection.NormalizeOptions()
collection.Schema.RemoveField("f3_id")
collection.Schema.AddField(&schema.SchemaField{
Id: "f4_id",
Name: "f4_name",
Type: schema.FieldTypeText,
Options: &schema.TextOptions{
Pattern: "`test backtick`123",
},
})
f := collection.Schema.GetFieldById("f2_id")
f.Name = "f2_name_new"
// save the changes and trigger automigrate
if err := app.Dao().SaveCollection(collection); err != nil {
t.Fatalf("Failed to save dummy collection changes, got %v", err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("Expected migrationsDir to be created, got: %v", err)
}
if total := len(files); total != 1 {
t.Fatalf("Expected 1 file to be generated, got %d", total)
}
expectedName := "_updated_test456." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("Expected filename to contains %q, got %q", expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("Failed to read the generated migration file: %v", err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("Expected template \n%v \ngot \n%v", s.expectedTemplate, v)
}
})
// create dummy collection
collection := &models.Collection{}
collection.Id = "test123"
collection.Name = "test456"
collection.Type = models.CollectionTypeAuth
collection.Created, _ = types.ParseDateTime("2022-01-01 00:00:00.000Z")
collection.Updated = collection.Created
collection.ListRule = types.Pointer("@request.auth.id != '' && created > 0")
collection.ViewRule = types.Pointer(`id = "1"`)
collection.UpdateRule = types.Pointer(`id = "2"`)
collection.CreateRule = nil
collection.DeleteRule = types.Pointer(`id = "3"`)
collection.Indexes = types.JsonArray[string]{"create index test1 on test456 (f1_name)"}
collection.SetOptions(models.CollectionAuthOptions{
ManageRule: types.Pointer("created > 0"),
MinPasswordLength: 20,
})
collection.MarkAsNew()
collection.Schema.AddField(&schema.SchemaField{
Id: "f1_id",
Name: "f1_name",
Type: schema.FieldTypeText,
Required: true,
})
collection.Schema.AddField(&schema.SchemaField{
Id: "f2_id",
Name: "f2_name",
Type: schema.FieldTypeNumber,
Unique: true,
Options: &schema.NumberOptions{
Min: types.Pointer(10.0),
},
})
collection.Schema.AddField(&schema.SchemaField{
Id: "f3_id",
Name: "f3_name",
Type: schema.FieldTypeBool,
})
// use different dao to avoid triggering automigrate while saving the dummy collection
if err := daos.New(app.DB()).SaveCollection(collection); err != nil {
t.Fatalf("[%d] Failed to save dummy collection, got %v", i, err)
}
// @todo remove after collections cache is replaced
app.Bootstrap()
collection.Name = "test456_update"
collection.Type = models.CollectionTypeBase
collection.DeleteRule = types.Pointer(`updated > 0 && @request.auth.id != ''`)
collection.ListRule = types.Pointer("@request.auth.id != ''")
collection.ViewRule = types.Pointer(`id = "1"`) // no change
collection.UpdateRule = types.Pointer(`id = "2_update"`)
collection.CreateRule = types.Pointer(`id = "nil_update"`)
collection.DeleteRule = nil
collection.Indexes = types.JsonArray[string]{
"create index test1 on test456_update (f1_name)",
}
collection.NormalizeOptions()
collection.Schema.RemoveField("f3_id")
collection.Schema.AddField(&schema.SchemaField{
Id: "f4_id",
Name: "f4_name",
Type: schema.FieldTypeText,
Options: &schema.TextOptions{
Pattern: "`test backtick`123",
},
})
f := collection.Schema.GetFieldById("f2_id")
f.Name = "f2_name_new"
// save the changes and trigger automigrate
if err := app.Dao().SaveCollection(collection); err != nil {
t.Fatalf("[%d] Failed to save dummy collection changes, got %v", i, err)
}
files, err := os.ReadDir(migrationsDir)
if err != nil {
t.Fatalf("[%d] Expected migrationsDir to be created, got: %v", i, err)
}
if total := len(files); total != 1 {
t.Fatalf("[%d] Expected 1 file to be generated, got %d", i, total)
}
expectedName := "_updated_test456." + s.lang
if !strings.Contains(files[0].Name(), expectedName) {
t.Fatalf("[%d] Expected filename to contains %q, got %q", i, expectedName, files[0].Name())
}
fullPath := filepath.Join(migrationsDir, files[0].Name())
content, err := os.ReadFile(fullPath)
if err != nil {
t.Fatalf("[%d] Failed to read the generated migration file: %v", i, err)
}
if v := strings.TrimSpace(string(content)); v != strings.TrimSpace(s.expectedTemplate) {
t.Fatalf("[%d] Expected template \n%v \ngot \n%v", i, s.expectedTemplate, v)
}
}
}