Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

Commit

Permalink
feat: create authenticate helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cesconix committed Dec 17, 2018
1 parent edd7dea commit 95ed025
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/plugin-external-helpers": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"axios-mock-adapter": "^1.15.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"coveralls": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const authenticate = async ({ clientId, clientPassword }) => {
const reqAuth = { username: clientId, password: clientPassword }
const { data } = await axios.post(reqUrl, qs.stringify(reqData), { auth: reqAuth })
return data.access_token
} catch (err) {
return Promise.reject(new Error(err))
} catch (e) {
return Promise.reject(new Error(e))
}
}

Expand Down
23 changes: 21 additions & 2 deletions test/authenticate.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

import authenticate from '../src/helpers/authenticate'

describe('authenticate', async () => {
it('should fetch an access token', async () => {})
const mock = new MockAdapter(axios)

const url = 'https://account.demandware.com/dw/oauth2/access_token'
const credentials = { clientId: 'test', clientPassword: 'test' }

describe('authenticate', () => {
it('should fetch an access token', async () => {
expect.assertions(1)
const mockResponse = { access_token: 'token' }
mock.onPost(url).reply(200, mockResponse)
await expect(authenticate(credentials)).resolves.toEqual(mockResponse.access_token)
})

it('should fails with an error', async () => {
expect.assertions(1)
mock.onPost(url).networkError()
await expect(authenticate(credentials)).rejects.toThrow()
})
})

0 comments on commit 95ed025

Please sign in to comment.