Add the website: Go server + Vue SPA in site/, with CI
A standalone container for web_server, shaped like job-tracker: serves /usaco/ with solved problems public and solving/skipped plus edits (status, link, notes) for the site admin. Git is the only store: the server clones adamf/usaco from Gitea, pulls on the push webhook, and commits edits back. CI builds the image only when site/ changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
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, "/usaco/api/problems", 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user