test: extend the permission resolver table, the admin migration history and the API token refusals
- resolver: several groups, numbers stored as text, empty group sets, a code only at user level, booleans - migrations: every id once, the history applied twice, the unique code after a down and an up - personal API tokens: no application, no principal, invalid bodies, another user's token
This commit is contained in:
@@ -107,3 +107,62 @@ func TestAdminColumnsMigrations(t *testing.T) {
|
||||
t.Fatal("re-up did not restore the admin columns")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminColumnsHistory: the three admin migrations are part of the
|
||||
// plugin's history under their own ids, each id once, and applying the whole
|
||||
// history twice changes nothing.
|
||||
func TestAdminColumnsHistory(t *testing.T) {
|
||||
sqlDB := dedicatedDB(t, "user_admin_history")
|
||||
gdb, err := lagoon.Use(t.Context(), sqlDB)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[string]int{}
|
||||
for _, migration := range All() {
|
||||
seen[migration.ID]++
|
||||
if migration.Migrate == nil || migration.Rollback == nil {
|
||||
t.Fatalf("migration %s lacks an up or a down", migration.ID)
|
||||
}
|
||||
}
|
||||
admin := []string{usersPermissionsMigrations[0].ID, usersLastSeenMigrations[0].ID, frontendPermissionsMigrations[0].ID}
|
||||
for _, id := range admin {
|
||||
if seen[id] != 1 {
|
||||
t.Fatalf("migration %s is in the history %d times", id, seen[id])
|
||||
}
|
||||
}
|
||||
for id, n := range seen {
|
||||
if n != 1 {
|
||||
t.Fatalf("migration id %s is used %d times", id, n)
|
||||
}
|
||||
}
|
||||
m := gormigrate.New(gdb, &gormigrate.Options{
|
||||
TableName: "summer_migrations_golem15_user",
|
||||
UseTransaction: true,
|
||||
}, All())
|
||||
for range 2 {
|
||||
if err := m.Migrate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var applied int64
|
||||
if err := gdb.Raw(`SELECT COUNT(*) FROM summer_migrations_golem15_user WHERE id IN ?`, admin).Scan(&applied).Error; err != nil || applied != 3 {
|
||||
t.Fatalf("applied admin migrations = %d err = %v", applied, err)
|
||||
}
|
||||
// The permission code stays unique after a down and an up of its table.
|
||||
if err := m.RollbackMigration(frontendPermissionsMigrations[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.Migrate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
insert := `INSERT INTO golem15_user_frontend_permissions (code, label, tab) VALUES (?, ?, ?)`
|
||||
if err := gdb.Exec(insert, "history.code", "One", "Tab").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := gdb.Exec(insert, "history.code", "Two", "Other").Error; err == nil {
|
||||
t.Fatal("a duplicate permission code was accepted after the re-up")
|
||||
}
|
||||
if err := gdb.Exec(insert, "History.Code", "Three", "Tab").Error; err != nil {
|
||||
t.Fatalf("codes differing by case are distinct: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user