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
This commit is contained in:
Chester Sng
2019-02-22 03:33:15 +08:00
committed by Sabe Jones
parent f23dcf59ff
commit d267f09d04
3 changed files with 35 additions and 12 deletions

View File

@@ -25,9 +25,9 @@ describe('GET /heroes/:heroId', () => {
it('validates req.params.heroId', async () => { it('validates req.params.heroId', async () => {
await expect(user.get('/hall/heroes/invalidUUID')).to.eventually.be.rejected.and.eql({ await expect(user.get('/hall/heroes/invalidUUID')).to.eventually.be.rejected.and.eql({
code: 400, code: 404,
error: 'BadRequest', error: 'NotFound',
message: t('invalidReqParams'), message: t('userWithIDNotFound', {userId: 'invalidUUID'}),
}); });
}); });
@@ -40,7 +40,7 @@ describe('GET /heroes/:heroId', () => {
}); });
}); });
it('returns only necessary hero data', async () => { it('returns only necessary hero data given user id', async () => {
let hero = await generateUser({ let hero = await generateUser({
contributor: {tier: 23}, contributor: {tier: 23},
}); });
@@ -53,4 +53,18 @@ describe('GET /heroes/:heroId', () => {
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']); expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);
expect(heroRes.profile).to.have.all.keys(['name']); 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']);
});
}); });

View File

@@ -8,7 +8,7 @@
.row .row
.form.col-6(v-if='!hero.profile') .form.col-6(v-if='!hero.profile')
.form-group .form-group
input.form-control(type='text', v-model='heroID', :placeholder="$t('UUID')") input.form-control(type='text', v-model='heroID', :placeholder="'User ID or Username'")
.form-group .form-group
button.btn.btn-secondary(@click='loadHero(heroID)') button.btn.btn-secondary(@click='loadHero(heroID)')
| {{ $t('loadUser') }} | {{ $t('loadUser') }}

View File

@@ -6,6 +6,7 @@ import {
} from '../../libs/errors'; } from '../../libs/errors';
import _ from 'lodash'; import _ from 'lodash';
import apiError from '../../libs/apiError'; import apiError from '../../libs/apiError';
import validator from 'validator';
let api = {}; let api = {};
@@ -142,7 +143,7 @@ api.getHeroes = {
const heroAdminFields = 'contributor balance profile.name purchased items auth flags.chatRevoked'; const heroAdminFields = 'contributor balance profile.name purchased items auth flags.chatRevoked';
/** /**
* @api {get} /api/v3/hall/heroes/:heroId Get any user ("hero") given the UUID * @api {get} /api/v3/hall/heroes/:heroId Get any user ("hero") given the UUID or Username
* @apiParam (Path) {UUID} heroId user ID * @apiParam (Path) {UUID} heroId user ID
* @apiName GetHero * @apiName GetHero
* @apiGroup Hall * @apiGroup Hall
@@ -162,15 +163,23 @@ api.getHero = {
url: '/hall/heroes/:heroId', url: '/hall/heroes/:heroId',
middlewares: [authWithHeaders(), ensureAdmin], middlewares: [authWithHeaders(), ensureAdmin],
async handler (req, res) { async handler (req, res) {
let heroId = req.params.heroId; let validationErrors;
req.checkParams('heroId', res.t('heroIdRequired')).notEmpty();
req.checkParams('heroId', res.t('heroIdRequired')).notEmpty().isUUID(); validationErrors = req.validationErrors();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors; if (validationErrors) throw validationErrors;
let hero = await User const heroId = req.params.heroId;
.findById(heroId)
let query;
if (validator.isUUID(heroId)) {
query = {_id: heroId};
} else {
query = {'auth.local.username': heroId};
}
const hero = await User
.findOne(query)
.select(heroAdminFields) .select(heroAdminFields)
.exec(); .exec();