oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/json"
5 "net/http"
6 "os"
7 "path/filepath"
8 "regexp"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// readingOff turns reading apps off for the owner at Base, and gives
16// Equipo the hint, as Base does on a change.
17func (o *oficinaRig) readingOff() {
18 o.t.Helper()
19 o.base.SetBoard(o.base.Token, 2, "Paul", wire.AbilityAppsRead)
20 status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "")
21 var out wire.AbilitiesRefreshed
22 if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != 2 {
23 o.t.Fatalf("hint: %d %s", status, body)
24 }
25}
26
27// With reading apps off, no Oficina connect card is raised, and the
28// person API's connect and finish answer 403 ability_off.
29func TestOficinaConnectNeedsAppsRead(t *testing.T) {
30 o := newOficinaRig(t)
31 o.readingOff()
32 o.begin("Put lunch in my calendar.")
33 o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"})
34 if cards := o.connectCards(); len(cards) != 0 {
35 t.Errorf("a connect card was raised: %+v", cards)
36 }
37 for path, body := range map[string]string{
38 "/api/v1/assistant/oficina/connect": `{"return_to":"` + o.base.Origin + `/ask"}`,
39 "/api/v1/assistant/oficina/connect/finish": `{"state":"s","code":"c"}`,
40 } {
41 status, _, out := raw(t, http.MethodPost, o.inst.URL+path, o.client.Token, nil, body)
42 var e wire.Error
43 if status != http.StatusForbidden || json.Unmarshal(out, &e) != nil || e.Code != wire.CodeAbilityOff {
44 t.Errorf("%s: %d %s", path, status, out)
45 }
46 }
47}
48
49// A connection made before reading went off is kept, unused: nothing is
50// listed or read through it, and disconnecting still works.
51func TestExistingOficinaConnectionKeptUnused(t *testing.T) {
52 o := newOficinaRig(t)
53 if status := o.viaBase(); status != "connected" {
54 t.Fatalf("connect: %s", status)
55 }
56 o.readingOff()
57 calls := len(o.fake.Calls())
58 o.begin("What's on my calendar?")
59 if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}); strings.Contains(res.Text, "calendar_") {
60 t.Errorf("look offers the apps: %s", res.Text)
61 }
62 if res := o.use("u1", "calendar_list", map[string]any{}); !res.IsError {
63 t.Errorf("use went through: %+v", res)
64 }
65 if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 1 {
66 t.Errorf("%d connections kept", n)
67 }
68 if got := len(o.fake.Calls()); got != calls {
69 t.Errorf("%d requests to connect while reading was off", got-calls)
70 }
71 // Disconnect still revokes.
72 status, _, _ := o.browse(http.MethodPost, o.inst.URL+"/oficina/connect/disconnect")
73 if status != http.StatusOK || o.rows("SELECT count(*) FROM oficina_connections") != 0 {
74 t.Errorf("disconnect while off: %d, %d left", status, o.rows("SELECT count(*) FROM oficina_connections"))
75 }
76}
77
78// Connecting from Equipo's own page doesn't start a consent with reading
79// off: the page says so, naming who can turn it on.
80func TestOficinaBrowserStartNeedsAppsRead(t *testing.T) {
81 o := newOficinaRig(t)
82 o.readingOff()
83 status, location, body := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start")
84 if status != http.StatusForbidden || location != "" || !strings.Contains(body, "Reading your Oficina apps is turned off for you. Paul can turn it on.") {
85 t.Errorf("start: %d %q %.300s", status, location, body)
86 }
87}
88
89// A consent begun with reading on and finished after it went off, through
90// Base or through Equipo's done page, exchanges no code and saves no
91// connection.
92func TestOficinaConsentFinishedAfterRevoke(t *testing.T) {
93 o := newOficinaRig(t)
94 state, code := o.handBack(o.base.Origin + "/ask?open=1")
95 status, start, _ := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start")
96 if status != http.StatusSeeOther {
97 t.Fatalf("browser start: %d", status)
98 }
99 _, callback, _ := o.browse(http.MethodGet, start)
100 _, done, _ := o.anonymous(callback)
101 o.readingOff()
102 if _, err := o.a.OficinaFinish(o.ctx, state, code); err == nil || !strings.Contains(err.Error(), "403") {
103 t.Errorf("finish through Base: %v", err)
104 }
105 if status, _, body := o.browse(http.MethodGet, o.inst.URL+done); status != http.StatusForbidden || !strings.Contains(body, "Paul can turn it on") {
106 t.Errorf("done page: %d %.200s", status, body)
107 }
108 if ex, _ := o.fake.Counts(); ex != 0 || o.rows("SELECT count(*) FROM oficina_connections") != 0 {
109 t.Errorf("%d exchanges, %d connections", ex, o.rows("SELECT count(*) FROM oficina_connections"))
110 }
111}
112
113// Only startOficina and finishOficina call the Oficina client's Start
114// and Finish: every way in is checked.
115func TestOnlyCheckedPathsCallOficina(t *testing.T) {
116 files, err := filepath.Glob("../equipo/*.go")
117 if err != nil || len(files) == 0 {
118 t.Fatal(files, err)
119 }
120 call := regexp.MustCompile(`\.oficina\.(Start|Finish)\(`)
121 funcStart := regexp.MustCompile(`(?m)^func (?:\([^)]*\) )?(\w+)`)
122 for _, f := range files {
123 if strings.HasSuffix(f, "_test.go") {
124 continue
125 }
126 src, err := os.ReadFile(f)
127 if err != nil {
128 t.Fatal(err)
129 }
130 for _, m := range call.FindAllIndex(src, -1) {
131 starts := funcStart.FindAllSubmatchIndex(src[:m[0]], -1)
132 name := ""
133 if len(starts) > 0 {
134 last := starts[len(starts)-1]
135 name = string(src[last[2]:last[3]])
136 }
137 if name != "startOficina" && name != "finishOficina" {
138 t.Errorf("%s: %s calls %s", f, name, src[m[0]:m[1]])
139 }
140 }
141 }
142}
143