Lot 2 (tests) : comptes et réglages partagés, clé hex:
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LNfwm4pfYR9C914C2EeiuB
This commit is contained in:
1 parent
479aa2477c
commit
235916157a
2 files changed
+242
No files matched your search
@@ -0,0 +1,161 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/vblogio/upandown/internal/catalog"
|
||||
)
|
||||
|
||||
// Comptes partagés par plusieurs instances via le catalogue PostgreSQL
|
||||
// (UPANDOWN_TEST_DATABASE_URL ; la table accounts est vidée).
|
||||
func catalogAccounts(t *testing.T) (*catalog.DB, func() *Accounts) {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("UPANDOWN_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("UPANDOWN_TEST_DATABASE_URL non défini")
|
||||
}
|
||||
ctx := context.Background()
|
||||
db, err := catalog.Open(ctx, dsn, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(db.Close)
|
||||
if _, err := db.Pool.Exec(ctx, "TRUNCATE accounts; DELETE FROM imports WHERE root = 'global' AND kind = 'accounts'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return db, func() *Accounts {
|
||||
a, err := OpenAccounts(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := a.UseCatalog(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogAccountsShared(t *testing.T) {
|
||||
_, open := catalogAccounts(t)
|
||||
a, b := open(), open()
|
||||
|
||||
// Inscriptions simultanées sur deux instances : un seul administrateur.
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
inst := a
|
||||
if i%2 == 1 {
|
||||
inst = b
|
||||
}
|
||||
_, err := inst.CreateLocal(NewAccount{Username: fmt.Sprintf("user%02d", i), Password: "mot-de-passe-1"})
|
||||
errs <- err
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
admins := 0
|
||||
for _, acc := range b.List() {
|
||||
if acc.Role == RoleAdmin {
|
||||
admins++
|
||||
}
|
||||
}
|
||||
if a.Count() != 10 || admins != 1 {
|
||||
t.Fatalf("comptes %d, administrateurs %d", a.Count(), admins)
|
||||
}
|
||||
if _, err := b.CreateLocal(NewAccount{Username: "user03", Password: "mot-de-passe-1"}); !errors.Is(err, ErrAccountExists) {
|
||||
t.Fatalf("doublon : %v", err)
|
||||
}
|
||||
|
||||
// Modification sur une instance, effet immédiat sur l'autre.
|
||||
disabled := StatusDisabled
|
||||
if _, err := a.Update("user04", Patch{Status: &disabled}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := b.Authenticate("user04", "mot-de-passe-1"); !errors.Is(err, ErrDisabled) {
|
||||
t.Fatalf("compte désactivé : %v", err)
|
||||
}
|
||||
acc, err := b.Authenticate("user05", "mot-de-passe-1")
|
||||
if err != nil || acc.LastLogin.IsZero() {
|
||||
t.Fatalf("connexion : %v %v", err, acc.LastLogin)
|
||||
}
|
||||
if got, _ := a.Get("user05"); got.LastLogin.IsZero() {
|
||||
t.Error("date de connexion non partagée")
|
||||
}
|
||||
if _, err := a.Authenticate("user05", "mauvais-mot-de-passe"); !errors.Is(err, ErrBadCredentials) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := b.Delete("user06"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := a.Get("user06"); ok {
|
||||
t.Error("compte supprimé encore visible")
|
||||
}
|
||||
|
||||
// Le dernier administrateur reste protégé, quelle que soit l'instance.
|
||||
var admin string
|
||||
for _, acc := range a.List() {
|
||||
if acc.Role == RoleAdmin {
|
||||
admin = acc.ID
|
||||
}
|
||||
}
|
||||
if err := b.Delete(admin); !errors.Is(err, ErrLastAdmin) {
|
||||
t.Fatalf("dernier administrateur : %v", err)
|
||||
}
|
||||
|
||||
// OIDC : même sujet sur deux instances, un seul compte.
|
||||
u := User{Sub: "sub-1", Name: "Zoé", Email: "zoe@example.org"}
|
||||
if _, err := a.UpsertOIDC(u, nil, OIDCPolicy{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := b.UpsertOIDC(u, nil, OIDCPolicy{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n := a.Count(); n != 10 {
|
||||
t.Errorf("après OIDC : %d comptes", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogAccountsImportOnce(t *testing.T) {
|
||||
db, _ := catalogAccounts(t)
|
||||
dir := t.TempDir()
|
||||
old, _ := OpenAccounts(dir)
|
||||
if _, err := old.CreateLocal(NewAccount{Username: "ancien", Password: "mot-de-passe-1"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a, _ := OpenAccounts(dir)
|
||||
if err := a.UseCatalog(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, accountsFile+".imported")); err != nil {
|
||||
t.Fatal("fichier non renommé")
|
||||
}
|
||||
if acc, err := a.Authenticate("ancien", "mot-de-passe-1"); err != nil || acc.Role != RoleAdmin {
|
||||
t.Fatalf("compte repris : %v %+v", err, acc)
|
||||
}
|
||||
// Second démarrage : pas de nouvelle reprise.
|
||||
other := t.TempDir()
|
||||
o, _ := OpenAccounts(other)
|
||||
_, _ = o.CreateLocal(NewAccount{Username: "intrus", Password: "mot-de-passe-1"})
|
||||
o2, _ := OpenAccounts(other)
|
||||
if err := o2.UseCatalog(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := o2.Get("intrus"); ok {
|
||||
t.Error("reprise effectuée deux fois")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user