oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/home/connect"
11)
12
13// A native contract, not copy: the native apps' Oficina ID transport
14// (native/apple/Sources/DocsKit/DocsOficinaID.swift,
15// DocsSessionWatch.signedOut) reads a 403 whose body is exactly
16// "signed out\n" as an ended session and re-mints it; any other 403 is a
17// refusal of the request, which the outbox holds as access lost. Every
18// guard that refuses a non-GET request for want of a live session must
19// keep these bytes: rastrillo sessions.Require's own refusal (no session,
20// an unknown or revoked one), and Docs' identity guard for a native
21// session past its seven days (refuseIdentity, sessionEndedBody). Changing
22// either strands a native window whose first request after the session
23// ends is a write.
24const nativeSignedOut = "signed out\n"
25
26func assertSignedOut(t *testing.T, where string, rec *httptest.ResponseRecorder) {
27 t.Helper()
28 if rec.Code != http.StatusForbidden || rec.Body.String() != nativeSignedOut {
29 t.Fatalf("%s = %d %q, want 403 %q (the native transport's re-mint signal)", where, rec.Code, rec.Body.String(), nativeSignedOut)
30 }
31}
32
33// nativeWrite is the native app's request as NativeHTTP sends it: JSON
34// wanted, the instance's own Origin, and the session cookie if any.
35func nativeWrite(f *routerHomeFixture, method, path string, cookie *http.Cookie) *httptest.ResponseRecorder {
36 r := httptest.NewRequest(method, bridgeOrigin+path, strings.NewReader(`{"title":"x"}`))
37 r.Header.Set("Content-Type", "application/json")
38 r.Header.Set("Accept", "application/json")
39 r.Header.Set("Origin", bridgeOrigin)
40 if cookie != nil {
41 r.AddCookie(cookie)
42 }
43 return f.browser().serve(r)
44}
45
46func TestNativeContractNoSessionSaysSignedOut(t *testing.T) {
47 f, _ := homeNativeFixture(t)
48 _, answer := f.nativeExchange(t, "pr_alice", nil)
49 assertSignedOut(t, "sessions.Require, no cookie", nativeWrite(f, "POST", "/documents", nil))
50 assertSignedOut(t, "sessions.Require, unknown session",
51 nativeWrite(f, "POST", "/documents", &http.Cookie{Name: answer.Name, Value: "not-a-session"}))
52}
53
54func TestNativeContractEndedNativeSessionSaysSignedOut(t *testing.T) {
55 f, _ := homeNativeFixture(t)
56 _, answer := f.nativeExchange(t, "pr_alice", nil)
57 cookie := &http.Cookie{Name: answer.Name, Value: answer.Value}
58 if rec := nativeWrite(f, "POST", "/documents", cookie); rec.Code != http.StatusCreated {
59 t.Fatalf("a fresh native session could not write: %d %q", rec.Code, rec.Body.String())
60 }
61 ageSession(t, f, answer.Value, connect.NativeLifetime+time.Hour)
62 assertSignedOut(t, "refuseIdentity, native session past seven days", nativeWrite(f, "POST", "/documents", cookie))
63 assertSignedOut(t, "refuseIdentity, native session past seven days, update log",
64 nativeWrite(f, "POST", "/d/anything/updates", cookie))
65 // A page request goes to sign-in, which the native client reads as an
66 // ended session too.
67 if rec := nativeWrite(f, "GET", "/", cookie); rec.Code != http.StatusSeeOther {
68 t.Fatalf("GET with an ended native session = %d, want 303 to sign-in", rec.Code)
69 }
70}
71
72func TestNativeContractTeamEndedNativeSessionSaysSignedOut(t *testing.T) {
73 f := newTeamRouterFixture(t)
74 _, answer := f.nativeExchange(t, teamSubject('b'), map[string]string{"choice": "new"})
75 if answer.Value == "" {
76 t.Fatal("the team native exchange minted nothing")
77 }
78 cookie := &http.Cookie{Name: answer.Name, Value: answer.Value}
79 ageSession(t, f.routerHomeFixture, answer.Value, connect.NativeLifetime+time.Hour)
80 assertSignedOut(t, "team mode, native session past seven days", nativeWrite(f.routerHomeFixture, "POST", "/documents", cookie))
81 assertSignedOut(t, "team mode, unknown session",
82 nativeWrite(f.routerHomeFixture, "POST", "/documents", &http.Cookie{Name: answer.Name, Value: "not-a-session"}))
83}
84
85// Only an ended session says so: a refused identity or request keeps the
86// generic line, or the native app would re-mint a session that is fine
87// and report a refusal as signed out.
88func TestNativeContractOtherRefusalsDoNotSaySignedOut(t *testing.T) {
89 r := httptest.NewRequest("POST", bridgeOrigin+"/documents", nil)
90 r.Header.Set("Accept", "application/json")
91 rec := httptest.NewRecorder()
92 refuseIdentity(rec, r, "/signin", false)
93 if rec.Code != http.StatusForbidden || rec.Body.String() == nativeSignedOut {
94 t.Fatalf("refuseIdentity, refused identity = %d %q, want a 403 that is not the ended-session body", rec.Code, rec.Body.String())
95 }
96 rec = httptest.NewRecorder()
97 refuseIdentity(rec, r, "/signin", true)
98 assertSignedOut(t, "refuseIdentity, ended", rec)
99 if sessionEndedBody+"\n" != nativeSignedOut {
100 t.Fatalf("sessionEndedBody = %q, the native contract is %q", sessionEndedBody, nativeSignedOut)
101 }
102}
103