From 6e21d154ae4a1f1e18b6d234c81a9ede4d9d08dc Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 20 Oct 2018 14:52:02 +0200 Subject: [PATCH] Do not throw an error when adding the same push device twice (#10770) * do not throw an error when adding the same push device twice * fix spelling * fix linting --- .../user/POST-user_push_device.test.js | 16 +++++++++------- .../controllers/api-v3/pushNotifications.js | 5 +++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/test/api/v3/integration/user/POST-user_push_device.test.js b/test/api/v3/integration/user/POST-user_push_device.test.js index 0c302c60b2..a0c302de71 100644 --- a/test/api/v3/integration/user/POST-user_push_device.test.js +++ b/test/api/v3/integration/user/POST-user_push_device.test.js @@ -39,14 +39,16 @@ describe('POST /user/push-devices', () => { }); }); - it('returns an error if user already has the push device', async () => { + it('fails silently if user already has the push device', async () => { await user.post('/user/push-devices', {type, regId}); - await expect(user.post('/user/push-devices', {type, regId})) - .to.eventually.be.rejected.and.eql({ - code: 401, - error: 'NotAuthorized', - message: t('pushDeviceAlreadyAdded'), - }); + const response = await user.post('/user/push-devices', {type, regId}); + await user.sync(); + + expect(response.message).to.equal(t('pushDeviceAdded')); + expect(response.data[0].type).to.equal(type); + expect(response.data[0].regId).to.equal(regId); + expect(user.pushDevices[0].type).to.equal(type); + expect(user.pushDevices[0].regId).to.equal(regId); }); it('adds a push device to the user', async () => { diff --git a/website/server/controllers/api-v3/pushNotifications.js b/website/server/controllers/api-v3/pushNotifications.js index 27c37f7812..14330e4e6b 100644 --- a/website/server/controllers/api-v3/pushNotifications.js +++ b/website/server/controllers/api-v3/pushNotifications.js @@ -1,6 +1,5 @@ import { authWithHeaders } from '../../middlewares/auth'; import { - NotAuthorized, NotFound, } from '../../libs/errors'; import { model as PushDevice } from '../../models/pushDevice'; @@ -39,8 +38,10 @@ api.addPushDevice = { type: req.body.type, }; + // When adding a duplicate push device, fail silently instead of throwing an error if (pushDevices.find(device => device.regId === item.regId)) { - throw new NotAuthorized(res.t('pushDeviceAlreadyAdded')); + res.respond(200, user.pushDevices, res.t('pushDeviceAdded')); + return; } // Concurrency safe update