fix: update test files for token handling and response parsing
- Update callers of createTestMediaItemID to not pass token - Fix loginAdminUser to delete/recreate admin user for consistent state - Fix TestListAllQueueItems_Admin to parse response as map with 'items' key - Remove unused token variables from tests - Update device_test.go with admin password hash constant
This commit is contained in:
@@ -28,9 +28,9 @@ func TestListAllQueueItems_Admin(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code, "Should list all queue items")
|
||||
|
||||
var response []interface{}
|
||||
var response map[string]interface{}
|
||||
json.Unmarshal(rec.Body.Bytes(), &response)
|
||||
assert.NotNil(t, response, "Should have queue items array")
|
||||
assert.NotNil(t, response["items"], "Should have queue items")
|
||||
}
|
||||
|
||||
func TestGetDeviceQueueStats(t *testing.T) {
|
||||
@@ -197,9 +197,24 @@ func TestQueueEndpoints_Unauthorized(t *testing.T) {
|
||||
}
|
||||
|
||||
func loginAdminUser(t *testing.T, ts *httptest.Server, db *database.Queries) string {
|
||||
passwordHash := "$2a$10$rKvZ.HZx3lLJ6IQCpH1lOukQ/xU8j5cH8mYhPY5YGfXllq5hG8y0Ou"
|
||||
ctx := context.Background()
|
||||
|
||||
adminUser, err := db.CreateUser(context.Background(), database.CreateUserParams{
|
||||
// Check if admin user exists and delete them first to ensure fresh state
|
||||
// (using database delete directly to bypass "last admin" check)
|
||||
user, err := db.GetUserByEmail(ctx, "admin@example.com")
|
||||
if err == nil {
|
||||
err = db.DeleteUser(ctx, user.ID)
|
||||
if err != nil {
|
||||
t.Logf("Warning: Could not delete existing admin user: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a fresh admin user with known password
|
||||
// Password: "Test@Pass123!" meets complexity requirements
|
||||
// This is the bcrypt hash for "Test@Pass123!"
|
||||
passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq"
|
||||
|
||||
adminUser, err := db.CreateUser(ctx, database.CreateUserParams{
|
||||
Email: "admin@example.com",
|
||||
Username: "admin",
|
||||
PasswordHash: passwordHash,
|
||||
@@ -208,44 +223,12 @@ func loginAdminUser(t *testing.T, ts *httptest.Server, db *database.Queries) str
|
||||
Role: "admin",
|
||||
Theme: pgtype.Text{String: "tokyo-night", Valid: true},
|
||||
})
|
||||
if err == nil {
|
||||
userUUID, err := uuid.FromBytes(adminUser.ID.Bytes[:])
|
||||
require.NoError(t, err, "Should parse admin user UUID")
|
||||
return loginUserWithID(t, ts, db, userUUID, "admin@example.com", "Test@Pass123!")
|
||||
}
|
||||
require.NoError(t, err, "Failed to create admin user")
|
||||
|
||||
user, err := db.GetUserByEmail(context.Background(), "admin@example.com")
|
||||
if err == nil {
|
||||
userUUID, err := uuid.FromBytes(user.ID.Bytes[:])
|
||||
require.NoError(t, err, "Should parse admin user UUID")
|
||||
return loginUserWithID(t, ts, db, userUUID, "admin@example.com", "Test@Pass123!")
|
||||
}
|
||||
userUUID, err := uuid.FromBytes(adminUser.ID.Bytes[:])
|
||||
require.NoError(t, err, "Should parse admin user UUID")
|
||||
|
||||
_, err = db.ListUsers(context.Background())
|
||||
if err == nil {
|
||||
return loginTestUser(t, ts, db)
|
||||
}
|
||||
|
||||
loginRequest := map[string]interface{}{
|
||||
"login": "testuser@example.com",
|
||||
"password": "Test@Pass123!",
|
||||
}
|
||||
body, _ := json.Marshal(loginRequest)
|
||||
|
||||
req, _ := http.NewRequest("POST", ts.URL+"/api/auth/login", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err, "Failed to login")
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
token, ok := result["access_token"].(string)
|
||||
require.True(t, ok, "Should have access_token")
|
||||
return token
|
||||
return loginUserWithID(t, ts, db, userUUID, "admin@example.com", "Test@Pass123!")
|
||||
}
|
||||
|
||||
func loginUserWithID(t *testing.T, ts *httptest.Server, db *database.Queries, userID uuid.UUID, email, password string) string {
|
||||
|
||||
Reference in New Issue
Block a user