Files
ResolveSpec/pkg/security/txsettings_test.go
T
Hein c9fa8c60f2 refactor(security): move all database access into pkg/security/lookup
pkg/security no longer contains SQL. Every provider calls a store interface
from lookup, implemented by a procedure backend (Postgres stored procedures,
the default there) and a direct backend (dialect-driven SQL for postgres,
sqlite, mysql and mssql with configurable table and column names).

- add sectypes, lookup, lookup/{dialect,procedure,direct,backends,ddl,conformance}
- split totp and providers sub packages out of the core package
- replace SQLNames/TableNames/QueryMode with lookup.Config (see breaking_changes.md)
- direct backend now covers column/row security and API-key login
- move txsettings SQL to lookup.ApplyTxSettings; remove password.go
- move schema scripts under lookup/, add reference DDL per dialect
- add a shared conformance suite; run it on sqlite, and on Postgres in a
  podman/docker container (RESOLVESPEC_TEST_CONTAINERS=1)
- fix procedure schema bugs found on real Postgres: duplicate p_data
  parameter, JSON null arrays, expires_at timezone casts, passkey list
  GROUP BY, missing resolvespec_passkey_login; accept zone-less timestamps
2026-10-01 13:19:44 +02:00

61 lines
1.6 KiB
Go

package security
import (
"context"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/bitechdev/ResolveSpec/pkg/common"
"github.com/bitechdev/ResolveSpec/pkg/common/adapters/database"
)
func txSettingsDB(t *testing.T) (common.Database, sqlmock.Sqlmock) {
t.Helper()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close() })
return database.NewPgSQLAdapter(db), mock
}
func TestStampTxSettingsUsesConfiguredFunc(t *testing.T) {
pool, mock := txSettingsDB(t)
sc := &mockSecurityContext{ctx: context.Background()}
list, err := NewSecurityList(&mockSecurityProvider{})
if err != nil {
t.Fatal(err)
}
// Nil list and unset func are no-ops (no SQL expected).
if err := StampTxSettings(sc, nil, pool); err != nil {
t.Fatal(err)
}
if err := StampTxSettings(sc, list, pool); err != nil {
t.Fatal(err)
}
list.SetTxSettings(func(SecurityContext) (map[string]string, error) {
return map[string]string{"app.user_id": "7"}, nil
})
mock.ExpectBegin()
mock.ExpectExec(`set_config\('app\.user_id'`).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectCommit()
if err := pool.RunInTransaction(context.Background(), func(tx common.Database) error {
return StampTxSettings(sc, list, tx)
}); err != nil {
t.Fatal(err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
boom := errors.New("no tenant")
list.SetTxSettings(func(SecurityContext) (map[string]string, error) { return nil, boom })
if err := StampTxSettings(sc, list, pool); !errors.Is(err, boom) {
t.Fatalf("func error must propagate, got %v", err)
}
}