Files
Tasks/backend/TEST_INSTRUCTIONS.md
2025-11-20 00:39:00 +02:00

7.4 KiB

How to Test the New Admin Questions API

Quick Start

1. Ensure Backend is Running

cd backend
npm start

The server should be running on http://localhost:3000

2. Run the Test Suite

node test-admin-questions-pagination.js

What Gets Tested

The test suite automatically:

  1. Logs in as admin user
  2. Creates a regular test user
  3. Starts a guest session
  4. Creates 8 test questions with different properties
  5. Runs 35 comprehensive tests
  6. Cleans up all test data

Test Categories

Authorization (3 tests)

  • Guest access denial
  • Regular user access denial
  • Admin access granted

Pagination (8 tests)

  • Default pagination
  • Custom page sizes
  • Metadata accuracy
  • Limit enforcement
  • Invalid page handling

Search (4 tests)

  • Search question text
  • Search explanations
  • No results handling
  • Special characters

Filters (6 tests)

  • By difficulty (easy/medium/hard)
  • By category
  • Invalid UUID handling

Combined Filters (4 tests)

  • Search + difficulty
  • Search + category
  • Category + difficulty
  • All filters together

Sorting (5 tests)

  • By creation date
  • By points
  • By difficulty
  • Invalid sort fields

Response Structure (4 tests)

  • Response format validation
  • Required fields check
  • Category object structure
  • Filter reflection

Edge Cases (3 tests)

  • Empty searches
  • Out of range pages
  • Accuracy calculations

Manual Testing

Get All Questions (First Page)

curl "http://localhost:3000/api/admin/questions" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Search for Questions

curl "http://localhost:3000/api/admin/questions?search=javascript" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Filter by Difficulty

curl "http://localhost:3000/api/admin/questions?difficulty=medium&limit=20" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Filter by Category

# Replace with actual category UUID from your database
curl "http://localhost:3000/api/admin/questions?category=68b4c87f-db0b-48ea-b8a4-b2f4fce785a2" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Paginate Through Questions

# Page 1
curl "http://localhost:3000/api/admin/questions?page=1&limit=10" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

# Page 2
curl "http://localhost:3000/api/admin/questions?page=2&limit=10" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Sort Questions

# By points (highest first)
curl "http://localhost:3000/api/admin/questions?sortBy=points&order=DESC" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

# By creation date (oldest first)
curl "http://localhost:3000/api/admin/questions?sortBy=createdAt&order=ASC" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Complex Query

curl "http://localhost:3000/api/admin/questions?search=async&difficulty=medium&sortBy=points&order=DESC&page=1&limit=15" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"

Get Admin Token

Option 1: From Test Output

Run any test file that logs in as admin and look for the token in console.

Option 2: Login Manually

curl -X POST "http://localhost:3000/api/auth/login" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "admin@quiz.com",
    "password": "Admin@123"
  }'

The token will be in response.data.data.token

Expected Test Output

========================================
Testing Admin Questions Pagination & Search API
========================================

Setting up test data...

✓ Logged in as admin
✓ Created and logged in as regular user
✓ Started guest session
✓ Created 8 test questions

--- Authorization Tests ---

✓ Test 1: Guest cannot access admin questions endpoint - PASSED
✓ Test 2: Regular user cannot access admin questions endpoint - PASSED
✓ Test 3: Admin can access questions endpoint - PASSED

--- Pagination Tests ---

✓ Test 4: Default pagination (page 1, limit 10) - PASSED
✓ Test 5: Custom pagination (page 2, limit 5) - PASSED
✓ Test 6: Pagination metadata is correct - PASSED
✓ Test 7: Maximum limit enforcement (max 100) - PASSED
✓ Test 8: Invalid page defaults to 1 - PASSED

--- Search Tests ---

✓ Test 9: Search by question text (async) - PASSED
✓ Test 10: Search by explanation text (promise) - PASSED
✓ Test 11: Search with no results - PASSED
✓ Test 12: Search with special characters is handled - PASSED

--- Filter Tests ---

✓ Test 13: Filter by difficulty (easy) - PASSED
✓ Test 14: Filter by difficulty (medium) - PASSED
✓ Test 15: Filter by difficulty (hard) - PASSED
✓ Test 16: Filter by category (JavaScript) - PASSED
✓ Test 17: Filter by category (Node.js) - PASSED
✓ Test 18: Invalid category UUID is ignored - PASSED

--- Combined Filter Tests ---

✓ Test 19: Search + difficulty filter - PASSED
✓ Test 20: Search + category filter - PASSED
✓ Test 21: Category + difficulty filter - PASSED
✓ Test 22: All filters combined - PASSED

--- Sorting Tests ---

✓ Test 23: Sort by createdAt DESC (default) - PASSED
✓ Test 24: Sort by createdAt ASC - PASSED
✓ Test 25: Sort by difficulty - PASSED
✓ Test 26: Sort by points DESC - PASSED
✓ Test 27: Invalid sort field defaults to createdAt - PASSED

--- Response Structure Tests ---

✓ Test 28: Response has correct structure - PASSED
✓ Test 29: Each question has required fields - PASSED
✓ Test 30: Category object has required fields - PASSED
✓ Test 31: Filters object in response matches query - PASSED
✓ Test 32: Admin can see correctAnswer field - PASSED

--- Performance & Edge Cases ---

✓ Test 33: Empty search string returns all questions - PASSED
✓ Test 34: Page beyond total pages returns empty array - PASSED
✓ Test 35: Accuracy is calculated correctly - PASSED

========================================
Cleaning up test data...
========================================

✓ Deleted 8 test questions

========================================
Test Summary
========================================
Total Tests: 35
Passed: 35 ✓
Failed: 0 ✗
Success Rate: 100.00%
========================================

Troubleshooting

"Setup failed: Network Error"

  • Ensure backend server is running on port 3000
  • Check if database connection is working

"Admin login failed"

  • Verify admin user exists in database
  • Check credentials: email: admin@quiz.com, password: Admin@123

"Category not found"

  • Run seeders to populate categories
  • Check CATEGORY_IDS in test file match your database

Tests fail with 500 errors

  • Check backend logs for detailed error messages
  • Ensure all required models are properly defined
  • Verify database schema is up to date

Documentation

  • API Documentation: See ADMIN_QUESTIONS_API.md
  • Implementation Summary: See QUESTIONS_API_IMPLEMENTATION_SUMMARY.md
  • Controller Code: See controllers/question.controller.js - getAllQuestions method
  • Route Definition: See routes/admin.routes.js
  • test-create-question.js - Test question creation
  • test-update-delete-question.js - Test updates and deletions
  • test-questions-by-category.js - Test public category endpoint
  • test-question-search.js - Test public search endpoint
  • test-question-by-id.js - Test single question retrieval

Next Steps

After successful testing:

  1. Review the API documentation
  2. Integrate with frontend admin dashboard
  3. Implement Angular Material paginator
  4. Add search and filter UI components
  5. Create question management interface