add tests for hall
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
|
||||
describe('GET /hall/heroes', () => {
|
||||
it('returns all heroes sorted by -contributor.level and with correct fields', async () => {
|
||||
let nonHero = await generateUser();
|
||||
let hero1 = await generateUser({
|
||||
contributor: {level: 1},
|
||||
});
|
||||
let hero2 = await generateUser({
|
||||
contributor: {level: 3},
|
||||
});
|
||||
|
||||
let heroes = await nonHero.get('/hall/heroes');
|
||||
expect(heroes.length).to.equal(2);
|
||||
expect(heroes[0]._id).to.equal(hero2._id);
|
||||
expect(heroes[1]._id).to.equal(hero1._id);
|
||||
|
||||
expect(heroes[0]).to.have.all.keys(['_id', 'contributor', 'backer', 'profile']);
|
||||
expect(heroes[1]).to.have.all.keys(['_id', 'contributor', 'backer', 'profile']);
|
||||
|
||||
expect(heroes[0].profile).to.have.all.keys(['name']);
|
||||
expect(heroes[1].profile).to.have.all.keys(['name']);
|
||||
|
||||
expect(heroes[0].profile.name).to.equal(hero2.profile.name);
|
||||
expect(heroes[1].profile.name).to.equal(hero1.profile.name);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('GET /heroes/:heroId', () => {
|
||||
let user;
|
||||
|
||||
before(async () => {
|
||||
user = await generateUser({
|
||||
contributor: {admin: true},
|
||||
});
|
||||
});
|
||||
|
||||
it('requires the caller to be an admin', async () => {
|
||||
let nonAdmin = await generateUser();
|
||||
|
||||
await expect(nonAdmin.get(`/hall/heroes/${user._id}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('noAdminAccess'),
|
||||
});
|
||||
});
|
||||
|
||||
it('validates req.params.heroId', async () => {
|
||||
await expect(user.get(`/hall/heroes/invalidUUID`)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
});
|
||||
|
||||
it('handles non-existing heroes', async () => {
|
||||
let dummyId = generateUUID();
|
||||
await expect(user.get(`/hall/heroes/${dummyId}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('userWithIDNotFound', {userId: dummyId}),
|
||||
});
|
||||
});
|
||||
|
||||
it('returns only necessary hero data', async () => {
|
||||
let hero = await generateUser({
|
||||
contributor: {tier: 23},
|
||||
});
|
||||
let heroRes = await user.get(`/hall/heroes/${hero._id}`);
|
||||
|
||||
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
|
||||
'_id', 'balance', 'profile', 'purchased',
|
||||
'contributor', 'auth', 'items',
|
||||
]);
|
||||
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
|
||||
expect(heroRes.profile).to.have.all.keys(['name']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
resetHabiticaDB,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import { times } from 'lodash';
|
||||
|
||||
describe('GET /hall/patrons', () => {
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetHabiticaDB();
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('fails if req.query.page is not numeric', async () => {
|
||||
await expect(user.get(`/hall/patrons?page=notNumber`)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
});
|
||||
|
||||
it('returns all patrons sorted by -backer.tier and with correct fields', async () => {
|
||||
let patron1 = await generateUser({
|
||||
backer: {tier: 1},
|
||||
});
|
||||
let patron2 = await generateUser({
|
||||
backer: {tier: 3},
|
||||
});
|
||||
|
||||
let patrons = await user.get('/hall/patrons');
|
||||
expect(patrons.length).to.equal(2);
|
||||
expect(patrons[0]._id).to.equal(patron2._id);
|
||||
expect(patrons[1]._id).to.equal(patron1._id);
|
||||
|
||||
expect(patrons[0]).to.have.all.keys(['_id', 'contributor', 'backer', 'profile']);
|
||||
expect(patrons[1]).to.have.all.keys(['_id', 'contributor', 'backer', 'profile']);
|
||||
|
||||
expect(patrons[0].profile).to.have.all.keys(['name']);
|
||||
expect(patrons[1].profile).to.have.all.keys(['name']);
|
||||
|
||||
expect(patrons[0].profile.name).to.equal(patron2.profile.name);
|
||||
expect(patrons[1].profile.name).to.equal(patron1.profile.name);
|
||||
});
|
||||
|
||||
it('returns only first 50 patrons per request, more if req.query.page is passed', async () => {
|
||||
await Promise.all(times(53, n => {
|
||||
return generateUser({backer: {tier: n}});
|
||||
}));
|
||||
|
||||
let patrons = await user.get('/hall/patrons');
|
||||
expect(patrons.length).to.equal(50);
|
||||
|
||||
let morePatrons = await user.get('/hall/patrons?page=1');
|
||||
expect(morePatrons.length).to.equal(2);
|
||||
expect(morePatrons[0].backer.tier).to.equal(2);
|
||||
expect(morePatrons[1].backer.tier).to.equal(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,148 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-v3-integration.helper';
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
describe('PUT /heroes/:heroId', () => {
|
||||
let user;
|
||||
|
||||
before(async () => {
|
||||
user = await generateUser({
|
||||
contributor: {admin: true},
|
||||
});
|
||||
});
|
||||
|
||||
it('requires the caller to be an admin', async () => {
|
||||
let nonAdmin = await generateUser();
|
||||
|
||||
await expect(nonAdmin.put(`/hall/heroes/${user._id}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('noAdminAccess'),
|
||||
});
|
||||
});
|
||||
|
||||
it('validates req.params.heroId', async () => {
|
||||
await expect(user.put(`/hall/heroes/invalidUUID`)).to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: t('invalidReqParams'),
|
||||
});
|
||||
});
|
||||
|
||||
it('handles non-existing heroes', async () => {
|
||||
let dummyId = generateUUID();
|
||||
await expect(user.put(`/hall/heroes/${dummyId}`)).to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('userWithIDNotFound', {userId: dummyId}),
|
||||
});
|
||||
});
|
||||
|
||||
it('updates contributor level, balance, ads, blocked', async () => {
|
||||
let hero = await generateUser();
|
||||
let heroRes = await user.put(`/hall/heroes/${hero._id}`, {
|
||||
balance: 3,
|
||||
contributor: {level: 1},
|
||||
purchased: {ads: true},
|
||||
auth: {blocked: true},
|
||||
});
|
||||
|
||||
// test response
|
||||
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
|
||||
'_id', 'balance', 'profile', 'purchased',
|
||||
'contributor', 'auth', 'items',
|
||||
]);
|
||||
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
|
||||
expect(heroRes.profile).to.have.all.keys(['name']);
|
||||
|
||||
// test response values
|
||||
expect(heroRes.balance).to.equal(3 + 0.75); // 3+0.75 for first contrib level
|
||||
expect(heroRes.contributor.level).to.equal(1);
|
||||
expect(heroRes.purchased.ads).to.equal(true);
|
||||
expect(heroRes.auth.blocked).to.equal(true);
|
||||
// test hero values
|
||||
await hero.sync();
|
||||
expect(hero.balance).to.equal(3 + 0.75); // 3+0.75 for first contrib level
|
||||
expect(hero.contributor.level).to.equal(1);
|
||||
expect(hero.flags.contributor).to.equal(true);
|
||||
expect(hero.purchased.ads).to.equal(true);
|
||||
expect(hero.auth.blocked).to.equal(true);
|
||||
});
|
||||
|
||||
it('updates contributor level', async () => {
|
||||
let hero = await generateUser({
|
||||
contributor: {level: 5},
|
||||
});
|
||||
let heroRes = await user.put(`/hall/heroes/${hero._id}`, {
|
||||
contributor: {level: 6},
|
||||
});
|
||||
|
||||
// test response
|
||||
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
|
||||
'_id', 'balance', 'profile', 'purchased',
|
||||
'contributor', 'auth', 'items',
|
||||
]);
|
||||
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
|
||||
expect(heroRes.profile).to.have.all.keys(['name']);
|
||||
|
||||
// test response values
|
||||
expect(heroRes.balance).to.equal(1); // 0+1 for sixth contrib level
|
||||
expect(heroRes.contributor.level).to.equal(6);
|
||||
expect(heroRes.items.pets['Dragon-Hydra']).to.equal(5);
|
||||
// test hero values
|
||||
await hero.sync();
|
||||
expect(hero.balance).to.equal(1); // 0+1 for sixth contrib level
|
||||
expect(hero.contributor.level).to.equal(6);
|
||||
expect(hero.flags.contributor).to.equal(true);
|
||||
expect(hero.items.pets['Dragon-Hydra']).to.equal(5);
|
||||
});
|
||||
|
||||
it('updates contributor data', async () => {
|
||||
let hero = await generateUser({
|
||||
contributor: {level: 5},
|
||||
});
|
||||
let heroRes = await user.put(`/hall/heroes/${hero._id}`, {
|
||||
contributor: {text: 'Astronaut'},
|
||||
});
|
||||
|
||||
// test response
|
||||
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
|
||||
'_id', 'balance', 'profile', 'purchased',
|
||||
'contributor', 'auth', 'items',
|
||||
]);
|
||||
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
|
||||
expect(heroRes.profile).to.have.all.keys(['name']);
|
||||
|
||||
// test response values
|
||||
expect(heroRes.contributor.level).to.equal(5); // doesn't modify previous values
|
||||
expect(heroRes.contributor.text).to.equal('Astronaut');
|
||||
// test hero values
|
||||
await hero.sync();
|
||||
expect(hero.contributor.level).to.equal(5); // doesn't modify previous values
|
||||
expect(hero.contributor.text).to.equal('Astronaut');
|
||||
});
|
||||
|
||||
it('updates items', async () => {
|
||||
let hero = await generateUser();
|
||||
let heroRes = await user.put(`/hall/heroes/${hero._id}`, {
|
||||
itemPath: 'items.special.snowball',
|
||||
itemVal: 5,
|
||||
});
|
||||
|
||||
// test response
|
||||
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
|
||||
'_id', 'balance', 'profile', 'purchased',
|
||||
'contributor', 'auth', 'items',
|
||||
]);
|
||||
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
|
||||
expect(heroRes.profile).to.have.all.keys(['name']);
|
||||
|
||||
// test response values
|
||||
expect(heroRes.items.special.snowball).to.equal(5);
|
||||
// test hero values
|
||||
await hero.sync();
|
||||
expect(hero.items.special.snowball).to.equal(5);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user