2022-11-12 20:05:16 +01:00
|
|
|
import { cat } from './cat.mjs'
|
2022-11-12 17:33:55 +01:00
|
|
|
|
|
|
|
export const personTests = async (chai, config, expect, store) => {
|
|
|
|
const data = {
|
|
|
|
jwt: {
|
|
|
|
name: 'Joost',
|
|
|
|
notes: 'These are them notes',
|
|
|
|
measies: {
|
|
|
|
chest: 1000,
|
|
|
|
neck: 420,
|
|
|
|
},
|
|
|
|
public: true,
|
2022-11-12 20:05:16 +01:00
|
|
|
unittest: 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,
|
|
|
|
unittest: true,
|
2022-11-12 20:36:47 +01:00
|
|
|
imperial: false,
|
2022-11-12 17:33:55 +01:00
|
|
|
},
|
|
|
|
}
|
2022-11-12 21:04:33 +01:00
|
|
|
store.person = {
|
|
|
|
jwt: {},
|
|
|
|
key: {},
|
|
|
|
}
|
2022-11-14 17:50:34 +01:00
|
|
|
store.altperson = {
|
|
|
|
jwt: {},
|
|
|
|
key: {},
|
|
|
|
}
|
2022-11-12 17:33:55 +01:00
|
|
|
|
|
|
|
for (const auth of ['jwt', 'key']) {
|
|
|
|
describe(`${store.icon('person', auth)} Person tests (${auth})`, () => {
|
2022-11-12 21:04:33 +01:00
|
|
|
step(`${store.icon('person', auth)} Should create a new person (${auth})`, (done) => {
|
2022-11-12 17:33:55 +01:00
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.post(`/people/${auth}`)
|
|
|
|
.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)
|
|
|
|
expect(res.body.result).to.equal(`success`)
|
|
|
|
for (const [key, val] of Object.entries(data[auth])) {
|
2022-11-12 20:05:16 +01:00
|
|
|
if (!['measies', 'img', 'unittest'].includes(key))
|
|
|
|
expect(res.body.person[key]).to.equal(val)
|
2022-11-12 17:33:55 +01:00
|
|
|
}
|
2022-11-12 21:04:33 +01:00
|
|
|
store.person[auth] = res.body.person
|
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']) {
|
2022-11-12 21:19:04 +01:00
|
|
|
it(`${store.icon('person', auth)} Should update the ${field} field (${auth})`, (done) => {
|
2022-11-12 21:04:33 +01:00
|
|
|
const data = {}
|
|
|
|
const val = store.person[auth][field] + '_updated'
|
|
|
|
data[field] = val
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[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.person[field]).to.equal(val)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-11-12 21:19:04 +01:00
|
|
|
|
|
|
|
for (const field of ['imperial', 'public']) {
|
|
|
|
it(`${store.icon('person', auth)} Should update the ${field} field (${auth})`, (done) => {
|
|
|
|
const data = {}
|
2022-11-14 18:26:20 +01:00
|
|
|
const val = !store.person[auth][field]
|
2022-11-12 21:19:04 +01:00
|
|
|
data[field] = val
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[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.person[field]).to.equal(val)
|
2022-11-14 18:26:20 +01:00
|
|
|
store.person[auth][field] = val
|
2022-11-12 21:19:04 +01:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const field of ['chest', 'neck', 'ankle']) {
|
|
|
|
it(`${store.icon(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should update the ${field} measurement (${auth})`, (done) => {
|
|
|
|
const data = { measies: {} }
|
|
|
|
const val = Math.ceil(Math.random() * 1000)
|
|
|
|
data.measies[field] = val
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[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.person.measies[field]).to.equal(val)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-14 17:50:34 +01:00
|
|
|
it(`${store.icon(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should not set an non-existing measurement (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[auth].id}/${auth}`)
|
2022-11-14 17:50:34 +01:00
|
|
|
.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.person.measies.ankle).to.equal(320)
|
|
|
|
expect(typeof res.body.person.measies.potatoe).to.equal('undefined')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it(`${store.icon('person', auth)} Should clear a measurement (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[auth].id}/${auth}`)
|
2022-11-14 17:50:34 +01:00
|
|
|
.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.person.measies.chest).to.equal('undefined')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it(`${store.icon('person', auth)} Should read a person (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.get(`/people/${store.person[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.person.measies).to.equal('object')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it(`${store.icon(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should not allow reading another user's person (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.get(`/people/${store.person[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(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should not allow updating another user's person (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
2022-12-23 15:12:24 +01:00
|
|
|
.patch(`/people/${store.person[auth].id}/${auth}`)
|
2022-11-14 17:50:34 +01:00
|
|
|
.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(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should not allow removing another user's person (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.delete(`/people/${store.person[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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-11-14 18:45:45 +01:00
|
|
|
it(`${store.icon('person', auth)} Should clone a person (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.post(`/people/${store.person[auth].id}/clone/${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.error).to.equal(`undefined`)
|
|
|
|
expect(typeof res.body.person.id).to.equal(`number`)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2022-11-14 18:26:20 +01:00
|
|
|
|
|
|
|
it(`${store.icon(
|
|
|
|
'person',
|
|
|
|
auth
|
|
|
|
)} Should (not) clone a public person across accounts (${auth})`, (done) => {
|
|
|
|
chai
|
|
|
|
.request(config.api)
|
|
|
|
.post(`/people/${store.person[auth].id}/clone/${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) => {
|
|
|
|
if (store.person[auth].public) {
|
|
|
|
expect(err === null).to.equal(true)
|
|
|
|
expect(res.status).to.equal(200)
|
|
|
|
expect(res.body.result).to.equal(`success`)
|
|
|
|
expect(typeof res.body.error).to.equal(`undefined`)
|
|
|
|
expect(typeof res.body.person.id).to.equal(`number`)
|
|
|
|
} 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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-11-14 17:50:34 +01:00
|
|
|
// TODO:
|
|
|
|
// - Clone person
|
|
|
|
// - Clone person accross accounts of they are public
|
|
|
|
})
|
2022-11-12 17:33:55 +01:00
|
|
|
}
|
|
|
|
}
|