test: drive the whole application over HTTP, and serialise migrations

The behaviour suite in internal/behaviour installs the application into an
empty database through the web installer, signs in, walks every flow, and then
replaces the process to confirm the state was persisted rather than held in
memory. It found four defects, each fixed here or in the preceding commit:

- Migrate was not safe to run from two instances at once. CREATE TABLE IF NOT
  EXISTS is not race free in PostgreSQL and collided on the pg_type unique
  index, and two replicas could apply the same file. The run now holds a
  session-level advisory lock, so a rolling deployment waits instead of failing.

- Bootstrap refused ownership whenever a tenant held any member, so installing
  with the demonstration seed and then setting a local administrator password
  produced an account that could never reach the console. It now grants
  ownership only while the tenant has no owner, which still never takes a tenant
  over from one that does.

Three of the fixes are covered by store-level regression tests in the same
commit: the ownership rule, the duplicate tenant slug conflict, and concurrent
migration runs.

The suite is skipped by go test ./... because it needs a scratch database and
the Google credentials for the installer. Run it with make behaviour-test, which
also resets the database afterwards. make test no longer inherits the database
variables, so unit runs need nothing.

Completes the earlier correction to the docs: the role matrix lives in the
database rather than in internal/role, and a local administrator claims the
system administrator role when no Google account has.
This commit is contained in:
Daniel Noulet 2026-10-05 19:37:09 +02:00
parent c7c81fb65c
commit 8d8970d5a7
18 changed files with 3050 additions and 4 deletions

View file

@ -0,0 +1,77 @@
package behaviour
import (
"net/http"
"net/url"
"testing"
)
// neturlValues builds a form body from plain strings, so phases do not have to
// repeat the url.Values literal for every field.
func neturlValues(fields map[string]string) url.Values {
values := url.Values{}
for name, value := range fields {
values.Set(name, value)
}
return values
}
// tenantPhases exercises the installation-wide tenant management, which is the
// one console action restricted to system administrators.
func tenantPhases(t *testing.T, j *journey) {
t.Helper()
phase(t, "a system administrator creates a tenant", func(t *testing.T) {
type tenant struct {
ID string `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
}
response := j.admin.json(http.MethodPost, j.adminHost(), "/api/v1/tenants", map[string]string{
"slug": "behaviour", "name": "Behaviour Tenant",
})
created := decode[tenant](t, response, http.StatusCreated)
if created.Slug != "behaviour" {
t.Errorf("slug = %q", created.Slug)
}
if created.ID == "" {
t.Error("no tenant id returned")
}
// Later phases build their own application in this tenant, so it isolates
// authorization from the seeded demonstration tenant.
j.scopedTenant, j.scopedTenantID = created.Slug, created.ID
})
phase(t, "a duplicate tenant slug is refused", func(t *testing.T) {
response := j.admin.json(http.MethodPost, j.adminHost(), "/api/v1/tenants", map[string]string{
"slug": "behaviour", "name": "Duplicate",
})
statusOnly(t, response, http.StatusConflict)
})
phase(t, "a malformed tenant slug is refused", func(t *testing.T) {
for _, slug := range []string{"Has Uppercase", "has/slash", "-leading", ""} {
response := j.admin.json(http.MethodPost, j.adminHost(), "/api/v1/tenants", map[string]string{
"slug": slug, "name": "Invalid",
})
statusOnly(t, response, http.StatusBadRequest)
}
})
phase(t, "a tenant administrator cannot create a tenant", func(t *testing.T) {
// The seeded NMBS interviewer is a tenant administrator, not a system one.
browser := j.browser()
host := j.adminHost()
token := csrfFrom(t, bodyOf(t, browser.get(host, "/login", nil)))
response := browser.form(host, "/auth/password", neturlValues(map[string]string{
"csrf_token": token, "email": "interview@nmbs.auth.dev", "password": "admin123",
}), nil)
if response.StatusCode != http.StatusSeeOther {
t.Fatalf("seeded administrator sign-in = %d: %s", response.StatusCode, bodyOf(t, response))
}
response = browser.json(http.MethodPost, host, "/api/v1/tenants", map[string]string{
"slug": "unauthorised", "name": "Unauthorised",
})
statusOnly(t, response, http.StatusForbidden)
})
}