File size: 8,977 Bytes
dacae32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Profile Management Testing Guide

Complete guide for testing the hierarchical profile management system.

## 🎯 Test Scenarios

### Scenario 1: Platform Admin Manages Field Agent Profiles

**Actors:**
- **Lewis Kamau** (Platform Admin) - `lewis.kamau421@gmail.com`
- **Lesley Wanjiru** (Field Agent) - `lesley@example.com`
- **Irene** (Field Agent) - `irene@example.com` (if invited)

**Flow:**
1. Lewis logs in as platform admin
2. Lewis finds Lesley's user account
3. Lewis views Lesley's profile and completion status
4. Lewis updates Lesley's:
   - Basic info (emergency contacts)
   - Health info (blood type, allergies)
   - PPE sizes (shirt, helmet, shoes)
5. Lewis validates Lesley's profile is complete
6. All changes are logged in audit trail

## πŸ“‹ Prerequisites

### 1. Lesley Must Accept Invitation First

```bash
# Run invitation acceptance test
node tests/integration/test_accept_invitation.js <invitation_token>
```

This creates Lesley's user account and returns her `user_id`.

### 2. Get Lesley's User ID

After acceptance, note the `user_id` from the response:
```json
{
  "user": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "email": "lesley@example.com",
    "full_name": "Lesley Wanjiru"
  }
}
```

## πŸ§ͺ Running the Tests

### Test 1: Admin Authentication
```bash
node tests/integration/test_admin_edit_profiles.js
```

This test:
- βœ… Logs in as Lewis (platform admin)
- βœ… Finds Lesley's account
- βœ… Shows available profile endpoints
- βœ… Demonstrates the workflow

### Test 2: Manual Profile Management

Once you have Lesley's `user_id`, use these curl commands:

#### Get Lesley's Complete Profile
```bash
curl -X GET https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id} \
  -H "Authorization: Bearer {admin_token}"
```

#### Check Edit Permissions
```bash
curl -X GET https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id}/permissions \
  -H "Authorization: Bearer {admin_token}"
```

Expected response:
```json
{
  "can_edit_basic_info": true,
  "can_edit_health_info": true,
  "can_edit_ppe_sizes": true,
  "can_edit_location": true,
  "can_edit_financial_accounts": true,
  "can_upload_documents": true,
  "can_edit_role": true,
  "can_edit_status": true,
  "can_assign_assets": true
}
```

#### Update Basic Info
```bash
curl -X PUT https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id}/basic \
  -H "Authorization: Bearer {admin_token}" \
  -H "Content-Type: application/json" \
  -d '{
    "emergency_contact_name": "Jane Wanjiru",
    "emergency_contact_phone": "+254723456789",
    "phone_alternate": "+254798765432"
  }'
```

#### Update Health Info
```bash
curl -X PUT https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id}/health \
  -H "Authorization: Bearer {admin_token}" \
  -H "Content-Type: application/json" \
  -d '{
    "blood_type": "O+",
    "allergies": "None",
    "chronic_conditions": "None",
    "medications": "None",
    "last_medical_check": "2024-01-15",
    "medical_notes": "Fit for field work"
  }'
```

#### Update PPE Sizes
```bash
curl -X PUT https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id}/ppe \
  -H "Authorization: Bearer {admin_token}" \
  -H "Content-Type: application/json" \
  -d '{
    "height": "165cm",
    "weight": "60kg",
    "shirt_size": "M",
    "pants_size": "30",
    "shoe_size": "38",
    "helmet_size": "M",
    "glove_size": "M",
    "vest_size": "M"
  }'
```

#### Validate Profile Completion
```bash
curl -X GET https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id}/validation \
  -H "Authorization: Bearer {admin_token}"
```

Expected response:
```json
{
  "is_valid": true,
  "missing_fields": [],
  "invalid_fields": {},
  "warnings": []
}
```

## πŸ” Verification Steps

### 1. Check Profile Completion
```bash
curl -X GET https://kamau1-swiftops-backend.hf.space/api/v1/profile/{user_id} \
  -H "Authorization: Bearer {admin_token}"
```

Look for:
```json
{
  "completion_status": {
    "basic_info": true,
    "health_info": true,
    "ppe_sizes": true,
    "financial_accounts": false,
    "documents": false,
    "location": true,
    "completion_percentage": 67
  }
}
```

### 2. Verify Audit Logs

Check database:
```sql
SELECT 
  action,
  description,
  user_email,
  changes,
  created_at
FROM audit_logs
WHERE entity_type = 'user'
  AND entity_id = '{user_id}'
ORDER BY created_at DESC
LIMIT 10;
```

Expected logs:
- `update` - User updated their basic profile
- `update` - Manager updated user's health information
- `update` - Manager updated user's PPE sizes

## 🎭 Test Different Permission Levels

### Test 1: Platform Admin (Lewis)
βœ… Can edit ALL fields for ANY user
```bash
# Lewis can edit Lesley's profile
PUT /api/v1/profile/{lesley_id}/basic
PUT /api/v1/profile/{lesley_id}/health
PUT /api/v1/profile/{lesley_id}/ppe
```

### Test 2: Field Agent (Lesley) - Self Edit
βœ… Can edit OWN profile (limited fields)
```bash
# Lesley can edit her own profile
PUT /api/v1/profile/me/basic  # Only phone_alternate, display_name
PUT /api/v1/profile/me/health  # All fields
PUT /api/v1/profile/me/ppe  # All fields
```

### Test 3: Field Agent (Lesley) - Edit Others
❌ CANNOT edit other users' profiles
```bash
# Lesley CANNOT edit Irene's profile
PUT /api/v1/profile/{irene_id}/basic  # 403 Forbidden
```

### Test 4: Project Manager - Edit Team Members
βœ… Can edit field agents in their projects
```bash
# Manager can edit their team's profiles
PUT /api/v1/profile/{field_agent_id}/basic
PUT /api/v1/profile/{field_agent_id}/ppe
```

## πŸ“Š Expected Results

### Profile Completion Progression

**Initial State (After Invitation Acceptance):**
```json
{
  "completion_percentage": 17,
  "basic_info": true,  // Name, phone from invitation
  "health_info": false,
  "ppe_sizes": false,
  "financial_accounts": false,
  "documents": false,
  "location": false
}
```

**After Admin Updates:**
```json
{
  "completion_percentage": 67,
  "basic_info": true,  // βœ… Emergency contacts added
  "health_info": true,  // βœ… Blood type, allergies added
  "ppe_sizes": true,  // βœ… All sizes added
  "financial_accounts": false,  // ⏳ Lesley needs to add
  "documents": false,  // ⏳ Lesley needs to upload
  "location": true  // βœ… From invitation
}
```

**Fully Complete:**
```json
{
  "completion_percentage": 100,
  "basic_info": true,
  "health_info": true,
  "ppe_sizes": true,
  "financial_accounts": true,  // βœ… Lesley added payout details
  "documents": true,  // βœ… Lesley uploaded ID
  "location": true
}
```

## πŸ› Troubleshooting

### Issue: "User not found"
**Solution:** Ensure Lesley accepted the invitation first
```bash
node tests/integration/test_accept_invitation.js <token>
```

### Issue: "403 Forbidden"
**Solution:** Check you're using admin token, not field agent token
```bash
# Get fresh admin token
node tests/integration/test_admin_edit_profiles.js
```

### Issue: "422 Validation Error"
**Solution:** Check field formats:
- Phone: Must start with `+` (e.g., `+254712345678`)
- Blood type: Must be one of: A+, A-, B+, B-, AB+, AB-, O+, O-
- Sizes: Must be: XS, S, M, L, XL, XXL, XXXL

### Issue: Cannot find user_id
**Solution:** Implement user search endpoint
```python
# Add to src/app/api/v1/users.py
@router.get("/users")
async def search_users(
    email: Optional[str] = None,
    current_user: User = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    query = db.query(User).filter(User.deleted_at == None)
    if email:
        query = query.filter(User.email == email)
    return query.all()
```

## 🎯 Success Criteria

βœ… Admin can login successfully
βœ… Admin can find field agent's account
βœ… Admin can view field agent's profile
βœ… Admin can check edit permissions (all should be true)
βœ… Admin can update basic info
βœ… Admin can update health info
βœ… Admin can update PPE sizes
βœ… Profile completion percentage increases
βœ… Profile validation shows no errors
βœ… All changes appear in audit logs
βœ… Field agent can see updated profile when they login

## πŸ“ Next Steps

After completing profile management tests:

1. **Financial Accounts** - Test adding payout details
2. **Document Upload** - Test uploading ID, license
3. **Asset Assignment** - Test assigning equipment to field agents
4. **Project Creation** - Create projects and assign team members
5. **Ticket Management** - Create and assign tickets to field agents

## πŸ”— Related Tests

- `test_invitation_flow.js` - Create and send invitations
- `test_accept_invitation.js` - Accept invitation and create account
- `test_auth_api.js` - Basic authentication tests
- `test_profile_management.py` - Python unit tests for profile service

## πŸ“š Documentation

- `docs/agent/PROFILE_MANAGEMENT_IMPLEMENTATION.md` - Implementation details
- `src/app/schemas/profile.py` - Profile schemas and validation
- `src/app/services/profile_service.py` - Business logic and permissions
- `src/app/api/v1/profile.py` - API endpoints