| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/connect" |
| 12 | "amadan.net/rastrillo/rastrillo/sessions" |
| 13 | ) |
| 14 | |
| 15 | // A native contract, not copy: the native app's Oficina ID transport |
| 16 | // (native/apple/Sources/SheetsKit/SheetsOficinaID.swift, |
| 17 | // SheetsOficinaTransport.signInFirst) reads a 403 whose body is exactly |
| 18 | // "Sign in first.\n" as an ended session and re-mints it. Every session |
| 19 | // guard that refuses a non-GET request with it must keep these bytes; a |
| 20 | // copy edit here strands a native window whose first request is a write. |
| 21 | const nativeSignInFirst = "Sign in first.\n" |
| 22 | |
| 23 | func assertSignInFirst(t *testing.T, where string, rec *httptest.ResponseRecorder) { |
| 24 | t.Helper() |
| 25 | if rec.Code != http.StatusForbidden || rec.Body.String() != nativeSignInFirst { |
| 26 | t.Fatalf("%s = %d %q, want 403 %q (the native transport's re-mint signal)", where, rec.Code, rec.Body.String(), nativeSignInFirst) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // nativeWrite is the native app's POST: JSON wanted, from the app's origin, |
| 31 | // with one session cookie. |
| 32 | func nativeWrite(app *App, cookie *http.Cookie) *httptest.ResponseRecorder { |
| 33 | r := httptest.NewRequest("POST", testOrigin+"/sheets", strings.NewReader(url.Values{"title": {"x"}}.Encode())) |
| 34 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 35 | r.Header.Set("Accept", "application/json") |
| 36 | r.Header.Set("Origin", testOrigin) |
| 37 | r.AddCookie(cookie) |
| 38 | rec := httptest.NewRecorder() |
| 39 | app.Router().ServeHTTP(rec, r) |
| 40 | return rec |
| 41 | } |
| 42 | |
| 43 | func TestNativeContractHomeRequireSaysSignInFirst(t *testing.T) { |
| 44 | f := newHomeFixture(t) |
| 45 | f.local(t, signedInAddress) |
| 46 | f.link(t, "pr_linked") |
| 47 | _, answer := f.nativeExchange(t, "pr_linked", nil) |
| 48 | f.age(t, answer, connect.NativeLifetime+time.Hour) |
| 49 | assertSignInFirst(t, "homeRequire, expired native session", nativeWrite(f.app, &http.Cookie{Name: answer.Name, Value: answer.Value})) |
| 50 | } |
| 51 | |
| 52 | func TestNativeContractTeamRequireSaysSignInFirst(t *testing.T) { |
| 53 | f := newTeamFixture(t) |
| 54 | b := teamSubject('b') |
| 55 | f.dir.Set(b, "member") |
| 56 | _, answer := f.nativeExchange(t, b, map[string]string{"choice": "new"}) |
| 57 | f.age(t, answer, connect.NativeLifetime+time.Hour) |
| 58 | assertSignInFirst(t, "teamRequire, expired native session", nativeWrite(f.app, &http.Cookie{Name: answer.Name, Value: answer.Value})) |
| 59 | } |
| 60 | |
| 61 | func TestNativeContractRejectDisabledHomeSaysSignInFirst(t *testing.T) { |
| 62 | app, _ := appWithOrigin(t, testOrigin) |
| 63 | d, err := app.db.DB() |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | s, err := sessions.New(sessions.Config{DB: d, Origin: testOrigin, SigninPath: "/signin"}) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | token, err := s.Mint(sessions.Session{Subject: "pr_native", Method: connect.NativeMethod, AuthTime: time.Now()}) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | assertSignInFirst(t, "rejectDisabledHome, native session with Home off", nativeWrite(app, &http.Cookie{Name: s.CookieName(), Value: token})) |
| 76 | } |
| 77 | |
| 78 | func TestNativeContractForbiddenSaysSignInFirst(t *testing.T) { |
| 79 | app, _ := appWithOrigin(t, testOrigin) |
| 80 | r := httptest.NewRequest("POST", testOrigin+"/sheets", nil) |
| 81 | r.Header.Set("Accept", "application/json") |
| 82 | rec := httptest.NewRecorder() |
| 83 | app.forbidden(rec, r) |
| 84 | assertSignInFirst(t, "a.forbidden, non-HTML", rec) |
| 85 | } |
| 86 | |