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
ee06973
commit fb404b0
Showing
7 changed files
with
621 additions
and
2 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
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 TokenResource from './TokenResource'; | ||
|
||
/** | ||
* Token list class | ||
* | ||
* @class | ||
*/ | ||
export default class TokenList extends ListResource { | ||
/** | ||
* Creates a new {@link TokenList}. | ||
* | ||
* @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:account/token', traversal); | ||
this.ListClass = TokenList; | ||
this.ItemClass = TokenResource; | ||
} | ||
} |
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,71 @@ | ||
import Resource from './Resource'; | ||
|
||
/** | ||
* TokenResource class | ||
* | ||
* @class | ||
*/ | ||
export default class TokenResource extends Resource { | ||
/** | ||
* Whether or not this is the token which is used by the current session. | ||
* | ||
* @returns {boolean} whether or not this is the current token | ||
*/ | ||
isCurrent() { | ||
return this.getProperty('isCurrent'); | ||
} | ||
|
||
/** | ||
* Issued {@link Date} for this token. | ||
* | ||
* @returns {Date} issued date | ||
*/ | ||
issued() { | ||
return new Date(this.getProperty('issued')); | ||
} | ||
|
||
/** | ||
* Valid until {@link Date} for this token. | ||
* | ||
* @returns {Date} valid until date | ||
*/ | ||
validUntil() { | ||
return new Date(this.getProperty('validUntil')); | ||
} | ||
|
||
/** | ||
* Will return ipAddress property. | ||
* | ||
* @returns {string} the ipAddress. | ||
*/ | ||
getIpAddress() { | ||
return this.getProperty('ipAddress'); | ||
} | ||
|
||
/** | ||
* Will return ipAddressLocation property. | ||
* | ||
* @returns {string} the ipAddressLocation. | ||
*/ | ||
getIpAddressLocation() { | ||
return this.getProperty('ipAddressLocation'); | ||
} | ||
|
||
/** | ||
* Will return accessTokenID property. | ||
* | ||
* @returns {string} the token id. | ||
*/ | ||
getAccessTokenID() { | ||
return this.getProperty('accessTokenID'); | ||
} | ||
|
||
/** | ||
* Will return device property. | ||
* | ||
* @returns {object} the device property. | ||
*/ | ||
getDevice() { | ||
return this.getProperty('device'); | ||
} | ||
} |
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,117 @@ | ||
/* eslint no-unused-expressions:0 */ | ||
|
||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
const fs = require('fs'); | ||
|
||
const TokenResource = require('../lib/resources/TokenResource').default; | ||
const TokenList = require('../lib/resources/TokenList').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('Token ListResource', () => { | ||
let listJson; | ||
let list; | ||
before(() => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(`${__dirname}/mocks/token-list.json`, 'utf-8', (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(JSON.parse(res)); | ||
}); | ||
}) | ||
.then((json) => { | ||
listJson = json; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
list = new TokenList(listJson); | ||
}); | ||
afterEach(() => { | ||
list = null; | ||
}); | ||
it('should be instance of ListResource', () => { | ||
list.should.be.instanceOf(ListResource); | ||
}); | ||
it('should be instance of TokenLisst', () => { | ||
list.should.be.instanceOf(TokenList); | ||
}); | ||
it('should have TokenResource items', () => { | ||
list.getAllItems().forEach(item => item.should.be.instanceOf(TokenResource)); | ||
}); | ||
}); | ||
|
||
describe('Token Resource', () => { | ||
let resourceJson; | ||
let resource; | ||
before(() => { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(`${__dirname}/mocks/token-single.json`, 'utf-8', (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(JSON.parse(res)); | ||
}); | ||
}) | ||
.then((json) => { | ||
resourceJson = json; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
resource = new TokenResource(resourceJson); | ||
}); | ||
afterEach(() => { | ||
resource = null; | ||
}); | ||
it('should be instance of Resource', () => { | ||
resource.should.be.instanceOf(Resource); | ||
}); | ||
it('should be instance of TokenResource', () => { | ||
resource.should.be.instanceOf(TokenResource); | ||
}); | ||
it('should return boolean on isCurrent', () => { | ||
resource.isCurrent().should.be.false; | ||
}); | ||
|
||
const dateGetter = [ | ||
'issued', 'validUntil', | ||
]; | ||
dateGetter.forEach((name) => { | ||
it(`should call resource.getProperty with ${name}`, () => { | ||
const spy = sinon.spy(resource, 'getProperty'); | ||
|
||
const property = resource[name](); | ||
spy.should.have.been.called.once; | ||
spy.should.have.been.calledWith(name); | ||
property.toISOString().should.be.equal(resource.getProperty(name)); | ||
|
||
spy.restore(); | ||
}); | ||
}); | ||
|
||
const getter = [ | ||
'ipAddress', 'ipAddressLocation', | ||
'accessTokenID', 'device', | ||
]; | ||
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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.