oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "html"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/home/connect"
12)
13
14// connectItem is the account menu's "Connect your Oficina account".
15const connectItem = `<a estilo-menu-item href="/account/home"><span estilo-menu-label>Connect your Oficina account</span></a>`
16
17// offersConnect renders the list and reports whether its account menu
18// offers the connection. It fails unless the page is a signed-in one, so
19// a missing item can never be a missing menu.
20func offersConnect(t *testing.T, f *homeFixture) bool {
21 t.Helper()
22 page := f.request(t, "GET", "/sheets", nil, false)
23 body := page.Body.String()
24 if page.Code != http.StatusOK || !strings.Contains(body, "<details estilo-account") {
25 t.Fatalf("/sheets is not a signed-in page: %d", page.Code)
26 }
27 return strings.Contains(body, connectItem)
28}
29
30// linkRows counts the stored Home links for one member.
31func linkRows(t *testing.T, f *homeFixture, member Member) int64 {
32 t.Helper()
33 var n int64
34 if err := f.app.db.Table("home_identity_links").Where("person_id = ?", member.ID).Count(&n).Error; err != nil {
35 t.Fatal(err)
36 }
37 return n
38}
39
40// emailSignIn is a real local sign-in through the emailed link, in this
41// fixture's browser: request the link, open it, press Sign in. It returns
42// the response to the local completion GET, which decides the landing.
43func emailSignIn(t *testing.T, f *homeFixture, address string) *httptest.ResponseRecorder {
44 t.Helper()
45 if w := f.request(t, "POST", "/signin", url.Values{"address": {address}, "force": {"1"}}, false); w.Code != http.StatusSeeOther {
46 t.Fatal("request link", w.Code)
47 }
48 u, err := url.Parse(f.app.testMailer(t).lastLink(t))
49 if err != nil {
50 t.Fatal(err)
51 }
52 open := f.request(t, "GET", u.RequestURI(), nil, false)
53 m := confirmForm.FindStringSubmatch(open.Body.String())
54 if open.Code != http.StatusOK || m == nil {
55 t.Fatalf("open link: %d, want the confirm door", open.Code)
56 }
57 verify := f.request(t, "POST", html.UnescapeString(m[1]), url.Values{"token": {html.UnescapeString(m[2])}}, false)
58 if verify.Code != http.StatusSeeOther || verify.Header().Get("Location") != connect.LocalCompletePath {
59 t.Fatal("local callback", verify.Code, verify.Header().Get("Location"))
60 }
61 return f.request(t, "GET", connect.LocalCompletePath, nil, false)
62}
63
64// The menu item follows the stored link, not how this session signed in.
65// Paul hit the bug in Docs: linked, signed in with the email link, and
66// still offered "Connect your Oficina account".
67func TestConnectItemFollowsTheStoredLink(t *testing.T) {
68 f := newHomeFixture(t)
69 member := f.local(t, signedInAddress)
70 if !offersConnect(t, f) {
71 t.Fatal("an unlinked local session is not offered the connection")
72 }
73 f.link(t, "linked-subject")
74 if linkRows(t, f, member) != 1 {
75 t.Fatal("the link flow stored no link")
76 }
77 if offersConnect(t, f) {
78 t.Error("the same session is still offered the connection after linking")
79 }
80 // A later sign-in with the app's own email link, by the linked person.
81 f.jar = map[string]*http.Cookie{}
82 f.local(t, signedInAddress)
83 if offersConnect(t, f) {
84 t.Error("a linked person signed in by email link is offered the connection")
85 }
86 g := newHomeFixture(t)
87 homeSignedIn(t, g)
88 if offersConnect(t, g) {
89 t.Error("a Home session is offered the connection")
90 }
91}
92
93// After the confirm POST, in the same session: the answer leads to
94// "Accounts connected", the link row exists, and the next render's menu
95// no longer offers the item. The confirm page itself is Sheets' copy and
96// its two buttons share one form-foot row, primary first.
97func TestConfirmLeadsToAccountsConnected(t *testing.T) {
98 f := newHomeFixture(t)
99 member := f.local(t, signedInAddress)
100 start := f.request(t, "POST", "/account/home", nil, false)
101 if start.Code != http.StatusSeeOther {
102 t.Fatal("link start", start.Code)
103 }
104 authStart := f.request(t, "GET", start.Header().Get("Location"), nil, false)
105 if finish := f.exchange(t, authStart, "confirm-subject"); finish.Header().Get("Location") != "/account/home/confirm" {
106 t.Fatal("finish link", finish.Code, finish.Header().Get("Location"))
107 }
108 confirm := f.request(t, "GET", "/account/home/confirm", nil, false)
109 body := confirm.Body.String()
110 if confirm.Code != http.StatusOK || !strings.Contains(body, "<h1>Connect these two accounts?</h1>") ||
111 !strings.Contains(body, "Connect it to the Sheets account below, and you can sign in to Sheets with Oficina from now on.") {
112 t.Fatalf("confirm page: %d, want Sheets' confirm copy\n%s", confirm.Code, body)
113 }
114 if strings.Contains(body, "Home account") {
115 t.Error("the confirm page names the hub")
116 }
117 row := strings.Index(body, "<div rst-form-foot>")
118 if row < 0 {
119 t.Fatal("the confirm page's buttons are not in one rst-form-foot row")
120 }
121 end := row + strings.Index(body[row:], "</div>")
122 primary := strings.Index(body[row:end], `<button rst-btn="primary" type="submit">Connect accounts</button>`)
123 ghost := strings.Index(body[row:end], `<a rst-btn="ghost" href="/">`)
124 if primary < 0 || ghost < 0 || primary > ghost {
125 t.Fatalf("form-foot row is not primary then ghost: %q", body[row:end])
126 }
127 if linkRows(t, f, member) != 0 {
128 t.Fatal("a link was stored before the confirm click")
129 }
130 posted := f.request(t, "POST", "/account/home/confirm", nil, false)
131 if posted.Code != http.StatusSeeOther || !strings.HasPrefix(posted.Header().Get("Location"), "/account/home/confirm?connected=1") {
132 t.Fatal("confirm POST", posted.Code, posted.Header().Get("Location"))
133 }
134 done := f.request(t, "GET", posted.Header().Get("Location"), nil, false)
135 if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") {
136 t.Fatalf("after confirm: %d, want Accounts connected\n%s", done.Code, done.Body.String())
137 }
138 if linkRows(t, f, member) != 1 {
139 t.Fatal("the confirm POST stored no link")
140 }
141 if offersConnect(t, f) {
142 t.Error("the menu still offers the connection right after confirming")
143 }
144}
145
146// An Oficina arrival with no link shows the unknown page; a local sign-in
147// in the same browser then begins the link itself, through a fresh Home
148// assertion to the confirm page, and the confirm click stores the link.
149// "Accounts connected" leads back to where sign-in began (PageFor).
150func TestUnknownArrivalResumesTheLinkAfterLocalSignIn(t *testing.T) {
151 f := newHomeFixture(t)
152 signin := f.request(t, "GET", "/signin/oficina?return_to=%2Fsheets", nil, false)
153 unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject")
154 body := unknown.Body.String()
155 if unknown.Code != http.StatusForbidden || !strings.Contains(body, `href="/signin/local"`) ||
156 !strings.Contains(body, "Sign in to your Sheets account once to connect it to your Oficina account.") {
157 t.Fatalf("unknown arrival: %d, want the unknown page\n%s", unknown.Code, body)
158 }
159 completed := emailSignIn(t, f, signedInAddress)
160 if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/auth/start?return_to=%2Faccount%2Fhome%2Fconfirm" {
161 t.Fatal("local sign-in after unknown arrival", completed.Code, completed.Header().Get("Location"))
162 }
163 if _, ok := f.jar["__Host-oficina-home-init"]; !ok {
164 t.Fatal("the local sign-in began no link")
165 }
166 authStart := f.request(t, "GET", completed.Header().Get("Location"), nil, false)
167 if finish := f.exchange(t, authStart, "unknown-subject"); finish.Code != http.StatusSeeOther || finish.Header().Get("Location") != "/account/home/confirm" {
168 t.Fatal("fresh assertion", finish.Code, finish.Header().Get("Location"))
169 }
170 member, ok := f.app.memberByID(1)
171 if !ok || member.Address != signedInAddress {
172 t.Fatal("local member missing")
173 }
174 if page := f.request(t, "GET", "/account/home/confirm", nil, false); page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "Connect these two accounts?") {
175 t.Fatal("confirm page", page.Code)
176 }
177 if linkRows(t, f, member) != 0 {
178 t.Fatal("a link was stored before the confirm click")
179 }
180 posted := f.request(t, "POST", "/account/home/confirm", nil, false)
181 if posted.Code != http.StatusSeeOther {
182 t.Fatal("confirm POST", posted.Code)
183 }
184 if linkRows(t, f, member) != 1 {
185 t.Fatal("the confirm POST stored no link")
186 }
187 done := f.request(t, "GET", posted.Header().Get("Location"), nil, false)
188 if done.Code != http.StatusOK || !strings.Contains(done.Body.String(), "<h1>Accounts connected</h1>") ||
189 !strings.Contains(done.Body.String(), `<a rst-btn="primary" href="/sheets">Back to Sheets</a>`) {
190 t.Fatalf("after confirm: %d, want Accounts connected leading back to /sheets\n%s", done.Code, done.Body.String())
191 }
192 if offersConnect(t, f) {
193 t.Error("the menu still offers the connection after the resumed link")
194 }
195}
196
197// Without the remembered arrival the local sign-in lands as before. The
198// marker lives ten minutes in the browser (its Max-Age), so a browser
199// that has dropped it is the app-level expiry; the sealed in-marker
200// expiry is checked upstream (home/connect TestUnknownMarkerExpiresToTheMenuPath),
201// since this fixture cannot move connect's navigation clock.
202func TestUnknownArrivalForgottenLandsAsBefore(t *testing.T) {
203 f := newHomeFixture(t)
204 signin := f.request(t, "GET", "/signin/oficina?return_to=%2Fsheets", nil, false)
205 unknown := f.exchange(t, f.request(t, "GET", signin.Header().Get("Location"), nil, false), "unknown-subject")
206 if unknown.Code != http.StatusForbidden {
207 t.Fatal("unknown arrival", unknown.Code)
208 }
209 const marker = "__Host-oficina-local-link-navigation"
210 c, ok := f.jar[marker]
211 if !ok || c.MaxAge != 600 {
212 t.Fatal("the unknown arrival left no ten-minute marker", ok)
213 }
214 delete(f.jar, marker)
215 completed := emailSignIn(t, f, signedInAddress)
216 if completed.Code != http.StatusSeeOther || completed.Header().Get("Location") != "/sheets" {
217 t.Fatal("local sign-in without the marker", completed.Code, completed.Header().Get("Location"))
218 }
219 if !offersConnect(t, f) {
220 t.Error("the unlinked person is not offered the connection")
221 }
222}
223