-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathindex.test.js
More file actions
139 lines (116 loc) · 4.85 KB
/
index.test.js
File metadata and controls
139 lines (116 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const request = require('supertest');
const express = require('express');
const {
RtcTokenBuilder,
RtcRole,
RtmTokenBuilder,
RtmRole,
} = require('agora-access-token');
// Mock environment variables
process.env.APP_ID = 'test-app-id';
process.env.APP_CERTIFICATE = 'test-app-certificate';
process.env.PORT = '3000';
// Import the express app
const app = require('./index.js');
// Main test suite for the Token Generation API
describe('Token Generation API', () => {
// Test suite for the health check endpoint
describe('GET /ping', () => {
it('should return pong message', async () => {
const response = await request(app).get('/ping');
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'pong' });
});
});
// Test suite for RTC (Real-Time Communication) token generation
describe('GET /rtc/:channel/:role/:tokentype/:uid', () => {
// Test successful RTC token generation
it('should generate RTC token with valid parameters', async () => {
const response = await request(app)
.get('/rtc/test-channel/publisher/uid/1234')
.query({ expiry: '3600' }); // Token expires in 1 hour
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('rtcToken');
expect(typeof response.body.rtcToken).toBe('string');
});
// Test error handling for missing channel parameter
it('should return error for missing channel', async () => {
const response = await request(app).get('/rtc//publisher/uid/1234');
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'channel is required' });
});
// Test error handling for invalid role parameter
it('should return error for invalid role', async () => {
const response = await request(app).get(
'/rtc/test-channel/invalid-role/uid/1234'
);
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'role is incorrect' });
});
});
// Test suite for RTM (Real-Time Messaging) token generation
describe('GET /rtm/:uid', () => {
// Test successful RTM token generation
it('should generate RTM token with valid parameters', async () => {
const response = await request(app)
.get('/rtm/1234')
.query({ expiry: '3600' }); // Token expires in 1 hour
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('rtmToken');
expect(typeof response.body.rtmToken).toBe('string');
});
// Test error handling for missing uid parameter
it('should return error for missing uid', async () => {
const response = await request(app).get('/rtm/');
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'uid is required' });
});
});
// Test suite for RTE (Combined RTC and RTM) token generation
describe('GET /rte/:channel/:role/:tokentype/:uid', () => {
// Test successful generation of both RTC and RTM tokens
it('should generate both RTC and RTM tokens with valid parameters', async () => {
const response = await request(app)
.get('/rte/test-channel/publisher/uid/1234')
.query({ expiry: '3600' }); // Tokens expire in 1 hour
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('rtcToken');
expect(response.body).toHaveProperty('rtmToken');
expect(typeof response.body.rtcToken).toBe('string');
expect(typeof response.body.rtmToken).toBe('string');
});
// Test error handling for missing channel parameter
it('should return error for missing channel', async () => {
const response = await request(app).get('/rte//publisher/uid/1234');
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'channel is required' });
});
// Test error handling for invalid role parameter
it('should return error for invalid role', async () => {
const response = await request(app).get(
'/rte/test-channel/invalid-role/uid/1234'
);
expect(response.status).toBe(400);
expect(response.body).toEqual({ error: 'role is incorrect' });
});
});
// Test suite for verifying response headers
describe('Response Headers', () => {
// Test presence of cache control headers
it('should include no-cache headers', async () => {
const response = await request(app).get('/ping');
expect(response.headers['cache-control']).toBe(
'private, no-cache, no-store, must-revalidate'
);
expect(response.headers['expires']).toBe('-1');
expect(response.headers['pragma']).toBe('no-cache');
});
// Test presence of CORS headers
it('should include CORS headers', async () => {
const response = await request(app).get(
'/rtc/test-channel/publisher/uid/1234'
);
expect(response.headers['access-control-allow-origin']).toBe('*');
});
});
});