New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[msal-node][msal-common] unit tests for msal-node changes #1449
Merged
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a4eb6d5
Add tests for msal-node
sangonzal ba91efe
Add test for getAuthCodeUrl
sangonzal 077f747
Merge branch 'sagonzal/device-code' into sagonzal/msal-node-tests
sangonzal 545e446
Getting all tests in msal-common and msal-browser green
sangonzal 79dfcaa
fix credscan errors
sangonzal dc39c0a
Bring up test coverage
sangonzal 1939bba
Fix error. Update device code sample
sangonzal 5a2016b
Make lint happy
sangonzal 4bcb14e
Merge branch 'sagonzal/device-code' into sagonzal/msal-node-tests
sangonzal 73f9a0c
Switch strictNullChecks: true for msal-node
sangonzal 9d999e3
Update gitignore
sangonzal d923b93
Merge pull request #1478 from AzureAD/sagonzal/msal-node-strict-null-…
sangonzal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
157 changes: 157 additions & 0 deletions
157
lib/msal-common/test/client/AuthorizationCodeClient.spec.ts
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,157 @@ | ||
import chai from "chai"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import sinon from "sinon"; | ||
import { | ||
Authority, | ||
AuthorizationCodeClient, | ||
AuthorizationCodeRequest, | ||
AuthorizationCodeUrlRequest, | ||
Configuration, | ||
Constants | ||
} from "../../src"; | ||
import { | ||
ALTERNATE_OPENID_CONFIG_RESPONSE, | ||
AUTHENTICATION_RESULT, | ||
DEFAULT_OPENID_CONFIG_RESPONSE, | ||
TEST_CONFIG, | ||
TEST_TOKENS, | ||
TEST_URIS | ||
} from "../utils/StringConstants"; | ||
import {BaseClient} from "../../src/client/BaseClient"; | ||
import {AADServerParamKeys, PromptValue, ResponseMode, SSOTypes} from "../../src/utils/Constants"; | ||
import {ClientTestUtils} from "./ClientTestUtils"; | ||
|
||
const expect = chai.expect; | ||
chai.use(chaiAsPromised); | ||
|
||
describe("AuthorizationCodeClient unit tests", () => { | ||
|
||
let config: Configuration; | ||
|
||
beforeEach(() => { | ||
config = ClientTestUtils.createTestClientConfiguration(); | ||
}); | ||
|
||
describe("Constructor", () => { | ||
|
||
it("creates a AuthorizationCodeClient", () => { | ||
const client = new AuthorizationCodeClient(config); | ||
expect(client).to.be.not.null; | ||
expect(client instanceof AuthorizationCodeClient).to.be.true; | ||
expect(client instanceof BaseClient).to.be.true; | ||
}); | ||
}); | ||
|
||
describe("Authorization url creation", () => { | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
|
||
it("Creates an authorization url with default parameters", async () => { | ||
|
||
sinon.stub(Authority.prototype, <any>"discoverEndpoints").resolves(DEFAULT_OPENID_CONFIG_RESPONSE); | ||
let client = new AuthorizationCodeClient(config); | ||
|
||
const authCodeUrlRequest: AuthorizationCodeUrlRequest = { | ||
redirectUri: TEST_URIS.TEST_REDIRECT_URI_LOCALHOST, | ||
scopes: TEST_CONFIG.DEFAULT_GRAPH_SCOPE | ||
}; | ||
const loginUrl = await client.getAuthCodeUrl(authCodeUrlRequest); | ||
expect(loginUrl).to.contain(Constants.DEFAULT_AUTHORITY); | ||
expect(loginUrl).to.contain(DEFAULT_OPENID_CONFIG_RESPONSE.body.authorization_endpoint.replace("{tenant}", "common")); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.SCOPE}=${TEST_CONFIG.DEFAULT_GRAPH_SCOPE}%20${Constants.OPENID_SCOPE}%20${Constants.PROFILE_SCOPE}%20${Constants.OFFLINE_ACCESS_SCOPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_TYPE}=${Constants.CODE_RESPONSE_TYPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(Constants.QUERY_RESPONSE_MODE)}`) | ||
}); | ||
|
||
it("Creates an authorization url passing in a default scope", async () => { | ||
|
||
sinon.stub(Authority.prototype, <any>"discoverEndpoints").resolves(DEFAULT_OPENID_CONFIG_RESPONSE); | ||
let client = new AuthorizationCodeClient(config); | ||
|
||
const authCodeUrlRequest: AuthorizationCodeUrlRequest = { | ||
redirectUri: TEST_URIS.TEST_REDIRECT_URI_LOCALHOST, | ||
scopes: [Constants.OPENID_SCOPE] | ||
}; | ||
const loginUrl = await client.getAuthCodeUrl(authCodeUrlRequest); | ||
expect(loginUrl).to.contain(Constants.DEFAULT_AUTHORITY); | ||
expect(loginUrl).to.contain(DEFAULT_OPENID_CONFIG_RESPONSE.body.authorization_endpoint.replace("{tenant}", "common")); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.SCOPE}=${Constants.OPENID_SCOPE}%20${Constants.PROFILE_SCOPE}%20${Constants.OFFLINE_ACCESS_SCOPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_TYPE}=${Constants.CODE_RESPONSE_TYPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(Constants.QUERY_RESPONSE_MODE)}`) | ||
}); | ||
|
||
it("Creates an authorization url passing in optional parameters", async () => { | ||
|
||
// Override with alternate authority openid_config | ||
sinon.stub(Authority.prototype, <any>"discoverEndpoints").resolves(ALTERNATE_OPENID_CONFIG_RESPONSE); | ||
let client = new AuthorizationCodeClient(config); | ||
|
||
const authCodeUrlRequest: AuthorizationCodeUrlRequest = { | ||
redirectUri: TEST_URIS.TEST_REDIRECT_URI_LOCALHOST, | ||
scopes: TEST_CONFIG.DEFAULT_GRAPH_SCOPE, | ||
authority: TEST_CONFIG.alternateValidAuthority, | ||
responseMode: ResponseMode.FORM_POST, | ||
codeChallenge: TEST_CONFIG.TEST_CHALLENGE, | ||
codeChallengeMethod: TEST_CONFIG.CODE_CHALLENGE_METHOD, | ||
state: TEST_CONFIG.STATE, | ||
prompt: PromptValue.SELECT_ACCOUNT, | ||
loginHint: TEST_CONFIG.LOGIN_HINT, | ||
domainHint: TEST_CONFIG.DOMAIN_HINT, | ||
claims: TEST_CONFIG.CLAIMS, | ||
nonce: TEST_CONFIG.NONCE, | ||
}; | ||
const loginUrl = await client.getAuthCodeUrl(authCodeUrlRequest); | ||
expect(loginUrl).to.contain(TEST_CONFIG.alternateValidAuthority); | ||
expect(loginUrl).to.contain(ALTERNATE_OPENID_CONFIG_RESPONSE.body.authorization_endpoint.replace("{tenant}", "common")); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.SCOPE}=${TEST_CONFIG.DEFAULT_GRAPH_SCOPE}%20${Constants.OPENID_SCOPE}%20${Constants.PROFILE_SCOPE}%20${Constants.OFFLINE_ACCESS_SCOPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_TYPE}=${Constants.CODE_RESPONSE_TYPE}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.RESPONSE_MODE}=${encodeURIComponent(ResponseMode.FORM_POST)}`) | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.STATE}=${encodeURIComponent(TEST_CONFIG.STATE)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.NONCE}=${encodeURIComponent(TEST_CONFIG.NONCE)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CODE_CHALLENGE}=${encodeURIComponent(TEST_CONFIG.TEST_CHALLENGE)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CODE_CHALLENGE_METHOD}=${encodeURIComponent(TEST_CONFIG.CODE_CHALLENGE_METHOD)}`); | ||
expect(loginUrl).to.contain(`${SSOTypes.LOGIN_HINT}=${encodeURIComponent(TEST_CONFIG.LOGIN_HINT)}`); | ||
expect(loginUrl).to.contain(`${SSOTypes.DOMAIN_HINT}=${encodeURIComponent(TEST_CONFIG.DOMAIN_HINT)}`); | ||
expect(loginUrl).to.contain(`${AADServerParamKeys.CLAIMS}=${encodeURIComponent(TEST_CONFIG.CLAIMS)}`); | ||
}); | ||
}); | ||
|
||
describe("Acquire a token", () => { | ||
|
||
it("Acquires a token successfully", async () => { | ||
|
||
sinon.stub(Authority.prototype, <any>"discoverEndpoints").resolves(DEFAULT_OPENID_CONFIG_RESPONSE); | ||
sinon.stub(AuthorizationCodeClient.prototype, "executePostToTokenEndpoint").resolves(AUTHENTICATION_RESULT); | ||
const createTokenRequestBodySpy = sinon.spy(AuthorizationCodeClient.prototype, "createTokenRequestBody"); | ||
|
||
const client = new AuthorizationCodeClient(config); | ||
const authCodeRequest: AuthorizationCodeRequest = { | ||
scopes: TEST_CONFIG.DEFAULT_GRAPH_SCOPE, | ||
redirectUri: TEST_URIS.TEST_REDIRECT_URI_LOCALHOST, | ||
code: TEST_TOKENS.AUTHORIZATION_CODE | ||
}; | ||
|
||
const authenticationResult = await client.acquireToken(authCodeRequest); | ||
|
||
expect(JSON.parse(authenticationResult)).to.deep.eq(AUTHENTICATION_RESULT.body); | ||
expect(createTokenRequestBodySpy.calledWith(authCodeRequest)).to.be.ok; | ||
|
||
console.log(createTokenRequestBodySpy.returnValues); | ||
|
||
expect(createTokenRequestBodySpy.returnValues[0]).to.contain(`${AADServerParamKeys.SCOPE}=${TEST_CONFIG.DEFAULT_GRAPH_SCOPE}%20${Constants.OPENID_SCOPE}%20${Constants.PROFILE_SCOPE}%20${Constants.OFFLINE_ACCESS_SCOPE}`); | ||
expect(createTokenRequestBodySpy.returnValues[0]).to.contain(`${AADServerParamKeys.CLIENT_ID}=${TEST_CONFIG.MSAL_CLIENT_ID}`); | ||
expect(createTokenRequestBodySpy.returnValues[0]).to.contain(`${AADServerParamKeys.REDIRECT_URI}=${encodeURIComponent(TEST_URIS.TEST_REDIRECT_URI_LOCALHOST)}`); | ||
expect(createTokenRequestBodySpy.returnValues[0]).to.contain(`${AADServerParamKeys.CODE}=${TEST_TOKENS.AUTHORIZATION_CODE}`); | ||
expect(createTokenRequestBodySpy.returnValues[0]).to.contain(`${AADServerParamKeys.GRANT_TYPE}=${Constants.CODE_GRANT_TYPE}`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/.vscode/settings.json
I like having this in the repo because it we can use to setup things like linting, etc in the repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok cool I'll remove this from .gitignore