# Testing the Leave Approval Workflow

## Prerequisites
- Create test users with roles: training_officer, warden, assistant_director, director
- Create test students: one hostel student, one day scholar student

---

## Test Case 1: Hostel Student Leave - Complete Approval

### Step 1: Student Submits Leave Request
1. Login as hostel student
2. Go to "Apply Leave"
3. Fill form with:
   - Student Type: **hostel** ← IMPORTANT
   - Leave Type: CL
   - Reason: "Leave reason here"
   - From Date: (any valid future date)
   - To Date: (same as from date for CL)
4. Submit

**Expected Results:**
- Leave request created with:
  - `student_type = 'hostel'`
  - `current_level = 1`
  - `status = 'pending'`
- Training Officer receives notification

---

### Step 2: Training Officer Approves
1. Login as Training Officer
2. Go to "Approvals" dashboard
3. Should see the leave request
4. Click "Review"
5. Select "Approve"
6. Click Submit

**Expected Results:**
- ApprovalLog created with:
  - `level = 1`
  - `approver_id = TO's ID`
  - `decision = 'approved'`
- Leave request updated:
  - `current_level = 2`
  - `status = 'forwarded'`
- **Warden** (not Assistant Director!) receives notification
  - This is because student_type = 'hostel'

---

### Step 3: Warden Approves
1. Login as Warden
2. Go to "Approvals" dashboard
3. Should see the leave request
4. Click "Review"
5. Select "Approve"
6. Click Submit

**Expected Results:**
- ApprovalLog created with:
  - `level = 2`
  - `approver_id = Warden's ID`
  - `decision = 'approved'`
  - Level Name: **"Warden"**
- Leave request updated:
  - `current_level = 3`
  - `status = 'forwarded'`
- **Director** receives notification

---

### Step 3: Assistant Director Final Approval (Hostel)
1. Login as Assistant Director
2. Go to "Approvals" dashboard
3. Should see the leave request
4. Click "Review"
5. Select "Approve"
6. Click Submit

**Expected Results:**
- ApprovalLog created with:
  - `level = 3`
  - `approver_id = Director's ID`
  - `decision = 'approved'`
- Leave request updated:
  - `status = 'approved'` ✓ FINAL
- **Student** receives notification: "Your leave request has been approved..."

---

## Test Case 2: Day Scholar Student Leave - Approved Path

### Step 1: Day Scholar Submits Leave Request
1. Login as day scholar student
2. Go to "Apply Leave"
3. Fill form with:
   - Student Type: **scholar** ← IMPORTANT (NOT hostel)
   - Leave Type: Medical
   - Reason: "Medical reason"
   - From Date: (any valid future date)
   - To Date: (select dates)
   - Attachment: Upload medical document
4. Submit

**Expected Results:**
- Leave request created with:
  - `student_type = 'scholar'`
  - `current_level = 1`
  - `status = 'pending'`
- Training Officer receives notification

---

### Step 2: Training Officer Approves
1. Login as Training Officer
2. Go to "Approvals"
3. Approve the leave

**Expected Results:**
- Leave request updated:
  - `current_level = 2`
  - `status = 'forwarded'`
- **Assistant Director** (not Warden!) receives notification
  - This is because student_type = 'scholar'

---

### Step 3: Assistant Director Approves
1. Login as Assistant Director
2. Go to "Approvals"
3. Approve the leave

**Expected Results:**
- ApprovalLog created with:
  - `level = 2`
  - `decision = 'approved'`
  - Level Name: **"Assistant Director"**
- Leave request updated:
  - `current_level = 3`
  - `status = 'forwarded'`
- Assistant Director receives notification

---

### Step 4: Assistant Director Final Approval (Hostel)
1. Login as Assistant Director
2. Go to "Approvals"
3. Approve the leave

**Expected Results:**
- Leave request updated:
  - `status = 'approved'` ✓ FINAL

---

## Test Case 3: Rejection at Any Level

### Example: Hostel Student Rejected at Warden Level
1. Follow Test Case 1 Steps 1-2
2. At Step 3 (Warden's approval):
   - Select "Reject"
   - Add remarks: "Leave reasons not valid"
   - Click Submit

**Expected Results:**
- ApprovalLog created with:
  - `level = 2`
  - `decision = 'rejected'`
- Leave request updated:
  - `status = 'rejected'` ✓ STOPS HERE
  - Does NOT go to Director
- Student receives notification with rejection reason
- Request appears in student's history as rejected

---

## Test Case 4: Wrong User Access Control

### Test: Warden tries to access day scholar's leave at level 2
1. Create and process a day scholar leave to level 2
2. Login as Warden
3. Try to access the leave request approval

**Expected Result:**
- Error 403: "This request is not at your approval level"
- Warden should only see hostel student leaves at level 2

---

## Test Case 5: Assistant Director Access

### Test: Assistant Director tries to access hostel student's leave at level 2
1. Create and process a hostel student leave to level 2
2. Login as Assistant Director
3. Try to access the leave request approval

**Expected Result:**
- Error 403: "This request is not at your approval level"
- Assistant Director should only see day scholar leaves at level 2

---

## SQL Queries to Verify

### Check all leaves for a specific student:
```sql
SELECT
    lr.id,
    lr.student_type,
    lr.current_level,
    lr.status,
    lr.created_at
FROM leave_requests lr
JOIN students s ON lr.student_id = s.id
WHERE s.id = (SELECT id FROM students WHERE user_id = ?);
```

### Check approval logs for a specific leave:
```sql
SELECT
    al.id,
    al.level,
    u.name as approver_name,
    u.role,
    al.decision,
    al.remarks,
    al.created_at
FROM approval_logs al
JOIN users u ON al.approver_id = u.id
WHERE al.leave_request_id = ?
ORDER BY al.level ASC;
```

### Check if level 2 routing is correct:
```sql
SELECT
    lr.id,
    lr.student_type,
    s.user_id,
    u.name as student_name,
    lr.current_level,
    lr.status,
    (CASE
        WHEN lr.student_type = 'hostel' THEN 'Should go to Warden'
        ELSE 'Should go to Assistant Director'
    END) as expected_next_approver
FROM leave_requests lr
JOIN students s ON lr.student_id = s.id
JOIN users u ON s.user_id = u.id
WHERE lr.current_level = 2;
```

---

## Common Issues & Solutions

### Issue: Assistant Director can't see hostel leaves
**Solution:** Correct behavior! AD only sees day scholar (scholar) leaves at level 2

### Issue: Warden can't see day scholar leaves
**Solution:** Correct behavior! Warden only sees hostel leaves at level 2

### Issue: Wrong approver notified
**Solution:** Check that `student_type` is set correctly when leave is created

### Issue: Leave shows "Unknown" for level name
**Solution:** Ensure ApprovalLog relationship to LeaveRequest is loaded

---

## Summary Checklist

✓ Level 1: Always Training Officer (everyone)
✓ Level 2: Routes to Warden (hostel) OR Assistant Director (scholar)
✓ Level 2: Warden (hostel) or Assistant Director (day scholar - final)
✓ Rejection stops process at any level
✓ Each approver only sees leaves at their level+correct student type
✓ Notifications go to correct next level approver
✓ Status updates: pending → forwarded → approved/rejected
