Revalidate admin roles from the database and serialize report generation registration with owner-scoped deletion. Refs #9 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package api_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/api"
|
|
"github.com/tradarr/backend/internal/api/handlers"
|
|
"github.com/tradarr/backend/internal/auth"
|
|
"github.com/tradarr/backend/internal/config"
|
|
"github.com/tradarr/backend/internal/models"
|
|
)
|
|
|
|
// These route contract tests deliberately use no database. They establish the
|
|
// public/authentication boundary; database-backed account lifecycle cases are
|
|
// covered separately when a disposable PostgreSQL test database is available.
|
|
func accountRouter() *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
return api.SetupRouter(handlers.New(currentRoleRepository(), &config.Config{JWTSecret: "test-secret"}, nil, nil, nil, nil), "test-secret")
|
|
}
|
|
|
|
func TestPublicRegistrationIsUnavailable(t *testing.T) {
|
|
request := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewBufferString(`{"email":"new@example.test","password":"secret123"}`))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
recorder := httptest.NewRecorder()
|
|
accountRouter().ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusNotFound && recorder.Code != http.StatusGone && recorder.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("public registration must be unavailable; status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAccountCreationIsAdminOnly(t *testing.T) {
|
|
token, err := auth.GenerateToken("viewer-id", "viewer@example.test", "user", "test-secret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest(http.MethodPost, "/api/admin/users", bytes.NewBufferString(`{"email":"new@example.test","password":"secret123","role":"user"}`))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
recorder := httptest.NewRecorder()
|
|
accountRouter().ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("viewer must be forbidden from creating accounts; status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestUserJSONNeverContainsPasswordSecrets(t *testing.T) {
|
|
user := models.User{ID: "u1", Email: "admin@example.test", Role: models.RoleAdmin, PasswordHash: "$2a$secret"}
|
|
payload, err := json.Marshal(user)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, secret := range []string{"password_hash", "$2a$secret", "password"} {
|
|
if bytes.Contains(payload, []byte(secret)) {
|
|
t.Fatalf("user payload contains secret %q: %s", secret, payload)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAccountCreationRejectsUnauthenticatedAndInvalidInput(t *testing.T) {
|
|
adminToken, err := auth.GenerateToken("admin-id", "admin@example.test", "admin", "test-secret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
token, body string
|
|
want int
|
|
}{
|
|
{"", `{"email":"new@example.test","password":"secret123","role":"user"}`, 401},
|
|
{adminToken, `{"email":"bad","password":"secret123","role":"user"}`, 400},
|
|
{adminToken, `{"email":"new@example.test","password":"short","role":"user"}`, 400},
|
|
{adminToken, `{"email":"new@example.test","password":"secret123","role":"root"}`, 400},
|
|
{adminToken, `{"email":"new@example.test","password":"secret123"}`, 400},
|
|
} {
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/users", bytes.NewBufferString(tc.body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if tc.token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+tc.token)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
accountRouter().ServeHTTP(w, req)
|
|
if w.Code != tc.want {
|
|
t.Fatalf("got %d want %d: %s", w.Code, tc.want, w.Body.String())
|
|
}
|
|
if bytes.Contains(w.Body.Bytes(), []byte("secret123")) {
|
|
t.Fatal("password leaked")
|
|
}
|
|
}
|
|
}
|