forked from neu-se/covey.town
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CoveyTownsREST.test.ts
381 lines (365 loc) · 13 KB
/
CoveyTownsREST.test.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import Express from 'express';
import CORS from 'cors';
import http from 'http';
import { nanoid } from 'nanoid';
import assert from 'assert';
import { AddressInfo } from 'net';
import request from 'supertest';
import TownsServiceClient, { TownListResponse } from './TownsServiceClient';
import addTownRoutes from '../router/towns';
import {closeDb, connectDb, dropBucket, TEST_BUCKET_NAME} from "../db";
import addFileRoutes from "../router/files";
import {logError} from "../Utils";
import io from 'socket.io';
import {StatusCodes} from "http-status-codes";
import * as fs from "fs";
type TestTownData = {
friendlyName: string, coveyTownID: string,
isPubliclyListed: boolean, townUpdatePassword: string
};
function expectTownListMatches(towns: TownListResponse, town: TestTownData) {
const matching = towns.towns.find(townInfo => townInfo.coveyTownID === town.coveyTownID);
if (town.isPubliclyListed) {
expect(matching)
.toBeDefined();
assert(matching);
expect(matching.friendlyName)
.toBe(town.friendlyName);
} else {
expect(matching)
.toBeUndefined();
}
}
describe('TownsServiceAPIREST', () => {
let server: http.Server;
let apiClient: TownsServiceClient;
let app: any;
let address: AddressInfo;
let socket: io.Server;
async function createTownForTesting(friendlyNameToUse?: string, isPublic = false): Promise<TestTownData> {
const friendlyName = friendlyNameToUse !== undefined ? friendlyNameToUse :
`${isPublic ? 'Public' : 'Private'}TestingTown=${nanoid()}`;
const ret = await apiClient.createTown({
friendlyName,
isPubliclyListed: isPublic,
});
return {
friendlyName,
isPubliclyListed: isPublic,
coveyTownID: ret.coveyTownID,
townUpdatePassword: ret.coveyTownPassword,
};
}
beforeAll(async () => {
app = Express();
app.use(CORS());
server = http.createServer(app);
socket = addTownRoutes(server, app);
addFileRoutes(TEST_BUCKET_NAME, app);
await server.listen();
address = server.address() as AddressInfo;
if(address){
apiClient = new TownsServiceClient(`http://127.0.0.1:${address.port}`);
}
return new Promise<void>(resolve => {
connectDb(() => {resolve();});
});
});
afterAll(async () => {
socket.close();
await server.close();
await new Promise<void>(resolve => {
dropBucket(TEST_BUCKET_NAME, (gridErr) => {
if (gridErr) {
logError(gridErr);
}
closeDb((dbErr) => {
if (dbErr) {
logError(dbErr);
}
resolve();
});
});
});
});
describe('CoveyTownCreateAPI', () => {
it('Allows for multiple towns with the same friendlyName', async () => {
const firstTown = await createTownForTesting();
const secondTown = await createTownForTesting(firstTown.friendlyName);
expect(firstTown.coveyTownID)
.not
.toBe(secondTown.coveyTownID);
});
it('Prohibits a blank friendlyName', async () => {
try {
await createTownForTesting('');
fail('createTown should throw an error if friendly name is empty string');
} catch (err) {
// OK
}
});
});
describe('CoveyTownListAPI', () => {
it('Lists public towns, but not private towns', async () => {
const pubTown1 = await createTownForTesting(undefined, true);
const privTown1 = await createTownForTesting(undefined, false);
const pubTown2 = await createTownForTesting(undefined, true);
const privTown2 = await createTownForTesting(undefined, false);
const towns = await apiClient.listTowns();
expectTownListMatches(towns, pubTown1);
expectTownListMatches(towns, pubTown2);
expectTownListMatches(towns, privTown1);
expectTownListMatches(towns, privTown2);
});
it('Allows for multiple towns with the same friendlyName', async () => {
const pubTown1 = await createTownForTesting(undefined, true);
const privTown1 = await createTownForTesting(pubTown1.friendlyName, false);
const pubTown2 = await createTownForTesting(pubTown1.friendlyName, true);
const privTown2 = await createTownForTesting(pubTown1.friendlyName, false);
const towns = await apiClient.listTowns();
expectTownListMatches(towns, pubTown1);
expectTownListMatches(towns, pubTown2);
expectTownListMatches(towns, privTown1);
expectTownListMatches(towns, privTown2);
});
});
describe('CoveyTownDeleteAPI', () => {
it('Throws an error if the password is invalid', async () => {
const { coveyTownID } = await createTownForTesting(undefined, true);
try {
await apiClient.deleteTown({
coveyTownID,
coveyTownPassword: nanoid(),
});
fail('Expected deleteTown to throw an error');
} catch (e) {
// Expected error
}
});
it('Throws an error if the townID is invalid', async () => {
const { townUpdatePassword } = await createTownForTesting(undefined, true);
try {
await apiClient.deleteTown({
coveyTownID: nanoid(),
coveyTownPassword: townUpdatePassword,
});
fail('Expected deleteTown to throw an error');
} catch (e) {
// Expected error
}
});
it('Deletes a town if given a valid password and town, no longer allowing it to be joined or listed', async () => {
const { coveyTownID, townUpdatePassword } = await createTownForTesting(undefined, true);
await apiClient.deleteTown({
coveyTownID,
coveyTownPassword: townUpdatePassword,
});
try {
await apiClient.joinTown({
userName: nanoid(),
coveyTownID,
});
fail('Expected joinTown to throw an error');
} catch (e) {
// Expected
}
const listedTowns = await apiClient.listTowns();
if (listedTowns.towns.find(r => r.coveyTownID === coveyTownID)) {
fail('Expected the deleted town to no longer be listed');
}
});
});
describe('CoveyTownUpdateAPI', () => {
it('Checks the password before updating any values', async () => {
const pubTown1 = await createTownForTesting(undefined, true);
expectTownListMatches(await apiClient.listTowns(), pubTown1);
try {
await apiClient.updateTown({
coveyTownID: pubTown1.coveyTownID,
coveyTownPassword: `${pubTown1.townUpdatePassword}*`,
friendlyName: 'broken',
isPubliclyListed: false,
});
fail('updateTown with an invalid password should throw an error');
} catch (err) {
// err expected
// TODO this should really check to make sure it's the *right* error, but we didn't specify
// the format of the exception :(
}
// Make sure name or vis didn't change
expectTownListMatches(await apiClient.listTowns(), pubTown1);
});
it('Updates the friendlyName and visbility as requested', async () => {
const pubTown1 = await createTownForTesting(undefined, false);
expectTownListMatches(await apiClient.listTowns(), pubTown1);
await apiClient.updateTown({
coveyTownID: pubTown1.coveyTownID,
coveyTownPassword: pubTown1.townUpdatePassword,
friendlyName: 'newName',
isPubliclyListed: true,
});
pubTown1.friendlyName = 'newName';
pubTown1.isPubliclyListed = true;
expectTownListMatches(await apiClient.listTowns(), pubTown1);
});
it('Does not update the visibility if visibility is undefined', async () => {
const pubTown1 = await createTownForTesting(undefined, true);
expectTownListMatches(await apiClient.listTowns(), pubTown1);
await apiClient.updateTown({
coveyTownID: pubTown1.coveyTownID,
coveyTownPassword: pubTown1.townUpdatePassword,
friendlyName: 'newName2',
});
pubTown1.friendlyName = 'newName2';
expectTownListMatches(await apiClient.listTowns(), pubTown1);
});
});
describe('CoveyMemberAPI', () => {
it('Throws an error if the town does not exist', async () => {
await createTownForTesting(undefined, true);
try {
await apiClient.joinTown({
userName: nanoid(),
coveyTownID: nanoid(),
});
fail('Expected an error to be thrown by joinTown but none thrown');
} catch (err) {
// OK, expected an error
// TODO this should really check to make sure it's the *right* error, but we didn't specify
// the format of the exception :(
}
});
it('Admits a user to a valid public or private town', async () => {
const pubTown1 = await createTownForTesting(undefined, true);
const privTown1 = await createTownForTesting(undefined, false);
const res = await apiClient.joinTown({
userName: nanoid(),
coveyTownID: pubTown1.coveyTownID,
});
expect(res.coveySessionToken)
.toBeDefined();
expect(res.coveyUserID)
.toBeDefined();
const res2 = await apiClient.joinTown({
userName: nanoid(),
coveyTownID: privTown1.coveyTownID,
});
expect(res2.coveySessionToken)
.toBeDefined();
expect(res2.coveyUserID)
.toBeDefined();
});
});
describe('File Related Testing', () => {
let townID:string;
let token:string;
beforeAll(async () => {
const pubTown1 = await createTownForTesting(undefined, true);
const res = await apiClient.joinTown({
userName: nanoid(),
coveyTownID: pubTown1.coveyTownID,
});
townID = pubTown1.coveyTownID;
token = res.coveySessionToken;
});
it('Check normal upload and download', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.OK)
.then(response => {
assert(response.body.response.name === 'small_test.txt')
const newName = response.body.response.fileName
request(app)
.get(`/files/${newName}`)
.expect(StatusCodes.OK)
.then(response => {
const file_text = fs.readFileSync('src/client/testFiles/small_test.txt');
assert(response.text === file_text.toString());
done();
})
})
});
it('Check block executable file', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.attach('chatFile', 'src/client/testFiles/no-op.js')
.expect(StatusCodes.UNSUPPORTED_MEDIA_TYPE, done)
});
it('Check block oversize file', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.attach('chatFile', 'src/client/testFiles/too_big_file.txt')
.expect(StatusCodes.REQUEST_TOO_LONG, done)
});
it('Check reject invalid token', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token + 'invalid')
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.UNAUTHORIZED, done)
});
it('Check reject invalid town', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID + 'invalid')
.field('token', token)
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.UNAUTHORIZED, done)
});
it('Check reject missing token', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID + 'invalid')
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.UNAUTHORIZED, done)
});
it('Check reject missing town', async (done) => {
request(app)
.post(`/files`)
.field('token', token)
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.UNAUTHORIZED, done)
});
it('Check reject missing file', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.expect(StatusCodes.BAD_REQUEST, done)
});
it('Check reject misnamed file field', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.attach('wrongName', 'src/client/testFiles/small_test.txt')
.expect(StatusCodes.BAD_REQUEST, done)
});
it('Check ignore second file', async (done) => {
request(app)
.post(`/files`)
.field('townId', townID)
.field('token', token)
.attach('chatFile', 'src/client/testFiles/small_test.txt')
.attach('chatFile', 'src/client/testFiles/too_big_file.txt')
.expect(StatusCodes.OK)
.then(response => {
assert(response.body.response.name === 'small_test.txt')
done();
})
});
it('Check get wrong file name', async (done) => {
request(app)
.get(`/files/invalid`)
.expect(StatusCodes.NOT_FOUND, done)
});
});
});