oficina / calendar Public

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

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "context"
5 "encoding/json"
6 "net/http/httptest"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11
12 access "amadan.net/oficina/agent-access.git"
13)
14
15// TestSuiteInteropFixture is an opt-in real Calendar process for the shared
16// cross-app acceptance harness. All credentials and storage are test fixtures.
17func TestSuiteInteropFixture(t *testing.T) {
18 dir := os.Getenv("OFICINA_INTEROP_DIR")
19 if dir == "" {
20 t.Skip("cross-app harness only")
21 }
22 f := newHomeRouterFixture(t)
23 f.local(t)
24 if w := f.do("GET", "/calendars", ""); w.Code != 200 {
25 t.Fatal(w.Code)
26 }
27 if w := f.do("GET", "/account/home", ""); w.Code != 200 {
28 t.Fatal(w.Code)
29 }
30 start := homeRedirect(t, f.do("POST", "/account/home", ""))
31 if homeRedirect(t, f.exchange(t, start, "opaque-home-alice")) != "/account/home/confirm" {
32 t.Fatal("link confirmation missing")
33 }
34 homeRedirect(t, f.do("POST", "/account/home/confirm", ""))
35 p := access.Principal{Subject: "opaque-home-alice", Client: "oficina-interop", Grant: "interop-grant", Teams: []string{"acme"}, Scopes: []string{"calendar:read", "calendar:write"}}
36 if err := f.app.mountAgent(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return p, nil }), "https://connect.example.test", "acme"); err != nil {
37 t.Fatal(err)
38 }
39 person, err := f.app.agentPerson(t.Context(), p, "acme")
40 if err != nil {
41 t.Fatal(err)
42 }
43 cal, err := f.app.store.DefaultCalendarID(t.Context(), person.ID)
44 if err != nil {
45 t.Fatal(err)
46 }
47 server := httptest.NewTLSServer(f.app.agent)
48 defer server.Close()
49 data, err := json.Marshal(map[string]any{"url": server.URL, "origin": f.app.cfg.Origin, "calendar_id": cal})
50 if err != nil {
51 t.Fatal(err)
52 }
53 if err := os.WriteFile(filepath.Join(dir, "calendar.json"), data, 0600); err != nil {
54 t.Fatal(err)
55 }
56 deadline := time.NewTimer(2 * time.Minute)
57 defer deadline.Stop()
58 tick := time.NewTicker(100 * time.Millisecond)
59 defer tick.Stop()
60 for {
61 select {
62 case <-deadline.C:
63 t.Fatal("cross-app harness timed out")
64 case <-t.Context().Done():
65 t.Fatal(t.Context().Err())
66 case <-tick.C:
67 if _, err := os.Stat(filepath.Join(dir, "stop")); err == nil {
68 return
69 }
70 }
71 }
72}
73