| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/internal/home/directorydata" |
| 12 | "amadan.net/rastrillo/rastrillo/migrate" |
| 13 | "amadan.net/rastrillo/rastrillo/sessions" |
| 14 | ) |
| 15 | |
| 16 | func TestBaseDirectoryRoutes(t *testing.T) { |
| 17 | found := false |
| 18 | for _, app := range directoryApps() { |
| 19 | if app.slug == "base" { |
| 20 | found = app.name == "Base" && app.integrated && !app.provisionable && app.foreignOrigin == "" |
| 21 | } |
| 22 | } |
| 23 | if !found { |
| 24 | t.Error("Base missing from integrated registry") |
| 25 | } |
| 26 | for _, label := range []string{"oficina", "blue"} { |
| 27 | origin, consumer := "https://blue.base.oficina.works", "blue-base" |
| 28 | if label == "oficina" { |
| 29 | origin, consumer = "https://base.oficina.works", "base" |
| 30 | } |
| 31 | route := directorydata.Route{Label: label, App: "base", Origin: origin, ConsumerID: consumer} |
| 32 | if !directoryRouteAllowed(route) { |
| 33 | t.Errorf("Base route refused: %+v", route) |
| 34 | } |
| 35 | for _, bad := range []string{origin + "/", origin + ":443", origin + ".evil.invalid", "https://other.base.oficina.works"} { |
| 36 | route.Origin = bad |
| 37 | if directoryRouteAllowed(route) { |
| 38 | t.Errorf("unsafe Base route admitted: %+v", route) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | if directorydata.CanonicalOrigin("base") != "https://base.oficina.works" { |
| 43 | t.Error("Base canonical origin missing") |
| 44 | } |
| 45 | // Base's CARLOS host before the move is never team oficina's Base. |
| 46 | if directoryRouteAllowed(directorydata.Route{Label: "oficina", App: "base", ConsumerID: "base", Origin: "https://base.bds.oncarlos.com"}) { |
| 47 | t.Error("Base's old host admitted as canonical") |
| 48 | } |
| 49 | for _, r := range []directorydata.Route{ |
| 50 | {Label: "other", App: "base", ConsumerID: "base", Origin: "https://base.oficina.works"}, |
| 51 | {Label: "oficina", App: "base", ConsumerID: "other", Origin: "https://base.oficina.works"}, |
| 52 | {Label: "oficina", App: "unknown", ConsumerID: "unknown", Origin: "https://unknown.oficina.works"}, |
| 53 | } { |
| 54 | if directoryRouteAllowed(r) { |
| 55 | t.Errorf("canonical binding widened: %+v", r) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestNativeDirectoryBaseLiveMembership(t *testing.T) { |
| 61 | f := newMountedChooser(t) |
| 62 | rec := httptest.NewRecorder() |
| 63 | req := httptest.NewRequest("GET", directoryOrigin+"/", nil) |
| 64 | if err := f.p.directory.sessions.SignIn(rec, req, sessions.Session{Subject: chooserPerson, Method: nativeSessionMethod, AuthTime: time.Now()}); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | cookie := sessionCookie(rec) |
| 68 | exec := func(q string, args ...any) { |
| 69 | t.Helper() |
| 70 | if _, err := f.p.directory.db.Writer().Exec(q, args...); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | } |
| 74 | canonical := f.team(t, "AAA111", "oficina", "Oficina", chooserPerson) |
| 75 | team := f.team(t, "BBB222", "blue", "Blue", chooserPerson) |
| 76 | foreign := f.team(t, "CCC333", "other", "Other", chooserOther) |
| 77 | // Keep a live, non-native Slides instance to pin the native-only filter. |
| 78 | exec("UPDATE team_instances SET state='disabled'") |
| 79 | exec("INSERT INTO team_instances VALUES(?,?,?,?,?)", canonical, "slides", "https://slides.oficina.works", "slides", "live") |
| 80 | exec("INSERT INTO team_instances VALUES(?,?,?,?,?)", canonical, "base", "https://base.oficina.works", "base", "pending") |
| 81 | exec("INSERT INTO team_instances VALUES(?,?,?,?,?)", team, "base", "https://blue.base.oficina.works", "blue-base", "live") |
| 82 | exec("INSERT INTO team_instances VALUES(?,?,?,?,?)", foreign, "base", "https://other.base.oficina.works", "other-base", "live") |
| 83 | read := func() []nativeTeam { |
| 84 | t.Helper() |
| 85 | w := directoryCall(t, f, http.MethodGet, nativeTeamsPath, "", cookie, nil) |
| 86 | var got []nativeTeam |
| 87 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &got) != nil { |
| 88 | t.Fatalf("teams: %d %s", w.Code, w.Body.String()) |
| 89 | } |
| 90 | return got |
| 91 | } |
| 92 | check := func(got []nativeTeam, sqid, label, name, origin, consumer string) { |
| 93 | t.Helper() |
| 94 | if len(got) != 1 || got[0].SQID != sqid || got[0].Label != label || got[0].Name != name || got[0].Role != "member" || len(got[0].Apps) != 1 || got[0].Apps[0] != (nativeTeamApp{"base", origin, consumer}) { |
| 95 | t.Fatalf("Base teams: %+v", got) |
| 96 | } |
| 97 | } |
| 98 | check(read(), "BBB222", "blue", "Blue", "https://blue.base.oficina.works", "blue-base") |
| 99 | exec("UPDATE team_instances SET state='disabled' WHERE team_id=? AND app='base'", team) |
| 100 | if got := read(); len(got) != 0 { |
| 101 | t.Fatalf("pending or disabled Base visible: %+v", got) |
| 102 | } |
| 103 | exec("UPDATE team_instances SET state='live' WHERE team_id=? AND app='base'", canonical) |
| 104 | check(read(), "AAA111", "oficina", "Oficina", "https://base.oficina.works", "base") |
| 105 | exec("DELETE FROM memberships WHERE person_ref=?", chooserPerson) |
| 106 | if got := read(); len(got) != 0 { |
| 107 | t.Fatalf("removed member still sees Base: %+v", got) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestNativeAssertBaseRegisteredOrigins(t *testing.T) { |
| 112 | app := newNativeApp(t) |
| 113 | if _, err := migrate.Apply(context.Background(), app.db, (Runtime{role: "credential"}).ServingSchema()); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | app.runtime.consumers["base"] = "https://base.oficina.works" |
| 117 | app.runtime.registry = newConsumerRegistry(app.runtime.consumers) |
| 118 | for _, row := range [][3]string{{"blue-base", "https://blue.base.oficina.works", "active"}, {"gone-base", "https://gone.base.oficina.works", "retired"}} { |
| 119 | if _, err := app.db.Writer().Exec("INSERT INTO consumers VALUES(?,?,'base',?,'2026-11-20',?)", row[0], row[1], row[2], fixtureToken(1)); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | } |
| 123 | if err := app.runtime.registry.load(context.Background(), app.db.Writer(), Origin, app.runtime.keys); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | d := newDevice(t) |
| 127 | grant, _ := d.signInByCode(t, app, "base@example.invalid") |
| 128 | _, challenge := verifierFor(91) |
| 129 | for _, origin := range []string{"https://base.oficina.works", "https://blue.base.oficina.works"} { |
| 130 | w := d.assert(t, app, grant, origin, challenge) |
| 131 | if w.Code != 200 { |
| 132 | t.Fatalf("registered Base: %d %s", w.Code, w.Body.String()) |
| 133 | } |
| 134 | claims := verifyAssertion(t, app, decodeReply(t, w)["assertion"].(string), origin) |
| 135 | if claims.Audience != origin || claims.Nonce != mustNativeNonce(t, challenge) || claims.ExpiresAt-claims.IssuedAt != 120 { |
| 136 | t.Fatalf("Base claims: %+v", claims) |
| 137 | } |
| 138 | } |
| 139 | for _, origin := range []string{"https://base.oficina.works/", "https://BASE.oficina.works", "https://base.oficina.works:443", "https://base.oficina.works.evil.invalid", "https://green.base.oficina.works", "https://gone.base.oficina.works"} { |
| 140 | w := d.assert(t, app, grant, origin, challenge) |
| 141 | if w.Code != 400 || decodeReply(t, w)["error"] != "audience" { |
| 142 | t.Fatalf("unregistered Base admitted: %s %d", origin, w.Code) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |