fix invitations and add missing tests

This commit is contained in:
Matteo Pagliazzi
2016-02-27 12:01:00 +01:00
parent 0014afb75e
commit b4e4e31be5
3 changed files with 31 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ import {
translate as t, translate as t,
} from '../../../../helpers/api-v3-integration.helper'; } from '../../../../helpers/api-v3-integration.helper';
import { v4 as generateUUID } from 'uuid'; import { v4 as generateUUID } from 'uuid';
import * as email from '../../../../../website/src/libs/api-v3/email';
const INVITES_LIMIT = 100; const INVITES_LIMIT = 100;
@@ -19,6 +20,10 @@ describe('Post /groups/:groupId/invite', () => {
}); });
}); });
afterEach(() => {
if (email.sendTxn.restore) email.sendTxn.restore();
});
describe('user id invites', () => { describe('user id invites', () => {
it('returns an error when invited user is not found', async () => { it('returns an error when invited user is not found', async () => {
let fakeID = generateUUID(); let fakeID = generateUUID();
@@ -70,9 +75,10 @@ describe('Post /groups/:groupId/invite', () => {
}); });
}); });
it('invites a user to a group by uuid', async () => { it.only('invites a user to a group by uuid', async () => {
let userToInvite = await generateUser(); let userToInvite = await generateUser();
sandbox.stub(email, 'sendTxn');
await expect(inviter.post(`/groups/${group._id}/invite`, { await expect(inviter.post(`/groups/${group._id}/invite`, {
uuids: [userToInvite._id], uuids: [userToInvite._id],
})).to.eventually.deep.equal([{ })).to.eventually.deep.equal([{
@@ -80,6 +86,13 @@ describe('Post /groups/:groupId/invite', () => {
name: groupName, name: groupName,
inviter: inviter._id, inviter: inviter._id,
}]); }]);
expect(email.sendTxn).to.be.calledOnce;
expect(email.sendTxn[0][0]._id).to.equal(userToInvite._id);
expect(email.sendTxn[0][1]).to.equal('invited-guild');
expect(email.sendTxn[0][2]).to.have.all.keys(['GUILD_NAME', 'GUILD_URL', 'INVITER']);
expect(email.sendTxn[0][2].INVITER).to.equal(inviter.profile.name);
await expect(userToInvite.get('/user')) await expect(userToInvite.get('/user'))
.to.eventually.have.deep.property('invitations.guilds[0].id', group._id); .to.eventually.have.deep.property('invitations.guilds[0].id', group._id);
}); });
@@ -102,6 +115,8 @@ describe('Post /groups/:groupId/invite', () => {
inviter: inviter._id, inviter: inviter._id,
}, },
]); ]);
expect(email.sendTxn).to.be.calledTwice;
await expect(userToInvite.get('/user')).to.eventually.have.deep.property('invitations.guilds[0].id', group._id); await expect(userToInvite.get('/user')).to.eventually.have.deep.property('invitations.guilds[0].id', group._id);
await expect(userToInvite2.get('/user')).to.eventually.have.deep.property('invitations.guilds[0].id', group._id); await expect(userToInvite2.get('/user')).to.eventually.have.deep.property('invitations.guilds[0].id', group._id);
}); });
@@ -173,13 +188,23 @@ describe('Post /groups/:groupId/invite', () => {
it('invites a user to a group by email', async () => { it('invites a user to a group by email', async () => {
await expect(inviter.post(`/groups/${group._id}/invite`, { await expect(inviter.post(`/groups/${group._id}/invite`, {
emails: [testInvite], emails: [testInvite],
inviter: 'inviter name',
})).to.exist; })).to.exist;
expect(email.sendTxn).to.be.calledOnce;
expect(email.sendTxn[0][0]).to.eql(testInvite);
expect(email.sendTxn[0][1]).to.equal('invite-friend-guild');
expect(email.sendTxn[0][2]).to.have.all.keys(['GUILD_NAME', 'LINK', 'INVITER']);
expect(email.sendTxn[0][2].INVITER).to.equal('inviter name');
}); });
it('invites multiple users to a group by email', async () => { it('invites multiple users to a group by email', async () => {
await expect(inviter.post(`/groups/${group._id}/invite`, { await expect(inviter.post(`/groups/${group._id}/invite`, {
emails: [testInvite, {name: 'test2', email: 'test2@habitica.com'}], emails: [testInvite, {name: 'test2', email: 'test2@habitica.com'}],
})).to.exist; })).to.exist;
expect(email.sendTxn).to.be.calledTwice;
expect(email.sendTxn[0][2].INVITER).to.equal(inviter.profile.name);
}); });
}); });
@@ -226,6 +251,7 @@ describe('Post /groups/:groupId/invite', () => {
expect(invite).to.exist; expect(invite).to.exist;
expect(invitedUser.invitations.guilds[0].id).to.equal(group._id); expect(invitedUser.invitations.guilds[0].id).to.equal(group._id);
expect(email.sendTxn).to.be.calledTwice;
}); });
}); });
@@ -320,6 +346,8 @@ describe('Post /groups/:groupId/invite', () => {
uuids: [userToInvite._id], uuids: [userToInvite._id],
}); });
expect((await userToInvite.get('/user')).invitations.party.id).to.equal(party._id); expect((await userToInvite.get('/user')).invitations.party.id).to.equal(party._id);
expect(email.sendTxn).to.be.calledOnce;
}); });
}); });
}); });

View File

@@ -548,14 +548,13 @@ async function _inviteByEmail (invite, group, inviter, req, res) {
let variables = [ let variables = [
{name: 'LINK', content: link}, {name: 'LINK', content: link},
{name: 'INVITER', content: inviter.profile.name}, {name: 'INVITER', content: req.body.inviter || inviter.profile.name},
]; ];
if (group.type === 'guild') { if (group.type === 'guild') {
variables.push({name: 'GUILD_NAME', content: group.name}); variables.push({name: 'GUILD_NAME', content: group.name});
} }
// TODO implement "users can only be invited once"
// Check for the email address not to be unsubscribed // Check for the email address not to be unsubscribed
let userIsUnsubscribed = await EmailUnsubscription.findOne({email: invite.email}).exec(); let userIsUnsubscribed = await EmailUnsubscription.findOne({email: invite.email}).exec();
let groupLabel = group.type === 'guild' ? '-guild' : ''; let groupLabel = group.type === 'guild' ? '-guild' : '';

View File

@@ -116,7 +116,6 @@ api.inviteToQuest = {
sendTxnEmail(membersToEmail, `invite-${quest.boss ? 'boss' : 'collection'}-quest`, [ sendTxnEmail(membersToEmail, `invite-${quest.boss ? 'boss' : 'collection'}-quest`, [
{name: 'QUEST_NAME', content: quest.text()}, {name: 'QUEST_NAME', content: quest.text()},
{name: 'INVITER', content: inviterVars.name}, {name: 'INVITER', content: inviterVars.name},
{name: 'REPLY_TO_ADDRESS', content: inviterVars.email},
{name: 'PARTY_URL', content: '/#/options/groups/party'}, {name: 'PARTY_URL', content: '/#/options/groups/party'},
]); ]);