forked from dilpreetj/JavaScript-API-Automation-Tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.js
More file actions
87 lines (71 loc) · 1.99 KB
/
posts.js
File metadata and controls
87 lines (71 loc) · 1.99 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
require('dotenv').config();
const faker = require('faker');
import request from '../config/supertest';
import { expect } from 'chai';
const {
createRandomUser,
createRandomUserWithFaker,
} = require('../helper/user');
const TOKEN = process.env.USER_TOKEN;
describe.only('Posts', () => {
let user, postId;
before(async () => {
// user = await createRandomUser();
user = await createRandomUserWithFaker();
});
after(() => {
// clean up
// delete a user
});
describe('POST', () => {
it('/posts', async () => {
const data = {
user_id: user.id,
title: faker.lorem.sentence(),
body: faker.lorem.paragraphs(),
};
const res = await request
.post('posts')
.set('Authorization', `Bearer ${TOKEN}`)
.send(data);
expect(res.body.data).to.deep.include(data);
postId = res.body.data.id;
});
// dependent on previous test
it('posts/:id', async () => {
if (postId) {
await request
.get(`posts/${postId}`)
.set('Authorization', `Bearer ${TOKEN}`)
.expect(200);
} else {
throw new Error(`postId is invalid - ${postId}`);
}
});
});
describe('Negative Tests', () => {
it('422 Data validation failed', async () => {
const data = {
user_id: user.id,
title: '',
body: faker.lorem.paragraphs(),
};
const res = await request
.post(`posts`)
.set('Authorization', `Bearer ${TOKEN}`)
.send(data);
expect(res.body.code).to.eq(422);
expect(res.body.data[0].message).to.eq("can't be blank");
});
it('401 Authentication failed', async () => {
const data = {
user_id: user.id,
title: faker.lorem.sentence(),
body: faker.lorem.paragraphs(),
};
const res = await request.post(`posts`).send(data);
expect(res.body.code).to.eq(401);
expect(res.body.data.message).to.eq('Authentication failed');
});
});
});