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 "errors"
6 "net/http/httptest"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/home/roster"
12 "amadan.net/rastrillo/rastrillo/assertion"
13 "amadan.net/rastrillo/rastrillo/sessions"
14 "github.com/carlosframework/docs/internal/docs/principalstore"
15)
16
17func TestTeamCompletionCannotAllocateAroundCurrentAccount(t *testing.T) {
18 app := newSampleDocumentsInternalTestApp(t, "0")
19 s := app.Store()
20 ctx := t.Context()
21 local, e := s.EnsurePerson(ctx, "local@example.com")
22 if e != nil {
23 t.Fatal(e)
24 }
25 if _, e = s.Create(ctx, local, "existing"); e != nil {
26 t.Fatal(e)
27 }
28 rt, clock, subject := testTeamRoster(t, s, app)
29 calls := 0
30 rt.principals, e = principalstore.New(app.db.Writer(), principalstore.Config{Issuer: rt.bindings.Issuer, Team: rt.bindings.Team, RequireMembership: func(ctx context.Context, q principalstore.MembershipRequest) error {
31 calls++
32 d := rt.cache.Check(ctx, q.Issuer, q.Team, q.Subject)
33 if d.State == roster.Member && d.Lease.Valid() {
34 return nil
35 }
36 if d.State == roster.NotMember {
37 return principalstore.ErrRefused
38 }
39 return principalstore.ErrUnavailable
40 }})
41 if e != nil {
42 t.Fatal(e)
43 }
44 b := &homeBridge{store: s, resolver: identityResolver{store: s, team: rt}}
45 result := &homeFinishResult{}
46 r := httptest.NewRequest("GET", "https://docs.example/auth/home/finish", nil).WithContext(context.WithValue(ctx, homeFinishKey{}, result))
47 claims := assertion.Claims{Issuer: rt.bindings.Issuer, Subject: subject}
48 current := &sessions.Session{Subject: "local@example.com", Method: "magiclink", At: time.Now()}
49 if e = b.completeTeamPrincipal(r, claims, current); e == nil || calls != 0 {
50 t.Fatal("current account conflict reached allocation", e)
51 }
52 if _, e = rt.principals.Resolve(ctx, claims.Issuer, subject); !errors.Is(e, principalstore.ErrRefused) {
53 t.Fatal("conflict allocated principal", e)
54 }
55 if e = b.completeTeamPrincipal(r, claims, nil); e != nil || calls != 1 {
56 t.Fatal("verified fresh member could not create stable identity", e)
57 }
58 p, e := rt.principals.Resolve(ctx, claims.Issuer, subject)
59 if e != nil || p.ID != principalstore.HomeIDMin || p.VerifiedAddress != nil {
60 t.Fatal("Home principal changed address or allocation", e)
61 }
62 var people, docs int64
63 s.db.Model(&Person{}).Count(&people)
64 s.db.Model(&Document{}).Count(&docs)
65 if people != 1 || docs != 1 {
66 t.Fatal("Home completion fabricated local person or sample documents")
67 }
68 clock.step(60 * time.Second)
69 if e = b.completeTeamPrincipal(r, claims, &sessions.Session{Subject: subject, Method: homeAssertionMethod}); e != nil || calls != 1 {
70 t.Fatal("known identity requires no new allocation or standing", e)
71 }
72 claims.Subject = "pr_" + strings.Repeat("b", 26)
73 if e = b.completeTeamPrincipal(r, claims, nil); e == nil || result.status != 503 {
74 t.Fatal("unavailable roster did not refuse unknown identity", e)
75 }
76 if _, e = rt.principals.Resolve(ctx, claims.Issuer, claims.Subject); !errors.Is(e, principalstore.ErrRefused) {
77 t.Fatal("unknown roster allocated principal", e)
78 }
79}
80