oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

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