Files
mtusi2026lab2-implementation/internal/handler/auth_test.go

103 lines
3.8 KiB
Go

package handler_test
import (
"net/http"
"strings"
"testing"
"time"
)
const (
deadDatabaseURL = "postgres://messenger:x@127.0.0.1:1/messenger?sslmode=disable"
deadRedisURL = "redis://127.0.0.1:1/0"
)
func TestSessionExpiry(t *testing.T) {
dbURL, redisURL := testURLs(t)
srv := newStack(t, dbURL, redisURL, 2*time.Second)
alice := createUser(t, srv, "alice")
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusOK {
t.Fatalf("fresh session: got %d, want 200", status)
}
time.Sleep(3 * time.Second)
resp := request(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, "")
resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("expired session: got %d, want 401", resp.StatusCode)
}
if got := resp.Header.Get("WWW-Authenticate"); !strings.HasPrefix(got, "Bearer") {
t.Errorf("WWW-Authenticate = %q, want Bearer challenge", got)
}
}
func TestLogout(t *testing.T) {
srv := newServer(t)
alice := createUser(t, srv, "alice")
resp := request(t, srv, http.MethodDelete, "/api/v1/auth/sessions/current", alice.Token, "")
resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatalf("logout: got %d, want 204", resp.StatusCode)
}
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusUnauthorized {
t.Errorf("revoked token on /users/me: got %d, want 401", status)
}
if status, _ := do(t, srv, http.MethodDelete, "/api/v1/auth/sessions/current", alice.Token, ""); status != http.StatusUnauthorized {
t.Errorf("repeated logout: got %d, want 401", status)
}
}
func TestWrongPassword(t *testing.T) {
srv := newServer(t)
alice := createUser(t, srv, "alice")
for name, password := range map[string]string{"wrong password": "Wrong-password-123", "unknown user": alice.Password} {
username := alice.Username
if name == "unknown user" {
username = "nobody_" + alice.Username[:10]
}
resp := login(t, srv, username, password)
resp.Body.Close()
if resp.StatusCode != http.StatusUnauthorized {
t.Errorf("%s: got %d, want 401", name, resp.StatusCode)
}
if got := resp.Header.Get("WWW-Authenticate"); got != `Basic realm="messenger", charset="UTF-8"` {
t.Errorf("%s: WWW-Authenticate = %q", name, got)
}
}
}
func TestRedisUnavailable(t *testing.T) {
dbURL, _ := testURLs(t)
srv := newStack(t, dbURL, deadRedisURL, time.Hour)
if status, body := do(t, srv, http.MethodGet, "/api/v1/users/me", "some-token-from-before-the-outage", ""); status != http.StatusServiceUnavailable || errorCode(body) != "unavailable" {
t.Errorf("bearer request: got %d %q, want 503 unavailable", status, errorCode(body))
}
if status, _ := do(t, srv, http.MethodGet, "/api/v1/users/me", "", ""); status != http.StatusUnauthorized {
t.Errorf("anonymous request: got %d, want 401", status)
}
if status, _ := do(t, srv, http.MethodGet, "/health/ready", "", ""); status != http.StatusServiceUnavailable {
t.Errorf("ready: got %d, want 503", status)
}
}
func TestDatabaseUnavailable(t *testing.T) {
dbURL, redisURL := testURLs(t)
healthy := newStack(t, dbURL, redisURL, time.Hour)
alice := createUser(t, healthy, "alice")
srv := newStack(t, deadDatabaseURL, redisURL, time.Hour)
if status, body := do(t, srv, http.MethodGet, "/api/v1/users/me", alice.Token, ""); status != http.StatusServiceUnavailable || errorCode(body) != "unavailable" {
t.Errorf("valid session: got %d %q, want 503 unavailable", status, errorCode(body))
}
if status, _ := do(t, srv, http.MethodPost, "/api/v1/users", "",
`{"username":"db_down_user","display_name":"X","password":"long-enough-pass"}`); status != http.StatusServiceUnavailable {
t.Errorf("register: got %d, want 503", status)
}
if status, _ := do(t, srv, http.MethodGet, "/health/ready", "", ""); status != http.StatusServiceUnavailable {
t.Errorf("ready: got %d, want 503", status)
}
}