Allow users to retake NextPath quiz
Homelab Main / validate (push) Failing after 1m30s Details
Homelab Main / running-update (push) Has been skipped Details
Homelab Main / debian-ops (push) Has been skipped Details

This commit is contained in:
juvenal.diaz 2026-12-05 17:10:14 -06:00
parent a29f0c385f
commit b90c6fa0a8
5 changed files with 65 additions and 2 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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 {

View File

@ -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) {

View File

@ -24,4 +24,4 @@ const loginPage = `{{define "content"}}<h1>Continue exploring.</h1><section clas
const assessmentPage = `{{define "content"}}<h1>What sounds interesting, {{.Name}}?</h1><p class="muted">Rate the activity itself—not whether you are already good at it.</p><div class="progress"><span style="width:{{progress .Answered .Total}}%"></span></div><p class="small muted">{{.Answered}} of {{.Total}} answered</p><section class="panel"><h2>{{.Question.Prompt}}</h2><form method="post" action="/assessment"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><input type="hidden" name="question_id" value="{{.Question.ID}}"><div class="answers">{{range $value := seq}}<label><input type="radio" name="answer" value="{{$value}}" required>{{$value}}</label>{{end}}</div><div class="answers small muted"><span>Not interested</span><span></span><span>Unsure</span><span></span><span>Very interested</span></div><button type="submit">Save and continue</button></form></section>{{end}}`
const resultsPage = `{{define "content"}}<h1>Your current interest profile</h1><p>Hello {{.Name}}. Your three-letter exploration code is <code>{{.Result.Code}}</code>.</p><section class="panel">{{range .Result.Scores}}<div class="score"><strong>{{.Dimension}}</strong><div class="bar"><span style="width:{{.Percent}}%"></span></div><span>{{.Percent}}%</span></div>{{end}}</section><section class="panel"><h2>Careers to explore next</h2><p class="muted">Interest fit and Arizona market outlook are separate signals. These percentages are market indexes, not your probability of being hired.</p>{{range .Matches}}<article class="panel"><div class="small muted">O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%</div><h3>{{.Occupation.Title}}</h3><p>{{.Occupation.Summary}}</p><p>{{.Reason}}</p>{{if .Outlook}}<div class="outlook"><h4>Arizona market evidence</h4><p><strong>Employability today: {{.Outlook.TodayEmployabilityPercent}}%</strong> · {{.Outlook.TodayLabel}}</p><p><strong>Five-year outlook: {{.Outlook.FiveYearEmployabilityPercent}}%</strong> · {{.Outlook.FiveYearLabel}}</p><p class="small">{{.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}}.</p><p class="small">Official projected employment growth: {{.Outlook.Evidence.ProjectedEmploymentGrowthPercent}}%. Historical graduate trend: {{.Outlook.GraduateAnnualTrendPercent}}%/year; modeled five-year graduate supply: {{.Outlook.ProjectedFiveYearGraduates}}.</p><p class="small muted">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"}}.</p><div class="small"><a href="{{.Outlook.Evidence.GraduateSourceURL}}" rel="noreferrer">Completion source</a> · <a href="{{.Outlook.Evidence.ProjectionSourceURL}}" rel="noreferrer">Projection source</a> · <a href="{{.Outlook.Evidence.PostingSourceURL}}" rel="noreferrer">Posting source</a></div></div>{{else}}<p class="muted"><strong>Arizona employability outlook unavailable.</strong> Complete source evidence has not been refreshed for this occupation; no percentage is estimated.</p>{{end}}<p><strong>Try it first:</strong> {{.Occupation.TryFirst}}</p><a href="{{.Occupation.SourceURL}}" rel="noreferrer">Review the O*NET occupation profile</a></article>{{end}}</section><section class="panel"><h2>How to use this</h2><p>{{.Result.InstrumentNotice}}</p><p class="muted">Confidence: {{.Result.Confidence}}. This describes interests, not ability or identity. Market indexes are contextual evidence, not a hiring promise.</p></section><div class="actions"><a class="button" href="/account/export">Export my data</a><form method="post" action="/logout"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><button class="secondary">Sign out</button></form></div><section class="panel"><h2>Delete account</h2><form method="post" action="/account/delete"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><label for="confirm">Type DELETE</label><input id="confirm" name="confirm" required><button class="secondary" type="submit">Delete profile and answers</button></form></section>{{end}}`
const resultsPage = `{{define "content"}}<h1>Your current interest profile</h1><p>Hello {{.Name}}. Your three-letter exploration code is <code>{{.Result.Code}}</code>.</p><section class="panel">{{range .Result.Scores}}<div class="score"><strong>{{.Dimension}}</strong><div class="bar"><span style="width:{{.Percent}}%"></span></div><span>{{.Percent}}%</span></div>{{end}}</section><section class="panel"><h2>Careers to explore next</h2><p class="muted">Interest fit and Arizona market outlook are separate signals. These percentages are market indexes, not your probability of being hired.</p>{{range .Matches}}<article class="panel"><div class="small muted">O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%</div><h3>{{.Occupation.Title}}</h3><p>{{.Occupation.Summary}}</p><p>{{.Reason}}</p>{{if .Outlook}}<div class="outlook"><h4>Arizona market evidence</h4><p><strong>Employability today: {{.Outlook.TodayEmployabilityPercent}}%</strong> · {{.Outlook.TodayLabel}}</p><p><strong>Five-year outlook: {{.Outlook.FiveYearEmployabilityPercent}}%</strong> · {{.Outlook.FiveYearLabel}}</p><p class="small">{{.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}}.</p><p class="small">Official projected employment growth: {{.Outlook.Evidence.ProjectedEmploymentGrowthPercent}}%. Historical graduate trend: {{.Outlook.GraduateAnnualTrendPercent}}%/year; modeled five-year graduate supply: {{.Outlook.ProjectedFiveYearGraduates}}.</p><p class="small muted">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"}}.</p><div class="small"><a href="{{.Outlook.Evidence.GraduateSourceURL}}" rel="noreferrer">Completion source</a> · <a href="{{.Outlook.Evidence.ProjectionSourceURL}}" rel="noreferrer">Projection source</a> · <a href="{{.Outlook.Evidence.PostingSourceURL}}" rel="noreferrer">Posting source</a></div></div>{{else}}<p class="muted"><strong>Arizona employability outlook unavailable.</strong> Complete source evidence has not been refreshed for this occupation; no percentage is estimated.</p>{{end}}<p><strong>Try it first:</strong> {{.Occupation.TryFirst}}</p><a href="{{.Occupation.SourceURL}}" rel="noreferrer">Review the O*NET occupation profile</a></article>{{end}}</section><section class="panel"><h2>How to use this</h2><p>{{.Result.InstrumentNotice}}</p><p class="muted">Confidence: {{.Result.Confidence}}. This describes interests, not ability or identity. Market indexes are contextual evidence, not a hiring promise.</p></section><div class="actions"><a class="button" href="/account/export">Export my data</a><form method="post" action="/assessment/reset"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><button class="secondary" type="submit">Retake the quiz</button></form><form method="post" action="/logout"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><button class="secondary">Sign out</button></form></div><section class="panel"><h2>Delete account</h2><form method="post" action="/account/delete"><input type="hidden" name="csrf_token" value="{{.CSRF}}"><label for="confirm">Type DELETE</label><input id="confirm" name="confirm" required><button class="secondary" type="submit">Delete profile and answers</button></form></section>{{end}}`