-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
71 lines (60 loc) · 3.44 KB
/
app.test.js
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
require('dotenv').config();
const request = require('supertest');
const jwt = require('jsonwebtoken');
const app = require('./app');
describe('API Test', () => {
const token = 'Bearer ' + jwt.sign({ username: '[email protected]' }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15m' });
const writer = 'Bearer ' + jwt.sign({ username: '[email protected]' }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15m' });
const approvedWriter = 'Bearer ' + jwt.sign({ username: '[email protected]' }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15m' });
const commenter = 'Bearer ' + jwt.sign({ username: '[email protected]' }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '15m' });
test('CRUD Post', async () => {
await request(app).get('/post').expect(200);
await request(app).get('/post/1').expect(200);
await request(app).post('/post').expect(401);
await request(app).put('/post/1').expect(401);
await request(app).delete('/post/1').expect(401);
await request(app).post('/post').set('Authorization', token).expect(200);
await request(app).put('/post/1').set('Authorization', token).expect(200);
await request(app).delete('/post/1').set('Authorization', token).expect(200);
});
test('CRUD Post by writer and commenter', async () => {
await request(app).post('/post').send({
published: false,
}).set('Authorization', writer).expect(200);
await request(app).put('/post/1').send({
published: false,
}).set('Authorization', writer).expect(200);
await request(app).delete('/post/1').set('Authorization', writer).expect(200);
await request(app).post('/post').set('Authorization', commenter).expect(403);
await request(app).put('/post/1').set('Authorization', commenter).expect(403);
await request(app).delete('/post/1').set('Authorization', commenter).expect(403);
});
test('CRUD Post by approved writer', async () => {
await request(app).post('/post').send({
published: true,
}).set('Authorization', approvedWriter).expect(200);
await request(app).put('/post/1').send({
published: true,
}).set('Authorization', approvedWriter).expect(200);
});
test('CRUD Author', async () => {
await request(app).get('/author').expect(200);
await request(app).get('/author/1').expect(200);
await request(app).post('/author').expect(401);
await request(app).put('/author/1').expect(401);
await request(app).delete('/author/1').expect(401);
await request(app).post('/author').set('Authorization', token).expect(200);
await request(app).put('/author/1').set('Authorization', token).expect(200);
await request(app).delete('/author/1').set('Authorization', token).expect(200);
});
test('CRUD Comment', async () => {
await request(app).get('/comment').expect(200);
await request(app).get('/comment/1').expect(200);
await request(app).post('/comment').expect(401);
await request(app).put('/comment/1').expect(401);
await request(app).delete('/comment/1').expect(401);
await request(app).post('/comment').set('Authorization', token).expect(200);
await request(app).put('/comment/1').set('Authorization', token).expect(200);
await request(app).delete('/comment/1').set('Authorization', token).expect(200);
});
});