Files
supermarket/internal/web/connections_test.go
T
davidandClaude Opus 5.5 a4dd36f5a4 Phase 10, part 6: Settings → Connections, with who connected each
Admins see every service Supermarket uses, whether it's connected,
failing or not set up, when it was last tested (every Test button now
records its result), and who connected or removed it and when. Keys and
sign-ins saved from the settings pages record the person; refreshed
tokens don't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 09:15:43 -07:00

68 lines
2.5 KiB
Go

package web
import (
"net/http"
"net/url"
"strings"
"testing"
"supermarket/internal/secrets"
"supermarket/internal/users"
)
func TestConnectionsPage(t *testing.T) {
s, h := newServerEdition(t)
addUser(t, s, "owner@example.com", users.Owner, nil)
admin := addUser(t, s, "ada@example.com", users.Admin, nil)
admin.Name = "Ada"
users.SaveProfile(s.DB, admin)
ac := sessionFor(t, s, admin)
member := addUser(t, s, "sam@example.com", users.Member, map[string]users.Access{"seo": users.Edit})
if rec := as(h, sessionFor(t, s, member), http.MethodGet, "/settings/connections", nil); rec.Code != http.StatusForbidden {
t.Errorf("member: %d", rec.Code)
}
rec := as(h, ac, http.MethodGet, "/settings/connections", nil)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Hunter.io") || !strings.Contains(rec.Body.String(), "Not set up") {
t.Fatalf("page: %d", rec.Code)
}
// Ada saves the Hunter.io key: she's recorded as having connected it.
if rec := as(h, ac, http.MethodPost, "/settings/sources", url.Values{secrets.HunterAPIKey: {"hk"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d", rec.Code)
}
if s.Secrets.Get(secrets.HunterAPIKey) != "hk" {
t.Fatal("not saved")
}
body := as(h, ac, http.MethodGet, "/settings/connections", nil).Body.String()
row := body[strings.Index(body, "Hunter.io"):]
row = row[:strings.Index(row, "</tr>")]
if !strings.Contains(row, "Connected") || !strings.Contains(row, "Ada, just now") {
t.Errorf("row: %s", row)
}
// A failed test shows it failing.
s.noteCheckErr("hunter", "401 Unauthorized")
body = as(h, ac, http.MethodGet, "/settings/connections", nil).Body.String()
row = body[strings.Index(body, "Hunter.io"):]
row = row[:strings.Index(row, "</tr>")]
if !strings.Contains(row, "Failing") || !strings.Contains(row, "401 Unauthorized") {
t.Errorf("failing row: %s", row)
}
// Removing it is recorded too.
as(h, ac, http.MethodPost, "/settings/sources", url.Values{"remove:" + secrets.HunterAPIKey: {"on"}})
body = as(h, ac, http.MethodGet, "/settings/connections", nil).Body.String()
if !strings.Contains(body, "Removed by Ada") {
t.Error("removal not recorded")
}
}
func TestServiceOf(t *testing.T) {
for name, want := range map[string]string{
secrets.AnthropicAPIKey: "ai", secrets.XRefreshToken: "x", secrets.LinkedInPageAccessToken: "linkedin_page",
secrets.MailPassword: "mail", secrets.GoogleClientID: "google", "totp:3": "",
} {
if got := serviceOf(name); got != want {
t.Errorf("serviceOf(%s) = %q, want %q", name, got, want)
}
}
}