generated from nimblehq/git-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#36] Add tests to authAdapter and refactor
- Loading branch information
1 parent
90c772d
commit e3b555e
Showing
3 changed files
with
52 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,55 @@ | ||
import nock from 'nock'; | ||
|
||
import AuthAdapter from './authAdapter'; | ||
import AuthAdapter, { commonParams } from './authAdapter'; | ||
|
||
/* eslint-disable camelcase */ | ||
const testCredentials = { | ||
const mockLoginCredentials = { | ||
email: '[email protected]', | ||
password: 'password123', | ||
}; | ||
|
||
const commonLoginParams = { | ||
grant_type: 'password', | ||
client_id: process.env.REACT_APP_API_CLIENT_ID, | ||
client_secret: process.env.REACT_APP_API_CLIENT_SECRET, | ||
}; | ||
/* eslint-enable camelcase */ | ||
|
||
describe('AuthAdapter', () => { | ||
afterAll(() => { | ||
nock.cleanAll(); | ||
nock.restore(); | ||
}); | ||
|
||
describe('login', () => { | ||
describe('loginWithEmailPassword', () => { | ||
test('The login endpoint is called with credientials from the request', async () => { | ||
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.post('/oauth/token', { | ||
...testCredentials, | ||
...commonLoginParams, | ||
...mockLoginCredentials, | ||
...commonParams, | ||
grant_type: 'password', | ||
}) | ||
.reply(200); | ||
|
||
expect(scope.isDone()).toBe(false); | ||
await AuthAdapter.loginWithEmailPassword({ ...mockLoginCredentials }); | ||
expect(scope.isDone()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('loginWithRefreshToken', () => { | ||
test('The refresh token endpoint is called with refresh token from the request', async () => { | ||
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`) | ||
.defaultReplyHeaders({ | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-credentials': 'true', | ||
}) | ||
.post('/oauth/token', { | ||
refresh_token: 'refresh_token', | ||
...commonParams, | ||
grant_type: 'refresh_token', | ||
}) | ||
.reply(200); | ||
|
||
expect(scope.isDone()).toBe(false); | ||
await AuthAdapter.loginWithEmailPassword({ ...testCredentials }); | ||
await AuthAdapter.loginWithRefreshToken('refresh_token'); | ||
expect(scope.isDone()).toBe(true); | ||
}); | ||
}); | ||
|
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