| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/home/connect/linkstore" |
| 11 | ) |
| 12 | |
| 13 | // TestAdoptedLinkSignsInAsTheLinkedPerson is the move an app makes once at |
| 14 | // boot: carry its own link row over inside one transaction, then serve. An |
| 15 | // ordinary Home sign-in for that subject is the same local person, exactly |
| 16 | // as after linking here. |
| 17 | func TestAdoptedLinkSignsInAsTheLinkedPerson(t *testing.T) { |
| 18 | f := newBridgeFixture(t) |
| 19 | f.local(t, "one@example.com", "magiclink", time.Time{}) |
| 20 | person := f.personID(t, "one@example.com") |
| 21 | ctx := t.Context() |
| 22 | tx, err := f.db.BeginTx(ctx, nil) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if err := f.b.Adopt(ctx, tx, "https://other.example", "opaque-subject-one", person); !errors.Is(err, linkstore.ErrRefused) { |
| 27 | t.Fatal("foreign issuer adopted", err) |
| 28 | } |
| 29 | if err := f.b.Adopt(ctx, tx, bridgeIssuer, "opaque-subject-one", person); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if err := tx.Commit(); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if linked, err := f.b.Linked(ctx, person); err != nil || !linked { |
| 36 | t.Fatal("adopted person not linked", linked, err) |
| 37 | } |
| 38 | pending, nonce := f.start(t, nil, "/d/existing") |
| 39 | inbox := f.callback(t, "opaque-subject-one", nonce, time.Now().Add(-time.Minute)) |
| 40 | finish := httptest.NewRecorder() |
| 41 | f.b.Finish(finish, bridgeRequest(http.MethodGet, "/auth/finish", []*http.Cookie{pending, inbox})) |
| 42 | if finish.Code != 303 || finish.Header().Get("Location") != "/d/existing" { |
| 43 | t.Fatalf("ordinary finish=%d %q", finish.Code, finish.Header().Get("Location")) |
| 44 | } |
| 45 | identity, err := f.b.ResolveSession(bridgeRequest(http.MethodGet, "/", []*http.Cookie{bridgeCookie(t, finish, f.b.sessionCookie)})) |
| 46 | if err != nil || identity.ID != person || identity.Label != "one@example.com" { |
| 47 | t.Fatal("adopted link resolved to", identity, err) |
| 48 | } |
| 49 | } |
| 50 | |