Files
habitica/test/api/v3/integration/hall/GET-hall_heroes_heroId.test.js
Chester Sng d267f09d04 Enable Username to be searched in Hall of Heroes - fixes #10972 (#10980)
* Add if block to search for username if not valid uuid

* Add validationError check

* Modify test case and added test case for username

* Update description of API

* Update Test

* Correct test

* Change placeholder text in heroes.vue

* Refactor code

* Add quotes

* Update hall.js
2019-02-21 13:33:15 -06:00

71 lines
2.2 KiB
JavaScript

import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
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: 404,
error: 'NotFound',
message: t('userWithIDNotFound', {userId: 'invalidUUID'}),
});
});
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 given user id', 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', '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']);
});
it('returns only necessary hero data given username', async () => {
let hero = await generateUser({
contributor: {tier: 23},
});
let heroRes = await user.get(`/hall/heroes/${hero.auth.local.username}`);
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
'_id', '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']);
});
});