diff --git a/test/api/v3/integration/groups/POST-groups_invite.test.js b/test/api/v3/integration/groups/POST-groups_invite.test.js index 157cf01294..02f8c5e20c 100644 --- a/test/api/v3/integration/groups/POST-groups_invite.test.js +++ b/test/api/v3/integration/groups/POST-groups_invite.test.js @@ -48,6 +48,19 @@ describe('Post /groups/:groupId/invite', () => { }); }); + it('returns error when recipient has blocked the senders', async () => { + const inviterNoBlocks = await inviter.update({ 'inbox.blocks': [] }); + const userWithBlockedInviter = await generateUser({ 'inbox.blocks': [inviter._id] }); + await expect(inviterNoBlocks.post(`/groups/${group._id}/invite`, { + usernames: [userWithBlockedInviter.auth.local.lowerCaseUsername], + })) + .to.eventually.be.rejected.and.eql({ + code: 401, + error: 'NotAuthorized', + message: t('notAuthorizedToSendMessageToThisUser'), + }); + }); + it('invites a user to a group by username', async () => { const userToInvite = await generateUser(); diff --git a/website/server/libs/invites/index.js b/website/server/libs/invites/index.js index e7f5055e2b..c45c38c246 100644 --- a/website/server/libs/invites/index.js +++ b/website/server/libs/invites/index.js @@ -207,6 +207,13 @@ async function inviteByUserName (username, group, inviter, req, res) { throw new BadRequest(res.t('cannotInviteSelfToGroup')); } + const objections = inviter.getObjectionsToInteraction('group-invitation', userToInvite); + if (objections.length > 0) { + throw new NotAuthorized(res.t( + objections[0], + { userId: userToInvite._id, username: userToInvite.profile.name }, + )); + } return addInvitationToUser(userToInvite, group, inviter, res); }