Skip to content

Commit

Permalink
feat: add getGroupsInfo route (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek authored Mar 2, 2020
1 parent b27d909 commit 2b86ee0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
21 changes: 11 additions & 10 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@

### Groups

| Method | Route | Action | Description |
| :----: | :------------------------------------- | :-----------------------------------: | :--------------------------------------------------: |
| GET | `/db/:dbname/groups` | Get all groups | |
| GET | `/db/:dbname/group/:name` | Get a group by name | |
| PUT | `/db/:dbname/group/:name` | Create a group | |
| DELETE | `/db/:dbname/group/:name` | Remove a group | |
| PUT | `/db/:dbname/group/:name/user/:user` | Add a user to an existing group | Group must exist. No-op if user is already in group |
| DELETE | `/db/:dbname/group/:name/user/:user` | Remove a user from an existing group | Group must exist. No-op if user is not in group |
| PUT | `/db/:dbname/group/:name/right/:right` | Add a right to an existing group | Group must exist. No-op if group already has right |
| DELETE | `/db/:dbname/group/:name/right/:right` | Remove a right from an existing group | Group must exist. No-op if group does not have right |
| Method | Route | Action | Description |
| :----: | :------------------------------------- | :-----------------------------------: | :----------------------------------------------------------------------------: |
| GET | `/db/:dbname/groups` | Get all groups | |
| GET | `/db/:dbname/groups/info` | Get all groups' info | Returns name and description for all groups regardless of rights on that group |
| GET | `/db/:dbname/group/:name` | Get a group by name | |
| PUT | `/db/:dbname/group/:name` | Create a group | |
| DELETE | `/db/:dbname/group/:name` | Remove a group | |
| PUT | `/db/:dbname/group/:name/user/:user` | Add a user to an existing group | Group must exist. No-op if user is already in group |
| DELETE | `/db/:dbname/group/:name/user/:user` | Remove a user from an existing group | Group must exist. No-op if user is not in group |
| PUT | `/db/:dbname/group/:name/right/:right` | Add a right to an existing group | Group must exist. No-op if group already has right |
| DELETE | `/db/:dbname/group/:name/right/:right` | Remove a right from an existing group | Group must exist. No-op if group does not have right |

### Tokens

Expand Down
29 changes: 29 additions & 0 deletions src/couch/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ const methods = {
}
},

/**
* Return info about every group
* Only contains the group's name and description
* Does not return the list of user's or type of group
* @param {*} user
*/
async getGroupsInfo(user) {
debug.trace('getGroupsInfo (%s)', user);

if (user === 'anonymous') {
throw new CouchError(
'user must be authenticated to get groups info',
'unauthorized',
);
}
await this.open();

const groups = await this._db.queryView(
'documentByType',
{ key: 'group', include_docs: true },
{ onlyDoc: true },
);

return groups.map((group) => ({
name: group.name,
description: group.description,
}));
},

/**
* Returns a list of groups that grant a given right to the user
* @param {string} user
Expand Down
4 changes: 4 additions & 0 deletions src/server/middleware/couch.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ exports.getGroups = composeWithError(async (ctx) => {
ctx.body = await ctx.state.couch.getGroups(ctx.state.userEmail);
});

exports.getGroupsInfo = composeWithError(async (ctx) => {
ctx.body = await ctx.state.couch.getGroupsInfo(ctx.state.userEmail);
});

exports.getGroupUsers = composeWithError(async (ctx) => {
const group = await ctx.state.couch.getDocByRights(
ctx.params.uuid,
Expand Down
1 change: 1 addition & 0 deletions src/server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ router.post(

// Groups
router.get('/:dbname/groups', couch.getGroups);
router.get('/:dbname/groups/info', couch.getGroupsInfo);
router.get('/:dbname/group/:name', couch.getGroup);
router.put('/:dbname/group/:name', couch.createGroup);
router.put(
Expand Down
1 change: 1 addition & 0 deletions test/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function populate(db) {
$type: 'group',
$owners: ['a@a.com'],
name: 'groupA',
description: 'groupA description',
users: ['a@a.com'],
rights: ['create', 'write', 'delete', 'read'],
}),
Expand Down
16 changes: 16 additions & 0 deletions test/unit/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ describe('group methods', () => {
});
});

test('getGroupsInfo should return name and description all the groups', () => {
return couch.getGroupsInfo('a@a.com').then(function(groups) {
expect(groups.length).toEqual(2);
expect(groups[0]).toMatchObject({
name: 'groupA',
description: 'groupA description',
});
});
});

test('getGroupsInfo should throw if user is anonymous', () => {
return expect(couch.getGroupsInfo('anonymous')).rejects.toThrow(
/user must be authenticated to get groups info/,
);
});

test('getGroups should return groups when owner not owner but has global readGroup right', () => {
return couch.getGroups('b@b.com').then(function(docs) {
expect(docs).toHaveLength(2);
Expand Down

0 comments on commit 2b86ee0

Please sign in to comment.