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 "context"
5 "testing"
6)
7
8// A grant implies admission.
9//
10// $DOCS_ALLOWED_ADDRESSES gates who may receive a magic link at all, and
11// gateBegin deliberately answers identically for a refused address so the
12// endpoint is not a membership oracle. Without this, sharing a document to
13// somebody outside the allowlist produces a grant that can never be redeemed:
14// the sharer sees success, the recipient gets a link that silently does
15// nothing, and neither of them is told why. That is the worst available
16// failure shape — it looks like it worked.
17//
18// So a grant widens admission. It is one clause in allows(), on purpose:
19// reversing this decision is deleting it, not unpicking a design.
20func TestAGrantAdmitsAnAddressTheAllowlistDoesNot(t *testing.T) {
21 list := allowlist{addrs: map[string]struct{}{
22 "alice@example.com": {},
23 }}
24
25 if list.admits("bob@example.com") {
26 t.Fatal("bob is admitted before any grant exists; this test proves nothing")
27 }
28
29 granted := map[string]bool{"bob@example.com": true}
30 list.granted = func(address string) (bool, error) { return granted[address], nil }
31
32 if !list.admits("bob@example.com") {
33 t.Error("bob holds a grant and is still refused admission — his invitation can never be redeemed")
34 }
35 if list.admits("carol@example.com") {
36 t.Error("carol holds no grant and was admitted; a grant admits its holder, not everybody")
37 }
38}
39
40// The address is normalised on the way in, the same way the grant stored it.
41func TestGrantedAdmissionNormalisesTheAddress(t *testing.T) {
42 list := allowlist{addrs: map[string]struct{}{}}
43 list.granted = func(address string) (bool, error) { return address == "bob@example.com", nil }
44
45 if !list.admits(" Bob@Example.COM ") {
46 t.Error("a grant made to bob did not admit the same address typed differently")
47 }
48}
49
50// An allowlist that admits everybody keeps doing so — the grant clause is a
51// widening, never a narrowing.
52func TestGrantsDoNotNarrowAnOpenAllowlist(t *testing.T) {
53 list := allowlist{} // addrs nil: development's admit-everyone
54 list.granted = func(string) (bool, error) { return false, nil }
55
56 if !list.admits("anybody@example.com") {
57 t.Error("an open allowlist stopped admitting everybody once grants were wired in")
58 }
59}
60
61// And the store answers the question the allowlist asks.
62func TestHasGrantAnswersForARealGrant(t *testing.T) {
63 app, err := NewForTest(t)
64 if err != nil {
65 t.Fatalf("NewForTest: %v", err)
66 }
67 ctx := context.Background()
68 store := app.Store()
69
70 alice, _ := store.EnsurePerson(ctx, "alice@example.com")
71 doc, err := store.Create(ctx, alice, "Alice's document")
72 if err != nil {
73 t.Fatalf("Create: %v", err)
74 }
75
76 if held, err := store.HasGrant(ctx, "bob@example.com"); err != nil || held {
77 t.Fatalf("bob holds a grant before one was made (held=%v err=%v)", held, err)
78 }
79 if err := store.Grant(ctx, alice, doc.Sqid, "bob@example.com", RoleReader); err != nil {
80 t.Fatalf("Grant: %v", err)
81 }
82 if held, err := store.HasGrant(ctx, "bob@example.com"); err != nil || !held {
83 t.Errorf("bob holds a grant and HasGrant says otherwise (held=%v err=%v)", held, err)
84 }
85}
86