-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathserver.test.js
More file actions
145 lines (132 loc) · 3.98 KB
/
server.test.js
File metadata and controls
145 lines (132 loc) · 3.98 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import request from 'supertest'
import { Server } from '../server'
import { dbNameFromFile } from 'arango-tools'
import { ensureDatabase as ensure } from '../testUtilities'
import dbschema from '../../database.json'
const {
DB_PASS: rootPass,
DB_URL: url,
DEPTH_LIMIT: maxDepth,
COST_LIMIT: complexityCost,
SCALAR_COST: scalarCost,
OBJECT_COST: objectCost,
LIST_FACTOR: listFactor,
} = process.env
const name = dbNameFromFile(__filename)
let drop
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
// create the database so that middleware can connect
;({ drop } = await ensure({
variables: {
dbname: name,
username: 'root',
rootPassword: rootPass,
password: rootPass,
url,
},
schema: dbschema,
}))
})
afterAll(() => drop())
afterEach(() => {
consoleOutput.length = 0
})
describe('/alive', () => {
it('returns 200', async () => {
const server = await Server({})
const response = await request(server).get('/alive')
expect(response.status).toEqual(200)
})
})
describe('/ready', () => {
it('returns 200', async () => {
const server = await Server({
arango: {
db: name,
url,
as: {
username: 'root',
password: rootPass,
},
},
})
const response = await request(server).get('/ready')
expect(response.status).toEqual(200)
})
})
describe('/graphql', () => {
describe('endpoint is alive', () => {
it('returns 200', async () => {
const response = await request(
await Server({
maxDepth,
complexityCost,
scalarCost,
objectCost,
listFactor,
tracing: false,
context: () => {
return { 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(
await Server({
maxDepth: 5,
complexityCost: 1,
scalarCost: 100,
objectCost: 100,
listFactor: 100,
context: () => {
return { 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(
await Server({
maxDepth: 1,
complexityCost: 1000,
scalarCost: 1,
objectCost: 1,
listFactor: 1,
context: () => {
return { 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'))
})
})
})
})
})