Skip to content

Latest commit

 

History

History
235 lines (186 loc) · 6.29 KB

File metadata and controls

235 lines (186 loc) · 6.29 KB

Testing the Event Chat Feature

Prerequisites

  1. Server is running on port 5001
  2. Client is running on port 3000
  3. MongoDB is connected
  4. You have a registered user account
  5. There's at least one event with registrations

Manual Testing Steps

1. Create a Test Event (Workshop)

Using Postman or a test script, create a workshop that starts soon:

POST http://localhost:5001/api/workshops
Authorization: Bearer <professor_token>

{
  "name": "Test Chat Workshop",
  "location": "GUC Cairo",
  "startAt": "2025-12-12T14:00:00Z",  // Set to current time + 2 minutes
  "endAt": "2025-12-12T16:00:00Z",
  "startTime": "14:00",
  "endTime": "16:00",
  "shortDescription": "Testing chat feature",
  "fullAgenda": "Test workshop for chat",
  "faculty": "Engineering",
  "professors": ["Test Professor"],
  "requiredBudget": 1000,
  "fundingSource": "GUC",
  "capacity": 50,
  "registrationDeadline": "2025-12-12T13:00:00Z",
  "status": "published"
}

2. Register for the Event

POST http://localhost:5001/api/registrations/register
Authorization: Bearer <user_token>

{
  "eventType": "workshop",
  "eventId": "<workshop_id_from_step_1>",
  "name": "Test User",
  "email": "test@guc.edu.eg",
  "id": "12345"
}

3. Wait for Event Start Time

The chat will automatically activate when:

  • The event's start time arrives (within 5 minutes)
  • The scheduled task runs (every 5 minutes)

Or manually trigger it by:

# In the server directory
node -e "require('./services/chatService').manageEventChats().then(() => process.exit())"

4. Test Chat UI

  1. Login to the application as a registered user
  2. Navigate to any page (home, events, etc.)
  3. Look for the minimized chat tab on the right side of the screen
  4. Click the chat tab to open it
  5. Send a test message
  6. Open another browser window/tab (incognito mode)
  7. Login as another registered user
  8. Verify that messages appear in real-time

5. Test Chat Features

Minimizing

  • Click the minimize button (−) in the chat header
  • Verify the chat collapses to a tab
  • Click the tab to reopen

Closing

  • Click the close button (×) in the chat header
  • Verify the chat disappears completely
  • Refresh the page
  • Verify the chat reappears if still active

Multiple Chats

  • Register for multiple events
  • Wait for them to start
  • Verify multiple minimized tabs appear
  • Click each tab to switch between chats

Message Display

  • Verify your messages appear on the right with blue background
  • Verify other users' messages appear on the left with white background
  • Verify timestamps are displayed correctly
  • Verify sender names are shown

Message Validation

  • Try sending an empty message (should be disabled)
  • Try sending a very long message (max 1000 chars)
  • Verify character limit is enforced

6. Test Chat Deactivation

Wait for the event end time or manually deactivate:

POST http://localhost:5001/api/chat/workshop/<event_id>/close
Authorization: Bearer <admin_token>

Verify:

  • Chat becomes read-only or closes
  • New messages cannot be sent
  • Chat tab disappears after refresh

Automated Testing Script

Create a file test-chat.js in the server directory:

const axios = require('axios');

const API_URL = 'http://localhost:5001';
let authToken = '';

async function testChat() {
  try {
    // 1. Login
    console.log('1. Logging in...');
    const loginRes = await axios.post(`${API_URL}/api/auth/login`, {
      email: 'test@guc.edu.eg',
      password: 'password123'
    });
    authToken = loginRes.data.token;
    console.log('✓ Logged in successfully');

    // 2. Get active chats
    console.log('\n2. Fetching active chats...');
    const chatsRes = await axios.get(`${API_URL}/api/active-chats`, {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    console.log(`✓ Found ${chatsRes.data.length} active chat(s)`);
    
    if (chatsRes.data.length === 0) {
      console.log('⚠ No active chats found. Create and register for an event first.');
      return;
    }

    // 3. Get messages for first chat
    const firstChat = chatsRes.data[0];
    console.log(`\n3. Getting messages for "${firstChat.eventName}"...`);
    const messagesRes = await axios.get(
      `${API_URL}/api/chat/${firstChat.eventType}/${firstChat.eventId}/messages`,
      { headers: { Authorization: `Bearer ${authToken}` } }
    );
    console.log(`✓ Found ${messagesRes.data.messages.length} message(s)`);

    console.log('\n✅ All tests passed!');
  } catch (error) {
    console.error('❌ Test failed:', error.response?.data || error.message);
  }
}

testChat();

Run with: node test-chat.js

Common Issues

Chat Not Appearing

  • Check browser console for errors
  • Verify Socket.io connection: Look for "Socket connected" in console
  • Check that event has started and status is correct
  • Verify you're registered for the event

Messages Not Sending

  • Check browser console for Socket errors
  • Verify authentication token is valid
  • Check server logs for Socket.io events
  • Ensure chat is still active

Real-time Updates Not Working

  • Verify Socket.io connection is established
  • Check CORS settings in server.js
  • Try refreshing the page
  • Check firewall/proxy settings

Browser Console Commands

Test Socket.io connection:

// Check if socket service is connected
import socketService from './services/socketService';
console.log('Connected:', socketService.isConnected());

// Manually connect
socketService.connect();

// Test sending a message
socketService.sendMessage('workshop', 'EVENT_ID', 'Test message');

Server Logs to Monitor

When testing, watch for these logs:

✓ Socket connected
✓ User <userId> joined room workshop-<eventId>
✓ Message sent in room workshop-<eventId> by <userName>
✓ Activated chat for workshop: <eventName>
✓ Deactivated chat for workshop: <eventName>

Performance Testing

Test with multiple users:

  1. Open 10+ browser tabs
  2. Login with different accounts in each
  3. All send messages simultaneously
  4. Verify all messages appear in real-time
  5. Check for lag or connection issues

Security Testing

  1. Try accessing chat without being registered
  2. Try sending messages without authentication
  3. Try XSS attacks in message text
  4. Verify message length limits
  5. Test with invalid event IDs