chore: establish local platform baseline
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"data-terminal-cloud/internal/identity"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestCurrentUserHandlerAuthenticationAccountAndRepositoryFailures(t *testing.T) {
|
||||
databaseURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if databaseURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL is not set")
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), databaseURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
handler := CurrentUserHandler(identity.NewRepository(pool))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/me", nil))
|
||||
if recorder.Code != http.StatusUnauthorized || !strings.Contains(recorder.Body.String(), "AUTHENTICATION_REQUIRED") {
|
||||
t.Fatalf("missing principal status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
|
||||
owner := identity.Principal{Issuer: "http://localhost:58180/realms/voice-local", Subject: "00000000-0000-0000-0000-000000000101"}
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
request = request.WithContext(identity.WithPrincipal(request.Context(), owner))
|
||||
recorder = httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), owner.Subject) || recorder.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatalf("owner status=%d headers=%v body=%s", recorder.Code, recorder.Header(), recorder.Body.String())
|
||||
}
|
||||
|
||||
unknown := identity.Principal{Issuer: owner.Issuer, Subject: "00000000-0000-0000-0000-000000009999"}
|
||||
request = httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
request = request.WithContext(identity.WithPrincipal(request.Context(), unknown))
|
||||
recorder = httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusForbidden || !strings.Contains(recorder.Body.String(), "RESOURCE_ACCESS_DENIED") {
|
||||
t.Fatalf("unknown account status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
|
||||
closedPool, err := pgxpool.New(context.Background(), databaseURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
closedPool.Close()
|
||||
handler = CurrentUserHandler(identity.NewRepository(closedPool))
|
||||
request = httptest.NewRequest(http.MethodGet, "/api/v1/me", nil)
|
||||
request = request.WithContext(identity.WithPrincipal(request.Context(), owner))
|
||||
recorder = httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusInternalServerError || !strings.Contains(recorder.Body.String(), "INTERNAL_ERROR") {
|
||||
t.Fatalf("closed repository status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user