-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathauthentication.test.js
158 lines (125 loc) · 3.63 KB
/
authentication.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
151
152
153
154
155
156
157
158
'use strict'
const { test } = require('tap')
const plugin = require('./authentication')
const axios = require('axios')
const fastify = require('fastify')
test('should be able to login', async (t) => {
t.plan(3)
const app = await server()
const { port } = app.server.address()
const result0 = await performLogin(port, 'abcdef')
const result1 = await toHomePage(port, result0.sessionCookie)
t.ok(result0.body.includes('logged in'))
t.ok(result1.body.includes('logged in'))
t.equal(result1.sessionCookie, result0.sessionCookie)
app.close()
})
test('should not be able to login with wrong password', async (t) => {
t.plan(2)
const app = await server()
const { port } = app.server.address()
const { location, statusCode } = await performLogin(port, '123456')
t.equal(location, '/login')
t.equal(statusCode, 401)
app.close()
})
test('should be not logged in', async (t) => {
t.plan(2)
const app = await server()
const { port } = app.server.address()
// login route
const { sessionCookie, body } = await requestPath(port)
t.ok(body.includes('please login'))
t.ok(sessionCookie.includes('sessionId'))
app.close()
})
test('should be able to logout', async (t) => {
t.plan(4)
const app = await server()
const { port } = app.server.address()
const result0 = await performLogin(port, 'abcdef')
const result1 = await toHomePage(port, result0.sessionCookie)
const result2 = await logout(port, result0.sessionCookie)
const result3 = await toHomePage(port, result2.sessionCookie)
t.ok(result1.body.includes('logged in'))
t.ok(result3.body.includes('please login'))
t.ok(result3.sessionCookie === result2.sessionCookie)
t.ok(result0.sessionCookie !== result2.sessionCookie)
app.close()
})
test('should be able to call logout if not logged in', async (t) => {
t.plan(2)
const app = await server()
const { port } = app.server.address()
// logout route
const result0 = await requestPath(port, 'logout')
const result1 = await toHomePage(port, result0.sessionCookie)
t.ok(result1.body.includes('please login'))
t.ok(result1.sessionCookie === result0.sessionCookie)
app.close()
})
test('should be able to request login page', async (t) => {
t.plan(1)
const app = await server()
const { port } = app.server.address()
const { statusCode } = await toLoginPage(port)
t.equal(statusCode, 200)
app.close()
})
async function server() {
const app = fastify()
app.register(plugin)
app.server.unref()
await app.listen()
return app
}
async function toHomePage(port, cookie) {
return request({
url: `http://localhost:${port}/`,
method: 'GET',
headers: { cookie }
});
}
async function performLogin(port, password) {
return request({
url: `http://localhost:${port}/login`,
method: 'POST',
data: { email: 'test@test.de', password },
});
}
async function toLoginPage(port) {
return request({
url: `http://localhost:${port}/login`,
method: 'GET'
});
}
async function logout(port, cookie) {
return request({
url: `http://localhost:${port}/logout`,
method: 'GET',
headers: { cookie }
});
}
async function requestPath(port, path = '') {
return request({
url: `http://localhost:${port}/${path}`,
method: 'GET'
});
}
async function request (options) {
try {
const res = await axios.request(options)
return {
statusCode: res.status,
body: res.data,
sessionCookie: res.headers['set-cookie'][0],
location: res.request.res.responseUrl
}
} catch (err) {
return {
body: err.response?.data,
statusCode: err.response?.status,
location: err.response?.headers?.location
}
}
}