# JWT Authentication Testing Guide

## Quick Start Verification (5 minutes)

### Step 1: Database Setup (2 min)
```powershell
cd "c:\Apps\LimozX\limozxAPI"
php artisan migrate
php artisan db:seed --class=UserSeeder
```

**Expected Output:**
```
Migration table created successfully.
Migrating: 2024_01_01_000000_create_users_table
Migrated: 2024_01_01_000000_create_users_table
Seeding database with UserSeeder
```

### Step 2: Start Laravel Server (1 min)
```powershell
php artisan serve
```

**Expected Output:**
```
   INFO  Server running on [http://127.0.0.1:8000]
```

### Step 3: Test Login Endpoint with Postman/cURL (2 min)

#### Using Postman:
1. Create new request
2. Method: `POST`
3. URL: `http://127.0.0.1:8000/api/auth/login`
4. Headers:
   - `Content-Type: application/json`
5. Body (raw JSON):
```json
{
  "email": "admin@limozx.com",
  "password": "12345678"
}
```
6. Click Send

#### Using PowerShell/cURL:
```powershell
$body = @{
    email = "admin@limozx.com"
    password = "12345678"
} | ConvertTo-Json

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/login" `
  -Method POST `
  -ContentType "application/json" `
  -Body $body

$response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 10
```

**Expected Response:**
```json
{
  "success": true,
  "message": "Login successful",
  "token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "user": {
    "id": 1,
    "name": "Platform Admin",
    "email": "admin@limozx.com",
    "role": "Platform Admin",
    "tenant": null,
    "company": null,
    "department": null,
    "grade": null,
    "phone": "+91-9999999999"
  },
  "data": {
    "id": 1,
    "name": "Platform Admin",
    "email": "admin@limozx.com",
    "role": "Platform Admin",
    "tenant": null,
    "company": null,
    "department": null,
    "grade": null
  }
}
```

---

## Test All User Roles

### 1. Platform Admin
**Credentials:**
- Email: `admin@limozx.com`
- Password: `12345678`

**Expected Dashboard:** System Administration Panel
**Permissions:** Full system access

---

### 2. Operator Admin (Ola Cabs)
**Credentials:**
- Email: `operator@olacabs.com`
- Password: `12345678`

**Expected Dashboard:** Operator Admin Dashboard
**Permissions:** Manage drivers, vehicles, trips for Ola Cabs

**Test Request:**
```powershell
$token = "paste_token_from_login_response_here"

$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/profile" `
  -Method GET `
  -Headers $headers | Select-Object -ExpandProperty Content
```

---

### 3. Operator Admin (Uber)
**Credentials:**
- Email: `operator@uber.com`
- Password: `12345678`

**Expected Dashboard:** Operator Admin Dashboard
**Permissions:** Manage drivers, vehicles, trips for Uber

---

### 4. Tenant Admin (Ola Cabs)
**Credentials:**
- Email: `tenant@olacabs.com`
- Password: `12345678`

**Expected Dashboard:** Tenant Admin Dashboard
**Permissions:** Manage corporate account for Ola Cabs

---

### 5. Tenant Admin (Uber)
**Credentials:**
- Email: `tenant@uber.com`
- Password: `12345678`

**Expected Dashboard:** Tenant Admin Dashboard
**Permissions:** Manage corporate account for Uber

---

### 6. Affiliate Admin (Premier Travel)
**Credentials:**
- Email: `affiliate@premiertravel.com`
- Password: `12345678`

**Expected Dashboard:** Affiliate Admin Dashboard
**Permissions:** Manage commissions and leads

---

### 7. Affiliate Admin (Corporate Travel)
**Credentials:**
- Email: `affiliate@corporatetravel.com`
- Password: `12345678`

**Expected Dashboard:** Affiliate Admin Dashboard
**Permissions:** Manage commissions and leads

---

### 8. Corporate Traveler (Sales)
**Credentials:**
- Email: `raj.kumar@techcorp.com`
- Password: `12345678`

**Expected Dashboard:** Corporate Traveler Dashboard
**Permissions:** Book and manage personal trips
**Company Details:**
- Company: TechCorp India
- Department: Sales
- Grade: Senior Executive

---

### 9. Corporate Traveler (Finance)
**Credentials:**
- Email: `priya.singh@techcorp.com`
- Password: `12345678`

**Expected Dashboard:** Corporate Traveler Dashboard
**Permissions:** Book and manage personal trips
**Company Details:**
- Company: TechCorp India
- Department: Finance
- Grade: Manager

---

### 10. Corporate Traveler (Operations)
**Credentials:**
- Email: `anil.patel@techcorp.com`
- Password: `12345678`

**Expected Dashboard:** Corporate Traveler Dashboard
**Permissions:** Book and manage personal trips
**Company Details:**
- Company: TechCorp India
- Department: Operations
- Grade: Executive

---

### 11. Driver (Ola Cabs)
**Credentials:**
- Email: `driver1@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Driver Mobile App
**Permissions:** Accept trips, update status

---

### 12. Driver (Ola Cabs - Driver 2)
**Credentials:**
- Email: `driver2@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Driver Mobile App
**Permissions:** Accept trips, update status

---

### 13. Driver (Uber)
**Credentials:**
- Email: `driver3@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Driver Mobile App
**Permissions:** Accept trips, update status

---

### 14. Driver (Uber - Driver 2)
**Credentials:**
- Email: `driver4@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Driver Mobile App
**Permissions:** Accept trips, update status

---

### 15. Customer
**Credentials:**
- Email: `sarah.customer@email.com`
- Password: `12345678`

**Expected Dashboard:** Customer Dashboard
**Permissions:** Book rides, view history

---

### 16. Customer (Corporate)
**Credentials:**
- Email: `john.corporate@email.com`
- Password: `12345678`

**Expected Dashboard:** Customer Dashboard
**Permissions:** Book rides, view history

---

### 17. Operations Manager
**Credentials:**
- Email: `ops@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Operations Manager Dashboard
**Permissions:** Monitor all operations

---

### 18. Finance Manager
**Credentials:**
- Email: `finance@limozx.com`
- Password: `12345678`

**Expected Dashboard:** Finance Manager Dashboard
**Permissions:** View financial reports

---

## API Endpoint Tests

### Test 1: Login
```powershell
# Test with correct credentials
$loginBody = @{
    email = "admin@limozx.com"
    password = "12345678"
} | ConvertTo-Json

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/login" `
  -Method POST `
  -ContentType "application/json" `
  -Body $loginBody

$response.StatusCode  # Should be 200
$loginResponse = $response.Content | ConvertFrom-Json
$token = $loginResponse.token
```

**Expected:** HTTP 200, token returned

---

### Test 2: Get Profile (Protected)
```powershell
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/profile" `
  -Method GET `
  -Headers $headers

$response.StatusCode  # Should be 200
$response.Content | ConvertFrom-Json | ConvertTo-Json
```

**Expected:** HTTP 200, user profile returned

---

### Test 3: Invalid Token
```powershell
$headers = @{
    "Authorization" = "Bearer invalid_token_here"
    "Content-Type" = "application/json"
}

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/profile" `
  -Method GET `
  -Headers $headers `
  -ErrorAction SilentlyContinue

$response.StatusCode  # Should be 401
```

**Expected:** HTTP 401, "Token is invalid" message

---

### Test 4: Wrong Credentials
```powershell
$loginBody = @{
    email = "admin@limozx.com"
    password = "wrongpassword"
} | ConvertTo-Json

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/login" `
  -Method POST `
  -ContentType "application/json" `
  -Body $loginBody `
  -ErrorAction SilentlyContinue

$response.StatusCode  # Should be 401
```

**Expected:** HTTP 401, "Invalid email or password" message

---

### Test 5: Refresh Token
```powershell
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/refresh" `
  -Method POST `
  -Headers $headers

$response.StatusCode  # Should be 200
$refreshResponse = $response.Content | ConvertFrom-Json
$newToken = $refreshResponse.token
```

**Expected:** HTTP 200, new token returned

---

### Test 6: Logout
```powershell
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type" = "application/json"
}

$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/api/auth/logout" `
  -Method POST `
  -Headers $headers

$response.StatusCode  # Should be 200
$response.Content | ConvertFrom-Json | ConvertTo-Json
```

**Expected:** HTTP 200, "Logout successful" message

---

## Angular Integration Test

### Step 1: Update API Configuration
In `src/app/config/api-config.ts`, ensure:
```typescript
export const API_CONFIG = {
  LOCAL: 'http://127.0.0.1:8000/api',
  PRODUCTION: 'https://api.limozx.com/api'
};

export const CURRENT_ENVIRONMENT = API_CONFIG.LOCAL;
```

### Step 2: Test Login from Angular
1. Open Angular application in browser (http://localhost:4200)
2. Navigate to login page
3. Enter: `admin@limozx.com` / `12345678`
4. Click Login

**Expected Results:**
- ✅ No errors in console
- ✅ Redirected to dashboard
- ✅ User name displayed
- ✅ Token visible in localStorage (DevTools > Application > Storage > localStorage)

### Step 3: Verify Token Persistence
1. Open DevTools (F12)
2. Go to Application > Storage > localStorage
3. Look for key: `token`
4. Value should be JWT token (eyJ0eXA...)

### Step 4: Test Protected Routes
1. Copy token from localStorage
2. Navigate to any protected route
3. Should NOT be redirected to login
4. User profile should load correctly

### Step 5: Test Role-Based Access
1. Login as `admin@limozx.com`
2. Should see System Admin options
3. Logout
4. Login as `operator@olacabs.com`
5. Should see Operator Admin options only

---

## Debugging Tips

### Enable Laravel Debug Mode
In `.env`:
```
APP_DEBUG=true
APP_ENV=local
```

### Check User Records
```powershell
php artisan tinker

# Count users
App\Models\User::count()

# Find specific user
App\Models\User::where('email', 'admin@limozx.com')->first()

# List all users by role
App\Models\User::where('role', 'Driver')->get()
```

### View Database
```powershell
# List all users
php artisan tinker
App\Models\User::select('id', 'name', 'email', 'role', 'tenant')->get()
```

### Check JWT Configuration
```powershell
php artisan config:show jwt
```

### Monitor Laravel Logs
```powershell
tail -f storage/logs/laravel.log
```

---

## Checklist

- [ ] Migrations run successfully
- [ ] Seeders populate 18 users
- [ ] Laravel server starts on port 8000
- [ ] Login endpoint returns JWT token
- [ ] Protected endpoints require valid token
- [ ] Invalid tokens are rejected
- [ ] Profile endpoint returns user data
- [ ] Token refresh works
- [ ] Logout invalidates token
- [ ] All 18 test users login successfully
- [ ] Angular receives token on login
- [ ] Token stored in localStorage
- [ ] Protected routes accessible with token
- [ ] Dashboard loaded based on user role
- [ ] Role-based features visible correctly

---

## Summary

**System Ready for Testing!** ✅

- Database: 18 test users created
- API: All endpoints functional
- Laravel: Running on http://127.0.0.1:8000
- Angular: Ready to test login
- JWT: Token generation working
- All roles: Can login with provided credentials

**Next Step:** Test login from Angular application with test credentials.
