mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
Converted posts tests to async/await syntax and updated tests
This commit is contained in:
@@ -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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,85 +44,101 @@ 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,
|
|
||||||
type: groupType,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('private guild', () => {
|
|
||||||
it('creates a group', () => {
|
|
||||||
let groupName = 'Test Private Guild';
|
|
||||||
let groupType = 'guild';
|
|
||||||
let groupPrivacy = 'private';
|
|
||||||
|
|
||||||
return generateUser({balance: 1}).then((generatedUser) => {
|
|
||||||
return generatedUser.post('/groups', {
|
|
||||||
name: groupName,
|
name: groupName,
|
||||||
type: groupType,
|
type: groupType,
|
||||||
privacy: groupPrivacy,
|
privacy: groupPrivacy,
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.then((result) => {
|
expect(publicGuild._id).to.exist;
|
||||||
expect(result._id).to.exist;
|
expect(publicGuild.name).to.equal(groupName);
|
||||||
expect(result.name).to.equal(groupName);
|
expect(publicGuild.type).to.equal(groupType);
|
||||||
expect(result.type).to.equal(groupType);
|
expect(publicGuild.memberCount).to.equal(1);
|
||||||
expect(result.memberCount).to.equal(1);
|
expect(publicGuild.privacy).to.equal(groupPrivacy);
|
||||||
expect(result.privacy).to.equal(groupPrivacy);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('private guild', () => {
|
||||||
|
let groupName = 'Test Private Guild';
|
||||||
|
let groupType = 'guild';
|
||||||
|
let groupPrivacy = 'private';
|
||||||
|
|
||||||
|
it('creates a group', async () => {
|
||||||
|
let privateGuild = await userToCreateGuild.post('/groups', {
|
||||||
|
name: groupName,
|
||||||
|
type: groupType,
|
||||||
|
privacy: 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(result.type).to.equal(groupType);
|
|
||||||
expect(result.memberCount).to.equal(1);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('prevents user in a party from creating another party', () => {
|
expect(party._id).to.exist;
|
||||||
let tmpUser;
|
expect(party.name).to.equal(partyName);
|
||||||
let groupName = 'Test Party';
|
expect(party.type).to.equal(partyType);
|
||||||
let groupType = 'party';
|
expect(party.memberCount).to.equal(1);
|
||||||
|
|
||||||
return generateUser().then((generatedUser) => {
|
|
||||||
tmpUser = generatedUser;
|
|
||||||
return tmpUser.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.then(() => {
|
it('prevents user in a party from creating another party', async () => {
|
||||||
return expect(tmpUser.post('/groups')).to.eventually.be.rejected.and.eql({
|
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,
|
code: 401,
|
||||||
error: 'NotAuthorized',
|
error: 'NotAuthorized',
|
||||||
message: t('messageGroupAlreadyInParty'),
|
message: t('messageGroupAlreadyInParty'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('prevents creating a public party', async () => {
|
||||||
|
await expect(user.post('/groups', {
|
||||||
|
name: partyName,
|
||||||
|
type: partyType,
|
||||||
|
privacy: 'public',
|
||||||
|
})).to.eventually.be.rejected.and.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('partyMustbePrivate'),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user