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
bd5b9a7
commit 6d43a25
Showing
10 changed files
with
486 additions
and
1 deletion.
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
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,22 @@ | ||
import ListResource from './ListResource'; | ||
import GroupResource from './GroupResource'; | ||
|
||
/** | ||
* GroupList list class | ||
* | ||
* @class | ||
*/ | ||
export default class GroupList extends ListResource { | ||
/** | ||
* Creates a new {@link GroupList}. | ||
* | ||
* @param {object} resource resource loaded from the API. | ||
* @param {string} environment the environment this resource is associated to. | ||
* @param {?object} traversal traversal from which traverson can continue. | ||
*/ | ||
constructor(resource, environment, traversal) { | ||
super(resource, environment, 'ec:acc/groups', traversal); | ||
this.ListClass = GroupList; | ||
this.ItemClass = GroupResource; | ||
} | ||
} |
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 Resource from './Resource'; | ||
|
||
/** | ||
* GroupResource class | ||
* | ||
* @class | ||
*/ | ||
export default class GroupResource extends Resource { | ||
/** | ||
* Will return groupID property. | ||
* | ||
* @returns {string} the groupID. | ||
*/ | ||
getGroupID() { | ||
return this.getProperty('groupID'); | ||
} | ||
|
||
/** | ||
* Will return name property. | ||
* | ||
* @returns {string} the name. | ||
*/ | ||
getName() { | ||
return this.getProperty('name'); | ||
} | ||
|
||
/** | ||
* Set a new value to name property. | ||
* | ||
* @param {string} value the value to assign. | ||
* @returns {GroupResource} this Resource for chainability | ||
*/ | ||
setName(value) { | ||
if (!value) { | ||
throw new Error('name must be defined'); | ||
} | ||
|
||
return this.setProperty('name', value); | ||
} | ||
|
||
/** | ||
* Will return permissions property. | ||
* | ||
* @returns {array<string>} the config. | ||
*/ | ||
getPermissions() { | ||
return this.getProperty('permissions'); | ||
} | ||
|
||
/** | ||
* Set a new value to permissions property. | ||
* | ||
* @param {array<string>} value the value to assign. | ||
* @returns {GroupResource} this Resource for chainability | ||
*/ | ||
setPermissions(value) { | ||
if (!value) { | ||
throw new Error('permissions must be defined'); | ||
} | ||
|
||
return this.setProperty('permissions', value); | ||
} | ||
|
||
/** | ||
* Adds a new permission to permissions array. | ||
* | ||
* @param {string} value the permission to add. | ||
* @returns {AccountResource} this Resource for chainability | ||
*/ | ||
addPermission(value) { | ||
if (!value) { | ||
throw new Error('permission must be defined'); | ||
} | ||
|
||
const current = this.getPermissions(); | ||
current.push(value); | ||
return this.setPermissions(current); | ||
} | ||
} |
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,119 @@ | ||
/* eslint no-unused-expressions:0 */ | ||
|
||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
const fs = require('fs'); | ||
|
||
const GroupResource = require('../lib/resources/GroupResource').default; | ||
const GroupList = require('../lib/resources/GroupList').default; | ||
const Resource = require('../lib/resources/Resource').default; | ||
const ListResource = require('../lib/resources/ListResource').default; | ||
|
||
const should = chai.should(); | ||
chai.use(sinonChai); | ||
|
||
function capitalizeFirstLetter(string) { | ||
return string.charAt(0).toUpperCase() + string.slice(1); | ||
} | ||
|
||
describe('Group ListResource', () => { | ||
let listJson; | ||
let list; | ||
before(() => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(`${__dirname}/mocks/group-list.json`, 'utf-8', (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(JSON.parse(res)); | ||
}); | ||
}) | ||
.then((json) => { | ||
listJson = json; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
list = new GroupList(listJson); | ||
}); | ||
afterEach(() => { | ||
list = null; | ||
}); | ||
it('should be instance of ListResource', () => { | ||
list.should.be.instanceOf(ListResource); | ||
}); | ||
it('should be instance of GroupList', () => { | ||
list.should.be.instanceOf(GroupList); | ||
}); | ||
it('should have TokenResource items', () => { | ||
list.getAllItems().forEach(item => item.should.be.instanceOf(GroupResource)); | ||
}); | ||
}); | ||
|
||
describe('Group Resource', () => { | ||
let resourceJson; | ||
let resource; | ||
before(() => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(`${__dirname}/mocks/group-single.json`, 'utf-8', (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(JSON.parse(res)); | ||
}); | ||
}) | ||
.then((json) => { | ||
resourceJson = json; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
resource = new GroupResource(resourceJson); | ||
}); | ||
afterEach(() => { | ||
resource = null; | ||
}); | ||
it('should be instance of Resource', () => { | ||
resource.should.be.instanceOf(Resource); | ||
}); | ||
it('should be instance of GroupResource', () => { | ||
resource.should.be.instanceOf(GroupResource); | ||
}); | ||
it('should add single permission', () => { | ||
resource.getPermissions().should.have.property('length', 2); | ||
resource.addPermission('acc:something'); | ||
resource.getPermissions().should.have.property('length', 3); | ||
}); | ||
|
||
const getter = [ | ||
'groupID', 'name', 'permissions', | ||
]; | ||
getter.forEach((name) => { | ||
it(`should call resource.getProperty with ${name}`, () => { | ||
const spy = sinon.spy(resource, 'getProperty'); | ||
|
||
const property = resource[`get${capitalizeFirstLetter(name)}`](); | ||
spy.should.have.been.called.once; | ||
spy.should.have.been.calledWith(name); | ||
property.should.be.equal(resource.getProperty(name)); | ||
|
||
spy.restore(); | ||
}); | ||
}); | ||
|
||
const setter = ['name', 'permissions']; | ||
setter.forEach((name) => { | ||
it(`should call resource.setProperty with ${name}`, () => { | ||
const spy = sinon.spy(resource, 'setProperty'); | ||
|
||
resource[`set${capitalizeFirstLetter(name)}`](resource.getProperty(name)); | ||
spy.should.have.been.called.once; | ||
spy.should.have.been.calledWith(name, resource.getProperty(name)); | ||
|
||
spy.restore(); | ||
}); | ||
it(`should throw on set${capitalizeFirstLetter(name)} with undefined value`, () => { | ||
const throws = () => resource[`set${capitalizeFirstLetter(name)}`](); | ||
throws.should.throw(Error); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.