Skip to content

Commit

Permalink
feat : plug user_page to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
SebDez committed May 14, 2017
1 parent b990d8f commit 7b393ce
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 38 deletions.
7 changes: 3 additions & 4 deletions src/app/common/auth/action/auth-action.js
Expand Up @@ -45,8 +45,8 @@ export default class AuthActions extends LucioleActions {
logUserIn (login, password) {
var token = null
return dispatch => {
return this.authService.logUserIn(login, password).then(res => {
token = res.body.access_token
return this.authService.logUserIn(login, password).then(mytoken => {
token = mytoken
return this.userService.getUserProfile(token)
}, err => {
const title = this.i18n.t('application.auth.tstFailTitle')
Expand All @@ -55,8 +55,7 @@ export default class AuthActions extends LucioleActions {
dispatch(this.logUserInFailureAction(err))
return Promise.reject(Constants.ERRORS.ALREADY_MANAGED)
})
.then(result => {
const user = result.body
.then(user => {
user.setToken(token)
dispatch(this.logUserInSuccessAction(user))
}, this.manageHttpErrors.bind(this))
Expand Down
12 changes: 3 additions & 9 deletions src/app/common/auth/action/auth-action.spec.js
Expand Up @@ -59,15 +59,9 @@ describe('AuthAction', () => {

describe('logUserIn', () => {
let mockService, mockUsrServ, mockActions, service, usrService, spy
const data = {
body: {
access_token: 'my-token'
}
}
const data = 'my-token'

const usrData = {
body: new User({username: 'my-username', mail: 'my-mail'})
}
const usrData = new User({username: 'my-username', mail: 'my-mail'})

beforeEach(() => {
service = new AuthService()
Expand Down Expand Up @@ -131,7 +125,7 @@ describe('AuthAction', () => {
mockActions.expects('logUserInSuccessAction').returns('logUserInSuccessAction-result')
spy = chai.spy.on(actions, 'logUserInSuccessAction')
actions.logUserIn('my-login', 'my-password')(TestHelper.dispatch).then(() => {
expect(spy).to.have.been.called.with(usrData.body)
expect(spy).to.have.been.called.with(usrData)
done()
})
})
Expand Down
13 changes: 12 additions & 1 deletion src/app/common/auth/service/api/auth-api.js
Expand Up @@ -15,7 +15,9 @@ export default class AuthApi extends LucioleApi {
const endpoint = this.getAppEndpoint()
const body = this.encodeLogData(login, password)
const uri = `${endpoint}/oauth2/token/owner`
return this.requestHelper.post(uri, body)
return this.requestHelper.post(uri, body).then(res => {
return this.decodeToken(res.body)
})
}

/**
Expand All @@ -29,6 +31,15 @@ export default class AuthApi extends LucioleApi {
return this.requestHelper.post(uri)
}

/**
* Decode JSON response to a Token object
* @param {Object} json The json to decode
* @return {Object} A Token object
*/
decodeToken (json) {
return json && json.access_token ? json.access_token : null
}

/**
* Encode log body to valid JSON format
* @param {string} login The user's login to use
Expand Down
50 changes: 47 additions & 3 deletions src/app/common/auth/service/api/auth-api.spec.js
Expand Up @@ -9,33 +9,62 @@ let sinon = require('sinon')

describe('AuthApi', () => {
describe('logUserIn', () => {
var serv, rHelper, mockService
var serv, rHelper, mockService, mockRequestHelper

beforeEach(() => {
rHelper = new MockRequestHelper()
serv = new AuthApi()
mockRequestHelper = sinon.mock(rHelper)
serv.requestHelper = rHelper
mockService = sinon.mock(serv)
})

afterEach(() => {
mockService.verify()
mockService.restore()
mockRequestHelper.verify()
mockRequestHelper.restore()
})

it('Expect to return a promise', () => {
mockService.expects('getAppEndpoint').returns('endpoint')
let response = {body: {}}
mockRequestHelper.expects('post').resolves(response)
expect(serv.logUserIn('login', 'password')).to.be.an.instanceof(Promise)
})

it('Expect to have call post method', (done) => {
mockService.expects('getAppEndpoint').returns('endpoint')
mockService.expects('encodeLogData').returns('encodedLogData')
let response = {body: {}}
mockRequestHelper.expects('post').resolves(response)
let spy = chai.spy.on(rHelper, 'post')
let uri = 'endpoint/oauth2/token/owner'
let body = 'encodedLogData'
serv.logUserIn('login', 'password').then(() => {
expect(spy).to.have.been.called.with(uri, body)
expect(spy).to.have.been.called.with(uri, 'encodedLogData')
done()
})
})

it('Expect to have call decodeToken', (done) => {
mockService.expects('getAppEndpoint').returns('endpoint')
mockService.expects('encodeLogData').returns('encodedLogData')
let response = {body: {}}
mockRequestHelper.expects('post').resolves(response)
let spy = chai.spy.on(serv, 'decodeToken')
serv.logUserIn('login', 'password').then(() => {
expect(spy).to.have.been.called.with(response.body)
done()
})
})

it('Expect to resolve the token', (done) => {
mockService.expects('getAppEndpoint').returns('endpoint')
mockService.expects('encodeLogData').returns('encodedLogData')
let response = {body: {access_token: 'mytoken'}}
mockRequestHelper.expects('post').resolves(response)
serv.logUserIn('login', 'password').then(res => {
expect(res).to.equal('mytoken')
done()
})
})
Expand Down Expand Up @@ -94,4 +123,19 @@ describe('AuthApi', () => {
expect(serv.encodeLogData('login', 'password').grant_type).to.equals('password')
})
})

describe('decodeToken', () => {
var serv
beforeEach(() => {
serv = new AuthApi()
})

it('Expect to return null if there is no token', () => {
expect(serv.decodeToken(null)).to.equals(null)
})

it('Expect to return valid token', () => {
expect(serv.decodeToken({access_token: 'mytoken'})).to.equals('mytoken')
})
})
})
7 changes: 3 additions & 4 deletions src/app/common/auth/service/auth-service.js
@@ -1,5 +1,4 @@
// import AuthApi from './api/auth-api'
import AuthMockApi from './mock/auth-mock'
import AuthApi from './api/auth-api'

/**
* Class for Authentication Service
Expand All @@ -11,8 +10,8 @@ export default class AuthService {
* Create a new AuthService
*/
constructor () {
/** @type {AuthMockApi} The api service to use */
this.api = new AuthMockApi()
/** @type {AuthApi} The api service to use */
this.api = new AuthApi()
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/app/common/component/avatar/user-avatar-component.js
@@ -1,5 +1,6 @@
import React, { PropTypes } from 'react'
import LucioleComponent from './../../core/abstract/luciole-component'
import Constants from './../../constants'

/**
* UserAvatar Component
Expand All @@ -12,9 +13,10 @@ class UserAvatar extends LucioleComponent {
* @return {Object} React component tree
*/
render () {
const src = this.props.src ? this.props.src : Constants.USER.AVATAR.DEFAULT
return (
<div className='user-avatar'>
<img src={this.props.src} />
<img src={src} />
</div>)
}
}
Expand All @@ -24,7 +26,7 @@ class UserAvatar extends LucioleComponent {
* @type {Object}
*/
UserAvatar.propTypes = {
src: PropTypes.string.isRequired
src: PropTypes.string
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/app/common/component/avatar/user-avatar-component.spec.js
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import {shallow} from 'enzyme'
import {expect} from 'chai'
import UserAvatar from './user-avatar-component'
import Constants from './../../constants'

describe('UserAvatar', () => {
describe('render', () => {
Expand All @@ -16,11 +17,19 @@ describe('UserAvatar', () => {
expect(actual).to.be.equal(expected)
})

it('Expect to contain an img with valid src', () => {
it('Expect to contain an img with prop src if given', () => {
const wrapper = shallow(<UserAvatar {...props} />)
const actual = wrapper.find('img').prop('src')
const expected = 'imgsrc'
expect(actual).to.be.equal(expected)
})

it('Expect to contain an img with default prop src if not given', () => {
const pp = { src: null }
const wrapper = shallow(<UserAvatar {...pp} />)
const actual = wrapper.find('img').prop('src')
const expected = Constants.USER.AVATAR.DEFAULT
expect(actual).to.be.equal(expected)
})
})
})
3 changes: 3 additions & 0 deletions src/app/common/constants.js
Expand Up @@ -41,6 +41,9 @@ export default {
GENDER: {
1: 'application.user.gender.male',
2: 'application.user.gender.female'
},
AVATAR: {
DEFAULT: 'https://thumbs.dreamstime.com/x/luciole-rougeoyante-5892712.jpg'
}
}
}
2 changes: 1 addition & 1 deletion src/app/common/helper/rest-helper.js
Expand Up @@ -24,7 +24,7 @@ export default class RestHelper {
* @type {Object} error The error received
*/
manageErrors (error) {
const httpCode = error.httpCode
const httpCode = error.response && error.response.status ? error.response.status : null
const errorCode = this.managedCodes.indexOf(httpCode) > -1 ? httpCode : 'other'
const httpResponses = {
401: {
Expand Down
17 changes: 12 additions & 5 deletions src/app/common/helper/rest-helper.spec.js
Expand Up @@ -23,37 +23,44 @@ describe('RestHelper', () => {
describe('manageErrors', () => {
it('Expect to call toastrHelper showMessage with good type param, case 401', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({httpCode: 401})
service.manageErrors({response: {status: 401}})
const expected = ['warning', 'httpErrors.401.title', 'httpErrors.401.message', null]
expect(spy).to.have.been.called.with(...expected)
})

it('Expect to call toastrHelper showMessage with good type param, case 403', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({httpCode: 403})
service.manageErrors({response: {status: 403}})
const expected = ['warning', 'httpErrors.403.title', 'httpErrors.403.message', null]
expect(spy).to.have.been.called.with(...expected)
})

it('Expect to call toastrHelper showMessage with good type param, case 500', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({httpCode: 500})
service.manageErrors({response: {status: 500}})
const expected = ['error', 'httpErrors.500.title', 'httpErrors.500.message', null]
expect(spy).to.have.been.called.with(...expected)
})

it('Expect to call toastrHelper showMessage with good type param, case other', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({httpCode: '62fdfother'})
service.manageErrors({response: {status: '62fdfother'}})
const expected = ['error', 'httpErrors.other.title', 'httpErrors.other.message', null]
expect(spy).to.have.been.called.with(...expected)
})

it('Expect to call toastrHelper showMessage with good type param, case no httpcode', () => {
it('Expect to call toastrHelper showMessage with good type param, case no response', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({})
const expected = ['error', 'httpErrors.other.title', 'httpErrors.other.message', null]
expect(spy).to.have.been.called.with(...expected)
})

it('Expect to call toastrHelper showMessage with good type param, case no status', () => {
spy = chai.spy.on(service.toastrHelper, 'showMessage')
service.manageErrors({response: null})
const expected = ['error', 'httpErrors.other.title', 'httpErrors.other.message', null]
expect(spy).to.have.been.called.with(...expected)
})
})
})
7 changes: 5 additions & 2 deletions src/app/common/helper/toString-helper.js
Expand Up @@ -21,7 +21,10 @@ export default class ToStringHelper {
* @return {string} The luciole date string
*/
dateToString (dateToConvert) {
return dateToConvert.toLocaleDateString()
if (dateToConvert && dateToConvert.toLocaleDateString) {
return dateToConvert.toLocaleDateString()
}
return null
}

/**
Expand All @@ -30,7 +33,7 @@ export default class ToStringHelper {
* @return {string} The gender string
*/
genderToString (genderCode) {
return this.i18n.t(Constants.USER.GENDER[genderCode])
return Constants.USER.GENDER[genderCode] ? this.i18n.t(Constants.USER.GENDER[genderCode]) : null
}

/**
Expand Down
22 changes: 20 additions & 2 deletions src/app/common/helper/toString-helper.spec.js
Expand Up @@ -16,15 +16,25 @@ describe('ToStringHelper', () => {
})

describe('genderToString', () => {
it('Expect to return a string', () => {
it('Expect to return a string if gender exist', () => {
expect(typeof service.genderToString(1)).to.equal('string')
})

it('Expect to have call i18n.t', () => {
it('Expect to have call i18n.t if gender exist', () => {
spy = chai.spy.on(service.i18n, 't')
service.genderToString(1)
expect(spy).to.have.been.called.once
})

it('Expect to return null if gender not exist', () => {
expect(service.genderToString(23)).to.equal(null)
})

it('Expect to not call i18n.t if gender not exist', () => {
spy = chai.spy.on(service.i18n, 't')
service.genderToString(23)
expect(spy).not.to.have.been.called
})
})

describe('cityToString', () => {
Expand Down Expand Up @@ -58,5 +68,13 @@ describe('ToStringHelper', () => {
service.dateToString(mydate)
expect(spy).to.have.been.called.once
})

it('Expect to return null if no date given', () => {
expect(service.dateToString(null)).to.equal(null)
})

it('Expect to return null if given date is not a date', () => {
expect(service.dateToString('notadate')).to.equal(null)
})
})
})

0 comments on commit 7b393ce

Please sign in to comment.