package slidestest import ( "crypto/sha256" "encoding/hex" "net/http" "net/url" "regexp" "strings" "testing" ) // newToken finds the token the share page was redirected with. It is // shown once, in the query string of that one redirect, and nowhere // else ever again. var newToken = regexp.MustCompile(`\?new=([A-Za-z0-9_-]{43})`) // revokeAction finds a link row's revoke form on the share page. var revokeAction = regexp.MustCompile(`/share/(\d+)/revoke`) // mintShareLink posts the create form on a deck's share page and // returns the token it was shown once. func mintShareLink(t *testing.T, a *testApp, deckPath string, cookies []*http.Cookie) string { t.Helper() rec := post(t, a, deckPath+"/share", nil, cookies...) if rec.Code != http.StatusSeeOther { t.Fatalf("POST %s/share: status %d, want 303\n%s", deckPath, rec.Code, rec.Body) } loc := rec.Header().Get("Location") m := newToken.FindStringSubmatch(loc) if m == nil { t.Fatalf("POST %s/share: Location = %q, carries no new token", deckPath, loc) } return m[1] } // TestShareLinkShownOnceAndOpensViewer: minting shows the URL once, // coming back never shows it again, and the URL itself opens the deck // for somebody with no session at all. func TestShareLinkShownOnceAndOpensViewer(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := createDeck(t, a, cookies, "Board update") empty := mustGet(t, a, path+"/share", http.StatusOK, cookies).Body.String() if !strings.Contains(empty, "No share links") { t.Errorf("a deck with no share links does not say so:\n%s", empty) } token := mintShareLink(t, a, path, cookies) shown := mustGet(t, a, path+"/share?new="+token, http.StatusOK, cookies).Body.String() full := testOrigin + "/s/" + token if !strings.Contains(shown, full) { t.Errorf("the share page does not show the new link %q:\n%s", full, shown) } if !strings.Contains(shown, "readonly") { t.Errorf("the new link is not in a read-only field:\n%s", shown) } // Come back for it and it is gone: only the hash was stored, so // there is nothing left to show. again := mustGet(t, a, path+"/share", http.StatusOK, cookies).Body.String() if strings.Contains(again, token) { t.Errorf("the share page shows the token a second time:\n%s", again) } // No cookies at all: the link is the whole capability. page := mustGet(t, a, "/s/"+token, http.StatusOK, nil).Body.String() if !strings.Contains(page, "Board update") { t.Errorf("the share link does not open the deck:\n%s", page) } if !strings.Contains(page, "