feat: add profile sync
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package profile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"proxui/backend/internal/auth"
|
||||
)
|
||||
|
||||
func TestEnsureProfileCreatesMissingProfile(t *testing.T) {
|
||||
ensurer := &stubEnsurer{}
|
||||
middleware := NewMiddleware(ensurer, slog.Default())
|
||||
handler := middleware.EnsureProfile(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/me", nil)
|
||||
req = req.WithContext(auth.ContextWithPrincipal(req.Context(), auth.Principal{
|
||||
Subject: "00000000-0000-0000-0000-000000000001",
|
||||
Email: "user@example.test",
|
||||
Role: "authenticated",
|
||||
}))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||
}
|
||||
if ensurer.calls != 1 {
|
||||
t.Fatalf("Ensure calls = %d, want 1", ensurer.calls)
|
||||
}
|
||||
if ensurer.id != "00000000-0000-0000-0000-000000000001" {
|
||||
t.Fatalf("id = %q", ensurer.id)
|
||||
}
|
||||
if ensurer.email != "user@example.test" {
|
||||
t.Fatalf("email = %q", ensurer.email)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureProfileRejectsMissingPrincipal(t *testing.T) {
|
||||
ensurer := &stubEnsurer{}
|
||||
middleware := NewMiddleware(ensurer, slog.Default())
|
||||
handler := middleware.EnsureProfile(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/me", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
|
||||
}
|
||||
if ensurer.calls != 0 {
|
||||
t.Fatalf("Ensure calls = %d, want 0", ensurer.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureProfileReturnsServerErrorOnSyncFailure(t *testing.T) {
|
||||
ensurer := &stubEnsurer{err: errors.New("db failed")}
|
||||
middleware := NewMiddleware(ensurer, slog.Default())
|
||||
handler := middleware.EnsureProfile(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/me", nil)
|
||||
req = req.WithContext(auth.ContextWithPrincipal(req.Context(), auth.Principal{
|
||||
Subject: "00000000-0000-0000-0000-000000000001",
|
||||
Email: "user@example.test",
|
||||
Role: "authenticated",
|
||||
}))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
type stubEnsurer struct {
|
||||
calls int
|
||||
id string
|
||||
email string
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubEnsurer) Ensure(_ context.Context, id string, email string) error {
|
||||
s.calls++
|
||||
s.id = id
|
||||
s.email = email
|
||||
return s.err
|
||||
}
|
||||
Reference in New Issue
Block a user