Skip to content

Commit

Permalink
feat : join token to api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
SebDez committed May 8, 2017
1 parent dae4ae9 commit b079421
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/app/common/auth/action/auth-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ export default class AuthActions extends LucioleActions {
* @return {Object} The action to dispatch
*/
disconnectUser () {
return dispatch => {
return this.authService.disconnectUser().then(() => {
return (dispatch, getState) => {
const token = this.getTokenFromGetState(getState)
return this.authService.disconnectUser(token).then(() => {
dispatch(this.disconnectUserInSuccessAction())
}, err => {
dispatch(this.disconnectUserInFailureAction(err))
Expand Down
5 changes: 5 additions & 0 deletions src/app/common/auth/action/auth-action.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe('AuthAction', () => {
})

it('Expect to have call disconnectUser', (done) => {
mockActions.expects('getTokenFromGetState').resolves('mytoken')
mockService.expects('disconnectUser').resolves()
mockActions.expects('disconnectUserInSuccessAction').returns('disconnectUserInSuccessAction-result')
spy = chai.spy.on(actions, 'disconnectUser')
Expand All @@ -158,6 +159,7 @@ describe('AuthAction', () => {
})

it('Expect to have call dispatch with good params in case of success', (done) => {
mockActions.expects('getTokenFromGetState').resolves('mytoken')
mockService.expects('disconnectUser').resolves()
mockActions.expects('disconnectUserInSuccessAction').returns('disconnectUserInSuccessAction-result')
spy = chai.spy.on(TestHelper, 'dispatch')
Expand All @@ -168,6 +170,7 @@ describe('AuthAction', () => {
})

it('Expect to have call disconnectUserInSuccessAction in case of success', (done) => {
mockActions.expects('getTokenFromGetState').resolves('mytoken')
mockService.expects('disconnectUser').resolves()
mockActions.expects('disconnectUserInSuccessAction').returns('disconnectUserInSuccessAction-result')
spy = chai.spy.on(actions, 'disconnectUserInSuccessAction')
Expand All @@ -178,6 +181,7 @@ describe('AuthAction', () => {
})

it('Expect to have call dispatch with error value in case of failure', (done) => {
mockActions.expects('getTokenFromGetState').resolves('mytoken')
mockService.expects('disconnectUser').resolves(Promise.reject('error'))
mockActions.expects('disconnectUserInFailureAction').returns('disconnectUserInFailureAction-result')
spy = chai.spy.on(TestHelper, 'dispatch')
Expand All @@ -188,6 +192,7 @@ describe('AuthAction', () => {
})

it('Expect to have call disconnectUserInFailureAction with good params in case of failure', (done) => {
mockActions.expects('getTokenFromGetState').resolves('mytoken')
mockService.expects('disconnectUser').resolves(Promise.reject('error'))
mockActions.expects('disconnectUserInFailureAction').returns('disconnectUserInFailureAction-result')
spy = chai.spy.on(actions, 'disconnectUserInFailureAction')
Expand Down
6 changes: 3 additions & 3 deletions src/app/common/auth/service/api/auth-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export default class AuthApi extends LucioleApi {

/**
* Disconnect an user
* @param {string} token The user's token
* @return {Object} A promise to resolve
*/
disconnectUser () {
// @TODO : add tokens to URI
disconnectUser (token) {
const endpoint = this.getAppEndpoint()
const uri = `${endpoint}/v1/logout`
const uri = this.addTokenQueryParamToUri(`${endpoint}/v1/logout`, token)
return this.requestHelper.post(uri)
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/common/auth/service/api/auth-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ describe('AuthApi', () => {

it('Expect to return a promise', () => {
mockService.expects('getAppEndpoint').returns('endpoint')
expect(serv.disconnectUser()).to.be.an.instanceof(Promise)
expect(serv.disconnectUser('mytoken')).to.be.an.instanceof(Promise)
})

it('Expect to have call post method', (done) => {
mockService.expects('getAppEndpoint').returns('endpoint')
let spy = chai.spy.on(rHelper, 'post')
let uri = 'endpoint/v1/logout'
serv.disconnectUser().then(() => {
let uri = 'endpoint/v1/logout?access_token=mytoken'
serv.disconnectUser('mytoken').then(() => {
expect(spy).to.have.been.called.with(uri)
done()
})
Expand Down
5 changes: 3 additions & 2 deletions src/app/common/auth/service/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export default class AuthService {

/**
* Disconnect an user
* @param {string} token The user's token
* @return {Object} A promise to resolve
*/
disconnectUser () {
return this.api.disconnectUser()
disconnectUser (token) {
return this.api.disconnectUser(token)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/app/common/auth/service/auth-service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('AuthService', () => {
describe('disconnectUser', () => {
it('Expect to have call api disconnectUser', (done) => {
spy = chai.spy.on(service.api, 'disconnectUser')
service.disconnectUser().then(() => {
expect(spy).to.have.been.called.once()
service.disconnectUser('mytoken').then(() => {
expect(spy).to.have.been.called.with('mytoken')
done()
})
})
Expand Down
9 changes: 9 additions & 0 deletions src/app/common/core/abstract/luciole-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class LucioleApi {
return `${this.appConf.api.url}:${this.appConf.api.port}`
}

/**
* Add token query params to api uri string
* @param {string} uri The uri to use
* @param {string} token The token to use
* @returns {string} The uri string with token query param
*/
addTokenQueryParamToUri (uri, token) {
return `${uri}?access_token=${token}`
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/app/common/core/abstract/luciole-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ describe('LucioleActions', () => {
expect(serv.getAppEndpoint()).to.equals('my-url:42')
})
})

describe('addTokenQueryParamToUri', () => {
var serv
beforeEach(() => {
serv = new LucioleApi()
})

it('Expect to return the endpoint', () => {
expect(serv.addTokenQueryParamToUri('my-uri', 'my-token')).to.equals('my-uri?access_token=my-token')
})
})
})

0 comments on commit b079421

Please sign in to comment.