forked from Brewskey/spark-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebhooksController.test.js
150 lines (121 loc) · 4.05 KB
/
WebhooksController.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
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
/* eslint-disable */
import type { Webhook, WebhookMutator } from '../src/types';
import test from 'ava';
import request from 'supertest';
import ouathClients from '../src/oauthClients.json';
import app from './setup/testApp';
import settings from './setup/settings';
import TestData from './setup/TestData';
const container = app.container;
const WEBHOOK_MODEL: WebhookMutator = {
event: 'testEvent',
requestType: 'GET',
url: 'http://webhooktest.com/',
};
let testUser;
let userToken;
let testWebhook;
test.before(async () => {
const USER_CREDENTIALS = TestData.getUser();
const userResponse = await request(app)
.post('/v1/users')
.send(USER_CREDENTIALS);
testUser = await container
.constitute('IUserRepository')
.getByUsername(USER_CREDENTIALS.username);
const tokenResponse = await request(app)
.post('/oauth/token')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({
client_id: ouathClients[0].clientId,
client_secret: ouathClients[0].clientSecret,
grant_type: 'password',
password: USER_CREDENTIALS.password,
username: USER_CREDENTIALS.username,
});
userToken = tokenResponse.body.access_token;
if (!userToken) {
throw new Error('test user creation fails');
}
});
test.serial('should create a new webhook object', async t => {
const response = await request(app)
.post('/v1/webhooks')
.query({ access_token: userToken })
.send({
event: WEBHOOK_MODEL.event,
requestType: WEBHOOK_MODEL.requestType,
url: WEBHOOK_MODEL.url,
});
testWebhook = response.body;
t.is(response.status, 200);
t.truthy(testWebhook.id && testWebhook.event && testWebhook.url);
});
test("should throw an error if event isn't provided", async t => {
const response = await request(app)
.post('/v1/webhooks')
.query({ access_token: userToken })
.send({
requestType: WEBHOOK_MODEL.requestType,
url: WEBHOOK_MODEL.url,
});
t.is(response.status, 400);
t.is(response.body.error, 'no event name provided');
});
test("should throw an error if url isn't provided", async t => {
const response = await request(app)
.post('/v1/webhooks')
.query({ access_token: userToken })
.send({
event: WEBHOOK_MODEL.event,
requestType: WEBHOOK_MODEL.requestType,
});
t.is(response.status, 400);
t.is(response.body.error, 'no url provided');
});
test("should throw an error if requestType isn't provided", async t => {
const response = await request(app)
.post('/v1/webhooks')
.query({ access_token: userToken })
.send({
event: WEBHOOK_MODEL.event,
url: WEBHOOK_MODEL.url,
});
t.is(response.status, 400);
t.is(response.body.error, 'no requestType provided');
});
test.serial('should return all webhooks', async t => {
const response = await request(app)
.get('/v1/webhooks')
.query({ access_token: userToken });
const webhooks = response.body;
t.is(response.status, 200);
t.truthy(Array.isArray(webhooks) && webhooks.length > 0);
});
test.serial('should return webhook object by id', async t => {
const response = await request(app)
.get(`/v1/webhooks/${testWebhook.id}`)
.query({ access_token: userToken });
t.is(response.status, 200);
t.is(testWebhook.id, response.body.id);
t.is(testWebhook.event, response.body.event);
t.is(testWebhook.url, response.body.url);
});
test.serial('should delete webhook', async t => {
const deleteResponse = await request(app)
.delete(`/v1/webhooks/${testWebhook.id}`)
.query({ access_token: userToken });
t.is(deleteResponse.status, 200);
const allWebhooksResponse = await request(app)
.get('/v1/webhooks')
.query({ access_token: userToken });
t.is(allWebhooksResponse.status, 200);
const webhooks = allWebhooksResponse.body;
t.falsy(
webhooks.some((webhook: Webhook): boolean => webhook.id === testWebhook.id),
);
});
test.after.always(async (): Promise<void> => {
await container.constitute('IWebhookRepository').deleteByID(testWebhook.id);
await container.constitute('IUserRepository').deleteByID(testUser.id);
});