Remove public registration, support administrator-managed accounts, and persist personal preferences. Refs #2 #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
104 lines
4.0 KiB
Go
104 lines
4.0 KiB
Go
package api_test
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/lib/pq"
|
|
"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"
|
|
)
|
|
|
|
// TEST_DATABASE_URL must identify a disposable PostgreSQL database. All tables
|
|
// live in a unique schema; no existing application tables are used or removed.
|
|
func TestAccountLifecyclePostgres(t *testing.T) {
|
|
dsn := postgresTestURL(t)
|
|
db, err := sql.Open("postgres", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
db.SetMaxOpenConns(1)
|
|
schema := fmt.Sprintf("account_test_%d", time.Now().UnixNano())
|
|
if _, err = db.Exec(`CREATE SCHEMA ` + pq.QuoteIdentifier(schema)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`)
|
|
if _, err = db.Exec(`SET search_path TO ` + pq.QuoteIdentifier(schema)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
migration, err := os.ReadFile("../database/migrations/000001_init.up.sql")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Reuse the production users definition without installing extensions or
|
|
// unrelated ingestion tables. gen_random_uuid is built into PostgreSQL 13+.
|
|
users := strings.Split(strings.Split(string(migration), "CREATE TABLE users (")[1], "CREATE TABLE user_assets")[0]
|
|
if _, err = db.Exec("CREATE TABLE users (" + users); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := models.NewRepository(db)
|
|
hash, _ := auth.HashPassword("secret123")
|
|
admin, err := repo.CreateUser("admin@example.test", hash, models.RoleAdmin)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token, _ := auth.GenerateToken(admin.ID, admin.Email, "admin", "test-secret")
|
|
router := api.SetupRouter(handlers.New(repo, &config.Config{JWTSecret: "test-secret"}, nil, nil, nil, nil), "test-secret")
|
|
request := func(method, path, body string, want int) []byte {
|
|
t.Helper()
|
|
r := httptest.NewRequest(method, path, bytes.NewBufferString(body))
|
|
r.Header.Set("Content-Type", "application/json")
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, r)
|
|
if w.Code != want {
|
|
t.Fatalf("%s %s: got %d want %d: %s", method, path, w.Code, want, w.Body.String())
|
|
}
|
|
if bytes.Contains(w.Body.Bytes(), []byte(hash)) || bytes.Contains(w.Body.Bytes(), []byte("password_hash")) || bytes.Contains(w.Body.Bytes(), []byte("secret123")) {
|
|
t.Fatal("secret leaked")
|
|
}
|
|
return w.Body.Bytes()
|
|
}
|
|
request("POST", "/api/admin/users", `{"email":"new@example.test","password":"secret123","role":"user"}`, 201)
|
|
request("POST", "/api/admin/users", `{"email":"new@example.test","password":"secret123","role":"user"}`, 409)
|
|
request("POST", "/api/admin/users", `{"email":"bad@example.test","password":"secret123","role":"root"}`, 400)
|
|
request("PUT", "/api/admin/users/"+admin.ID, `{"email":"admin@example.test","role":"user"}`, 400)
|
|
request("DELETE", "/api/admin/users/"+admin.ID, "", 400)
|
|
payload := request("POST", "/api/auth/login", `{"email":"new@example.test","password":"secret123"}`, 200)
|
|
var result struct {
|
|
Data struct {
|
|
Token string `json:"token"`
|
|
}
|
|
}
|
|
if err = json.Unmarshal(payload, &result); err != nil || result.Data.Token == "" {
|
|
t.Fatal("login missing token", err)
|
|
}
|
|
token = result.Data.Token
|
|
request("POST", "/api/admin/users", `{"email":"blocked@example.test","password":"secret123","role":"user"}`, 403)
|
|
token, _ = auth.GenerateToken(admin.ID, admin.Email, "admin", "test-secret")
|
|
request("POST", "/api/admin/users", `{"email":"second@example.test","password":"secret123","role":"admin"}`, 201)
|
|
request("PUT", "/api/admin/users/"+admin.ID, `{"email":"admin@example.test","role":"user"}`, 200)
|
|
count, err := repo.CountAdmins()
|
|
if err != nil || count != 1 {
|
|
t.Fatal("wrong admin count", count, err)
|
|
}
|
|
second, err := repo.GetUserByEmail("second@example.test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = repo.DeleteUser(second.ID); err != models.ErrLastAdmin {
|
|
t.Fatal("last admin deletion", err)
|
|
}
|
|
}
|