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 "bytes"
5 "context"
6 "net/http"
7 "net/http/httptest"
8 "net/url"
9 "testing"
10
11 "amadan.net/oficina/home/handoff/seal"
12 assertioncrypto "amadan.net/rastrillo/rastrillo/crypto"
13)
14
15const bridgeOrigin = "https://blue.docs.oficina.works"
16const bridgeIssuer = "https://id.oficina.works"
17
18// connect's cookies, as a browser holds them.
19const (
20 homeInitCookie = "__Host-oficina-home-init"
21 homeProofCookie = "__Host-oficina-home-proof"
22 localLinkNavigationCookie = "__Host-oficina-local-link-navigation"
23 connectReturnCookie = "__Host-oficina-home-return"
24)
25
26// connectedURL is where connect sends a confirmed link: "Accounts
27// connected", with the page the link began from.
28func connectedURL(path string) string {
29 return "/account/home/confirm?" + url.Values{"connected": {"1"}, "return_to": {path}}.Encode()
30}
31
32type navigationMail struct{}
33
34func (navigationMail) Send(context.Context, string, string, string) error { return nil }
35
36// homeFixture is Docs' side of connect (newHome) over a test App's database,
37// not mounted on its router.
38type homeFixture struct {
39 h *homeRoutes
40 app *App
41 key *assertioncrypto.Keypair
42}
43
44func newBridgeFixture(t *testing.T) *homeFixture {
45 t.Helper()
46 app, err := NewForTestWith(t, Config{Origin: bridgeOrigin, SampleDocuments: "false"})
47 if err != nil {
48 t.Fatal(err)
49 }
50 key, err := assertioncrypto.Generate()
51 if err != nil {
52 t.Fatal(err)
53 }
54 settings := &homeSettings{issuer: bridgeIssuer, consumerID: "blue-docs", keys: map[string][]byte{"key": key.SignPub()}, dek: bytes.Repeat([]byte{41}, 32)}
55 h, err := newHome(settings, bridgeOrigin, app.db.Writer(), app.store, false)
56 if err != nil {
57 t.Fatal(err)
58 }
59 return &homeFixture{h: h, app: app, key: key}
60}
61
62func bridgeRequest(method, path string, cookies []*http.Cookie) *http.Request {
63 r := httptest.NewRequest(method, bridgeOrigin+path, nil)
64 for _, cookie := range cookies {
65 r.AddCookie(cookie)
66 }
67 return r
68}
69
70func bridgeCookie(t *testing.T, w *httptest.ResponseRecorder, name string) *http.Cookie {
71 t.Helper()
72 for _, cookie := range w.Result().Cookies() {
73 if cookie.Name == name && cookie.MaxAge >= 0 {
74 if !cookie.Secure || !cookie.HttpOnly || cookie.Domain != "" || cookie.Path != "/" || cookie.SameSite != http.SameSiteLaxMode {
75 t.Fatal("unsafe cookie attributes")
76 }
77 return cookie
78 }
79 }
80 t.Fatalf("missing cookie %s; status=%d", name, w.Code)
81 return nil
82}
83
84const navigationOrigin = "https://docs.example.com"
85
86// navigationFixture is Docs' return memory, sealed as a Home instance's is.
87func navigationFixture(t *testing.T) (*App, *returnMemory) {
88 t.Helper()
89 app, err := NewForTestWith(t, Config{Origin: navigationOrigin, SampleDocuments: "0", Mailer: navigationMail{}})
90 if err != nil {
91 t.Fatal(err)
92 }
93 sealer, err := seal.New(bytes.Repeat([]byte{19}, 32), homeNamespace)
94 if err != nil {
95 t.Fatal(err)
96 }
97 n := newReturnMemory(sealer, navigationOrigin)
98 if n == nil {
99 t.Fatal("return memory refused")
100 }
101 return app, n
102}
103