| new file mode 100644 |
| index 0000000..74c6fba |
| --- /dev/null |
| +++ b/internal/webtest/oficina_origin_test.go |
| @@ -0,0 +1,222 @@ |
| +package webtest |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "fmt" |
| + "net/http" |
| + "net/url" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/home/connect/linkstore" |
| + |
| + "amadan.net/carlos/base/internal/store" |
| +) |
| + |
| +// Moving Base to another BASE_ORIGIN. connect stamps each Oficina ID link |
| +// with Base's origin (its person binding), so a link made at the old origin |
| +// resolves nobody at the new one, and would block linking again. Base |
| +// treats such a link as gone: the member links again, proving who they are |
| +// by email as at first, and the dead link makes way for the new one. |
| + |
| +// oldOrigin is where Base ran before the move. |
| +const oldOrigin = "https://base.old.test" |
| + |
| +// linkAtOldOrigin stores the link connect would have made between email's |
| +// member and subject while Base ran at oldOrigin: the same key, issuer and |
| +// namespace, another origin. It is connect's own carry-over (Adopt), so the |
| +// row is exactly the one linking there wrote. |
| +func (a *testApp) linkAtOldOrigin(email, subject string) { |
| + a.t.Helper() |
| + ctx := context.Background() |
| + old, err := linkstore.New(a.DB, bytes.Repeat([]byte{9}, 32), oldOrigin, homeIssuer, "carlos/base", |
| + func(ctx context.Context, q linkstore.Queryer, n int64) (string, error) { |
| + return store.MemberAddress(ctx, q, n) |
| + }) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + m, ok, err := store.MemberByEmail(ctx, a.DB, email) |
| + if err != nil || !ok { |
| + a.t.Fatalf("member %s: %v", email, err) |
| + } |
| + n, err := store.MemberNumber(ctx, a.DB, m.ID) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + tx, err := a.DB.BeginTx(ctx, nil) |
| + if err != nil { |
| + a.t.Fatal(err) |
| + } |
| + defer tx.Rollback() |
| + if err := old.Adopt(ctx, tx, homeIssuer, subject, n); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + if err := tx.Commit(); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + // At the old origin it resolves; here it does not. |
| + if _, _, err := old.Resolve(ctx, homeIssuer, subject); err != nil { |
| + a.t.Fatalf("the old link does not resolve at the old origin: %v", err) |
| + } |
| +} |
| + |
| +// linkRows is how many Oficina ID links name email's member. |
| +func (a *testApp) linkRows(email string) int { |
| + a.t.Helper() |
| + var n int |
| + if err := a.DB.QueryRow(`SELECT count(*) FROM home_identity_links l JOIN member_numbers n ON n.number = l.person_id |
| + JOIN members m ON m.id = n.member_id WHERE m.email = ?`, email).Scan(&n); err != nil { |
| + a.t.Fatal(err) |
| + } |
| + return n |
| +} |
| + |
| +// The move itself: the old link refuses Oficina sign-in, as it must, and |
| +// the member links again the way an unlinked member does (Oficina, then |
| +// email once), after which Oficina ID signs them in. |
| +func TestOficinaLinkFromAnOldOriginLinksAgain(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + |
| + unknown := a.signInWithOficina("subject-owner") |
| + if unknown.Code != http.StatusForbidden || !strings.Contains(unknown.Body, "Oficina account not connected") { |
| + t.Fatalf("Oficina sign-in on a link from the old origin: %d\n%s", unknown.Code, unknown.Body) |
| + } |
| + r := a.signIn(ownerAddress) |
| + resume := a.get(r.Location()) |
| + if resume.Code != 303 || !strings.HasPrefix(resume.Location(), "/auth/start") { |
| + t.Fatalf("local sign-in did not resume the link: %d %q", resume.Code, resume.Location()) |
| + } |
| + a.confirmLink(a.atOficina(a.get(resume.Location()), "subject-owner", 30*time.Second), ownerAddress) |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("links: %d", n) |
| + } |
| + b := a.newBrowser() |
| + if fin := b.signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| + t.Fatalf("Oficina sign-in after linking again: %d %q\n%s", fin.Code, fin.Location(), fin.Body) |
| + } |
| + if home := b.get("/"); home.Code != 200 { |
| + t.Fatalf("Home: %d", home.Code) |
| + } |
| + if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| + t.Errorf("Settings still offers Connect:\n%s", body) |
| + } |
| +} |
| + |
| +// Settings no longer says the dead link is connected: it offers Connect, |
| +// and connecting from there works too. |
| +func TestOficinaLinkFromAnOldOriginConnectsFromSettings(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + a.link(ownerAddress, "subject-owner") // fails unless Settings offers Connect and the confirm goes through |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("links: %d", n) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 || fin.Location() != "/" { |
| + t.Fatalf("Oficina sign-in: %d %q", fin.Code, fin.Location()) |
| + } |
| +} |
| + |
| +// A link that still resolves is never replaced: another Oficina ID can't |
| +// take a linked member over, even with their email. |
| +func TestOficinaLiveLinkIsNotReplaced(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.link(ownerAddress, "subject-owner") |
| + if body := a.get("/settings").Body; strings.Contains(body, `href="/account/home"`) { |
| + t.Fatalf("Settings offers Connect to a linked member") |
| + } |
| + toIssuer := a.get(a.post("/account/home", nil).Location()) |
| + finish := a.atOficina(toIssuer, "subject-intruder", 30*time.Second) |
| + if finish.Code != 303 || finish.Location() != "/account/home/confirm" { |
| + t.Fatalf("link finish: %d %q", finish.Code, finish.Location()) |
| + } |
| + if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| + t.Fatalf("confirming a second Oficina ID: %d %q", r.Code, r.Location()) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-owner"); fin.Code != 303 { |
| + t.Errorf("the live link stopped working: %d", fin.Code) |
| + } |
| + if fin := a.newBrowser().signInWithOficina("subject-intruder"); fin.Code != http.StatusForbidden { |
| + t.Errorf("the other Oficina ID signs in: %d", fin.Code) |
| + } |
| +} |
| + |
| +// Linking again clears only the member's own dead link: another member's |
| +// dead link to the same Oficina ID stays, and the confirm is refused. |
| +func TestOficinaOldLinkOfAnotherMemberStays(t *testing.T) { |
| + a := newHomeApp(t) |
| + if _, _, err := store.Invite(context.Background(), a.DB, "ana@acme.test"); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.linkAtOldOrigin("ana@acme.test", "subject-shared") |
| + a.signedInOwner() |
| + toIssuer := a.get(a.post("/account/home", nil).Location()) |
| + a.atOficina(toIssuer, "subject-shared", 30*time.Second) |
| + if r := a.post("/account/home/confirm", nil); r.Code != http.StatusForbidden { |
| + t.Fatalf("the owner took Ana's Oficina ID: %d %q", r.Code, r.Location()) |
| + } |
| + if a.linkRows("ana@acme.test") != 1 || a.linkRows(ownerAddress) != 0 { |
| + t.Fatalf("links: ana %d, owner %d", a.linkRows("ana@acme.test"), a.linkRows(ownerAddress)) |
| + } |
| +} |
| + |
| +// Nothing but the member's own fresh email sign-in clears a dead link: |
| +// not a signed-out POST, nor a POST from another site, nor an old sign-in. |
| +func TestOficinaOldLinkClearedOnlyByTheMember(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + before := a.linkRow(ownerAddress) |
| + unchanged := func(what string) { |
| + t.Helper() |
| + if after := a.linkRow(ownerAddress); after != before { |
| + t.Fatalf("%s changed the dead link: %q, was %q", what, after, before) |
| + } |
| + } |
| + if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden { |
| + t.Fatalf("signed-out cross-site confirm: %d", r.Code) |
| + } |
| + unchanged("a signed-out cross-site confirm") |
| + if r := a.post("/account/home/confirm", nil); r.Code == http.StatusOK { |
| + t.Fatalf("signed-out confirm: %d", r.Code) |
| + } |
| + unchanged("a signed-out confirm") |
| + // The owner freshly signed in by email, and a page on another site |
| + // posting the confirm in their browser: refused before anything is |
| + // cleared. |
| + a.signedInOwner() |
| + if r := a.postFrom(homeIssuer, "/account/home/confirm", url.Values{}); r.Code != http.StatusForbidden { |
| + t.Fatalf("signed-in cross-site confirm: %d", r.Code) |
| + } |
| + unchanged("a signed-in cross-site confirm") |
| +} |
| + |
| +// linkRow is the Oficina ID link naming email's member, as stored. |
| +func (a *testApp) linkRow(email string) string { |
| + a.t.Helper() |
| + var subject, binding string |
| + var person int64 |
| + if err := a.DB.QueryRow(`SELECT l.subject, l.person_id, l.person_binding FROM home_identity_links l |
| + JOIN member_numbers n ON n.number = l.person_id JOIN members m ON m.id = n.member_id WHERE m.email = ?`, email). |
| + Scan(&subject, &person, &binding); err != nil { |
| + a.t.Fatalf("link row for %s: %v", email, err) |
| + } |
| + return fmt.Sprintf("%s/%d/%s", subject, person, binding) |
| +} |
| + |
| +// An email sign-in older than connect's five minutes clears nothing. |
| +func TestOficinaOldLinkNeedsAFreshSignIn(t *testing.T) { |
| + a := newHomeApp(t) |
| + a.linkAtOldOrigin(ownerAddress, "subject-owner") |
| + a.signedInOwner() |
| + old := time.Now().Add(-10 * time.Minute).UTC().Format(time.RFC3339) |
| + if _, err := a.DB.Exec(`UPDATE sessions SET created_at = ?, auth_time = ?`, old, old); err != nil { |
| + t.Fatal(err) |
| + } |
| + a.post("/account/home/confirm", nil) |
| + if n := a.linkRows(ownerAddress); n != 1 { |
| + t.Fatalf("an old sign-in cleared the link: %d", n) |
| + } |
| +} |