Converted posts tests to async/await syntax and updated tests

This commit is contained in:
Keith Holliday
2016-01-11 12:35:46 -06:00
parent f235010536
commit ffbd4696e3
3 changed files with 101 additions and 70 deletions

View File

@@ -46,5 +46,6 @@
"winnerIdRequired": "\"winnerId\" must be a valid UUID.", "winnerIdRequired": "\"winnerId\" must be a valid UUID.",
"challengeNotFound": "Challenge not found.", "challengeNotFound": "Challenge not found.",
"onlyLeaderDeleteChal": "Only the challenge leader can delete it.", "onlyLeaderDeleteChal": "Only the challenge leader can delete it.",
"winnerNotFound": "Winner with id \"<%= userId %>\" not found or not part of the challenge." "winnerNotFound": "Winner with id \"<%= userId %>\" not found or not part of the challenge.",
"partyMustbePrivate": "Parties must be private"
} }

View File

@@ -6,21 +6,34 @@ import {
describe('POST /group', () => { describe('POST /group', () => {
let user; let user;
beforeEach(() => { beforeEach(async () => {
return generateUser().then((generatedUser) => { user = await generateUser();
user = generatedUser; });
context('All Groups', () => {
it('it returns validation error when type is not provided', async () => {
let userToCreateGroup = await generateUser({balance: 1});
await expect(userToCreateGroup.post('/groups', { name: 'Test Group Without Type' }))
.to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'Group validation failed',
});
}); });
}); });
context('Guilds', () => { context('Guilds', () => {
it('returns an error when a user with insufficient funds attempts to create a guild', () => { let userToCreateGuild;
let groupName = 'Test Public Guild';
let groupType = 'guild';
return expect( beforeEach(async () => {
userToCreateGuild = await generateUser({balance: 1});
});
it('returns an error when a user with insufficient funds attempts to create a guild', async () => {
await expect(
user.post('/groups', { user.post('/groups', {
name: groupName, name: 'Test Public Guild',
type: groupType, type: 'guild',
}) })
) )
.to.eventually.be.rejected.and.eql({ .to.eventually.be.rejected.and.eql({
@@ -31,84 +44,100 @@ describe('POST /group', () => {
}); });
context('public guild', () => { context('public guild', () => {
it('creates a group', () => { it('creates a group', async () => {
let groupName = 'Test Public Guild'; let groupName = 'Test Public Guild';
let groupType = 'guild'; let groupType = 'guild';
let groupPrivacy = 'public';
return generateUser({balance: 1}).then((generatedUser) => { let publicGuild = await userToCreateGuild.post('/groups', {
return generatedUser.post('/groups', { name: groupName,
name: groupName, type: groupType,
type: groupType, privacy: groupPrivacy,
});
})
.then((result) => {
expect(result._id).to.exist;
expect(result.name).to.equal(groupName);
expect(result.type).to.equal(groupType);
expect(result.memberCount).to.equal(1);
}); });
expect(publicGuild._id).to.exist;
expect(publicGuild.name).to.equal(groupName);
expect(publicGuild.type).to.equal(groupType);
expect(publicGuild.memberCount).to.equal(1);
expect(publicGuild.privacy).to.equal(groupPrivacy);
}); });
}); });
context('private guild', () => { context('private guild', () => {
it('creates a group', () => { let groupName = 'Test Private Guild';
let groupName = 'Test Private Guild'; let groupType = 'guild';
let groupType = 'guild'; let groupPrivacy = 'private';
let groupPrivacy = 'private';
return generateUser({balance: 1}).then((generatedUser) => { it('creates a group', async () => {
return generatedUser.post('/groups', { let privateGuild = await userToCreateGuild.post('/groups', {
name: groupName, name: groupName,
type: groupType, type: groupType,
privacy: groupPrivacy, privacy: groupPrivacy,
});
})
.then((result) => {
expect(result._id).to.exist;
expect(result.name).to.equal(groupName);
expect(result.type).to.equal(groupType);
expect(result.memberCount).to.equal(1);
expect(result.privacy).to.equal(groupPrivacy);
}); });
expect(privateGuild._id).to.exist;
expect(privateGuild.name).to.equal(groupName);
expect(privateGuild.type).to.equal(groupType);
expect(privateGuild.memberCount).to.equal(1);
expect(privateGuild.privacy).to.equal(groupPrivacy);
});
it('deducts gems from user and adds them to guild bank', async () => {
let privateGuild = await userToCreateGuild.post('/groups', {
name: groupName,
type: groupType,
privacy: groupPrivacy,
});
expect(privateGuild.balance).to.eql(1);
let updatedUser = await userToCreateGuild.get('/user');
expect(updatedUser.balance).to.eql(0);
}); });
}); });
}); });
context('Parties', () => { context('Parties', () => {
it('creates a party', () => { let partyName = 'Test Party';
let groupName = 'Test Party'; let partyType = 'party';
let groupType = 'party';
return user.post('/groups', { it('creates a party', async () => {
name: groupName, let party = await user.post('/groups', {
type: groupType, name: partyName,
}) type: partyType,
.then((result) => { });
expect(result._id).to.exist;
expect(result.name).to.equal(groupName); expect(party._id).to.exist;
expect(result.type).to.equal(groupType); expect(party.name).to.equal(partyName);
expect(result.memberCount).to.equal(1); expect(party.type).to.equal(partyType);
expect(party.memberCount).to.equal(1);
});
it('prevents user in a party from creating another party', async () => {
let userToCreateTwoParties = await generateUser();
await userToCreateTwoParties.post('/groups', {
name: partyName,
type: partyType,
});
await expect(userToCreateTwoParties.post('/groups'))
.to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('messageGroupAlreadyInParty'),
}); });
}); });
it('prevents user in a party from creating another party', () => { it('prevents creating a public party', async () => {
let tmpUser; await expect(user.post('/groups', {
let groupName = 'Test Party'; name: partyName,
let groupType = 'party'; type: partyType,
privacy: 'public',
return generateUser().then((generatedUser) => { })).to.eventually.be.rejected.and.eql({
tmpUser = generatedUser; code: 401,
return tmpUser.post('/groups', { error: 'NotAuthorized',
name: groupName, message: t('partyMustbePrivate'),
type: groupType,
});
})
.then(() => {
return expect(tmpUser.post('/groups')).to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: t('messageGroupAlreadyInParty'),
});
}); });
}); });
}); });

View File

@@ -43,6 +43,7 @@ api.createGroup = {
user.balance--; user.balance--;
user.guilds.push(group._id); user.guilds.push(group._id);
} else { } else {
if (group.privacy === 'public') throw new NotAuthorized(res.t('partyMustbePrivate'));
if (user.party._id) throw new NotAuthorized(res.t('messageGroupAlreadyInParty')); if (user.party._id) throw new NotAuthorized(res.t('messageGroupAlreadyInParty'));
user.party._id = group._id; user.party._id = group._id;