package web import ( "io" "net/http" "net/http/cookiejar" "net/http/httptest" "net/url" "path/filepath" "regexp" "strings" "testing" "my-homelab-configs/cmd/nextpath/internal/account" "my-homelab-configs/cmd/nextpath/internal/assessment" ) var csrfPattern = regexp.MustCompile(`name="csrf_token" value="([^"]+)"`) func TestCompletePrivateAssessmentFlow(t *testing.T) { accounts, err := account.Open(filepath.Join(t.TempDir(), "accounts.json"), 2) if err != nil { t.Fatal(err) } testServer := httptest.NewServer(New(accounts, Config{}).Handler()) defer testServer.Close() jar, _ := cookiejar.New(nil) client := &http.Client{Jar: jar, CheckRedirect: func(_ *http.Request, _ []*http.Request) error { return http.ErrUseLastResponse }} csrf := getCSRF(t, client, testServer.URL+"/register") response := postForm(t, client, testServer.URL+"/register", url.Values{ "csrf_token": {csrf}, "email": {"teen@example.test"}, "display_name": {"Taylor"}, "age_band": {"13-17"}, "zip_code": {"85001"}, "password": {"a family test password"}, "consent": {"yes"}, }) if response.StatusCode != http.StatusSeeOther || response.Header.Get("Location") != "/assessment" { t.Fatalf("register = %d %q", response.StatusCode, response.Header.Get("Location")) } response.Body.Close() for range assessment.Questions { response = get(t, client, testServer.URL+"/assessment") body := readBody(t, response) question := regexp.MustCompile(`name="question_id" value="([^"]+)"`).FindStringSubmatch(body) csrfMatch := csrfPattern.FindStringSubmatch(body) if len(question) != 2 || len(csrfMatch) != 2 { t.Fatalf("assessment form missing question or CSRF: %s", body) } response = postForm(t, client, testServer.URL+"/assessment", url.Values{ "csrf_token": {csrfMatch[1]}, "question_id": {question[1]}, "answer": {"4"}, }) if response.StatusCode != http.StatusSeeOther { t.Fatalf("answer returned %d", response.StatusCode) } response.Body.Close() } response = get(t, client, testServer.URL+"/results") body := readBody(t, response) if response.StatusCode != http.StatusOK || !strings.Contains(body, "interest profile") || !strings.Contains(body, "not ability, identity, or employability") || !strings.Contains(body, "Careers to explore next") { t.Fatalf("unexpected results response %d: %s", response.StatusCode, body) } response = get(t, client, testServer.URL+"/account/export") export := readBody(t, response) if !strings.Contains(export, `"interest_result"`) || strings.Contains(export, "password_hash") { t.Fatalf("unsafe or incomplete export: %s", export) } } func TestFormsRejectMissingCSRF(t *testing.T) { accounts, _ := account.Open(filepath.Join(t.TempDir(), "accounts.json"), 2) testServer := httptest.NewServer(New(accounts, Config{}).Handler()) defer testServer.Close() response, err := http.PostForm(testServer.URL+"/register", url.Values{"email": {"person@example.test"}}) if err != nil { t.Fatal(err) } defer response.Body.Close() if response.StatusCode != http.StatusBadRequest { t.Fatalf("status = %d, want 400", response.StatusCode) } } func getCSRF(t *testing.T, client *http.Client, endpoint string) string { t.Helper() response := get(t, client, endpoint) body := readBody(t, response) match := csrfPattern.FindStringSubmatch(body) if len(match) != 2 { t.Fatalf("CSRF token missing from %s", endpoint) } return match[1] } func get(t *testing.T, client *http.Client, endpoint string) *http.Response { t.Helper() response, err := client.Get(endpoint) if err != nil { t.Fatal(err) } return response } func postForm(t *testing.T, client *http.Client, endpoint string, values url.Values) *http.Response { t.Helper() response, err := client.PostForm(endpoint, values) if err != nil { t.Fatal(err) } return response } func readBody(t *testing.T, response *http.Response) string { t.Helper() defer response.Body.Close() data, err := io.ReadAll(response.Body) if err != nil { t.Fatal(err) } return string(data) }