From b90c6fa0a834177d4a01ed56c9e3e4555b339481 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Sat, 5 Dec 2026 17:10:14 -0600 Subject: [PATCH] Allow users to retake NextPath quiz --- cmd/nextpath/internal/account/store.go | 23 +++++++++++++++++++++ cmd/nextpath/internal/account/store_test.go | 11 ++++++++++ cmd/nextpath/internal/web/server.go | 13 ++++++++++++ cmd/nextpath/internal/web/server_test.go | 18 +++++++++++++++- cmd/nextpath/internal/web/templates.go | 2 +- 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/cmd/nextpath/internal/account/store.go b/cmd/nextpath/internal/account/store.go index 98ccd20..ef53072 100644 --- a/cmd/nextpath/internal/account/store.go +++ b/cmd/nextpath/internal/account/store.go @@ -213,6 +213,29 @@ func (store *Store) SaveAnswers(id, version string, answers map[string]int) (Use return cloneUser(user.User), nil } +// ResetAssessment clears only the saved questionnaire state. Identity, +// consent, and account credentials remain unchanged. +func (store *Store) ResetAssessment(id string) (User, error) { + store.mu.Lock() + defer store.mu.Unlock() + user, ok := store.state.Users[id] + if !ok { + return User{}, ErrNotFound + } + previousAnswers := user.Answers + previousVersion := user.AssessmentSeen + user.Answers = make(map[string]int) + user.AssessmentSeen = "" + store.state.Users[id] = user + if err := store.persistLocked(); err != nil { + user.Answers = previousAnswers + user.AssessmentSeen = previousVersion + store.state.Users[id] = user + return User{}, err + } + return cloneUser(user.User), nil +} + func (store *Store) Delete(id string) error { store.mu.Lock() defer store.mu.Unlock() diff --git a/cmd/nextpath/internal/account/store_test.go b/cmd/nextpath/internal/account/store_test.go index 6feb4d4..49a9979 100644 --- a/cmd/nextpath/internal/account/store_test.go +++ b/cmd/nextpath/internal/account/store_test.go @@ -48,6 +48,13 @@ func TestRegistrationAuthenticationPersistenceAndDeletion(t *testing.T) { if stored.Answers["r01"] != 5 { t.Fatal("caller mutated persisted answers") } + reset, err := store.ResetAssessment(registered.ID) + if err != nil { + t.Fatal(err) + } + if reset.Email != registered.Email || len(reset.Answers) != 0 || reset.AssessmentSeen != "" { + t.Fatalf("reset changed identity or retained assessment: %+v", reset) + } reopened, err := Open(path, 10) if err != nil { @@ -56,6 +63,10 @@ func TestRegistrationAuthenticationPersistenceAndDeletion(t *testing.T) { if _, ok := reopened.Authenticate(registered.Email, "a sufficiently long password"); !ok { t.Fatal("persisted account did not authenticate") } + persistedReset, err := reopened.Get(registered.ID) + if err != nil || len(persistedReset.Answers) != 0 { + t.Fatalf("reset was not persisted: user=%+v error=%v", persistedReset, err) + } data, err := os.ReadFile(path) if err != nil { t.Fatal(err) diff --git a/cmd/nextpath/internal/web/server.go b/cmd/nextpath/internal/web/server.go index 68548f7..909666a 100644 --- a/cmd/nextpath/internal/web/server.go +++ b/cmd/nextpath/internal/web/server.go @@ -75,6 +75,7 @@ func (server *Server) routes() { server.mux.HandleFunc("POST /logout", server.logout) server.mux.HandleFunc("GET /assessment", server.assessmentForm) server.mux.HandleFunc("POST /assessment", server.answerAssessment) + server.mux.HandleFunc("POST /assessment/reset", server.resetAssessment) server.mux.HandleFunc("GET /results", server.results) server.mux.HandleFunc("GET /account/export", server.exportAccount) server.mux.HandleFunc("POST /account/delete", server.deleteAccount) @@ -203,6 +204,18 @@ func (server *Server) answerAssessment(writer http.ResponseWriter, request *http http.Redirect(writer, request, "/assessment", http.StatusSeeOther) } +func (server *Server) resetAssessment(writer http.ResponseWriter, request *http.Request) { + user, token, ok := server.requireUser(writer, request) + if !ok || !server.prepareForm(writer, request) || !server.validSessionCSRF(request, token) { + return + } + if _, err := server.accounts.ResetAssessment(user.ID); err != nil { + server.internalError(writer, "reset assessment", err) + return + } + http.Redirect(writer, request, "/assessment", http.StatusSeeOther) +} + func (server *Server) results(writer http.ResponseWriter, request *http.Request) { user, token, ok := server.requireUser(writer, request) if !ok { diff --git a/cmd/nextpath/internal/web/server_test.go b/cmd/nextpath/internal/web/server_test.go index 85702f9..4bcc33e 100644 --- a/cmd/nextpath/internal/web/server_test.go +++ b/cmd/nextpath/internal/web/server_test.go @@ -68,7 +68,7 @@ func TestCompletePrivateAssessmentFlow(t *testing.T) { 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 or identity") || !strings.Contains(body, "Careers to explore next") || !strings.Contains(body, "Employability today: 88%") || !strings.Contains(body, "Five-year outlook: 90%") || !strings.Contains(body, "2022: 100, 2023: 100, 2024: 100") { + if response.StatusCode != http.StatusOK || !strings.Contains(body, "interest profile") || !strings.Contains(body, "not ability or identity") || !strings.Contains(body, "Careers to explore next") || !strings.Contains(body, "Employability today: 88%") || !strings.Contains(body, "Five-year outlook: 90%") || !strings.Contains(body, "2022: 100, 2023: 100, 2024: 100") || !strings.Contains(body, "Retake the quiz") { t.Fatalf("unexpected results response %d: %s", response.StatusCode, body) } response = get(t, client, testServer.URL+"/account/export") @@ -76,6 +76,22 @@ func TestCompletePrivateAssessmentFlow(t *testing.T) { if !strings.Contains(export, `"interest_result"`) || strings.Contains(export, "password_hash") { t.Fatalf("unsafe or incomplete export: %s", export) } + + csrfMatch := csrfPattern.FindStringSubmatch(body) + response = postForm(t, client, testServer.URL+"/assessment/reset", url.Values{"csrf_token": {csrfMatch[1]}}) + if response.StatusCode != http.StatusSeeOther || response.Header.Get("Location") != "/assessment" { + t.Fatalf("reset = %d %q", response.StatusCode, response.Header.Get("Location")) + } + response.Body.Close() + response = get(t, client, testServer.URL+"/assessment") + resetBody := readBody(t, response) + if response.StatusCode != http.StatusOK || !strings.Contains(resetBody, "0 of") { + t.Fatalf("reset did not return to first question: %d %s", response.StatusCode, resetBody) + } + user, ok := accounts.Authenticate("teen@example.test", "a family test password") + if !ok || len(user.Answers) != 0 || user.AssessmentSeen != "" { + t.Fatalf("reset did not preserve account and clear answers: %+v", user) + } } func TestFormsRejectMissingCSRF(t *testing.T) { diff --git a/cmd/nextpath/internal/web/templates.go b/cmd/nextpath/internal/web/templates.go index 00c8701..de78ed4 100644 --- a/cmd/nextpath/internal/web/templates.go +++ b/cmd/nextpath/internal/web/templates.go @@ -24,4 +24,4 @@ const loginPage = `{{define "content"}}

Continue exploring.

What sounds interesting, {{.Name}}?

Rate the activity itself—not whether you are already good at it.

{{.Answered}} of {{.Total}} answered

{{.Question.Prompt}}

{{range $value := seq}}{{end}}
Not interestedUnsureVery interested
{{end}}` -const resultsPage = `{{define "content"}}

Your current interest profile

Hello {{.Name}}. Your three-letter exploration code is {{.Result.Code}}.

{{range .Result.Scores}}
{{.Dimension}}
{{.Percent}}%
{{end}}

Careers to explore next

Interest fit and Arizona market outlook are separate signals. These percentages are market indexes, not your probability of being hired.

{{range .Matches}}
O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%

{{.Occupation.Title}}

{{.Occupation.Summary}}

{{.Reason}}

{{if .Outlook}}

Arizona market evidence

Employability today: {{.Outlook.TodayEmployabilityPercent}}% · {{.Outlook.TodayLabel}}

Five-year outlook: {{.Outlook.FiveYearEmployabilityPercent}}% · {{.Outlook.FiveYearLabel}}

{{.Outlook.Evidence.ArizonaProjectedOpenings}} projected openings/year and {{.Outlook.Evidence.ActivePostings}} sampled current postings. Arizona exact-program completions: {{.Outlook.Evidence.ArizonaGraduateHistory}}; three-year average {{.Outlook.ThreeYearAverageGraduates}}. Demand-to-average-graduates ratio: {{.Outlook.AnnualDemandToGraduateRatio}}.

Official projected employment growth: {{.Outlook.Evidence.ProjectedEmploymentGrowthPercent}}%. Historical graduate trend: {{.Outlook.GraduateAnnualTrendPercent}}%/year; modeled five-year graduate supply: {{.Outlook.ProjectedFiveYearGraduates}}.

Today = 75% projected annual demand coverage + 25% sampled current posting coverage. Five-year = 75% projected demand coverage against trend-modeled supply + 25% official employment-growth signal. Supply trend is capped at ±10%/year; official growth from -5% to +20% maps to the 0–100 growth signal. {{.Outlook.Confidence}} evidence confidence. Snapshot {{.Outlook.Evidence.PostingsObservedAt.Format "Jan 2, 2006"}}.

{{else}}

Arizona employability outlook unavailable. Complete source evidence has not been refreshed for this occupation; no percentage is estimated.

{{end}}

Try it first: {{.Occupation.TryFirst}}

Review the O*NET occupation profile
{{end}}

How to use this

{{.Result.InstrumentNotice}}

Confidence: {{.Result.Confidence}}. This describes interests, not ability or identity. Market indexes are contextual evidence, not a hiring promise.

Delete account

{{end}}` +const resultsPage = `{{define "content"}}

Your current interest profile

Hello {{.Name}}. Your three-letter exploration code is {{.Result.Code}}.

{{range .Result.Scores}}
{{.Dimension}}
{{.Percent}}%
{{end}}

Careers to explore next

Interest fit and Arizona market outlook are separate signals. These percentages are market indexes, not your probability of being hired.

{{range .Matches}}
O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%

{{.Occupation.Title}}

{{.Occupation.Summary}}

{{.Reason}}

{{if .Outlook}}

Arizona market evidence

Employability today: {{.Outlook.TodayEmployabilityPercent}}% · {{.Outlook.TodayLabel}}

Five-year outlook: {{.Outlook.FiveYearEmployabilityPercent}}% · {{.Outlook.FiveYearLabel}}

{{.Outlook.Evidence.ArizonaProjectedOpenings}} projected openings/year and {{.Outlook.Evidence.ActivePostings}} sampled current postings. Arizona exact-program completions: {{.Outlook.Evidence.ArizonaGraduateHistory}}; three-year average {{.Outlook.ThreeYearAverageGraduates}}. Demand-to-average-graduates ratio: {{.Outlook.AnnualDemandToGraduateRatio}}.

Official projected employment growth: {{.Outlook.Evidence.ProjectedEmploymentGrowthPercent}}%. Historical graduate trend: {{.Outlook.GraduateAnnualTrendPercent}}%/year; modeled five-year graduate supply: {{.Outlook.ProjectedFiveYearGraduates}}.

Today = 75% projected annual demand coverage + 25% sampled current posting coverage. Five-year = 75% projected demand coverage against trend-modeled supply + 25% official employment-growth signal. Supply trend is capped at ±10%/year; official growth from -5% to +20% maps to the 0–100 growth signal. {{.Outlook.Confidence}} evidence confidence. Snapshot {{.Outlook.Evidence.PostingsObservedAt.Format "Jan 2, 2006"}}.

{{else}}

Arizona employability outlook unavailable. Complete source evidence has not been refreshed for this occupation; no percentage is estimated.

{{end}}

Try it first: {{.Occupation.TryFirst}}

Review the O*NET occupation profile
{{end}}

How to use this

{{.Result.InstrumentNotice}}

Confidence: {{.Result.Confidence}}. This describes interests, not ability or identity. Market indexes are contextual evidence, not a hiring promise.

Export my data

Delete account

{{end}}`