# Test Plan: Wholesale Customer Registration & Approval

## Test Environment Setup

1. **Backend**: `http://localhost:8000`
2. **Frontend**: `http://localhost:3000`
3. **Email**: Check configured `COMPANY_MAIL` in `.env`

---

## Test Case 1: Wholesale Customer Registration

### Steps:
1. Navigate to `/register`
2. Click "Wholesale Partner" card
3. Fill in the form:
   - **Contact Person**: John Doe
   - **Email**: wholesale@test.com
   - **Phone**: +263771234567
   - **Company Name**: Test ISP Ltd
   - **Industry**: Internet Service Provider
   - **TIN Number**: 12345678
   - **VAT Number**: VAT123456
   - **Website**: https://testisp.com
   - **Job Title**: CEO
   - **Password**: Test@1234
   - **Confirm Password**: Test@1234
4. Upload POTRAZ license (use any PDF/image file < 5MB)
5. Accept terms and conditions
6. Click "Create Account"

### Expected Results:
✅ Success message screen appears
✅ Message says "Registration Submitted Successfully!"
✅ Shows "Your wholesale partner application is under review"
✅ Timeline explains approval process
✅ Links to login and home page work

### Backend Verification:
```sql
SELECT id, name, email, type, approval_status, potraz_license_path, odoo_partner_id 
FROM customers 
WHERE email = 'wholesale@test.com';
```

Expected:
- `type` = 'wholesale'
- `approval_status` = 'pending'
- `potraz_license_path` = 'potraz_licenses/potraz_...'
- `odoo_partner_id` = (some number)

### Odoo Verification:
1. Login to Odoo: https://dfaz.ipos.co.zw
2. Go to Contacts
3. Search for "Test ISP Ltd"
4. Check fields:
   - `active` = False (archived)
   - `comment` contains "APPROVAL STATUS: PENDING"
   - `ref` = 12345678 (TIN)
   - `vat` = VAT123456

### Email Verification:
Check email at `COMPANY_MAIL` (tawona@quatrohaus.com):
- Subject: "New Wholesale Partner Registration - Approval Required"
- Contains company details
- POTRAZ license attached
- Link to Odoo partner record

---

## Test Case 2: Wholesale Login (Before Approval)

### Steps:
1. Navigate to `/login`
2. Enter:
   - **Email**: wholesale@test.com
   - **Password**: Test@1234
3. Click "Login"

### Expected Results:
❌ Login fails with error toast
✅ Message: "Your account is pending approval. You will receive an email once approved."
✅ Toast duration: 5 seconds
✅ No redirect to dashboard

---

## Test Case 3: Business Customer Registration

### Steps:
1. Navigate to `/register`
2. Click "Business Customer" card
3. Fill in the form:
   - **Full Name**: Jane Smith
   - **Email**: business@test.com
   - **Phone**: +263771234568
   - **Password**: Test@1234
   - **Confirm Password**: Test@1234
4. Accept terms and conditions
5. Click "Create Account"

### Expected Results:
✅ Success toast: "Registration successful! Please login to continue."
✅ Redirects to `/login` after 2 seconds
✅ No approval required

### Backend Verification:
```sql
SELECT id, name, email, type, approval_status, odoo_partner_id 
FROM customers 
WHERE email = 'business@test.com';
```

Expected:
- `type` = 'business'
- `approval_status` = 'approved'
- `approved_at` = (current timestamp)
- `odoo_partner_id` = (some number)

### Odoo Verification:
1. Search for "Jane Smith" in Odoo Contacts
2. Check fields:
   - `active` = True (not archived)
   - `is_company` = False (individual)

---

## Test Case 4: Business Login (Immediate)

### Steps:
1. Navigate to `/login`
2. Enter:
   - **Email**: business@test.com
   - **Password**: Test@1234
3. Click "Login"

### Expected Results:
✅ Login successful
✅ Redirects to `/dashboard`
✅ Full portal access

---

## Test Case 5: File Upload Validation

### Test 5a: Invalid File Type
1. Start wholesale registration
2. Try to upload `.txt` or `.doc` file

**Expected**: ❌ Error toast: "Please upload a PDF, JPG, or PNG file"

### Test 5b: File Too Large
1. Start wholesale registration
2. Try to upload file > 5MB

**Expected**: ❌ Error toast: "File size must be less than 5MB"

### Test 5c: Missing File
1. Start wholesale registration
2. Fill all fields but don't upload POTRAZ license
3. Click "Create Account"

**Expected**: ❌ Error toast: "Please upload your POTRAZ license"

### Test 5d: Valid File
1. Start wholesale registration
2. Upload valid PDF (< 5MB)

**Expected**: ✅ Success toast: "POTRAZ license uploaded successfully"
✅ File preview shows with name and size
✅ Remove button appears

### Test 5e: Remove File
1. Upload a file
2. Click the X button

**Expected**: ✅ File removed
✅ Upload area reappears

---

## Test Case 6: Approval in Odoo (Manual)

### Steps:
1. Login to Odoo
2. Go to Contacts
3. Find "Test ISP Ltd" (use Filters → Archived)
4. Open the contact record
5. Click "Unarchive" or set `active = True`
6. Save

### Portal Verification (TODO - requires webhook):
Currently manual update needed:
```sql
UPDATE customers 
SET approval_status = 'approved', 
    approved_at = NOW(), 
    approved_by = 'Admin' 
WHERE email = 'wholesale@test.com';
```

### Login Test:
1. Navigate to `/login`
2. Enter wholesale@test.com credentials
3. Click "Login"

**Expected**: ✅ Login successful
✅ Redirects to dashboard

---

## Test Case 7: Rejection in Odoo (Manual)

### Steps:
1. Create another wholesale customer
2. In Odoo, add rejection note to `comment` field
3. Keep `active = False`

### Portal Update (Manual):
```sql
UPDATE customers 
SET approval_status = 'rejected', 
    rejection_reason = 'Invalid POTRAZ license' 
WHERE email = 'wholesale2@test.com';
```

### Login Test:
1. Try to login with rejected account

**Expected**: ❌ Error toast: "Your account registration was rejected. Reason: Invalid POTRAZ license. Please contact support."
✅ Toast duration: 6 seconds

---

## Test Case 8: Form Validation

### Test 8a: Missing Required Fields
1. Start wholesale registration
2. Leave required fields empty
3. Try to submit

**Expected**: ❌ Validation errors under each field

### Test 8b: Invalid Email
1. Enter invalid email format
2. Try to submit

**Expected**: ❌ "Invalid email" error

### Test 8c: Password Mismatch
1. Enter different passwords
2. Try to submit

**Expected**: ❌ "Passwords must match" error

### Test 8d: Duplicate Email
1. Try to register with existing email
2. Submit

**Expected**: ❌ Error toast: "An account with this email already exists"

---

## Test Case 9: UI/UX Verification

### Category Selection:
- [ ] Cards have hover effects
- [ ] Tooltips appear on hover
- [ ] Tooltips positioned correctly (not under panel)
- [ ] Color coding: Purple (wholesale), Green (business)
- [ ] Icons display correctly

### Registration Form:
- [ ] Back button works
- [ ] Form sections clearly labeled
- [ ] Required fields marked
- [ ] Password visibility toggle works
- [ ] File upload area styled correctly
- [ ] File preview shows correctly

### Success Screen:
- [ ] Checkmark icon displays
- [ ] Yellow info box styled correctly
- [ ] Timeline information clear
- [ ] Action buttons work
- [ ] Responsive on mobile

---

## Performance Tests

### File Upload Speed:
- [ ] 1MB file uploads in < 2 seconds
- [ ] 5MB file uploads in < 5 seconds
- [ ] Progress indicator (if implemented)

### Form Submission:
- [ ] Registration completes in < 3 seconds
- [ ] Loading state shows during submission
- [ ] Button disabled during submission

---

## Browser Compatibility

Test on:
- [ ] Chrome (latest)
- [ ] Firefox (latest)
- [ ] Safari (latest)
- [ ] Edge (latest)
- [ ] Mobile Chrome
- [ ] Mobile Safari

---

## Security Tests

### File Upload:
- [ ] Only allowed file types accepted
- [ ] File size limit enforced
- [ ] Files stored outside public directory
- [ ] File names sanitized

### API:
- [ ] CSRF protection (if applicable)
- [ ] Rate limiting on registration
- [ ] SQL injection prevention
- [ ] XSS prevention

---

## Cleanup After Testing

```sql
-- Delete test customers
DELETE FROM customers WHERE email IN ('wholesale@test.com', 'business@test.com', 'wholesale2@test.com');

-- Delete uploaded files
-- Manually delete from storage/app/potraz_licenses/

-- Delete from Odoo (optional)
-- Archive or delete test partners in Odoo
```

---

## Known Issues / TODO

1. **Webhook Integration**: Odoo approval doesn't auto-sync to portal
   - **Workaround**: Manual SQL update
   - **Fix**: Implement Odoo webhook listener

2. **Email Notifications**: Customer doesn't receive approval/rejection emails
   - **TODO**: Create approval/rejection email templates
   - **TODO**: Trigger emails on status change

3. **Admin Dashboard**: No UI for managing approvals
   - **TODO**: Create admin panel for approvals
   - **TODO**: Add POTRAZ license viewer

4. **File Preview**: No inline PDF viewer
   - **Enhancement**: Add PDF preview modal

---

## Success Criteria

✅ Wholesale customers can register with POTRAZ license
✅ Admin receives email with attachment
✅ Wholesale customers blocked until approved
✅ Business customers auto-approved
✅ File upload validation works
✅ Error messages clear and helpful
✅ UI responsive and user-friendly
✅ Odoo integration working
✅ No security vulnerabilities
