-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3caaaeb
commit 5111625
Showing
3 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import QueryBuilder from './query-builder' | ||
|
||
/** | ||
* Syncano groups | ||
* @property {Function} | ||
*/ | ||
class Groups extends QueryBuilder { | ||
url () { | ||
const {instanceName} = this.instance | ||
|
||
return `${this._getInstanceURL(instanceName)}/groups/` | ||
} | ||
|
||
/** | ||
* Groups list helper | ||
* | ||
* @param {string} url | ||
* | ||
* @return {Array} | ||
*/ | ||
async _getGroups (url) { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'GET', | ||
} | ||
|
||
try { | ||
const res = await fetch(url, options) | ||
const groups = res.objects | ||
|
||
if(res.next) { | ||
groups.concat(this._getGroups(res.next)) | ||
} | ||
|
||
return groups | ||
} catch(err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Get group with users list | ||
* | ||
* @param {string} label - group name | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* const group = await get('admin') | ||
*/ | ||
async get (label) { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'GET', | ||
} | ||
|
||
const groups = await this.list() | ||
const group = groups.find(elem => elem.label === label) | ||
if(group) { | ||
const res = await fetch(`${this.url()}${group.id}/users/`, options) | ||
group.users = res.objects | ||
} | ||
|
||
return group | ||
} | ||
|
||
/** | ||
* Get groups list | ||
* | ||
* @returns {Promise} | ||
* | ||
* @example {@lang javascript} | ||
* const groupsList = await groups.list() | ||
*/ | ||
async list() { | ||
try { | ||
return await this._getGroups(this.url()) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Create new group | ||
* | ||
* @param {string} label - group name | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* const adminGroup = await groups.create('admin') | ||
*/ | ||
async create (label, description = '') { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'POST', | ||
body: JSON.stringify({label, description}) | ||
} | ||
|
||
try { | ||
const group = await this.get(label) | ||
|
||
if(!group) { | ||
return await fetch(this.url(), options) | ||
} else { | ||
throw new Error('Group already exist') | ||
} | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Delete group | ||
* | ||
* @param {string} label - group name | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* groups.delete('admin') | ||
*/ | ||
async delete (label) { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'DELETE' | ||
} | ||
|
||
try { | ||
const group = await this.get(label) | ||
|
||
return await fetch(`${this.url()}${group.id}/`, options) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Get user groups list | ||
* | ||
* @param {number} user - user id | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* const userGroups = await groups.user(1) | ||
*/ | ||
async user (user) { | ||
const {instanceName} = this.instance | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'GET' | ||
} | ||
|
||
try { | ||
const res = await fetch(`${this._getInstanceURL(instanceName)}/users/${user}/groups/`, options) | ||
|
||
return res.objects.map(elem => elem.group) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Add user to group | ||
* | ||
* @param {string} label - group name | ||
* @param {number} user - user id | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* groups.addUser('admin', 1) | ||
*/ | ||
async addUser (label, user) { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'POST', | ||
body: JSON.stringify({user}) | ||
} | ||
|
||
try { | ||
const group = await this.get(label) | ||
|
||
return await fetch(`${this.url()}${group.id}/users/`, options) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Remove user from group | ||
* | ||
* @param {string} label - group name | ||
* @param {number} user - user id | ||
* | ||
* @return {Promise} | ||
* | ||
* @example [@lang javascript] | ||
* groups.removeUser('admin', 1) | ||
*/ | ||
async removeUser (label, user) { | ||
const fetch = this.fetch.bind(this) | ||
const options = { | ||
method: 'DELETE' | ||
} | ||
|
||
try { | ||
const group = await this.get(label) | ||
|
||
return await fetch(`${this.url()}${group.id}/users/${user}/`, options) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
export default Groups |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import nock from 'nock' | ||
import Server from '../../src' | ||
import chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
|
||
chai.use(chaiAsPromised) | ||
chai.should() | ||
|
||
describe('Groups', () => { | ||
const instanceName = 'testInstance' | ||
const groupName = "testGroup" | ||
let api | ||
let groups | ||
|
||
beforeEach(() => { | ||
const server = new Server({ | ||
token: 'testKey', | ||
instanceName | ||
}) | ||
groups = server.groups | ||
api = nock(`https://${process.env.SYNCANO_HOST || 'api.syncano.io'}`) | ||
}) | ||
|
||
describe('#list', () => { | ||
it('get list of groups', async () => { | ||
const list = await groups.list() | ||
list.should.be.a('array') | ||
}) | ||
}) | ||
|
||
describe('#create', () => { | ||
it('Create group', async () => { | ||
const group = await groups.create(groupName, 'Test group') | ||
|
||
group.should.be.a('object') | ||
group.should.have.property('id') | ||
group.should.have.property('label').equal(groupName) | ||
}) | ||
}) | ||
|
||
describe('#get', () => { | ||
it('get group', async () => { | ||
const group = await groups.get(groupName) | ||
|
||
group.should.be.a('object') | ||
group.should.have.property('id') | ||
group.should.have.property('label').equal(groupName) | ||
}) | ||
}) | ||
|
||
describe('#addUser', () => { | ||
it('add user to group', async () => { | ||
const user = await groups.addUser(groupName, 1) | ||
|
||
user.should.be.a('object') | ||
user.should.have.property('user') | ||
}) | ||
}) | ||
|
||
describe('#removeUser', () => { | ||
it('remove user from group', () => { | ||
groups.removeUser(groupName, 1) | ||
}) | ||
}) | ||
|
||
describe('#delete', () => { | ||
it('delete group', () => { | ||
groups.delete(groupName) | ||
}) | ||
}) | ||
|
||
describe('#user', () => { | ||
it('get user groups', async () => { | ||
const userGroups = await groups.user(1) | ||
|
||
userGroups.should.be.a('object') | ||
}) | ||
}) | ||
}) |