Skip to content

Commit

Permalink
Merge pull request #13 from CMU-313/evelynnc/merge-p1-changes
Browse files Browse the repository at this point in the history
Merge individual changes to src/api/groups.js from project 1
  • Loading branch information
evelynnchen-cmu authored Sep 21, 2024
2 parents 936b81c + 6a6ec6d commit a30f699
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/api/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,39 @@ async function canSearchMembers(uid, groupName) {
}
}

groupsAPI.join = async function (caller, data) {
function validateJoinRequest(caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
if (caller.uid <= 0 || !data.uid) {
throw new Error('[[error:invalid-uid]]');
}
}

const groupName = await groups.getGroupNameByGroupSlug(data.slug);
if (!groupName) {
throw new Error('[[error:no-group]]');
}

async function checkPrivileges(caller, groupName) {
const isCallerAdmin = await privileges.admin.can('admin:groups', caller.uid);
if (!isCallerAdmin && (
groups.systemGroups.includes(groupName) ||
groups.isPrivilegeGroup(groupName)
)) {
throw new Error('[[error:not-allowed]]');
}
return isCallerAdmin;
}

function isGroupJoinDisabledForCaller(isCallerAdmin, isSelf, groupData) {
return !isCallerAdmin && isSelf && groupData.private && groupData.disableJoinRequests;
}

groupsAPI.join = async function (caller, data) {
validateJoinRequest(caller, data);

const groupName = await groups.getGroupNameByGroupSlug(data.slug);
if (!groupName) {
throw new Error('[[error:no-group]]');
}

const isCallerAdmin = await checkPrivileges(caller, groupName);

const [groupData, userExists] = await Promise.all([
groups.getGroupData(groupName),
Expand All @@ -159,7 +172,7 @@ groupsAPI.join = async function (caller, data) {
return;
}

if (!isCallerAdmin && isSelf && groupData.private && groupData.disableJoinRequests) {
if (isGroupJoinDisabledForCaller(isCallerAdmin, isSelf, groupData)) {
throw new Error('[[error:group-join-disabled]]');
}

Expand Down
10 changes: 10 additions & 0 deletions test/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,16 @@ describe('Groups', () => {
assert.equal(updatedData.private, false);
});

it('should fail to join group if user does not exist', async () => {
const nonExistentUid = 123456789;
try {
await apiGroups.join({ uid: adminUid }, { uid: nonExistentUid, slug: 'test' });
assert(false);
} catch (err) {
assert.strictEqual(err.message, '[[error:invalid-uid]]');
}
});

it('should fail to create a group with name guests', async () => {
try {
await apiGroups.create({ uid: adminUid }, { name: 'guests' });
Expand Down

0 comments on commit a30f699

Please sign in to comment.