1
0
Fork 0
freesewing/sites/backend/tests/shared.mjs

89 lines
2.1 KiB
JavaScript
Raw Normal View History

import dotenv from 'dotenv'
import axios from 'axios'
import chai from 'chai'
import http from 'chai-http'
import { verifyConfig } from '../src/config.mjs'
import { randomString } from '../src/utils/crypto.mjs'
2022-11-07 19:06:58 +01:00
dotenv.config()
2022-11-07 19:06:58 +01:00
2022-11-12 17:33:55 +01:00
const config = verifyConfig(true)
const expect = chai.expect
chai.use(http)
export const setup = async () => {
// Initial store contents
const store = {
chai,
expect,
config,
account: {
email: `test_${randomString()}@${config.tests.domain}`,
language: 'en',
password: randomString(),
},
icons: {
user: '🧑 ',
jwt: '🎫 ',
key: '🎟️ ',
2022-11-12 17:33:55 +01:00
person: '🧕 ',
},
randomString,
}
store.icon = (icon1, icon2 = false) => store.icons[icon1] + (icon2 ? store.icons[icon2] : '')
2022-11-07 19:06:58 +01:00
// Get confirmation ID
let result
try {
result = await axios.post(`${store.config.api}/signup`, {
email: store.account.email,
language: store.account.language,
unittest: true,
2022-11-07 19:06:58 +01:00
})
} catch (err) {
console.log('Failed at first setup request', err)
process.exit()
}
store.account.confirmation = result.data.confirmation
// Confirm account
try {
result = await axios.post(`${store.config.api}/confirm/signup/${store.account.confirmation}`, {
consent: 1,
})
} catch (err) {
console.log('Failed at account confirmation request', err)
process.exit()
}
store.account.token = result.data.token
store.account.username = result.data.account.username
store.account.userid = result.data.account.id
// Create API key
try {
result = await axios.post(
`${store.config.api}/apikey/jwt`,
{
name: 'Test API key',
level: 4,
expiresIn: 60,
},
{
headers: {
authorization: `Bearer ${store.account.token}`,
},
}
)
} catch (err) {
console.log('Failed at API key creation request', err)
process.exit()
}
store.account.apikey = result.data.apikey
return { chai, config, expect, store }
2022-11-07 19:06:58 +01:00
}
export const teardown = async function (store) {
console.log(store)
}