-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds unit tests for basic auth with password
OKTA-382083 <<<Jenkins Check-In of Tested SHA: 0b2579e for eng_productivity_ci_bot_okta@okta.com>>> Artifact: okta-auth-js Files changed count: 7 PR Link: "#733"
- Loading branch information
1 parent
6fcce31
commit 60e13da
Showing
7 changed files
with
900 additions
and
7 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,133 @@ | ||
import { authenticate } from '../../../lib/idx/authenticate'; | ||
|
||
jest.mock('../../../lib/idx/interact', () => { | ||
return { | ||
interact: () => {} | ||
}; | ||
}); | ||
|
||
const mocked = { | ||
interact: require('../../../lib/idx/interact'), | ||
}; | ||
|
||
describe('idx/authenticate', () => { | ||
let testContext; | ||
beforeEach(() => { | ||
const successResponse = { | ||
interactionCode: 'idx-interactionCode', | ||
neededToProceed: [] | ||
}; | ||
const identifyResponse = { | ||
proceed: () => Promise.resolve(successResponse), | ||
neededToProceed: [{ | ||
name: 'identify', | ||
value:[{ | ||
name: 'identifier', | ||
label: 'Username' | ||
}] | ||
}] | ||
}; | ||
const challengePasswordResponse = { | ||
proceed: () => Promise.resolve(successResponse), | ||
neededToProceed: [{ | ||
name: 'challenge-authenticator', | ||
value: [{ | ||
name: 'credentials' | ||
}], | ||
relatesTo: { | ||
value: { | ||
type: 'password' | ||
} | ||
} | ||
}] | ||
}; | ||
const interactResponse = { | ||
idxResponse: identifyResponse, | ||
stateHandle: 'idx-stateHandle' | ||
}; | ||
|
||
jest.spyOn(mocked.interact, 'interact').mockImplementation(() => testContext.interactResponse); | ||
|
||
const transactionMeta = { | ||
state: 'meta-state', | ||
codeVerifier: 'meta-code', | ||
clientId: 'meta-clientId', | ||
redirectUri: 'meta-redirectUri', | ||
scopes: ['meta'], | ||
urls: { authorizeUrl: 'meta-authorizeUrl' }, | ||
ignoreSignature: true | ||
}; | ||
const tokenResponse = { | ||
tokens: { | ||
fakeToken: true | ||
} | ||
}; | ||
const authClient = { | ||
transactionManager: { | ||
load: () => transactionMeta, | ||
clear: () => {} | ||
}, | ||
token: { | ||
exchangeCodeForTokens: () => Promise.resolve(tokenResponse) | ||
} | ||
}; | ||
jest.spyOn(authClient.token, 'exchangeCodeForTokens'); | ||
|
||
testContext = { | ||
interactResponse, | ||
successResponse, | ||
identifyResponse, | ||
challengePasswordResponse, | ||
tokenResponse, | ||
transactionMeta, | ||
authClient | ||
}; | ||
}); | ||
|
||
it('returns an auth transaction', async () => { | ||
const { authClient, tokenResponse } = testContext; | ||
const res = await authenticate(authClient, { username: 'fake' }); | ||
expect(res).toEqual({ | ||
'data': { | ||
'status': 0, | ||
'tokens': tokenResponse.tokens, | ||
}, | ||
'status': 0, | ||
'tokens': tokenResponse.tokens, | ||
}); | ||
}); | ||
|
||
describe('basic authentication', () => { | ||
beforeEach(() => { | ||
const { identifyResponse, challengePasswordResponse } = testContext; | ||
identifyResponse.proceed = () => Promise.resolve(challengePasswordResponse); | ||
jest.spyOn(identifyResponse, 'proceed'); | ||
jest.spyOn(challengePasswordResponse, 'proceed'); | ||
}); | ||
|
||
it('can authenticate, passing username and password', async () => { | ||
const { authClient, identifyResponse, challengePasswordResponse, tokenResponse } = testContext; | ||
const res = await authenticate(authClient, { username: 'fakeuser', password: 'fakepass' }); | ||
expect(res).toEqual({ | ||
'data': { | ||
'status': 0, | ||
'tokens': tokenResponse.tokens, | ||
}, | ||
'status': 0, | ||
'tokens': tokenResponse.tokens, | ||
}); | ||
expect(identifyResponse.proceed).toHaveBeenCalledWith('identify', { identifier: 'fakeuser' }); | ||
expect(challengePasswordResponse.proceed).toHaveBeenCalledWith('challenge-authenticator', { credentials: { passcode: 'fakepass' }}); | ||
expect(authClient.token.exchangeCodeForTokens).toHaveBeenCalledWith({ | ||
clientId: 'meta-clientId', | ||
codeVerifier: 'meta-code', | ||
ignoreSignature: true, | ||
interactionCode: 'idx-interactionCode', | ||
redirectUri: 'meta-redirectUri', | ||
scopes: ['meta'] | ||
}, { | ||
authorizeUrl: 'meta-authorizeUrl' | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.