mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 22:27:26 +01:00
fix(tests): POST groups
This commit is contained in:
@@ -35,8 +35,8 @@ describe('POST /group', () => {
|
|||||||
|
|
||||||
it('sets the group leader to the user who created the group', async () => {
|
it('sets the group leader to the user who created the group', async () => {
|
||||||
const group = await user.post('/groups', {
|
const group = await user.post('/groups', {
|
||||||
name: 'Test Public Guild',
|
name: 'Test Party',
|
||||||
type: 'guild',
|
type: 'party',
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(group.leader).to.eql({
|
expect(group.leader).to.eql({
|
||||||
@@ -51,7 +51,7 @@ describe('POST /group', () => {
|
|||||||
const name = 'Test Group';
|
const name = 'Test Group';
|
||||||
const group = await user.post('/groups', {
|
const group = await user.post('/groups', {
|
||||||
name,
|
name,
|
||||||
type: 'guild',
|
type: 'party',
|
||||||
});
|
});
|
||||||
|
|
||||||
const updatedGroup = await user.get(`/groups/${group._id}`);
|
const updatedGroup = await user.get(`/groups/${group._id}`);
|
||||||
@@ -64,7 +64,7 @@ describe('POST /group', () => {
|
|||||||
const summary = 'Test Summary';
|
const summary = 'Test Summary';
|
||||||
const group = await user.post('/groups', {
|
const group = await user.post('/groups', {
|
||||||
name,
|
name,
|
||||||
type: 'guild',
|
type: 'party',
|
||||||
summary,
|
summary,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ describe('POST /group', () => {
|
|||||||
const summary = 'A'.repeat(MAX_SUMMARY_SIZE_FOR_GUILDS + 1);
|
const summary = 'A'.repeat(MAX_SUMMARY_SIZE_FOR_GUILDS + 1);
|
||||||
await expect(user.post('/groups', {
|
await expect(user.post('/groups', {
|
||||||
name,
|
name,
|
||||||
type: 'guild',
|
type: 'party',
|
||||||
summary,
|
summary,
|
||||||
})).to.eventually.be.rejected.and.eql({
|
})).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
@@ -88,157 +88,6 @@ describe('POST /group', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Guilds', () => {
|
|
||||||
it('returns an error when a user with insufficient funds attempts to create a guild', async () => {
|
|
||||||
await user.update({ balance: 0 });
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
user.post('/groups', {
|
|
||||||
name: 'Test Public Guild',
|
|
||||||
type: 'guild',
|
|
||||||
}),
|
|
||||||
).to.eventually.be.rejected.and.eql({
|
|
||||||
code: 401,
|
|
||||||
error: 'NotAuthorized',
|
|
||||||
message: t('messageInsufficientGems'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('adds guild to user\'s list of guilds', async () => {
|
|
||||||
const guild = await user.post('/groups', {
|
|
||||||
name: 'some guild',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
});
|
|
||||||
|
|
||||||
const updatedUser = await user.get('/user');
|
|
||||||
|
|
||||||
expect(updatedUser.guilds).to.include(guild._id);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('awards the Joined Guild achievement', async () => {
|
|
||||||
await user.post('/groups', {
|
|
||||||
name: 'some guild',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
});
|
|
||||||
|
|
||||||
const updatedUser = await user.get('/user');
|
|
||||||
|
|
||||||
expect(updatedUser.achievements.joinedGuild).to.eql(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
context('public guild', () => {
|
|
||||||
it('creates a group', async () => {
|
|
||||||
const groupName = 'Test Public Guild';
|
|
||||||
const groupType = 'guild';
|
|
||||||
const groupPrivacy = 'public';
|
|
||||||
|
|
||||||
const publicGuild = await user.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
|
|
||||||
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);
|
|
||||||
expect(publicGuild.leader).to.eql({
|
|
||||||
_id: user._id,
|
|
||||||
profile: {
|
|
||||||
name: user.profile.name,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns an error when a user with no chat privileges attempts to create a public guild', async () => {
|
|
||||||
await user.update({ 'flags.chatRevoked': true });
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
user.post('/groups', {
|
|
||||||
name: 'Test Public Guild',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
}),
|
|
||||||
).to.eventually.be.rejected.and.eql({
|
|
||||||
code: 401,
|
|
||||||
error: 'NotAuthorized',
|
|
||||||
message: t('chatPrivilegesRevoked'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('private guild', () => {
|
|
||||||
const groupName = 'Test Private Guild';
|
|
||||||
const groupType = 'guild';
|
|
||||||
const groupPrivacy = 'private';
|
|
||||||
|
|
||||||
it('creates a group', async () => {
|
|
||||||
const privateGuild = await user.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);
|
|
||||||
expect(privateGuild.leader).to.eql({
|
|
||||||
_id: user._id,
|
|
||||||
profile: {
|
|
||||||
name: user.profile.name,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('creates a private guild when the user has no chat privileges', async () => {
|
|
||||||
await user.update({ 'flags.chatRevoked': true });
|
|
||||||
const privateGuild = await user.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(privateGuild._id).to.exist;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('deducts gems from user and adds them to guild bank', async () => {
|
|
||||||
const privateGuild = await user.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(privateGuild.balance).to.eql(1);
|
|
||||||
|
|
||||||
const updatedUser = await user.get('/user');
|
|
||||||
|
|
||||||
expect(updatedUser.balance).to.eql(user.balance - 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not deduct the gems from user when guild creation fails', async () => {
|
|
||||||
const stub = sinon.stub(Group.prototype, 'save').rejects();
|
|
||||||
const promise = user.post('/groups', {
|
|
||||||
name: groupName,
|
|
||||||
type: groupType,
|
|
||||||
privacy: groupPrivacy,
|
|
||||||
});
|
|
||||||
|
|
||||||
await expect(promise).to.eventually.be.rejected;
|
|
||||||
|
|
||||||
const updatedUser = await user.get('/user');
|
|
||||||
|
|
||||||
expect(updatedUser.balance).to.eql(user.balance);
|
|
||||||
|
|
||||||
stub.restore();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('Parties', () => {
|
context('Parties', () => {
|
||||||
const partyName = 'Test Party';
|
const partyName = 'Test Party';
|
||||||
const partyType = 'party';
|
const partyType = 'party';
|
||||||
|
|||||||
@@ -18,81 +18,24 @@ describe('POST /group/:groupId/join', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Joining a public guild', () => {
|
|
||||||
let user; let joiningUser; let
|
|
||||||
publicGuild;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
const { group, groupLeader } = await createAndPopulateGroup({
|
|
||||||
groupDetails: {
|
|
||||||
name: 'Test Guild',
|
|
||||||
type: 'guild',
|
|
||||||
privacy: 'public',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
publicGuild = group;
|
|
||||||
user = groupLeader;
|
|
||||||
joiningUser = await generateUser();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('allows non-invited users to join public guilds', async () => {
|
|
||||||
const res = await joiningUser.post(`/groups/${publicGuild._id}/join`);
|
|
||||||
|
|
||||||
await expect(joiningUser.get('/user')).to.eventually.have.property('guilds').to.include(publicGuild._id);
|
|
||||||
expect(res.leader._id).to.eql(user._id);
|
|
||||||
expect(res.leader.profile.name).to.eql(user.profile.name);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns an error if user was already a member', async () => {
|
|
||||||
await joiningUser.post(`/groups/${publicGuild._id}/join`);
|
|
||||||
await expect(joiningUser.post(`/groups/${publicGuild._id}/join`)).to.eventually.be.rejected.and.eql({
|
|
||||||
code: 401,
|
|
||||||
error: 'NotAuthorized',
|
|
||||||
message: t('youAreAlreadyInGroup'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('promotes joining member in a public empty guild to leader', async () => {
|
|
||||||
await user.post(`/groups/${publicGuild._id}/leave`);
|
|
||||||
|
|
||||||
await joiningUser.post(`/groups/${publicGuild._id}/join`);
|
|
||||||
|
|
||||||
await expect(joiningUser.get(`/groups/${publicGuild._id}`)).to.eventually.have.nested.property('leader._id', joiningUser._id);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('increments memberCount when joining guilds', async () => {
|
|
||||||
const oldMemberCount = publicGuild.memberCount;
|
|
||||||
|
|
||||||
await joiningUser.post(`/groups/${publicGuild._id}/join`);
|
|
||||||
|
|
||||||
await expect(joiningUser.get(`/groups/${publicGuild._id}`)).to.eventually.have.property('memberCount', oldMemberCount + 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('awards Joined Guild achievement', async () => {
|
|
||||||
await joiningUser.post(`/groups/${publicGuild._id}/join`);
|
|
||||||
|
|
||||||
await expect(joiningUser.get('/user')).to.eventually.have.nested.property('achievements.joinedGuild', true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('Joining a private guild', () => {
|
context('Joining a private guild', () => {
|
||||||
let user; let invitedUser; let
|
let user;
|
||||||
guild;
|
let invitedUser;
|
||||||
|
let guild;
|
||||||
|
let invitees;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const { group, groupLeader, invitees } = await createAndPopulateGroup({
|
({ group: guild, groupLeader: user, invitees } = await createAndPopulateGroup({
|
||||||
groupDetails: {
|
groupDetails: {
|
||||||
name: 'Test Guild',
|
name: 'Test Guild',
|
||||||
type: 'guild',
|
type: 'guild',
|
||||||
privacy: 'private',
|
privacy: 'private',
|
||||||
},
|
},
|
||||||
invites: 1,
|
invites: 1,
|
||||||
});
|
upgradeToGroupPlan: true,
|
||||||
|
}));
|
||||||
|
|
||||||
guild = group;
|
[invitedUser] = invitees;
|
||||||
user = groupLeader;
|
|
||||||
invitedUser = invitees[0]; // eslint-disable-line prefer-destructuring
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns error when user is not invited to private guild', async () => {
|
it('returns error when user is not invited to private guild', async () => {
|
||||||
@@ -182,7 +125,7 @@ describe('POST /group/:groupId/join', () => {
|
|||||||
|
|
||||||
party = group;
|
party = group;
|
||||||
user = groupLeader;
|
user = groupLeader;
|
||||||
invitedUser = invitees[0]; // eslint-disable-line prefer-destructuring
|
[invitedUser] = invitees;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns error when user is not invited to party', async () => {
|
it('returns error when user is not invited to party', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user