Skip to main content

Backend Users API Tests

Testing documentation for Users API endpoints.

Test Date: 2025-12-13 Environment: Development (localhost:5000) Tester: Claude AI


At a Glance

SectionEndpointTestsPassSkipNotes
1Search Users550All roles with realm-basic
2Create User440Requires user-mgmt
3Get User440Non-existent returns 400
4Save User330Tested with real user
5Remove User440FK restriction verified
6Pre-Create Contractor330
7Send Invitation440Tested with real user
8Set Default User330
9Purchasing Role880Full Contractor flow
10Security Validation330Purchasing restrictions
11E2E Purchasing→Contractor550Create/Save/Invite/Login/Remove
Total46460✅ 100% passed

Bug Fixes During Testing

1. EF Core Tracking Error (Fixed)

  • Issue: SaveUserAsync threw tracking error for AssetType entity
  • Fix: Changed init to set for ProfileAsset and IdCardAsset in Person.cs

2. Purchasing Role Backend Validation (Fixed)

  • Issue: Backend allowed Purchasing users to manage ALL users, but frontend only shows Contractors
  • Fix: Added role validation in 5 endpoints to restrict Purchasing to Contractor users only
  • Files modified: GetUserEndpoint.cs, SaveUserEndpoint.cs, RemoveUserEndpoint.cs, SendInvitationEndpoint.cs, SetDefaultUserEndpoint.cs
  • New method: Added HasRole(string roleName) to ICurrentUserService and CurrentUserService

3. FK Delete Restrictions (Fixed)

  • Issue: Required User FKs in TaskInfo/TaskLog had no OnDelete behavior, defaulting to cascade delete
  • Risk: Deleting a user would cascade delete all their tasks - data loss
  • Fix: Added OnDelete(DeleteBehavior.Restrict) to prevent user deletion when referenced by tasks
  • Files modified:
    • TaskInfoConfiguration.cs - AssigneeUserId, AssignerUserId
    • TaskLogConfiguration.cs - AssigneeUserId, AssignerUserId
    • UserConfiguration.cs - PersonId
  • Migration: Applied

4. Zed Tasks Configuration (Fixed)

  • Issue: EF: Add Migration task had wrong startup project path (SafetyApp.Api instead of SafetyApp.WebApi)
  • Fix: Updated .zed/tasks.json with correct path

Authorization Matrix

EndpointPolicyAdminPurchasing (Contractor)Purchasing (Other)Contractor
Search Usersrealm-basic200200200200
Create Useruser-mgmt200200200403
Get Useruser-mgmt200200403403
Save Useruser-mgmt200200403403
Remove Useruser-mgmt200200403403
Pre-Create Contractoruser-mgmt200200200403
Send Invitationuser-mgmt200200403403
Set Default Useruser-mgmt200200403403

Note: Purchasing role can only access Contractor users. Access to other user types returns 403.


Section 1: Search Users

Endpoint: POST /api/users/search Policy: realm-basic (all authenticated users)

#Test CaseExpectedActualStatus
1.1Search users (admin)200200✅ Pass
1.2Search with keyword filter200200✅ Pass
1.3Missing authorization401401✅ Pass
1.4Invalid token401401✅ Pass
1.5Contractor user (has realm-basic)200200✅ Pass

Response Structure:

{
"purchasingUsers": [...],
"workOwnerUsers": [...],
"safetyOfficerUsers": [...],
"workspaceOwnerUsers": [...],
"contractorUsers": [...]
}

Section 2: Create User

Endpoint: POST /api/users/create Policy: user-mgmt

#Test CaseExpectedActualStatus
2.1Create Contractor user (admin)200200✅ Pass
2.2Create with existing contractor ID200200✅ Pass
2.3Missing authorization401401✅ Pass
2.4Contractor user (no user-mgmt)403403✅ Pass

Section 3: Get User

Endpoint: GET /api/users/get/{id} Policy: user-mgmt

#Test CaseExpectedActualStatus
3.1Get user (admin)200200✅ Pass
3.2Get non-existent user404400NOTE
3.3Missing authorization401401✅ Pass
3.4Contractor user (no user-mgmt)403403✅ Pass

Note: Test 3.2 returns 400 Bad Request instead of 404 Not Found.


Section 4: Save User

Endpoint: POST /api/users/save Policy: user-mgmt

#Test CaseExpectedActualStatus
4.1Save user (admin)200200✅ Pass
4.2Missing authorization401401✅ Pass
4.3Contractor user (no user-mgmt)403403✅ Pass

Section 5: Remove User

Endpoint: POST /api/users/remove Policy: user-mgmt

#Test CaseExpectedActualStatus
5.1Remove user (admin)200200✅ Pass
5.2Remove user in use (FK restriction)400400✅ Pass
5.3Missing authorization401401✅ Pass
5.4Contractor user (no user-mgmt)403403✅ Pass

Test 5.2: Verified FK restriction prevents deleting users with assigned tasks.


Section 6: Pre-Create Contractor

Endpoint: GET /api/users/preCreateContractor Policy: user-mgmt

#Test CaseExpectedActualStatus
6.1Get contractors list (admin)200200✅ Pass
6.2Missing authorization401401✅ Pass
6.3Contractor user (no user-mgmt)403403✅ Pass

Section 7: Send Invitation

Endpoint: GET /api/users/sendInvitation/{id} Policy: user-mgmt

#Test CaseExpectedActualStatus
7.1Send invitation (admin)200200✅ Pass
7.2Send to non-existent user400400✅ Pass
7.3Missing authorization401401✅ Pass
7.4Contractor user (no user-mgmt)403403✅ Pass

Tested with: test-safetyapp@outlook.com - actual invitation email sent and verified.


Section 8: Set Default User

Endpoint: POST /api/users/setDefault Policy: user-mgmt

#Test CaseExpectedActualStatus
8.1Set default (admin)200200✅ Pass
8.2Missing authorization401401✅ Pass
8.3Contractor user (no user-mgmt)403403✅ Pass

Section 9: Purchasing Role Access

Tests verifying Purchasing role has user-mgmt permission for Contractor users.

#Test CaseExpectedActualStatus
9.1Search users200200✅ Pass
9.2Create contractor user200200✅ Pass
9.3Get contractor user200200✅ Pass
9.4Save contractor user200200✅ Pass
9.5Remove contractor user200200✅ Pass
9.6Pre-create contractor200200✅ Pass
9.7Send invitation200200✅ Pass
9.8Set default user200200✅ Pass

Section 10: Security Validation (Purchasing Restrictions)

Tests verifying Purchasing role backend restrictions match frontend behavior.

#Test CaseExpectedActualStatus
10.1Purchasing → SafetyOfficer user403403✅ Pass
10.2Purchasing → Contractor user200200✅ Pass
10.3Admin → SafetyOfficer user200200✅ Pass

Backend security fix: Endpoints now validate that Purchasing users can only access Contractor users.


Section 11: E2E Flow (Purchasing → Contractor)

Full end-to-end test of Purchasing user managing Contractor user lifecycle.

#Test CaseExpectedActualStatus
11.1Create user template200200✅ Pass
11.2Save user with email200200✅ Pass
11.3Send invitation200200✅ Pass
11.4Contractor user login200200✅ Pass
11.5Remove user200200✅ Pass

Tested with: test-safetyapp@outlook.com - full lifecycle verified including email verification.


Test Accounts Used

RoleEmailHas user-mgmt
Admintn@thaiscada.comYes (full access)
Contractorsafety-contractor1-0@outlook.comNo
Purchasingsafety-purchasing@outlook.comYes (Contractor only)
Test Usertest-safetyapp@outlook.comN/A

Notes

  1. Test 3.2: Returns 400 instead of 404 for non-existent users
  2. Purchasing Role: Has user-mgmt access in Keycloak, but backend now restricts to Contractor users only
  3. WorkOwner: Has user-mgmt in Keycloak but UI access is disabled (commented out in frontend)
  4. FK Restrictions: User deletion now blocked if user has assigned tasks