-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.test.js
More file actions
80 lines (64 loc) · 2.08 KB
/
Copy pathapi.test.js
File metadata and controls
80 lines (64 loc) · 2.08 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
const request = require('supertest');
const app = require('./app');
describe('API Tests', () => {
let server;
let messageId;
beforeAll(() => {
server = app.listen(3001);
});
afterAll((done) => {
server.close(done);
});
it('should create a new message via API', async () => {
const response = await request(app)
.post('/api/share')
.send({ text: 'Test API message' })
.expect(200);
expect(response.body).toHaveProperty('id');
expect(response.body).toHaveProperty('link');
messageId = response.body.id;
});
it('should retrieve a message via API', async () => {
const response = await request(app)
.get(`/api/view/${messageId}`)
.expect(200);
expect(response.body).toHaveProperty('message', 'Test API message');
});
it('should not find a message after it has been viewed', async () => {
await request(app)
.get(`/api/view/${messageId}`)
.expect(404);
});
it('should handle PIN protected messages', async () => {
const createResponse = await request(app)
.post('/api/share')
.send({ text: 'PIN protected message', pin: '1234' })
.expect(200);
const viewResponse = await request(app)
.get(`/api/view/${createResponse.body.id}`)
.expect(200);
expect(viewResponse.body).toHaveProperty('pinProtected', true);
const incorrectPinResponse = await request(app)
.post(`/api/view/${createResponse.body.id}`)
.send({ pin: '4321' })
.expect(400);
const correctPinResponse = await request(app)
.post(`/api/view/${createResponse.body.id}`)
.send({ pin: '1234' })
.expect(200);
expect(correctPinResponse.body).toHaveProperty('message', 'PIN protected message');
});
it('should return 400 for empty message', async () => {
await request(app)
.post('/api/share')
.send({ text: '' })
.expect(400);
});
it('should return 400 for too long message', async () => {
const longText = 'a'.repeat(10001);
await request(app)
.post('/api/share')
.send({ text: longText })
.expect(400);
});
});