This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
148 lines (114 loc) · 3.56 KB
/
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
const http = require('http')
const test = require('ava')
const listen = require('test-listen')
const request = require('request-promise')
const nock = require('nock')
const qs = require('qs')
const facecampAuth = require('./index')
const serviceEndpoint = async (endpoint) => {
const service = new http.Server(facecampAuth)
const baseUrl = await listen(service)
const url = new URL(endpoint, baseUrl)
return {
baseUrl,
request: () => request(url.toString()),
close: () => service.close()
}
}
test('healtcheck', async (t) => {
const endpoint = await serviceEndpoint('/healthcheck')
const body = await endpoint.request()
t.deepEqual(JSON.parse(body), {
version: require('./package.json').version,
env: 'test'
})
endpoint.close()
})
test('app redirect', async (t) => {
const endpoint = await serviceEndpoint('/app')
nock('https://appurl.test')
.get('/')
.reply('200', 'Redirected to app')
t.deepEqual(await endpoint.request(), 'Redirected to app')
endpoint.close()
})
test('default auth', async (t) => {
const endpoint = await serviceEndpoint('/')
nock('https://slack.com')
.get('/oauth/authorize')
.query({
client_id: '1',
client_secret: '2',
redirect_uri: 'https://authhost.test/token',
scope: 'files:write:user channels:read'
})
.reply(200, 'Redirected to slack')
t.deepEqual(await endpoint.request(), 'Redirected to slack')
endpoint.close()
})
test('auth with all types', async (t) => {
const endpoint = await serviceEndpoint('/?types=channels,groups,im,mpim')
nock('https://slack.com')
.get('/oauth/authorize')
.query({
client_id: '1',
client_secret: '2',
redirect_uri: 'https://authhost.test/token',
scope:
'files:write:user channels:read groups:read users:read im:read mpim:read'
})
.reply(200, 'Redirected to slack')
t.deepEqual(await endpoint.request(), 'Redirected to slack')
endpoint.close()
})
test('auth with only im', async (t) => {
const endpoint = await serviceEndpoint('/?types=im')
nock('https://slack.com')
.get('/oauth/authorize')
.query({
client_id: '1',
client_secret: '2',
redirect_uri: 'https://authhost.test/token',
scope: 'files:write:user users:read im:read'
})
.reply(200, 'Redirected to slack')
t.deepEqual(await endpoint.request(), 'Redirected to slack')
endpoint.close()
})
test('redirect from token', async (t) => {
const endpoint = await serviceEndpoint('/token?code=123')
nock('https://slack.com')
.get('/api/oauth.access')
.query({
code: '123',
client_id: '1',
client_secret: '2',
redirect_uri: 'https://authhost.test/token'
})
.reply(200, { ok: true, access_token: 100 })
nock('https://appurl.test')
.get('/')
.query({ query: JSON.stringify({ ok: true, access_token: 100 }) })
.reply('200', 'Redirected to app')
t.deepEqual(await endpoint.request(), 'Redirected to app')
endpoint.close()
})
test('token error', async (t) => {
const endpoint = await serviceEndpoint('/token?error=badstuff')
await t.throwsAsync(endpoint.request, '403 - "Error"')
endpoint.close()
})
test('token oauth error', async (t) => {
const endpoint = await serviceEndpoint('/token?code=123')
nock('https://slack.com')
.get('/api/oauth.access')
.query({
code: '123',
client_id: '1',
client_secret: '2',
redirect_uri: 'https://authhost.test/token'
})
.reply(200, { ok: false, error: 'Slack said no' })
await t.throwsAsync(endpoint.request, '403 - "Slack said no"')
endpoint.close()
})