-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from screwdriver-cd/user-tokens
feat: Add token models and tests
- Loading branch information
Showing
12 changed files
with
424 additions
and
9 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
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 @@ | ||
'use strict'; | ||
|
||
const BaseModel = require('./base'); | ||
|
||
class TokenModel extends BaseModel { | ||
/** | ||
* Construct a TokenModel object | ||
* @method constructor | ||
* @param {Object} config | ||
* @param {Object} config.datastore Object that will perform operations on the datastore | ||
* @param {Number} config.userId The ID of the associated user | ||
* @param {String} config.uuid UUID for revoking the token | ||
* @param {String} config.name The token name | ||
* @param {String} config.description The token description | ||
* @param {String} config.lastUsed The last time the token was used (ISO String) | ||
*/ | ||
constructor(config) { | ||
super('token', config); | ||
} | ||
} | ||
|
||
module.exports = TokenModel; |
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,59 @@ | ||
'use strict'; | ||
|
||
const BaseFactory = require('./baseFactory'); | ||
const Token = require('./token'); | ||
|
||
let instance; | ||
|
||
class TokenFactory extends BaseFactory { | ||
/** | ||
* Construct a TokenFactory object | ||
* @method constructor | ||
* @param {Object} config | ||
* @param {Object} config.datastore Object that will perform operations on the datastore | ||
*/ | ||
constructor(config) { | ||
super('token', config); | ||
} | ||
|
||
/** | ||
* Instantiate a Token class | ||
* @method createClass | ||
* @param {Object} config | ||
* @return {Token} | ||
*/ | ||
createClass(config) { | ||
return new Token(config); | ||
} | ||
|
||
/** | ||
* Create a token model | ||
* @method create | ||
* @param {Object} config | ||
* @param {String} config.userId The ID of the associated user | ||
* @param {String} config.value The token value | ||
* @param {String} config.name The token name | ||
* @param {String} config.description The token description | ||
* @return {Promise} | ||
*/ | ||
create(config) { | ||
config.lastUsed = null; | ||
|
||
return super.create(config); | ||
} | ||
|
||
/** | ||
* Get an instance of the TokenFactory | ||
* @method getInstance | ||
* @param {Object} config | ||
* @param {Datastore} config.datastore A datastore instance | ||
* @return {TokenFactory} | ||
*/ | ||
static getInstance(config) { | ||
instance = BaseFactory.getInstance(TokenFactory, instance, config); | ||
|
||
return instance; | ||
} | ||
} | ||
|
||
module.exports = TokenFactory; |
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
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
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,75 @@ | ||
'use strict'; | ||
|
||
const assert = require('chai').assert; | ||
const sinon = require('sinon'); | ||
const mockery = require('mockery'); | ||
const schema = require('screwdriver-data-schema'); | ||
const BaseModel = require('../../lib/base'); | ||
const TokenModel = require('../../lib/token'); | ||
|
||
sinon.assert.expose(assert, { prefix: '' }); | ||
require('sinon-as-promised'); | ||
|
||
describe('Token Model', () => { | ||
const password = 'password'; | ||
let datastore; | ||
let createConfig; | ||
let token; | ||
|
||
before(() => { | ||
mockery.enable({ | ||
useCleanCache: true, | ||
warnOnUnregistered: false | ||
}); | ||
datastore = { | ||
update: sinon.stub() | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
datastore.update.resolves({}); | ||
|
||
createConfig = { | ||
datastore, | ||
userId: 12345, | ||
uuid: '1a2b3c', | ||
id: 6789, | ||
name: 'Mobile client auth token', | ||
description: 'For the mobile app', | ||
lastUsed: '2017-05-10T01:49:59.327Z', | ||
password | ||
}; | ||
token = new TokenModel(createConfig); | ||
}); | ||
|
||
after(() => { | ||
mockery.disable(); | ||
}); | ||
|
||
it('is constructed properly', () => { | ||
assert.instanceOf(token, TokenModel); | ||
assert.instanceOf(token, BaseModel); | ||
schema.models.token.allKeys.forEach((key) => { | ||
assert.strictEqual(token[key], createConfig[key]); | ||
}); | ||
}); | ||
|
||
describe('update', () => { | ||
it('promises to update a token', () => { | ||
const newTimestamp = '2017-05-13T02:01:17.588Z'; | ||
|
||
token.lastUsed = newTimestamp; | ||
|
||
return token.update() | ||
.then(() => { | ||
assert.calledWith(datastore.update, { | ||
table: 'tokens', | ||
params: { | ||
id: 6789, | ||
lastUsed: newTimestamp | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.