# End-to-End Test Scenarios

## Overview
Dokumentasi ini berisi skenario test end-to-end untuk sistem dynamic article approval. Setiap skenario mencakup langkah-langkah lengkap dari setup hingga completion, termasuk API calls, expected responses, dan business logic yang terjadi.

---

## 📋 Table of Contents
1. [Skenario 1: Setup Workflow Standard 3-Level](#skenario-1-setup-workflow-standard-3-level)
2. [Skenario 2: Artikel Approval Process Normal](#skenario-2-artikel-approval-process-normal)
3. [Skenario 3: Dynamic Approval Toggle](#skenario-3-dynamic-approval-toggle)
4. [Skenario 4: Artikel Rejection & Revision](#skenario-4-artikel-rejection--revision)
5. [Skenario 5: Multi-Client dengan Approval Berbeda](#skenario-5-multi-client-dengan-approval-berbeda)
6. [Skenario 6: Breaking News Mode](#skenario-6-breaking-news-mode)
7. [Skenario 7: User Exemption Management](#skenario-7-user-exemption-management)
8. [Skenario 8: Workflow Modification](#skenario-8-workflow-modification)
9. [Skenario 9: Audit Trail & Reporting](#skenario-9-audit-trail--reporting)
10. [Skenario 10: Performance & Load Testing](#skenario-10-performance--load-testing)

---

## 🎯 Skenario 1: Setup Workflow Standard 3-Level

### **Business Context**
Setup workflow approval standard untuk editorial process dengan 3 level: Editor → Senior Editor → Editor in Chief.

### **Test Steps**

#### **Step 1: Create Approval Workflow**
```bash
POST /approval-workflows
Headers:
  X-Client-Key: client_123
  Authorization: Bearer {jwt_token}

Body:
{
    "name": "3-Level Editorial Review",
    "description": "Standard editorial workflow for quality content",
    "is_default": true,
    "is_active": true,
    "requires_approval": true,
    "auto_publish": false
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "id": 1,
        "name": "3-Level Editorial Review",
        "description": "Standard editorial workflow for quality content",
        "is_default": true,
        "is_active": true,
        "requires_approval": true,
        "auto_publish": false,
        "created_at": "2024-01-15T10:00:00Z"
    }
}
```

#### **Step 2: Create Workflow Steps**
```bash
# Step 1: Editor Review
POST /approval-workflow-steps
{
    "workflow_id": 1,
    "step_order": 1,
    "step_name": "Editor Review",
    "step_description": "Initial content review by editor",
    "required_user_level_id": 2,
    "estimated_days": 1,
    "is_required": true,
    "can_skip": false
}

# Step 2: Senior Editor Review
POST /approval-workflow-steps
{
    "workflow_id": 1,
    "step_order": 2,
    "step_name": "Senior Editor Review",
    "step_description": "Secondary review by senior editor",
    "required_user_level_id": 3,
    "estimated_days": 2,
    "is_required": true,
    "can_skip": false
}

# Step 3: Editor in Chief
POST /approval-workflow-steps
{
    "workflow_id": 1,
    "step_order": 3,
    "step_name": "Editor in Chief",
    "step_description": "Final approval by editor in chief",
    "required_user_level_id": 4,
    "estimated_days": 1,
    "is_required": true,
    "can_skip": false
}
```

#### **Step 3: Verify Workflow Setup**
```bash
GET /approval-workflows/1/with-steps
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "id": 1,
        "name": "3-Level Editorial Review",
        "steps": [
            {
                "id": 1,
                "step_order": 1,
                "step_name": "Editor Review",
                "required_user_level_id": 2,
                "estimated_days": 1
            },
            {
                "id": 2,
                "step_order": 2,
                "step_name": "Senior Editor Review",
                "required_user_level_id": 3,
                "estimated_days": 2
            },
            {
                "id": 3,
                "step_order": 3,
                "step_name": "Editor in Chief",
                "required_user_level_id": 4,
                "estimated_days": 1
            }
        ]
    }
}
```

### **Validation Points**
- ✅ Workflow created dengan 3 steps
- ✅ Step order sequential (1, 2, 3)
- ✅ User level requirements set correctly
- ✅ Estimated days reasonable

---

## 📝 Skenario 2: Artikel Approval Process Normal

### **Business Context**
Artikel "Breaking News: Tech Conference 2024" perlu melalui approval process normal.

### **Test Steps**

#### **Step 1: Submit Article for Approval**
```bash
POST /articles/123/submit-approval
{
    "workflow_id": 1,
    "message": "Ready for editorial review - Tech conference coverage"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "approval_flow_id": 456,
        "article_id": 123,
        "current_step": 1,
        "status": "pending_approval",
        "workflow_name": "3-Level Editorial Review"
    }
}
```

#### **Step 2: Check Approval Status**
```bash
GET /articles/123/approval-status
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "article_id": 123,
        "current_status": "pending_approval",
        "current_step": 1,
        "total_steps": 3,
        "workflow_name": "3-Level Editorial Review",
        "current_step_name": "Editor Review",
        "next_step_name": "Senior Editor Review",
        "pending_approver": {
            "user_level_id": 2,
            "user_level_name": "Editor",
            "estimated_days": 1
        }
    }
}
```

#### **Step 3: Editor Approves (Step 1)**
```bash
POST /article-approval-flows/456/approve
{
    "message": "Content quality meets standards",
    "comments": "Good coverage, minor grammar fixes suggested"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "approval_flow_id": 456,
        "current_step": 2,
        "status": "pending_approval",
        "next_approver": "Senior Editor"
    }
}
```

#### **Step 4: Senior Editor Approves (Step 2)**
```bash
POST /article-approval-flows/456/approve
{
    "message": "Content approved for final review",
    "comments": "Excellent coverage, ready for final approval"
}
```

#### **Step 5: Editor in Chief Approves (Step 3)**
```bash
POST /article-approval-flows/456/approve
{
    "message": "Final approval granted",
    "comments": "Publish immediately - breaking news"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "approval_flow_id": 456,
        "status": "approved",
        "article_status": "published",
        "completion_date": "2024-01-15T15:30:00Z"
    }
}
```

### **Validation Points**
- ✅ Artikel berhasil submit ke approval
- ✅ Progress melalui 3 steps berurutan
- ✅ Status update real-time
- ✅ Artikel otomatis publish setelah approval lengkap

---

## ⚡ Skenario 3: Dynamic Approval Toggle

### **Business Context**
Breaking news terjadi, perlu disable approval system sementara untuk immediate publishing.

### **Test Steps**

#### **Step 1: Check Current Settings**
```bash
GET /client-approval-settings
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "requires_approval": true,
        "auto_publish_articles": false,
        "default_workflow_id": 1
    }
}
```

#### **Step 2: Disable Approval System**
```bash
POST /client-approval-settings/disable
{
    "reason": "Breaking news mode - immediate publishing required",
    "handle_action": "auto_approve"
}
```

**Expected Response:**
```json
{
    "success": true,
    "messages": ["Approval system successfully disabled with auto-publish enabled"]
}
```

#### **Step 3: Create Article (Should Auto-Publish)**
```bash
POST /articles
{
    "title": "BREAKING: Major Tech Acquisition",
    "content": "Breaking news content...",
    "category_id": 1
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "id": 124,
        "status": "published",
        "is_publish": true,
        "published_at": "2024-01-15T16:00:00Z",
        "approval_bypassed": true
    }
}
```

#### **Step 4: Re-enable Approval System**
```bash
POST /client-approval-settings/enable
{
    "default_workflow_id": 1,
    "reason": "Returning to normal approval process"
}
```

**Expected Response:**
```json
{
    "success": true,
    "messages": ["Approval system successfully enabled with smooth transition"]
}
```

#### **Step 5: Verify Settings Restored**
```bash
GET /client-approval-settings
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "requires_approval": true,
        "auto_publish_articles": false,
        "default_workflow_id": 1
    }
}
```

### **Validation Points**
- ✅ Approval system berhasil di-disable
- ✅ Artikel baru auto-publish tanpa approval
- ✅ Settings berhasil di-restore
- ✅ Approval system kembali normal

---

## ❌ Skenario 4: Artikel Rejection & Revision

### **Business Context**
Artikel "Product Review: Smartphone X" ditolak di step 2, perlu revision.

### **Test Steps**

#### **Step 1: Submit Article for Approval**
```bash
POST /articles/125/submit-approval
{
    "workflow_id": 1,
    "message": "Product review ready for approval"
}
```

#### **Step 2: Editor Approves (Step 1)**
```bash
POST /article-approval-flows/457/approve
{
    "message": "Initial review passed",
    "comments": "Good structure, ready for senior review"
}
```

#### **Step 3: Senior Editor Rejects (Step 2)**
```bash
POST /article-approval-flows/457/reject
{
    "reason": "Insufficient technical details",
    "feedback": "Please add more technical specifications and benchmark comparisons",
    "return_to_step": 1
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "approval_flow_id": 457,
        "status": "revision_requested",
        "article_status": "draft",
        "returned_to_step": 1,
        "rejection_reason": "Insufficient technical details"
    }
}
```

#### **Step 4: Check Article Status**
```bash
GET /articles/125/approval-status
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "revision_requested",
        "current_step": 1,
        "rejection_feedback": "Please add more technical specifications and benchmark comparisons",
        "returned_to_step": 1
    }
}
```

#### **Step 5: Resubmit After Revision**
```bash
POST /article-approval-flows/457/resubmit
{
    "message": "Article revised with additional technical details",
    "revision_summary": "Added benchmark comparisons and technical specifications"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "approval_flow_id": 457,
        "status": "pending_approval",
        "current_step": 1,
        "revision_count": 1
    }
}
```

### **Validation Points**
- ✅ Artikel berhasil di-reject dengan feedback
- ✅ Status berubah ke revision_requested
- ✅ Artikel kembali ke step 1
- ✅ Revision tracking berfungsi
- ✅ Resubmission berhasil

---

## 🏢 Skenario 5: Multi-Client dengan Approval Berbeda

### **Business Context**
3 client berbeda dengan kebutuhan approval yang berbeda:
- Client A: Always requires approval
- Client B: Never requires approval
- Client C: Conditional approval

### **Test Steps**

#### **Step 1: Setup Client A (Always Approval)**
```bash
# Set client context
X-Client-Key: client_a

POST /client-approval-settings
{
    "requires_approval": true,
    "auto_publish_articles": false,
    "default_workflow_id": 1
}
```

#### **Step 2: Setup Client B (No Approval)**
```bash
# Set client context
X-Client-Key: client_b

POST /client-approval-settings
{
    "requires_approval": false,
    "auto_publish_articles": true
}
```

#### **Step 3: Setup Client C (Conditional)**
```bash
# Set client context
X-Client-Key: client_c

POST /client-approval-settings
{
    "requires_approval": true,
    "auto_publish_articles": false,
    "default_workflow_id": 1,
    "approval_exempt_users": [10, 15],
    "approval_exempt_categories": [5],
    "skip_approval_for": ["announcement", "update"]
}
```

#### **Step 4: Test Client A (Always Approval)**
```bash
X-Client-Key: client_a

POST /articles
{
    "title": "Client A Article",
    "content": "Content from client A"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "draft",
        "approval_required": true
    }
}
```

#### **Step 5: Test Client B (No Approval)**
```bash
X-Client-Key: client_b

POST /articles
{
    "title": "Client B Article",
    "content": "Content from client B"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "published",
        "is_publish": true,
        "approval_bypassed": true
    }
}
```

#### **Step 6: Test Client C (Conditional)**
```bash
X-Client-Key: client_c

# Test exempt user
POST /articles
{
    "title": "Exempt User Article",
    "content": "Content from exempt user",
    "author_id": 10
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "published",
        "approval_bypassed": true,
        "exemption_reason": "user_exempt"
    }
}

# Test exempt category
POST /articles
{
    "title": "Announcement",
    "content": "Company announcement",
    "category_id": 5
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "published",
        "approval_bypassed": true,
        "exemption_reason": "category_exempt"
    }
}
```

### **Validation Points**
- ✅ Client A selalu require approval
- ✅ Client B auto-publish tanpa approval
- ✅ Client C conditional approval berfungsi
- ✅ Exemption rules diterapkan dengan benar
- ✅ Multi-client isolation berfungsi

---

## 🚨 Skenario 6: Breaking News Mode

### **Business Context**
Emergency situation memerlukan immediate publishing tanpa approval process.

### **Test Steps**

#### **Step 1: Activate Breaking News Mode**
```bash
POST /client-approval-settings/disable
{
    "reason": "EMERGENCY: Breaking news situation",
    "handle_action": "auto_approve"
}
```

#### **Step 2: Handle Pending Articles**
```bash
# Check pending articles
GET /article-approval-flows?status=pending_approval

# Expected: All pending articles auto-approved
```

#### **Step 3: Create Emergency Articles**
```bash
POST /articles
{
    "title": "EMERGENCY ALERT: System Maintenance",
    "content": "Emergency maintenance required...",
    "priority": "critical"
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "published",
        "is_publish": true,
        "published_at": "2024-01-15T18:00:00Z",
        "emergency_mode": true
    }
}
```

#### **Step 4: Monitor Auto-Publishing**
```bash
# Check recent articles
GET /articles?status=published&limit=10

# Expected: All articles published immediately
```

#### **Step 5: Return to Normal Mode**
```bash
POST /client-approval-settings/enable
{
    "default_workflow_id": 1,
    "reason": "Emergency situation resolved"
}
```

### **Validation Points**
- ✅ Breaking news mode aktif
- ✅ Pending articles auto-approved
- ✅ New articles auto-publish
- ✅ Normal mode restored
- ✅ Emergency tracking berfungsi

---

## 👥 Skenario 7: User Exemption Management

### **Business Context**
Manage user exemptions untuk approval system.

### **Test Steps**

#### **Step 1: Add Exempt User**
```bash
POST /client-approval-settings/exempt-users/add/25
```

**Expected Response:**
```json
{
    "success": true,
    "messages": ["User successfully added to approval exemption"]
}
```

#### **Step 2: Add Exempt Role**
```bash
POST /client-approval-settings/exempt-roles/add/3
```

#### **Step 3: Add Exempt Category**
```bash
POST /client-approval-settings/exempt-categories/add/7
```

#### **Step 4: Test Exempt User**
```bash
POST /articles
{
    "title": "Exempt User Article",
    "content": "Content from exempt user",
    "author_id": 25
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "published",
        "approval_bypassed": true,
        "exemption_reason": "user_exempt"
    }
}
```

#### **Step 5: Remove Exemption**
```bash
POST /client-approval-settings/exempt-users/remove/25
```

#### **Step 6: Verify Exemption Removed**
```bash
POST /articles
{
    "title": "Non-Exempt Article",
    "content": "Content from non-exempt user",
    "author_id": 25
}
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "status": "draft",
        "approval_required": true
    }
}
```

### **Validation Points**
- ✅ User exemption berhasil ditambahkan
- ✅ Exempt user bypass approval
- ✅ Exemption berhasil di-remove
- ✅ Non-exempt user require approval

---

## 🔧 Skenario 8: Workflow Modification

### **Business Context**
Modify existing workflow untuk menambah step baru.

### **Test Steps**

#### **Step 1: Get Current Workflow**
```bash
GET /approval-workflows/1/with-steps
```

#### **Step 2: Add New Step**
```bash
POST /approval-workflow-steps
{
    "workflow_id": 1,
    "step_order": 2,
    "step_name": "Legal Review",
    "step_description": "Legal compliance check",
    "required_user_level_id": 5,
    "estimated_days": 1
}
```

#### **Step 3: Reorder Steps**
```bash
# Move Legal Review to position 2
PUT /approval-workflow-steps/4/reorder
{
    "new_order": 2
}

# Update other steps
PUT /approval-workflow-steps/2/reorder
{
    "new_order": 3
}

PUT /approval-workflow-steps/3/reorder
{
    "new_order": 4
}
```

#### **Step 4: Verify New Order**
```bash
GET /approval-workflows/1/with-steps
```

**Expected Response:**
```json
{
    "success": true,
    "data": {
        "steps": [
            {"step_order": 1, "step_name": "Editor Review"},
            {"step_order": 2, "step_name": "Legal Review"},
            {"step_order": 3, "step_name": "Senior Editor Review"},
            {"step_order": 4, "step_name": "Editor in Chief"}
        ]
    }
}
```

#### **Step 5: Test Modified Workflow**
```bash
POST /articles/126/submit-approval
{
    "workflow_id": 1,
    "message": "Test modified workflow"
}
```

### **Validation Points**
- ✅ New step berhasil ditambahkan
- ✅ Step order berhasil di-reorder
- ✅ Workflow modification tidak impact existing flows
- ✅ New workflow berfungsi dengan benar

---

## 📊 Skenario 9: Audit Trail & Reporting

### **Business Context**
Generate comprehensive audit trail dan reporting untuk compliance.

### **Test Steps**

#### **Step 1: Create Multiple Approval Actions**
```bash
# Submit article
POST /articles/127/submit-approval
# Approve step 1
POST /article-approval-flows/458/approve
# Approve step 2
POST /article-approval-flows/458/approve
# Approve step 3
POST /article-approval-flows/458/approve
```

#### **Step 2: Get Approval Step Logs**
```bash
GET /article-approval-step-logs/flow/458
```

**Expected Response:**
```json
{
    "success": true,
    "data": [
        {
            "id": 1,
            "action": "submitted",
            "action_by_user_id": 20,
            "action_date": "2024-01-15T10:00:00Z",
            "comments": "Ready for review"
        },
        {
            "id": 2,
            "action": "approved",
            "action_by_user_id": 25,
            "action_date": "2024-01-15T11:00:00Z",
            "comments": "Step 1 approved"
        },
        {
            "id": 3,
            "action": "approved",
            "action_by_user_id": 30,
            "action_date": "2024-01-15T12:00:00Z",
            "comments": "Step 2 approved"
        },
        {
            "id": 4,
            "action": "approved",
            "action_by_user_id": 35,
            "action_date": "2024-01-15T13:00:00Z",
            "comments": "Final approval"
        }
    ]
}
```

#### **Step 3: Get User Activity Logs**
```bash
GET /article-approval-step-logs/user/25
```

#### **Step 4: Get Workflow Performance**
```bash
GET /approval-workflows/1/performance
```

### **Validation Points**
- ✅ All actions logged dengan detail
- ✅ User activity tracking berfungsi
- ✅ Timestamp dan metadata lengkap
- ✅ Audit trail searchable dan filterable

---

## ⚡ Skenario 10: Performance & Load Testing

### **Business Context**
Test system performance dengan multiple concurrent requests.

### **Test Steps**

#### **Step 1: Concurrent Article Submissions**
```bash
# Simulate 10 concurrent submissions
for i in {1..10}; do
  POST /articles/submit-approval &
done
wait
```

#### **Step 2: Concurrent Approvals**
```bash
# Simulate multiple approvers working simultaneously
for i in {1..5}; do
  POST /article-approval-flows/$i/approve &
done
wait
```

#### **Step 3: Load Test Approval Status Checks**
```bash
# Simulate 50 concurrent status checks
for i in {1..50}; do
  GET /articles/$i/approval-status &
done
wait
```

#### **Step 4: Database Query Performance**
```bash
# Test pagination performance
GET /article-approval-flows?page=1&limit=100
GET /article-approval-flows?page=1&limit=1000
```

### **Validation Points**
- ✅ System handle concurrent requests
- ✅ Response time acceptable (< 500ms)
- ✅ Database queries optimized
- ✅ No deadlocks atau race conditions

---

## 🧪 Test Data Setup

### **Required Test Data**
```sql
-- User Levels
INSERT INTO user_levels (id, name) VALUES 
(1, 'Author'),
(2, 'Editor'),
(3, 'Senior Editor'),
(4, 'Editor in Chief'),
(5, 'Legal Reviewer');

-- Test Users
INSERT INTO users (id, name, user_level_id) VALUES
(20, 'John Author', 1),
(25, 'Alice Editor', 2),
(30, 'Bob Senior Editor', 3),
(35, 'Carol Editor in Chief', 4),
(40, 'David Legal', 5);

-- Test Categories
INSERT INTO article_categories (id, name) VALUES
(1, 'News'),
(2, 'Opinion'),
(3, 'Review'),
(4, 'Tutorial'),
(5, 'Announcement'),
(6, 'Update'),
(7, 'Press Release');
```

---

## 📋 Test Checklist

### **Functional Testing**
- [ ] Workflow creation dan modification
- [ ] Article submission dan approval flow
- [ ] Dynamic approval toggle
- [ ] User exemption management
- [ ] Multi-client isolation
- [ ] Audit trail generation

### **Performance Testing**
- [ ] Response time < 500ms
- [ ] Concurrent request handling
- [ ] Database query optimization
- [ ] Memory usage monitoring

### **Security Testing**
- [ ] Client isolation
- [ ] User authorization
- [ ] Data validation
- [ ] SQL injection prevention

### **Integration Testing**
- [ ] API endpoint connectivity
- [ ] Database consistency
- [ ] Error handling
- [ ] Logging functionality

---

## 🚀 Running the Tests

### **Environment Setup**
```bash
# Set environment variables
export CLIENT_KEY="test_client_123"
export JWT_TOKEN="your_test_jwt_token"
export API_BASE_URL="http://localhost:8080"

# Run test scenarios
./run-test-scenarios.sh
```

### **Test Execution Order**
1. Setup workflows dan basic configuration
2. Test normal approval flow
3. Test dynamic toggle functionality
4. Test edge cases dan error scenarios
5. Test performance dan load
6. Test multi-client scenarios

---

## 📝 Notes

### **Important Considerations**
- Semua test menggunakan test client ID
- JWT token harus valid untuk test duration
- Database state harus clean sebelum test
- Monitor system resources selama performance test

### **Test Data Cleanup**
```bash
# Cleanup test data after testing
DELETE FROM article_approval_step_logs WHERE test_flag = true;
DELETE FROM article_approval_flows WHERE test_flag = true;
DELETE FROM articles WHERE test_flag = true;
```

---

*Dokumentasi ini akan diupdate sesuai dengan perkembangan sistem dan feedback dari testing.*
