Files
job-tracker/internal/auth/auth_test.go
T
adamfandClaude Opus 5.5 67cb8137f9
Test and publish / test (push) Successful in 6m39s
Test and publish / publish (push) Failing after 0s
Split the job tracker out of web_server into its own app
The applications tracker, leads inbox, email pipeline and capture userscript
move here from web_server's backend, Python service and Vue app, as one Go
binary that serves its own Vue SPA under /jobs/.

- The Go services, resolvers and tests move over almost unchanged.
- The three Claude calls are ported from Python to Go (internal/claude),
  with the same prompts, per-model request shaping and error messages.
- There is no login: internal/auth asks the site backend's
  /auth/validate-admin with the visitor's cookies, and relays refreshed
  cookies back.
- A Settings page replaces the admin-panel capture token card and adds the
  Microsoft sign-in link and a manual email sync.
- The userscript is 2.0.0, at /jobs/, and migrates a saved API base.
- Dockerfile, a standalone compose with Postgres, Makefile, and a Gitea CI
  workflow that tests and publishes the image.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 16:24:16 +01:00

101 lines
2.6 KiB
Go

package auth
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
// site fakes the backend's validate-admin: 200 for the "admin" session,
// 403 for any other cookie, 401 for none, and a rotated cookie on "stale".
func site(t *testing.T, calls *atomic.Int32) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
c, err := r.Cookie("access_token")
switch {
case err != nil:
w.WriteHeader(http.StatusUnauthorized)
case c.Value == "admin":
w.WriteHeader(http.StatusOK)
case c.Value == "stale":
http.SetCookie(w, &http.Cookie{Name: "access_token", Value: "fresh"})
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusForbidden)
}
}))
t.Cleanup(srv.Close)
return srv
}
func ask(f *Forward, cookie string) (bool, *httptest.ResponseRecorder) {
r := httptest.NewRequest(http.MethodPost, "/jobs/api/graphql", nil)
if cookie != "" {
r.Header.Set("Cookie", cookie)
}
w := httptest.NewRecorder()
return f.IsAdmin(w, r), w
}
func TestForwardVerdicts(t *testing.T) {
var calls atomic.Int32
f := NewForward(site(t, &calls).URL)
cases := []struct {
name, cookie string
want bool
}{
{"admin session", "access_token=admin", true},
{"signed in, not admin", "access_token=someone", false},
{"cookies but no session", "theme=dark", false},
{"no cookies at all", "", false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got, _ := ask(f, c.cookie); got != c.want {
t.Fatalf("IsAdmin = %v, want %v", got, c.want)
}
})
}
}
func TestForwardCachesOnlyYes(t *testing.T) {
var calls atomic.Int32
f := NewForward(site(t, &calls).URL)
ask(f, "access_token=admin")
ask(f, "access_token=admin")
if n := calls.Load(); n != 1 {
t.Fatalf("admin checked %d times, want 1 (second answer from cache)", n)
}
calls.Store(0)
ask(f, "access_token=someone")
ask(f, "access_token=someone")
if n := calls.Load(); n != 2 {
t.Fatalf("refusal checked %d times, want 2 (refusals are not cached)", n)
}
}
func TestForwardRelaysRefreshedCookies(t *testing.T) {
var calls atomic.Int32
f := NewForward(site(t, &calls).URL)
ok, w := ask(f, "access_token=stale")
if !ok {
t.Fatal("refreshed session refused")
}
if got := w.Header().Get("Set-Cookie"); got != "access_token=fresh" {
t.Fatalf("Set-Cookie = %q, want the backend's rotated cookie", got)
}
}
func TestForwardBackendDownRefuses(t *testing.T) {
f := NewForward("http://127.0.0.1:1/auth/validate-admin")
if ok, _ := ask(f, "access_token=admin"); ok {
t.Fatal("admitted while the site backend was unreachable")
}
}