hako/internal/testutil/users.go
Felipe M. 484b225af7
feat: add user administration API and UI
Add admin-only CRUD endpoints for user management under /api/v1/system/users
with full frontend implementation including create, edit, change password,
and delete operations with self-protection guards.

- Extend AuthDomain interface with ListUsers, GetUser, UpdateUser,
  UpdateUserPassword, DeleteUser methods
- Add List, Update, Delete methods to UserStore with shared scanUser helper
- Create UserHandler with 5 endpoints protected by admin middleware
- Complete Users.vue admin page with modals and error handling
- Make CreateUser accept role parameter for atomic user creation
- Fix auth middleware to use database role as source of truth instead of
  stale JWT claims, preventing privilege persistence after demotion
- Fix pre-existing gofmt issues in yt_dlp.go and config.go
2026-04-02 10:33:36 +02:00

50 lines
1.3 KiB
Go

package testutil
import (
"testing"
"git.nakama.town/fmartingr/hako/internal/model"
)
// NewTestUser creates a test user with the specified email and password
// and returns the user and a JWT token for authentication
func NewTestUser(t *testing.T, deps model.Dependencies, email, password string) (*model.User, string, error) {
t.Helper()
user, err := deps.Domains().Auth().CreateUser(email, password, "user")
if err != nil {
return nil, "", err
}
_, token, err := deps.Domains().Auth().Login(email, password)
if err != nil {
return nil, "", err
}
tokenString := token.Token
return user, tokenString, nil
}
// NewDefaultTestUser creates a default test user with email "test@example.com" and password "test"
func NewDefaultTestUser(t *testing.T, deps model.Dependencies) (*model.User, string, error) {
t.Helper()
return NewTestUser(t, deps, "test@example.com", "test")
}
// NewTestAdminUser creates a test user with admin role
func NewTestAdminUser(t *testing.T, deps model.Dependencies, email, password string) (*model.User, string, error) {
t.Helper()
user, err := deps.Domains().Auth().CreateUser(email, password, "admin")
if err != nil {
return nil, "", err
}
_, token, err := deps.Domains().Auth().Login(email, password)
if err != nil {
return nil, "", err
}
tokenString := token.Token
return user, tokenString, nil
}