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

128 lines
3.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'
import {
cisFemaleAdult34 as her,
cisMaleAdult42 as him,
} from '../../../packages/models/src/index.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)
const people = { her, him }
export const setup = async () => {
// Initial store contents
const store = {
chai,
expect,
config,
account: {
email: `test_${randomString()}@${config.tests.domain}`,
language: 'en',
password: randomString(),
people: {},
},
altaccount: {
email: `test_${randomString()}@${config.tests.domain}`,
language: 'en',
password: randomString(),
people: {},
},
icons: {
user: '🧑 ',
2022-11-17 20:41:21 +01:00
mfa: '🔒 ',
jwt: '🎫 ',
key: '🎟️ ',
2022-11-12 17:33:55 +01:00
person: '🧕 ',
pattern: '👕 ',
},
randomString,
}
store.icon = (icon1, icon2 = false) => store.icons[icon1] + (icon2 ? store.icons[icon2] : '')
2022-11-07 19:06:58 +01:00
for (const acc of ['account', 'altaccount']) {
// Get confirmation ID
let result
try {
result = await axios.post(`${store.config.api}/signup`, {
email: store[acc].email,
language: store[acc].language,
unittest: true,
})
} catch (err) {
console.log('Failed at first setup request', err)
process.exit()
}
store[acc].confirmation = result.data.confirmation
// Confirm account
try {
result = await axios.post(`${store.config.api}/confirm/signup/${store[acc].confirmation}`, {
consent: 1,
})
} catch (err) {
console.log('Failed at account confirmation request', err)
process.exit()
}
store[acc].token = result.data.token
store[acc].username = result.data.account.username
store[acc].id = result.data.account.id
// Create API key
try {
result = await axios.post(
`${store.config.api}/apikeys/jwt`,
{
name: 'Test API key',
level: 4,
expiresIn: 60,
},
{
headers: {
authorization: `Bearer ${store[acc].token}`,
},
}
)
} catch (err) {
console.log('Failed at API key creation request', err)
process.exit()
}
store[acc].apikey = result.data.apikey
// Create people key
for (const name in people) {
try {
result = await axios.post(
`${store.config.api}/people/jwt`,
{
name: `This is ${name} name`,
2022-12-29 13:40:25 -08:00
notes: `These are ${name} notes`,
measies: people[name],
},
{
headers: {
authorization: `Bearer ${store[acc].token}`,
},
}
)
} catch (err) {
console.log('Failed at API key creation request', err)
process.exit()
}
store[acc].people[name] = result.data.person
}
}
return { chai, config, expect, store }
2022-11-07 19:06:58 +01:00
}
export const teardown = async function (/*store*/) {
2022-12-23 14:43:04 +01:00
//console.log(store)
2022-11-07 19:06:58 +01:00
}