This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtests.js
More file actions
67 lines (62 loc) · 2.18 KB
/
Copy pathtests.js
File metadata and controls
67 lines (62 loc) · 2.18 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
const { test } = require('@ianwalter/bff')
const { requester } = require('@ianwalter/requester')
const { createExpressServer } = require('@ianwalter/test-server')
const session = require('express-session')
const { csrfGeneration, csrfValidation, setLogLevel } = require('.')
const sessionMiddleware = session({
secret: 'Booksmart Devil',
resave: false,
saveUninitialized: false
})
// Set CSRF middleware log level to debug.
setLogLevel('debug')
test(
'GET method is allowed to pass through without a CSRF header'
)(async ({ expect }) => {
const server = await createExpressServer()
const message = 'One chance to move you'
server.use(sessionMiddleware)
server.use(csrfGeneration)
server.use(csrfValidation)
server.get('/', (req, res) => res.json({ message }))
const response = await requester.get(server.url)
expect(response.statusCode).toBe(200)
expect(response.body.message).toBe(message)
await server.close()
})
test.skip(
'POST method is not allowed to pass through without a CSRF header'
)(async ({ expect }) => {
const server = await createExpressServer()
server.use(sessionMiddleware)
server.use(csrfGeneration)
server.use(csrfValidation)
server.post('/', (req, res) => res.status(204).end())
server.useErrorMiddleware()
const response = await requester.post(server.url)
expect(response.statusCode).toBe(401)
await server.close()
})
test(
'POST method is allowed to pass through with a valid CSRF header'
)(async ({ expect }) => {
const server = await createExpressServer()
const message = "What's tne scoop, Cook?"
server.use(sessionMiddleware)
server.use(csrfGeneration)
server.use(csrfValidation)
server.get('/', (req, res) => {
res.json({ csrfToken: req.generateCsrfToken() })
})
server.post('/message', (req, res) => res.status(201).json({ message }))
server.useErrorMiddleware()
let response = await requester.get(server.url)
const headers = {
'csrf-token': response.body.csrfToken,
cookie: response.headers['set-cookie']
}
const options = { headers, body: { message } }
response = await requester.post(`${server.url}/message`, options)
expect(response.statusCode).toBe(201)
expect(response.body.message).toBe(message)
})