Ported addPushDevice. Added unit tests. Added addPushDevice route. Added integration tests

This commit is contained in:
Keith Holliday
2016-04-10 09:45:56 -05:00
parent 7a24bceac9
commit 42ef779b46
7 changed files with 157 additions and 13 deletions

View File

@@ -164,5 +164,9 @@
"cannotRevive": "Cannot revive if not dead", "cannotRevive": "Cannot revive if not dead",
"rebirthComplete": "You have been reborn!", "rebirthComplete": "You have been reborn!",
"petNotOwned": "You do not own this pet.", "petNotOwned": "You do not own this pet.",
"rerollComplete": "Reroll complete!" "rerollComplete": "Reroll complete!",
"resetComplete": "Reset has completed",
"regIdRequired": "RegId is required",
"pushDeviceAdded": "Push device added successfully",
"pushDeviceAlreadyAdded": "The user already has the push device"
} }

View File

@@ -119,6 +119,7 @@ import blockUser from './ops/blockUser';
import clearPMs from './ops/clearPMs'; import clearPMs from './ops/clearPMs';
import deletePM from './ops/deletePM'; import deletePM from './ops/deletePM';
import reroll from './ops/reroll'; import reroll from './ops/reroll';
import addPushDevice from './ops/addPushDevice';
api.ops = { api.ops = {
scoreTask, scoreTask,
@@ -154,6 +155,7 @@ api.ops = {
clearPMs, clearPMs,
deletePM, deletePM,
reroll, reroll,
addPushDevice,
}; };
import handleTwoHanded from './fns/handleTwoHanded'; import handleTwoHanded from './fns/handleTwoHanded';

View File

@@ -1,20 +1,43 @@
import _ from 'lodash'; import _ from 'lodash';
import i18n from '../i18n';
import splitWhitespace from '../libs/splitWhitespace';
import {
BadRequest,
NotAuthorized,
} from '../libs/errors';
module.exports = function addPushDevice (user, req = {}) {
let regId = _.get(req, 'body.regId');
if (!regId) throw new BadRequest(i18n.t('regIdRequired', req.language));
let type = _.get(req, 'body.type');
if (!type) throw new BadRequest(i18n.t('typeRequired', req.language));
module.exports = function(user, req, cb) {
var i, item, pd;
if (!user.pushDevices) { if (!user.pushDevices) {
user.pushDevices = []; user.pushDevices = [];
} }
pd = user.pushDevices;
item = { let pushDevices = user.pushDevices;
regId: req.body.regId,
type: req.body.type let item = {
regId,
type,
}; };
i = _.findIndex(pd, {
regId: item.regId let indexOfPushDevice = _.findIndex(pushDevices, {
regId: item.regId,
}); });
if (i === -1) {
pd.push(item); if (indexOfPushDevice !== -1) {
throw new NotAuthorized(i18n.t('pushDeviceAlreadyAdded', req.language));
} }
return typeof cb === "function" ? cb(null, user.pushDevices) : void 0;
pushDevices.push(item);
let response = {
data: _.pick(user, splitWhitespace('pushDevices')),
message: i18n.t('pushDeviceAdded', req.language),
};
return response;
}; };

View File

@@ -10,7 +10,6 @@ const COMMON_FILES = [
'./common/script/**/*.js', './common/script/**/*.js',
// @TODO remove these negations as the files are converted over. // @TODO remove these negations as the files are converted over.
'!./common/script/content/index.js', '!./common/script/content/index.js',
'!./common/script/ops/addPushDevice.js',
'!./common/script/ops/reset.js', '!./common/script/ops/reset.js',
'!./common/script/fns/crit.js', '!./common/script/fns/crit.js',
'!./common/script/fns/randomDrop.js', '!./common/script/fns/randomDrop.js',

View File

@@ -0,0 +1,35 @@
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
describe('POST /user/addPushDevice', () => {
let user;
let regId = '10';
let type = 'someRandomType';
beforeEach(async () => {
user = await generateUser();
});
it('returns an error if user already has the push device', async () => {
await user.post('/user/addPushDevice', {type, regId});
await expect(user.post('/user/addPushDevice', {type, regId}))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('pushDeviceAlreadyAdded'),
});
});
// More tests in common code unit tests
it('adds a push device to the user', async () => {
let response = await user.post('/user/addPushDevice', {type, regId});
await user.sync();
expect(response.message).to.equal(t('pushDeviceAdded'));
expect(user.pushDevices[0].type).to.equal(type);
expect(user.pushDevices[0].regId).to.equal(regId);
});
});

View File

@@ -0,0 +1,59 @@
import addPushDevice from '../../../common/script/ops/addPushDevice';
import i18n from '../../../common/script/i18n';
import {
generateUser,
} from '../../helpers/common.helper';
import {
NotAuthorized,
BadRequest,
} from '../../../common/script/libs/errors';
describe('shared.ops.addPushDevice', () => {
let user;
let regId = '10';
let type = 'someRandomType';
beforeEach(() => {
user = generateUser();
user.stats.hp = 0;
});
it('returns an error when regId is not provided', (done) => {
try {
addPushDevice(user);
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('regIdRequired'));
done();
}
});
it('returns an error when type is not provided', (done) => {
try {
addPushDevice(user, {body: {regId}});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.equal(i18n.t('typeRequired'));
done();
}
});
it('adds a push device', () => {
let response = addPushDevice(user, {body: {regId, type}});
expect(response.message).to.equal(i18n.t('pushDeviceAdded'));
expect(user.pushDevices[0].type).to.equal(type);
expect(user.pushDevices[0].regId).to.equal(regId);
});
it('does not a push device twice', (done) => {
try {
addPushDevice(user, {body: {regId, type}});
addPushDevice(user, {body: {regId, type}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('pushDeviceAlreadyAdded'));
done();
}
});
});

View File

@@ -1061,4 +1061,26 @@ api.userReroll = {
}, },
}; };
/*
* @api {post} /user/addPushDevice Adds a push device to a user.
* @apiVersion 3.0.0
* @apiName UserAddPushDevice
* @apiGroup User
*
* @apiSuccess {Object} data `pushDevices`
*/
api.userAddPushDevice = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/addPushDevice',
async handler (req, res) {
let user = res.locals.user;
let addPushDeviceResponse = common.ops.addPushDevice(user, req);
await user.save();
res.respond(200, addPushDeviceResponse);
},
};
module.exports = api; module.exports = api;