78 lines
2 KiB
Go
78 lines
2 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"backend/internal/auth"
|
|
"backend/internal/db/sqlc"
|
|
)
|
|
|
|
func TestLinkedProviders_UnauthRedirectsToLogin(t *testing.T) {
|
|
router := newAuthPageRouter(t, AuthDeps{})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/account/providers", nil)
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusSeeOther {
|
|
t.Fatalf("status = %d; want 303", rec.Code)
|
|
}
|
|
if loc := rec.Header().Get("Location"); loc != "/login" {
|
|
t.Fatalf("Location = %q; want /login", loc)
|
|
}
|
|
}
|
|
|
|
func TestLinkedProviders_ShowsConnectedAndNotConnectedRows(t *testing.T) {
|
|
pool, cleanup := setupTestDB(t)
|
|
defer cleanup()
|
|
|
|
ctx := context.Background()
|
|
q := sqlc.New(pool)
|
|
store := auth.NewStore(q)
|
|
user := preInsertUser(t, ctx, q, "providers@example.com", "correct-horse-12chars")
|
|
if _, err := q.InsertUserIdentity(ctx, sqlc.InsertUserIdentityParams{
|
|
UserID: user.ID,
|
|
Provider: "google",
|
|
ProviderSubject: "providers-google-subject",
|
|
Email: "providers@example.com",
|
|
EmailVerified: true,
|
|
}); err != nil {
|
|
t.Fatalf("InsertUserIdentity: %v", err)
|
|
}
|
|
cookieValue, _, err := store.Create(ctx, user.ID)
|
|
if err != nil {
|
|
t.Fatalf("Create session: %v", err)
|
|
}
|
|
router := newTestRouter(q, store)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/account/providers", nil)
|
|
req.AddCookie(&http.Cookie{Name: auth.SessionCookieName, Value: cookieValue})
|
|
rec := httptest.NewRecorder()
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d; want 200; body: %s", rec.Code, rec.Body.String())
|
|
}
|
|
body := rec.Body.String()
|
|
for _, want := range []string{
|
|
"Linked providers",
|
|
"Google",
|
|
"Connected",
|
|
"providers@example.com",
|
|
"Apple",
|
|
"Not connected",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Fatalf("body missing %q; body: %s", want, body)
|
|
}
|
|
}
|
|
for _, notWant := range []string{"Unlink", "Add password"} {
|
|
if strings.Contains(body, notWant) {
|
|
t.Fatalf("body must not contain %q; body: %s", notWant, body)
|
|
}
|
|
}
|
|
}
|