forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.js
More file actions
121 lines (112 loc) · 3.34 KB
/
server.test.js
File metadata and controls
121 lines (112 loc) · 3.34 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
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
import request from 'supertest'
import { Server } from '../server'
const {
DEPTH_LIMIT: maxDepth,
COST_LIMIT: complexityCost,
SCALAR_COST: scalarCost,
OBJECT_COST: objectCost,
LIST_FACTOR: listFactor,
} = process.env
describe('parse server', () => {
const consoleOutput = []
const mockedLog = (output) => consoleOutput.push(output)
const mockedWarn = (output) => consoleOutput.push(output)
beforeAll(async () => {
console.log = mockedLog
console.warn = mockedWarn
})
afterEach(() => {
consoleOutput.length = 0
})
describe('/alive', () => {
it('returns 200', async () => {
const response = await request(Server({ query: jest.fn() })).get('/alive')
expect(response.status).toEqual(200)
})
})
describe('/ready', () => {
it('returns 200', async () => {
const response = await request(Server({ query: jest.fn() })).get('/ready')
expect(response.status).toEqual(200)
})
})
describe('/graphql', () => {
describe('endpoint is alive', () => {
it('returns 200', async () => {
const response = await request(
Server({
maxDepth,
complexityCost,
scalarCost,
objectCost,
listFactor,
tracing: false,
context: {
query: jest.fn(),
collections: jest.fn(),
transaction: jest.fn(),
},
}),
)
.post('/graphql')
.set('Accept', 'application/json')
.send({ query: '{__schema {types {kind}}}' })
expect(response.status).toEqual(200)
})
})
describe('validation rule is broken', () => {
describe('query cost is too high', () => {
it('returns an error message', async () => {
const response = await request(
Server({
maxDepth,
complexityCost: 1,
scalarCost: 100,
objectCost: 100,
listFactor: 100,
context: {
query: jest.fn(),
collections: jest.fn(),
transaction: jest.fn(),
},
}),
)
.post('/graphql')
.set('Accept', 'application/json')
.send({ query: '{__schema {types {kind}}}' })
expect(response.status).toEqual(400)
expect(response.text).toEqual(
expect.stringContaining('Query error, query is too complex.'),
)
})
})
describe('query depth is too high', () => {
it('returns an error message', async () => {
const response = await request(
Server({
maxDepth: 1,
complexityCost: 1000,
scalarCost: 1,
objectCost: 1,
listFactor: 1,
context: {
query: jest.fn(),
collections: jest.fn(),
transaction: jest.fn(),
},
}),
)
.post('/graphql')
.set('Accept', 'application/json')
.send({
query: '{findVerifiedDomains (first: 5) { edges { node { id }}}}',
})
expect(response.status).toEqual(400)
expect(response.text).toEqual(
expect.stringContaining('exceeds maximum operation depth'),
)
})
})
})
})
})