2022-11-10 20:09:30 +01:00
|
|
|
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
|
|
|
|
2022-11-10 20:09:30 +01:00
|
|
|
dotenv.config()
|
2022-11-07 19:06:58 +01:00
|
|
|
|
2022-11-12 17:33:55 +01:00
|
|
|
const config = verifyConfig(true)
|
2022-11-10 20:09:30 +01:00
|
|
|
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: '🎫 ',
|
2022-11-11 18:02:28 +01:00
|
|
|
key: '🎟️ ',
|
2022-11-12 17:33:55 +01:00
|
|
|
person: '🧕 ',
|
2022-11-10 20:09:30 +01:00
|
|
|
},
|
2022-11-11 18:02:28 +01:00
|
|
|
randomString,
|
2022-11-10 20:09:30 +01:00
|
|
|
}
|
|
|
|
store.icon = (icon1, icon2 = false) => store.icons[icon1] + (icon2 ? store.icons[icon2] : '')
|
2022-11-07 19:06:58 +01:00
|
|
|
|
2022-11-10 20:09:30 +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
|
|
|
})
|
2022-11-10 20:09:30 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.log('Failed at first setup request', err)
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
store.account.confirmation = result.data.confirmation
|
2022-11-08 22:41:30 +01:00
|
|
|
|
2022-11-10 20:09:30 +01:00
|
|
|
// Confirm account
|
|
|
|
try {
|
|
|
|
result = await axios.post(`${store.config.api}/confirm/signup/${store.account.confirmation}`, {
|
|
|
|
consent: 1,
|
2022-11-08 22:41:30 +01:00
|
|
|
})
|
2022-11-10 20:09:30 +01:00
|
|
|
} 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)
|
|
|
|
}
|