package idtest // The admission probe through Equipo's real guard: removal at the door, // the refusal on the plain door only, an undecided guard, and sessions the // door never judges. import ( "net/http" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/oficinaid" "amadan.net/rastrillo/rastrillo/pow" ) // A removed member's session reaching the door is ended there, on the // door's own response, and the door says why; the next load is plainly // signed out, with no refusal and no loop. A remembered email way is kept. func TestDoorEndsARemovedMembersSession(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { const who = "removed@example.test" f.member(t, who) s, record := f.emailRecord(t, who) f.remove(t, who) if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusForbidden { t.Fatal("the guard admits a removed member", w.Code) } door := f.request("GET", "/signin", "", nil, s, record) body := door.Body.String() if door.Code != http.StatusOK || !strings.Contains(body, refusal) || title(body) != "Problem signing in to Equipo" || !strings.Contains(body, `name="address"`) { t.Fatalf("refused door %d %q", door.Code, title(body)) } if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, s) { t.Fatal("the refused session outlived the door") } if _, clears := set(door, jarCookie); clears { t.Fatal("the door forgot a remembered email way") } if strings.Contains(body, "rst-shell") || strings.Contains(body, "/signout") { t.Fatal("the refused door is drawn signed in") } again := f.request("GET", "/signin", "", nil, record) if again.Code != http.StatusOK || strings.Contains(again.Body.String(), refusal) { t.Fatal("the refusal outlived the session", again.Code) } }) } // The refusal is on the plain door only: the chooser and a way's own page // are how a refused person reaches another way in, Oficina ID included. // The session is ended there too. func TestDoorRefusalLeavesOtherWaysOpen(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { f.remove(t, address) for _, target := range []string{"/signin?ways=1", "/signin?way=email"} { s := f.local(t, address) w := f.request("GET", target, "", nil, s) if w.Code != http.StatusOK || strings.Contains(w.Body.String(), refusal) { t.Errorf("%s: %d, or drew the refusal", target, w.Code) } if f.sessionAlive(t, s) { t.Errorf("%s: the refused session lived on", target) } } if home { s := f.local(t, address) w := f.request("GET", "/signin/oficina?return_to=%2Fwork", "", nil, s) if _, clears := set(w, sessionCookie); w.Header().Get("Location") != "/auth/home?return_to=%2Fwork" || !clears { t.Fatal("a refused session at /signin/oficina", w.Header().Get("Location")) } local := f.request("GET", "/signin/local", "", nil, f.local(t, address)) if !strings.Contains(local.Body.String(), refusal) { t.Fatal("/signin/local drew no refusal") } } // Anybody can type ?err=refused; it says nothing. if w := f.request("GET", "/signin?err=refused", "", nil); strings.Contains(w.Body.String(), refusal) { t.Fatal("a typed query drew the refusal") } }) } // An Oficina ID session whose member was removed: refused, ended, and the // remembered Oficina ID forgotten, so the door cannot offer it again. func TestDoorForgetsOficinaIDForARefusedSession(t *testing.T) { t.Parallel() f := newFixture(t, true) f.link(t) done := f.oficinaSignin(t) s, record := cookie(t, done, sessionCookie), cookie(t, done, jarCookie) f.remove(t, address) door := f.request("GET", "/signin", "", nil, s, record) if !strings.Contains(door.Body.String(), refusal) { t.Fatal("no refusal", door.Code) } if _, clears := set(door, jarCookie); !clears { t.Fatal("the door kept offering Oficina ID to a refused person") } if _, clears := set(door, sessionCookie); !clears || f.sessionAlive(t, s) { t.Fatal("the refused Oficina ID session lived on") } } // A guard that cannot decide (storage failed) is no refusal: the door is // drawn, nothing is ended or forgotten, and the page answers 503. func TestDoorUndecidedIsNoRefusal(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { sessions := []*http.Cookie{f.local(t, address)} var record *http.Cookie if home { f.link(t) done := f.oficinaSignin(t) sessions = append(sessions, cookie(t, done, sessionCookie)) record = cookie(t, done, jarCookie) } f.breakRoster(t) for _, s := range sessions { if w := f.request("GET", "/work", "", nil, s); w.Code != http.StatusServiceUnavailable { t.Errorf("the guard on a storage failure: %d, want 503", w.Code) } door := f.request("GET", "/signin?sent=1", "", nil, s, record) if door.Code != http.StatusOK || strings.Contains(door.Body.String(), refusal) { t.Errorf("undecided door %d, or a refusal", door.Code) } if c, _ := set(door, sessionCookie); c != nil || !f.sessionAlive(t, s) { t.Error("an undecided session was ended") } if c, _ := set(door, jarCookie); c != nil { t.Error("an undecided door touched the remembered way") } } }) } // The native apps' sessions are never judged at the door. func TestDoorNeverJudgesANativeSession(t *testing.T) { t.Parallel() f := newFixture(t, true) f.remove(t, address) native := f.sessionAs(t, person, oficinaid.NativeMethod) w := f.request("GET", "/signin", "", nil, native) if w.Code != http.StatusOK || strings.Contains(w.Body.String(), refusal) || !f.sessionAlive(t, native) { t.Fatal("the door judged a native session", w.Code) } } // An address that verified but may not sign in (auth's Refused) gets the // door with the refusal, in a 403, rather than rastrillo's bare text. func TestDoorRefusesAVerifiedStranger(t *testing.T) { t.Parallel() modes(t, func(t *testing.T, f *fixture, home bool) { page := f.request("GET", "/signin?way=email", "", nil) form := fields(page.Body.String(), "/auth/begin") form.Set("address", "stranger@example.test") time.Sleep(pow.DefaultMinAge + 20*time.Millisecond) begin := f.request("POST", "/auth/begin", f.origin, form) jar := mergeCookies(page.Result().Cookies(), begin.Result().Cookies()) confirm := f.request("GET", f.mailedLink(t), "", nil, jar...) done := f.request("POST", "/auth/verify", f.origin, fields(confirm.Body.String(), "/auth/verify"), mergeCookies(jar, confirm.Result().Cookies())...) if done.Code != http.StatusForbidden || !strings.Contains(done.Body.String(), refusal) || !strings.Contains(done.Body.String(), "estilo-signin-screen") { t.Fatal("verified stranger", done.Code, done.Body.String()) } if c, _ := set(done, sessionCookie); c != nil { t.Fatal("a refused sign-in minted a session") } }) }