-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.test.mjs
149 lines (127 loc) · 4.06 KB
/
app.test.mjs
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
import fs from 'fs'
import t from 'tap'
import nock from 'nock'
import oauth2 from 'fastify-oauth2'
import appFactory from '../lib/app.mjs'
const fakeTokenResponse = {
token: {
access_token: '111111111111111111111111111111',
expires_in: 604800,
refresh_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
scope: 'identify',
token_type: 'Bearer'
}
}
const fakeUserProfile = {
id: '424242424242424242',
username: 'Foo Bar',
avatar: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
discriminator: '5262',
public_flags: 0,
flags: 0,
email: 'mail@mail.com',
verified: true,
locale: 'it',
mfa_enabled: false
}
t.beforeEach(function (done, childTest) {
// TODO move this config
const config = {
NODE_ENV: 'test',
BASE_URL: 'http://localhost:3000',
DISCORD_CLIENT_ID: '12345678',
DISCORD_SECRET: 'XXXXXXXXXXXXXXXXX',
DB_URI: 'mongodb://localhost:27017/'
}
const server = appFactory(config)
childTest.context.server = server
childTest.teardown(() => { server.close() })
done()
})
t.test('the application starts', async t => {
await t.context.server.ready()
})
t.test('the application load the homepage', async t => {
const res = await t.context.server.inject('/')
t.equal(res.payload.substr(0, 500), fs.readFileSync('./pages/homepage.hbs', 'utf8').substr(0, 500))
})
t.test('the application has an health check', async t => {
const res = await t.context.server.inject('/health')
t.equal(res.statusCode, 200)
})
t.test('the application redirect when 404', async t => {
const res = await t.context.server.inject(`/${Math.random()}/${Math.random()}`)
t.equal(res.statusCode, 302)
})
t.test('OAUTH2 Code Flow', async t => {
await t.test('click on login', async t => {
const res = await t.context.server.inject('/auth/discord')
t.equal(res.statusCode, 302)
t.like(res.headers.location, 'discord.com')
const redirect = new URL(res.headers.location)
const oauthState = redirect.searchParams.get('state')
await t.test('succesfull login', async t => {
const codeQueryParams = `code=ABC123&state=${oauthState}`
nock(oauth2.DISCORD_CONFIGURATION.tokenHost)
.post(oauth2.DISCORD_CONFIGURATION.tokenPath)
.reply(200, fakeTokenResponse)
.get('/api/users/@me')
.times(2)
.reply(200, fakeUserProfile)
const res = await t.context.server.inject(`/auth/discord/callback?${codeQueryParams}`)
t.equal(res.statusCode, 200)
t.like(res.payload, '<h5 class="card-title">Hi Foo Bar!</h5>', 'the page is loaded')
t.equals(res.cookies.length, 1, 'one session cookie must be returned')
t.equals(res.cookies[0].name, 'sessionid', 'the session id cookie')
const resCached = await t.context.server.inject({
method: 'GET',
url: '/auth/discord',
cookies: {
[res.cookies[0].name]: res.cookies[0].value
}
})
t.equal(resCached.statusCode, 200)
t.like(resCached.payload, '<h5 class="card-title">Hi Foo Bar!</h5>', 'the page is loaded')
})
})
})
t.test('receive a callback error', async t => {
const res = await t.context.server.inject('/auth/discord/callback?code=ABC123')
t.equal(res.statusCode, 500)
t.like(res.payload, '<title>Error</title>', 'the error page is returned')
})
t.test('api sign book', async t => {
const res = await t.context.server.inject({
method: 'PUT',
url: `/api/users/${fakeUserProfile.id}`,
payload: fakeUserProfile
})
t.equal(res.statusCode, 201)
t.deepEqual(res.json(), { userId: fakeUserProfile.id })
})
t.test('api read book', async t => {
await createUser(1)
await createUser(2)
await createUser(3)
await createUser(4)
await createUser(5)
await createUser(6)
const res = await t.context.server.inject({
method: 'GET',
url: '/api/users',
query: {
offset: 2,
limit: 3
}
})
t.equal(res.statusCode, 200)
t.equals(res.json().rows.length, 3)
t.equals(res.json().total, 7)
function createUser (id) {
return t.context.server.inject({
method: 'PUT',
url: `/api/users/${id}`,
payload: fakeUserProfile
})
}
})