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

410 lines
13 KiB
JavaScript
Raw Normal View History

2022-11-12 20:05:16 +01:00
import { cat } from './cat.mjs'
2022-11-12 17:33:55 +01:00
export const setTests = async (chai, config, expect, store) => {
2022-11-12 17:33:55 +01:00
const data = {
jwt: {
name: 'Joost',
notes: 'These are them notes',
measies: {
chest: 1000,
neck: 420,
},
public: true,
test: true,
2022-11-12 20:36:47 +01:00
imperial: true,
2022-11-12 17:33:55 +01:00
},
key: {
name: 'Sorcha',
notes: 'These are also notes',
measies: {
chest: 930,
neck: 360,
},
public: false,
2022-11-12 20:05:16 +01:00
img: cat,
test: true,
2022-11-12 20:36:47 +01:00
imperial: false,
2022-11-12 17:33:55 +01:00
},
}
store.set = {
2022-11-12 21:04:33 +01:00
jwt: {},
key: {},
}
store.altset = {
jwt: {},
key: {},
}
2022-11-12 17:33:55 +01:00
for (const auth of ['jwt', 'key']) {
describe(`${store.icon('set', auth)} Set tests (${auth})`, () => {
2023-02-26 16:31:04 +01:00
it(`${store.icon('set', auth)} Should create a new set (${auth})`, (done) => {
2022-11-12 17:33:55 +01:00
chai
.request(config.api)
.post(`/sets/${auth}`)
2022-11-12 17:33:55 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.send(data[auth])
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(201)
2023-08-13 09:39:05 +02:00
expect(res.body.result).to.equal(`created`)
2022-11-12 17:33:55 +01:00
for (const [key, val] of Object.entries(data[auth])) {
if (!['measies', 'img', 'test'].includes(key)) expect(res.body.set[key]).to.equal(val)
2022-11-12 17:33:55 +01:00
}
store.set[auth] = res.body.set
2022-11-12 17:33:55 +01:00
done()
})
2022-11-12 21:19:04 +01:00
}).timeout(5000)
2022-11-12 21:04:33 +01:00
for (const field of ['name', 'notes']) {
it(`${store.icon('set', auth)} Should update the ${field} field (${auth})`, (done) => {
2022-11-12 21:04:33 +01:00
const data = {}
const val = store.set[auth][field] + '_updated'
2022-11-12 21:04:33 +01:00
data[field] = val
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
2022-11-12 21:04:33 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(
`${store.account.apikey.key}:${store.account.apikey.secret}`
).toString('base64')
)
.send(data)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
expect(res.body.set[field]).to.equal(val)
2022-11-12 21:04:33 +01:00
done()
})
})
}
2022-11-12 21:19:04 +01:00
for (const field of ['imperial', 'public']) {
it(`${store.icon('set', auth)} Should update the ${field} field (${auth})`, (done) => {
2022-11-12 21:19:04 +01:00
const data = {}
const val = !store.set[auth][field]
2022-11-12 21:19:04 +01:00
data[field] = val
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
2022-11-12 21:19:04 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(
`${store.account.apikey.key}:${store.account.apikey.secret}`
).toString('base64')
)
.send(data)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
expect(res.body.set[field]).to.equal(val)
store.set[auth][field] = val
2022-11-12 21:19:04 +01:00
done()
})
})
}
const rand = () => Math.ceil(Math.random() * 1000)
const testMeasies = {
chest: rand(),
neck: rand(),
ankle: rand(),
}
it(`${store.icon(
'set',
auth
)} Should update several measuremens at once (${auth})`, (done) => {
const data = { measies: testMeasies }
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.send(data)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
for (const m in testMeasies) {
expect(res.body.set.measies[m]).to.equal(data.measies[m])
}
done()
})
})
for (const field in testMeasies) {
2022-11-12 21:19:04 +01:00
it(`${store.icon(
'set',
2022-11-12 21:19:04 +01:00
auth
)} Should update the ${field} measurement (${auth})`, (done) => {
testMeasies[field] = rand()
const data = { measies: testMeasies }
2022-11-12 21:19:04 +01:00
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
2022-11-12 21:19:04 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(
`${store.account.apikey.key}:${store.account.apikey.secret}`
).toString('base64')
)
.send(data)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
for (const m in testMeasies) {
expect(res.body.set.measies[m]).to.equal(data.measies[m])
}
2022-11-12 21:19:04 +01:00
done()
})
})
}
it(`${store.icon(
'set',
auth
)} Should not set an non-existing measurement (${auth})`, (done) => {
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.send({
measies: {
ankle: 320,
potatoe: 12,
},
})
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
expect(res.body.set.measies.ankle).to.equal(320)
expect(typeof res.body.set.measies.potatoe).to.equal('undefined')
done()
})
})
it(`${store.icon('set', auth)} Should clear a measurement (${auth})`, (done) => {
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.send({
measies: {
chest: null,
},
})
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
expect(typeof res.body.set.measies.chest).to.equal('undefined')
done()
})
})
it(`${store.icon('set', auth)} Should read a set (${auth})`, (done) => {
chai
.request(config.api)
.get(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(200)
expect(res.body.result).to.equal(`success`)
expect(typeof res.body.set.measies).to.equal('object')
done()
})
})
it(`${store.icon(
'set',
auth
)} Should not allow reading another user's set (${auth})`, (done) => {
chai
.request(config.api)
.get(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.altaccount.token
: 'Basic ' +
new Buffer(
`${store.altaccount.apikey.key}:${store.altaccount.apikey.secret}`
).toString('base64')
)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(403)
expect(res.body.result).to.equal(`error`)
expect(res.body.error).to.equal(`insufficientAccessLevel`)
done()
})
})
it(`${store.icon(
'set',
auth
)} Should not allow updating another user's set (${auth})`, (done) => {
chai
.request(config.api)
.patch(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.altaccount.token
: 'Basic ' +
new Buffer(
`${store.altaccount.apikey.key}:${store.altaccount.apikey.secret}`
).toString('base64')
)
.send({
bio: 'I have been taken over',
})
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(403)
expect(res.body.result).to.equal(`error`)
expect(res.body.error).to.equal(`insufficientAccessLevel`)
done()
})
})
it(`${store.icon(
'set',
auth
)} Should not allow removing another user's set (${auth})`, (done) => {
chai
.request(config.api)
.delete(`/sets/${store.set[auth].id}/${auth}`)
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.altaccount.token
: 'Basic ' +
new Buffer(
`${store.altaccount.apikey.key}:${store.altaccount.apikey.secret}`
).toString('base64')
)
.end((err, res) => {
expect(err === null).to.equal(true)
expect(res.status).to.equal(403)
expect(res.body.result).to.equal(`error`)
expect(res.body.error).to.equal(`insufficientAccessLevel`)
done()
})
})
it(`${store.icon('set', auth)} Should clone a set (${auth})`, (done) => {
2022-11-14 18:45:45 +01:00
chai
.request(config.api)
.post(`/sets/${store.set[auth].id}/clone/${auth}`)
2022-11-14 18:45:45 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.account.token
: 'Basic ' +
new Buffer(`${store.account.apikey.key}:${store.account.apikey.secret}`).toString(
'base64'
)
)
.end((err, res) => {
expect(err === null).to.equal(true)
2023-08-13 09:39:05 +02:00
expect(res.status).to.equal(201)
expect(res.body.result).to.equal(`created`)
2022-11-14 18:45:45 +01:00
expect(typeof res.body.error).to.equal(`undefined`)
expect(typeof res.body.set.id).to.equal(`number`)
2022-11-14 18:45:45 +01:00
done()
})
})
2022-11-14 18:26:20 +01:00
it(`${store.icon(
'set',
2022-11-14 18:26:20 +01:00
auth
)} Should (not) clone a public set across accounts (${auth})`, (done) => {
2022-11-14 18:26:20 +01:00
chai
.request(config.api)
.post(`/sets/${store.set[auth].id}/clone/${auth}`)
2022-11-14 18:26:20 +01:00
.set(
'Authorization',
auth === 'jwt'
? 'Bearer ' + store.altaccount.token
: 'Basic ' +
new Buffer(
`${store.altaccount.apikey.key}:${store.altaccount.apikey.secret}`
).toString('base64')
)
.end((err, res) => {
if (store.set[auth].public) {
2022-11-14 18:26:20 +01:00
expect(err === null).to.equal(true)
2023-08-13 09:39:05 +02:00
expect(res.status).to.equal(201)
expect(res.body.result).to.equal(`created`)
2022-11-14 18:26:20 +01:00
expect(typeof res.body.error).to.equal(`undefined`)
expect(typeof res.body.set.id).to.equal(`number`)
2022-11-14 18:26:20 +01:00
} else {
expect(err === null).to.equal(true)
expect(res.status).to.equal(403)
expect(res.body.result).to.equal(`error`)
expect(res.body.error).to.equal(`insufficientAccessLevel`)
}
done()
})
})
// TODO:
// - Clone set
// - Clone set accross accounts of they are public
})
2022-11-12 17:33:55 +01:00
}
}