-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtestUtilities.js
More file actions
63 lines (57 loc) · 1.85 KB
/
testUtilities.js
File metadata and controls
63 lines (57 loc) · 1.85 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
import { ensure } from 'arango-tools'
import { Database } from 'arangojs'
import { tokenize } from './auth'
import { createContext } from './create-context'
export async function ensureDatabase(options) {
let variables
if (options.variables) {
variables = options.variables
variables.name = variables.dbname
} else {
variables = { ...options }
}
const systemDatabase = new Database({ url: variables.url, databaseName: '_system' })
await systemDatabase.login('root', variables.rootPassword)
const databases = await systemDatabase.listDatabases()
if (!databases.includes(variables.name)) {
try {
await systemDatabase.createDatabase(variables.name)
} catch (e) {
console.error(`Failed to create database ${variables.name}: ${e.message}`)
process.exit(1)
}
}
let ensureOptions
if (options.variables) {
ensureOptions = {
variables: options.variables,
schema: { ...options.schema },
}
} else {
ensureOptions = options
}
const ensured = await ensure(ensureOptions)
const db = new Database({ url: variables.url, databaseName: variables.name })
await db.login(variables.username || 'root', variables.password || variables.rootPassword)
return { ...ensured, db }
}
export function createUserContextGenerator({ query, db, transaction, collectionNames, i18n, secret, salt }) {
return async function createUserContext({ userKey, expiry = '60m', language = 'en', loginRequiredBool = true }) {
const signedToken = tokenize({
expiresIn: expiry,
parameters: { userKey: userKey },
secret: secret,
})
return await createContext({
query,
db,
transaction,
collections: collectionNames,
req: { headers: { authorization: signedToken } },
i18n,
language: language,
loginRequiredBool: loginRequiredBool,
salt: salt,
})
}
}