Skip to content

test: Implement StreamsGateway WebSocket integration test #195

@Xhristin3

Description

@Xhristin3

Problem Statement

The StreamsGateway (api/src/gateways/streams.gateway.ts) has zero automated tests. It handles JWT-authenticated WebSocket connections, stream room subscription/unsubscription, and real-time event broadcasting (stream:started, stream:stopped, stream:error). All of this critical real-time infrastructure is untested. A failure in token extraction, room management, or event routing would go undetected until production.

Evidence

The api/src/ directory has only one test file for the gateway module: none for streams.gateway.ts. The unit test suite for auth service and request logger exists, but the WebSocket layer has no coverage.

Impact

WebSocket bugs (authentication bypass, event misrouting, connection leaks) cannot be caught before deployment. The gateway is one of the most security-sensitive components (JWT verification, real-time data access) with zero automated verification.

Proposed Solution

Use @nestjs/testing and socket.io-client or socket.io mock to create integration tests:

  1. Test handleConnection with valid JWT → connected
  2. Test handleConnection with invalid JWT → disconnected with error
  3. Test handleConnection with missing token → disconnected
  4. Test handleSubscribe for authenticated client → joins room
  5. Test handleSubscribe for unauthenticated client → returns error
  6. Test handleUnsubscribe → leaves room
  7. Test emitStarted, emitStopped, emitError → events broadcast to room only
  8. Test handleDisconnect → client removed from rooms

Technical Requirements

  • Must use NestJS testing utilities (Test.createTestingModule)
  • Must mock JwtService with known-good tokens
  • Must use a real or mocked socket.io server
  • Must test edge cases: null payloads, missing fields, duplicate subscriptions

Acceptance Criteria

  • Connection with valid JWT succeeds
  • Connection with expired/invalid JWT is rejected with INVALID_TOKEN error
  • Connection with no token is rejected with MISSING_TOKEN error
  • Subscribe returns room name on success
  • Unsubscribe returns room name on success
  • Emit helpers broadcast only to the correct stream room
  • Unauthenticated clients are rejected from subscribe/unsubscribe
  • All tests pass in CI

File Map

  • api/src/gateways/streams.gateway.spec.ts — new test file
  • api/src/gateways/streams.gateway.ts — may need minor refactoring for testability

Dependencies

  • Related: REPO-001 (JWT auth consistency)

Testing Strategy

  • Unit: Test each method in isolation with mocked Server and Socket
  • Integration: Test full connection lifecycle with real socket.io in-memory
  • Test token extraction from all three sources (auth header, query, handshake auth)
  • Test error handling in handleDisconnect (should never throw)

Security Considerations

Test that invalid tokens are rejected before any room join operations. Test that the gateway never accepts connections without proper JWT verification.

Definition of Done

  • Code implemented and peer-reviewed
  • Tests written and passing (unit + integration where applicable)
  • Documentation updated if behavior changed
  • No new linting errors or type errors introduced
  • Security considerations addressed
  • PR linked to this issue and merged

Labels: testing, security, high impact
Priority: High
Difficulty: Advanced
Estimated Effort: 2d
Milestone: v1.0-alpha


Labels: testing,security,high impact
Priority: High | Difficulty: Advanced | Estimated Effort: 2d
Backlog ID: REPO-012

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official Campaignhigh impactsecuritySecurity related issuestestingTests and test coverage

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions