308 lines
10 KiB
Go
308 lines
10 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func TestMySQLIdentityDomainLifecycle(t *testing.T) {
|
|
database := mysqlTestDatabase(t)
|
|
r := NewMySQLIdentityRepository(database)
|
|
ns := NewMySQLNamespaceRepository(database)
|
|
ctx := context.Background()
|
|
if e := r.PrepareSchema(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := ns.CreateCampus(ctx, "home", "キャンパス", "ja", "standalone", "home.example.test", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
campuses, e := ns.Campuses(ctx)
|
|
if e != nil || len(campuses) != 1 {
|
|
t.Fatal(campuses, e)
|
|
}
|
|
campus := campuses[0]
|
|
if e := r.CreateLocalDomain(ctx, campus.UUID, "default", "ドメイン", "", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
domain, e := ns.ResolveHost(ctx, "home.example.test")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
const password = "synthetic password for identity pilot"
|
|
owner, e := r.BootstrapOwner(ctx, domain.UUID, "owner", password, "所有者", "owner@example.test")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
member, e := r.CreateAccount(ctx, domain.UUID, "member", password, "利用者", "member@example.test")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := r.CreateAccount(ctx, domain.UUID, "member", password, "private duplicate title", ""); e == nil || strings.Contains(e.Error(), "private duplicate title") {
|
|
t.Fatal("duplicate account handling", e)
|
|
}
|
|
people, e := r.Principals(ctx, domain.UUID)
|
|
if e != nil || len(people) != 2 {
|
|
t.Fatal(people, e)
|
|
}
|
|
g, e := r.CreateGroup(ctx, domain.UUID, owner.UUID, "team", "チーム", "説明", "members")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
if e := r.AddMember(ctx, domain.UUID, g.UUID, "member", "member"); e != nil {
|
|
t.Fatal("repeat add", e)
|
|
}
|
|
if e := r.SetPermission(ctx, domain.UUID, g.UUID, "/literal%_", "read_write"); e != nil {
|
|
t.Fatal("repeat permission", e)
|
|
}
|
|
if e := r.SetPrincipalState(ctx, domain.UUID, member.UUID, "active"); e != nil {
|
|
t.Fatal("repeat state", e)
|
|
}
|
|
}
|
|
members, e := r.Members(ctx, domain.UUID, g.UUID)
|
|
if e != nil || len(members) != 2 {
|
|
t.Fatal(members, e)
|
|
}
|
|
if e := r.AddMember(ctx, domain.UUID, g.UUID, "owner", "member"); e == nil {
|
|
t.Fatal("owner downgraded")
|
|
}
|
|
for _, m := range members {
|
|
if m.PrincipalUUID == member.UUID {
|
|
if e := r.RemoveMember(ctx, domain.UUID, g.UUID, m.MembershipUUID, false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
}
|
|
if e := r.CreateLocalDomain(ctx, campus.UUID, "child", "Child", "child", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
results := make(chan error, 2)
|
|
for _, name := range []string{"race-one", "race-two"} {
|
|
go func(name string) { results <- r.CreateLocalDomain(ctx, campus.UUID, name, "Race", "same-host", true) }(name)
|
|
}
|
|
success := 0
|
|
for i := 0; i < 2; i++ {
|
|
if <-results == nil {
|
|
success++
|
|
}
|
|
}
|
|
if success != 1 {
|
|
t.Fatal("Domain host concurrency", success)
|
|
}
|
|
child, e := ns.ResolveHost(ctx, "child.home.example.test")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if got, e := ns.Config(ctx, child.UUID, "locale"); e != nil || got != "ja" {
|
|
t.Fatal("locale inheritance", got, e)
|
|
}
|
|
if allowed, e := r.IsBuiltinAdmin(ctx, child.UUID, owner.UUID, "campus_admin"); e != nil || !allowed {
|
|
t.Fatal("admin inheritance", e)
|
|
}
|
|
if _, e := r.Members(ctx, child.UUID, g.UUID); !errors.Is(e, sql.ErrNoRows) {
|
|
t.Fatal("cross-domain group", e)
|
|
}
|
|
root, e := r.Group(ctx, domain.UUID, "campus_admin")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
if e := r.SetInheritance(ctx, domain.UUID, root.UUID, true); e != nil {
|
|
t.Fatal("repeat inheritance", e)
|
|
}
|
|
}
|
|
token, e := r.store.CreateBrowserAuthSession(child.UUID, owner.UUID, 1)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
sessions, e := r.Sessions(ctx, child.UUID)
|
|
if e != nil || len(sessions) != 1 {
|
|
t.Fatal("session administration", sessions, e)
|
|
}
|
|
if e := r.RevokeSession(ctx, domain.UUID, sessions[0].UUID); !errors.Is(e, sql.ErrNoRows) {
|
|
t.Fatal("cross-domain session revocation", e)
|
|
}
|
|
if e := r.RevokeSession(ctx, child.UUID, sessions[0].UUID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := r.store.PrincipalForSession(child.UUID, token); !errors.Is(e, ErrAccessDenied) {
|
|
t.Fatal("administratively revoked session accepted", e)
|
|
}
|
|
token, e = r.store.CreateBrowserAuthSession(child.UUID, owner.UUID, 1)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := ns.CreateCampus(ctx, "other", "Other", "en", "standalone", "other.example.test", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
campuses, e = ns.Campuses(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var other string
|
|
for _, c := range campuses {
|
|
if c.UUID != campus.UUID {
|
|
other = c.UUID
|
|
}
|
|
}
|
|
if e := r.UpdateLocalDomain(ctx, child.ID, other, child.Name, "Moved", child.Domain, true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if allowed, e := r.IsBuiltinAdmin(ctx, child.UUID, owner.UUID, "campus_admin"); e != nil || allowed {
|
|
t.Fatal("stale inherited admin", e)
|
|
}
|
|
if _, e := r.store.PrincipalForSession(child.UUID, token); !errors.Is(e, ErrAccessDenied) {
|
|
t.Fatal("stale session", e)
|
|
}
|
|
if _, e := database.Exec(`UPDATE domains SET placement='remote' WHERE uuid=?`, child.UUID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.UpdateLocalDomain(ctx, child.ID, other, child.Name, "Must not change", child.Domain, true); !errors.Is(e, ErrBackendFeatureUnavailable) {
|
|
t.Fatal("remote Domain adopted locally", e)
|
|
}
|
|
if _, e := database.Exec(`UPDATE domains SET placement='local' WHERE uuid=?`, child.UUID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.CreateLocalDomain(ctx, campus.UUID, "collision", "Collision", "", true); e == nil {
|
|
t.Fatal("host collision")
|
|
}
|
|
if e := ns.CreateCampus(ctx, "controller", "Controller", "en", "controller", "controller.example.test", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
campuses, e = ns.Campuses(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, c := range campuses {
|
|
if c.Name == "controller" {
|
|
if e := r.CreateLocalDomain(ctx, c.UUID, "remote", "Remote", "remote", true); !errors.Is(e, ErrBackendFeatureUnavailable) {
|
|
t.Fatal("federation bypass", e)
|
|
}
|
|
}
|
|
}
|
|
// A failing inheritance write must roll back Domain, locale and built-ins.
|
|
before, e := ns.Domains(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := database.Exec(`ALTER TABLE campus_admin_grants RENAME COLUMN source TO missing_source`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.CreateLocalDomain(ctx, campus.UUID, "rollback", "Rollback", "rollback", true); e == nil {
|
|
t.Fatal("partial Domain accepted")
|
|
}
|
|
after, e := ns.Domains(ctx)
|
|
if e != nil || len(before) != len(after) {
|
|
t.Fatal("partial Domain remains", e)
|
|
}
|
|
}
|
|
|
|
func TestMySQLIdentitySchemaAndCancellation(t *testing.T) {
|
|
database := mysqlTestDatabase(t)
|
|
r := NewMySQLIdentityRepository(database)
|
|
ctx := context.Background()
|
|
if status, e := r.InspectSchema(ctx); e != nil || status.Pending != 6 {
|
|
t.Fatal(status, e)
|
|
}
|
|
if e := r.PrepareSchema(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.PrepareSchema(ctx); e != nil {
|
|
t.Fatal("repeat prepare", e)
|
|
}
|
|
if _, e := database.Exec(`UPDATE schema_mysql_migrations SET state='running',next_operation=1 WHERE component='store-auth-core' AND version=3`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.PrepareSchema(ctx); !errors.Is(e, db.ErrMySQLMigrationDirty) {
|
|
t.Fatal(e)
|
|
}
|
|
if e := r.ResumeSchema(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if status, e := r.InspectSchema(ctx); e != nil || status.Pending != 0 || status.Dirty {
|
|
t.Fatal(status, e)
|
|
}
|
|
cancelled, cancel := context.WithCancel(ctx)
|
|
cancel()
|
|
if _, e := r.store.ListDomainPrincipalsContext(cancelled, "unused"); !errors.Is(e, context.Canceled) {
|
|
t.Fatal(e)
|
|
}
|
|
const validID = "00000000-0000-4000-a000-000000000001"
|
|
if _, e := r.Sessions(cancelled, validID); !errors.Is(e, context.Canceled) {
|
|
t.Fatal("cancel session list", e)
|
|
}
|
|
if e := r.RevokeSession(cancelled, validID, validID); !errors.Is(e, context.Canceled) {
|
|
t.Fatal("cancel session revocation", e)
|
|
}
|
|
r.store.ReadOnly = true
|
|
if e := r.RevokeSession(ctx, validID, validID); !errors.Is(e, ErrReadOnly) {
|
|
t.Fatal("read-only session revocation", e)
|
|
}
|
|
r.store.ReadOnly = false
|
|
if e := r.CreateLocalDomain(cancelled, "00000000-0000-4000-a000-000000000001", "cancelled", "Cancelled", "cancelled", true); !errors.Is(e, context.Canceled) {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := database.Exec(`ALTER TABLE group_permissions MODIFY scope_path VARCHAR(256) NOT NULL`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e := r.InspectSchema(ctx); e == nil {
|
|
t.Fatal("schema drift accepted")
|
|
}
|
|
}
|
|
|
|
func TestMySQLIdentitySchemaUpgradePreservesAccounts(t *testing.T) {
|
|
for _, version := range []int{1, 2} {
|
|
t.Run(fmt.Sprint(version), func(t *testing.T) {
|
|
testMySQLIdentitySchemaUpgrade(t, version)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testMySQLIdentitySchemaUpgrade(t *testing.T, version int) {
|
|
database := mysqlTestDatabase(t)
|
|
ctx := context.Background()
|
|
if err := NewMySQLNamespaceRepository(database).PrepareSchema(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan := mysqlAuthMigrations()
|
|
if err := db.ApplyMySQLMigrations(ctx, database, "store-auth-core", plan[:version]); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var before string
|
|
if err := database.QueryRow(`SELECT GROUP_CONCAT(checksum ORDER BY version) FROM schema_mysql_migrations WHERE component='store-auth-core' AND version<=?`, version).Scan(&before); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := database.Exec(`INSERT INTO principals(uuid,display_name,created_at_ms,updated_at_ms) VALUES('upgrade-user','既存の利用者',1,1)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := NewMySQLIdentityRepository(database)
|
|
if status, err := r.InspectSchema(ctx); err != nil || status.Pending != 3-version || status.Applied != 3+version {
|
|
t.Fatal(status, err)
|
|
}
|
|
if err := r.PrepareSchema(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var after, title string
|
|
if err := database.QueryRow(`SELECT GROUP_CONCAT(checksum ORDER BY version) FROM schema_mysql_migrations WHERE component='store-auth-core' AND version<=?`, version).Scan(&after); err != nil || after != before {
|
|
t.Fatal("historical checksum changed", err)
|
|
}
|
|
if err := database.QueryRow(`SELECT display_name FROM principals WHERE uuid='upgrade-user'`).Scan(&title); err != nil || title != "既存の利用者" {
|
|
t.Fatal("existing principal changed", err)
|
|
}
|
|
if status, err := r.InspectSchema(ctx); err != nil || status.Pending != 0 || status.Applied != 6 {
|
|
t.Fatal(status, err)
|
|
}
|
|
if _, err := database.Exec(`ALTER TABLE auth_providers DROP INDEX uq_0`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.InspectSchema(ctx); err == nil {
|
|
t.Fatal("provider uniqueness drift accepted")
|
|
}
|
|
}
|