Spaces:
Sleeping
Sleeping
File size: 7,659 Bytes
659c5be |
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 |
# User Invitation Test Guide
## Quick Start
Run the complete invitation flow test:
```bash
node tests/integration/test_invitation_flow.js
```
## What This Test Does
### Step-by-Step Flow
1. **Login as Platform Admin**
- Email: `lewis.kamau421@gmail.com`
- Password: `TestPass123`
- Gets authentication token
2. **Create Contractor Organization**
- Name: TechInstall Kenya Ltd
- Email: info@techinstall.co.ke
- Competencies: FTTH, Fixed Wireless, Fiber Splicing
- Returns existing if already created
3. **Invite User to Contractor**
- Email: `irene@example.com`
- Phone: `+254799456976`
- Role: `field_agent`
- Method: WhatsApp (with email fallback)
4. **Validate Invitation Token**
- Public endpoint (no auth required)
- Verifies token is valid and not expired
- Returns organization details
5. **List All Invitations**
- Shows all invitations created by admin
- Includes pagination info
## Expected Results
### Successful Test Output
```
π SwiftOps User Invitation Flow Test
π Server: https://kamau1-swiftops-backend.hf.space
π€ Admin: lewis.kamau421@gmail.com
π§ Inviting: irene@example.com (+254799456976)
β
Login as Admin
β
Create Contractor
β
Invite User
π± Invitation URL:
https://swiftops.atomio.tech/accept-invitation?token=xxx
β
Validate Invitation
β
List Invitations
π All steps completed successfully!
```
### What Happens Next
1. **Irene receives WhatsApp message** on +254799456976:
```
Hi Irene,
You've been invited to join TechInstall Kenya Ltd on SwiftOps
as a Field Agent.
π Accept your invitation here:
https://swiftops.atomio.tech/accept-invitation?token=xxx
β° This invitation expires in 72 hours.
```
2. **Irene clicks the link** β Opens registration page
3. **Registration form shows**:
- Email: `irene@example.com` (pre-filled)
- Phone: `+254799456976` (pre-filled)
- Organization: TechInstall Kenya Ltd
- Role: Field Agent
- Fields to fill: First Name, Last Name, Password
4. **Irene submits form** β Account created β Auto logged in
## Notification Delivery
### WhatsApp (Primary)
- Sent via WaSender API
- Requires: `WASENDER_API_KEY`, `WASENDER_PHONE_NUMBER`
- Format: Plain text with link
### Email (Fallback)
- Sent via Resend API if WhatsApp fails
- Requires: `RESEND_API_KEY`, `RESEND_FROM_EMAIL`
- Format: Professional HTML template
### Delivery Tracking
The test shows:
- `whatsapp_sent: true/false`
- `whatsapp_sent_at: timestamp`
- `email_sent: true/false`
- `email_sent_at: timestamp`
## Testing Different Scenarios
### Test 1: Invite to Contractor
```javascript
// Already configured in test_invitation_flow.js
const INVITE_ROLE = 'field_agent';
const contractor_id = 'uuid';
```
### Test 2: Invite to Client
Modify the test to create a client instead:
```javascript
// In step2_CreateContractor, change to:
const response = await makeRequest('POST', '/api/v1/clients', {
name: 'Safaricom Kenya',
industry: 'Telecommunications',
main_email: 'info@safaricom.co.ke',
main_phone: '+254700000000',
default_sla_days: 7
}, authToken);
// In step3_InviteUser, change to:
client_id: clientId, // instead of contractor_id
role: 'client_admin' // or other client role
```
### Test 3: Different Roles
Change the role in the test:
```javascript
const INVITE_ROLE = 'contractor_admin'; // Admin role
const INVITE_ROLE = 'project_manager'; // Manager role
const INVITE_ROLE = 'dispatcher'; // Dispatcher role
const INVITE_ROLE = 'sales_agent'; // Sales role
```
### Test 4: Email-Only Delivery
Change invitation method:
```javascript
invitation_method: 'email' // Skip WhatsApp, use email only
```
### Test 5: Both Methods
Send via both channels:
```javascript
invitation_method: 'both' // Send WhatsApp AND email
```
## Troubleshooting
### Issue: "Cannot reach server"
**Solution:**
- Check server is running at `https://kamau1-swiftops-backend.hf.space`
- Verify network connectivity
- Try accessing `/health` endpoint
### Issue: "Login failed"
**Solution:**
- Verify admin credentials are correct
- Check if user exists in database
- Ensure Supabase Auth is configured
### Issue: "Create contractor failed"
**Solution:**
- Check if contractor already exists (should return existing)
- Verify admin has `platform_admin` role
- Check database connection
### Issue: "Invitation failed"
**Solution:**
- Verify contractor/client ID is valid
- Check role is valid for organization type
- Ensure no duplicate pending invitation exists
### Issue: "WhatsApp not sent"
**Solution:**
- Check `WASENDER_API_KEY` is set and valid
- Verify phone number format (+country code)
- System should fallback to email automatically
- Check test output for `whatsapp_error` field
### Issue: "Email not sent"
**Solution:**
- Check `RESEND_API_KEY` is set and valid
- Verify `RESEND_FROM_EMAIL` is verified in Resend
- Check test output for `email_error` field
## Manual Testing via API
### 1. Get Admin Token
```bash
curl -X POST "https://kamau1-swiftops-backend.hf.space/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{
"email": "lewis.kamau421@gmail.com",
"password": "TestPass123"
}'
```
### 2. Create Contractor
```bash
curl -X POST "https://kamau1-swiftops-backend.hf.space/api/v1/contractors" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "TechInstall Kenya Ltd",
"description": "Leading field service provider",
"main_email": "info@techinstall.co.ke",
"main_phone": "+254700123456",
"competencies": ["FTTH", "Fixed Wireless"]
}'
```
### 3. Create Invitation
```bash
curl -X POST "https://kamau1-swiftops-backend.hf.space/api/v1/invitations" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"email": "irene@example.com",
"phone": "+254799456976",
"role": "field_agent",
"contractor_id": "CONTRACTOR_ID",
"invitation_method": "whatsapp"
}'
```
### 4. Validate Token (No Auth)
```bash
curl -X POST "https://kamau1-swiftops-backend.hf.space/api/v1/invitations/validate" \
-H "Content-Type: application/json" \
-d '{
"token": "INVITATION_TOKEN"
}'
```
## Environment Variables Checklist
Before running tests, ensure these are set:
- [ ] `APP_DOMAIN=swiftops.atomio.tech`
- [ ] `APP_PROTOCOL=https`
- [ ] `INVITATION_TOKEN_EXPIRY_HOURS=72`
- [ ] `RESEND_API_KEY=re_xxx`
- [ ] `RESEND_FROM_EMAIL=swiftops@atomio.tech`
- [ ] `WASENDER_API_KEY=xxx`
- [ ] `WASENDER_PHONE_NUMBER=+254xxx`
- [ ] `WASENDER_API_URL=https://api.wasender.com/v1`
## Success Criteria
β
Admin can login
β
Contractor organization created (or existing returned)
β
Invitation created successfully
β
WhatsApp notification sent (or email fallback)
β
Invitation token is valid
β
Invitation appears in list
β
Irene receives invitation on WhatsApp
β
Invitation URL works
## Next Steps After Test
1. **Check WhatsApp** - Verify Irene received the message
2. **Test Acceptance** - Have Irene click the link and register
3. **Verify User Created** - Check user appears in database
4. **Test Login** - Irene should be able to login
5. **Check Permissions** - Verify Irene has correct role and org access
## Production Checklist
Before going live:
- [ ] Test with real phone numbers
- [ ] Verify WhatsApp delivery
- [ ] Test email fallback
- [ ] Check invitation expiry (72 hours)
- [ ] Test duplicate prevention
- [ ] Verify RLS policies work
- [ ] Test all user roles
- [ ] Check authorization rules
- [ ] Test invitation cancellation
- [ ] Test invitation resending
|