package thintest import ( "encoding/json" "net/http" "os" "path/filepath" "regexp" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // readingOff turns reading apps off for the owner at Base, and gives // Equipo the hint, as Base does on a change. func (o *oficinaRig) readingOff() { o.t.Helper() o.base.SetBoard(o.base.Token, 2, "Paul", wire.AbilityAppsRead) status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "") var out wire.AbilitiesRefreshed if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != 2 { o.t.Fatalf("hint: %d %s", status, body) } } // With reading apps off, no Oficina connect card is raised, and the // person API's connect and finish answer 403 ability_off. func TestOficinaConnectNeedsAppsRead(t *testing.T) { o := newOficinaRig(t) o.readingOff() o.begin("Put lunch in my calendar.") o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}) if cards := o.connectCards(); len(cards) != 0 { t.Errorf("a connect card was raised: %+v", cards) } for path, body := range map[string]string{ "/api/v1/assistant/oficina/connect": `{"return_to":"` + o.base.Origin + `/ask"}`, "/api/v1/assistant/oficina/connect/finish": `{"state":"s","code":"c"}`, } { status, _, out := raw(t, http.MethodPost, o.inst.URL+path, o.client.Token, nil, body) var e wire.Error if status != http.StatusForbidden || json.Unmarshal(out, &e) != nil || e.Code != wire.CodeAbilityOff { t.Errorf("%s: %d %s", path, status, out) } } } // A connection made before reading went off is kept, unused: nothing is // listed or read through it, and disconnecting still works. func TestExistingOficinaConnectionKeptUnused(t *testing.T) { o := newOficinaRig(t) if status := o.viaBase(); status != "connected" { t.Fatalf("connect: %s", status) } o.readingOff() calls := len(o.fake.Calls()) o.begin("What's on my calendar?") if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}); strings.Contains(res.Text, "calendar_") { t.Errorf("look offers the apps: %s", res.Text) } if res := o.use("u1", "calendar_list", map[string]any{}); !res.IsError { t.Errorf("use went through: %+v", res) } if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 1 { t.Errorf("%d connections kept", n) } if got := len(o.fake.Calls()); got != calls { t.Errorf("%d requests to connect while reading was off", got-calls) } // Disconnect still revokes. status, _, _ := o.browse(http.MethodPost, o.inst.URL+"/oficina/connect/disconnect") if status != http.StatusOK || o.rows("SELECT count(*) FROM oficina_connections") != 0 { t.Errorf("disconnect while off: %d, %d left", status, o.rows("SELECT count(*) FROM oficina_connections")) } } // Connecting from Equipo's own page doesn't start a consent with reading // off: the page says so, naming who can turn it on. func TestOficinaBrowserStartNeedsAppsRead(t *testing.T) { o := newOficinaRig(t) o.readingOff() status, location, body := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start") if status != http.StatusForbidden || location != "" || !strings.Contains(body, "Reading your Oficina apps is turned off for you. Paul can turn it on.") { t.Errorf("start: %d %q %.300s", status, location, body) } } // A consent begun with reading on and finished after it went off, through // Base or through Equipo's done page, exchanges no code and saves no // connection. func TestOficinaConsentFinishedAfterRevoke(t *testing.T) { o := newOficinaRig(t) state, code := o.handBack(o.base.Origin + "/ask?open=1") status, start, _ := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start") if status != http.StatusSeeOther { t.Fatalf("browser start: %d", status) } _, callback, _ := o.browse(http.MethodGet, start) _, done, _ := o.anonymous(callback) o.readingOff() if _, err := o.a.OficinaFinish(o.ctx, state, code); err == nil || !strings.Contains(err.Error(), "403") { t.Errorf("finish through Base: %v", err) } if status, _, body := o.browse(http.MethodGet, o.inst.URL+done); status != http.StatusForbidden || !strings.Contains(body, "Paul can turn it on") { t.Errorf("done page: %d %.200s", status, body) } if ex, _ := o.fake.Counts(); ex != 0 || o.rows("SELECT count(*) FROM oficina_connections") != 0 { t.Errorf("%d exchanges, %d connections", ex, o.rows("SELECT count(*) FROM oficina_connections")) } } // Only startOficina and finishOficina call the Oficina client's Start // and Finish: every way in is checked. func TestOnlyCheckedPathsCallOficina(t *testing.T) { files, err := filepath.Glob("../equipo/*.go") if err != nil || len(files) == 0 { t.Fatal(files, err) } call := regexp.MustCompile(`\.oficina\.(Start|Finish)\(`) funcStart := regexp.MustCompile(`(?m)^func (?:\([^)]*\) )?(\w+)`) for _, f := range files { if strings.HasSuffix(f, "_test.go") { continue } src, err := os.ReadFile(f) if err != nil { t.Fatal(err) } for _, m := range call.FindAllIndex(src, -1) { starts := funcStart.FindAllSubmatchIndex(src[:m[0]], -1) name := "" if len(starts) > 0 { last := starts[len(starts)-1] name = string(src[last[2]:last[3]]) } if name != "startOficina" && name != "finishOficina" { t.Errorf("%s: %s calls %s", f, name, src[m[0]:m[1]]) } } } }