fix(security): synchronize report lifecycle and current roles
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>
This commit is contained in:
93
backend/internal/api/auth_role_ticket9_test.go
Normal file
93
backend/internal/api/auth_role_ticket9_test.go
Normal file
@ -0,0 +1,93 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestAdminJWTMustMatchCurrentDatabaseAccountPostgres(t *testing.T) {
|
||||
previousMode := gin.Mode()
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Cleanup(func() { gin.SetMode(previousMode) })
|
||||
|
||||
const adminID = "00000000-0000-0000-0000-000000000901"
|
||||
const tokenSecret = "ticket-9-role-test-secret"
|
||||
tests := []struct {
|
||||
name string
|
||||
prepare func(t *testing.T, dbExec func(string) error)
|
||||
wantStatus int
|
||||
}{
|
||||
{
|
||||
name: "current admin remains allowed",
|
||||
prepare: func(*testing.T, func(string) error) {},
|
||||
wantStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "downgraded admin token is forbidden",
|
||||
prepare: func(t *testing.T, exec func(string) error) {
|
||||
if err := exec(`UPDATE users SET role='user' WHERE id='` + adminID + `'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
wantStatus: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "deleted admin token is unauthorized",
|
||||
prepare: func(t *testing.T, exec func(string) error) {
|
||||
if err := exec(`DELETE FROM users WHERE id='` + adminID + `'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
wantStatus: http.StatusUnauthorized,
|
||||
},
|
||||
{
|
||||
name: "role lookup failure returns server error and denies access",
|
||||
prepare: func(t *testing.T, exec func(string) error) {
|
||||
if err := exec(`DROP TABLE users`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
wantStatus: http.StatusInternalServerError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
db, _ := ticket7DB(t)
|
||||
for _, statement := range []string{
|
||||
`CREATE TABLE users (id UUID PRIMARY KEY, email TEXT NOT NULL, password_hash TEXT NOT NULL, role TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`,
|
||||
`INSERT INTO users (id,email,password_hash,role) VALUES ('` + adminID + `','admin@example.test','hash','admin')`,
|
||||
} {
|
||||
if _, err := db.Exec(statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
tt.prepare(t, func(statement string) error {
|
||||
_, err := db.Exec(statement)
|
||||
return err
|
||||
})
|
||||
|
||||
token, err := auth.GenerateToken(adminID, "admin@example.test", "admin", tokenSecret)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := models.NewRepository(db)
|
||||
router := api.SetupRouter(handlers.New(repo, &config.Config{JWTSecret: tokenSecret}, nil, nil, nil, nil), tokenSecret)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/admin/users", nil)
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != tt.wantStatus {
|
||||
t.Errorf("admin route after database account change: got %d, want %d; body=%s", response.Code, tt.wantStatus, response.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user