Skip to content

Commit

Permalink
Fixes reset password flow fails due to incorrect policy #6
Browse files Browse the repository at this point in the history
  • Loading branch information
GSingh01 committed Jan 13, 2020
1 parent 04daaa5 commit 8605c63
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ad-b2c-react-native",
"version": "1.1.0",
"version": "1.1.1",
"description": "React Native Azure B2C auth using JS. This doesn't require eject from expo.",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -58,7 +58,7 @@
"react-addons-test-utils": "^15.6.2",
"react-dom": "^16.9.0",
"react-native": "^0.60.5",
"react-native-webview": "^7.1.0",
"react-native-webview": "^7.0.1",
"react-test-renderer": "^16.9.0"
}
}
9 changes: 3 additions & 6 deletions src/ADService.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ADService {
if (!this._isTokenValid(this.tokenResult)) {
const result = await this.fetchAndSetTokenAsync(
this.tokenResult.refreshToken,
false,
this.loginPolicy,
true
);

Expand All @@ -84,7 +84,7 @@ class ADService {

getIdToken = () => this.tokenResult.idToken;

fetchAndSetTokenAsync = async (authCode, isProfileEdit, isRefreshTokenGrant) => {
fetchAndSetTokenAsync = async (authCode, policy, isRefreshTokenGrant) => {
if (!authCode) {
return Result(false, 'Empty auth code');
}
Expand All @@ -105,10 +105,7 @@ class ADService {
}

const body = this.getFormUrlEncoded(params);
const url = this._getStaticURI(
isProfileEdit ? this.profileEditPolicy : this.loginPolicy,
'token',
);
const url = this._getStaticURI(policy, 'token');
const response = await fetch(url, {
method: 'POST',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/EditView.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class EditView extends React.PureComponent {
if (result.requestType === RequestType.Code) {
const reqResult = await adService.fetchAndSetTokenAsync(
result.data,
true,
adService.profileEditPolicy,
);
if (reqResult.isValid) {
this.props.onSuccess();
Expand Down
4 changes: 3 additions & 1 deletion src/LoginView.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export default class LoginView extends PureComponent {
}

if (result.requestType === RequestType.Code) {
const reqResult = await adService.fetchAndSetTokenAsync(result.data);
const policy = currentUri.indexOf(adService.passwordResetPolicy) > -1 ?
adService.passwordResetPolicy : adService.loginPolicy;
const reqResult = await adService.fetchAndSetTokenAsync(result.data, policy);
if (reqResult.isValid) {
this.props.onSuccess();
} else {
Expand Down
16 changes: 4 additions & 12 deletions tests/ADService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,25 +229,17 @@ describe('ADService', ()=>{
await testInvalidAuthCode(null);
});

test('calls fetch with correct parms when isProfileEdit not set', async ()=>{
await adService.fetchAndSetTokenAsync("testCode");

const expectedUrl = "https://testtenant.b2clogin.com/testtenant.onmicrosoft.com/testloginPolicy/oauth2/v2.0/token?";
const expectedArg2 = {"body": "client_id=testId&scope=testId%20offline_access&redirect_uri=test%2520redirectURI&grant_type=authorization_code&code=testCode", "headers": {"Content-Type": "application/x-www-form-urlencoded"}, "method": "POST"}

expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(expectedUrl,expectedArg2);
});

test('calls fetch with correct parms when isProfileEdit set', async ()=>{
await adService.fetchAndSetTokenAsync("testCode", true);
const policy = "testProfileEditPolicy";
await adService.fetchAndSetTokenAsync("testCode", policy);

const expectedUrl = "https://testtenant.b2clogin.com/testtenant.onmicrosoft.com/testProfileEditPolicy/oauth2/v2.0/token?";
const expectedUrl = `https://testtenant.b2clogin.com/testtenant.onmicrosoft.com/${policy}/oauth2/v2.0/token?`;
const expectedArg2 = {"body": "client_id=testId&scope=testId%20offline_access&redirect_uri=test%2520redirectURI&grant_type=authorization_code&code=testCode", "headers": {"Content-Type": "application/x-www-form-urlencoded"}, "method": "POST"}

expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(expectedUrl,expectedArg2);
});

test('calls secureStore.setItemAsync with correct params', async ()=>{
const fetchResult = {
token_type:"testType",
Expand Down
15 changes: 7 additions & 8 deletions tests/EditView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import adService from '../src/ADService';

describe('EditView',()=>{
const props = {
onSuccess: jest.fn(),
onFail: jest.fn(),
renderLoading:()=><Text>loading</Text>
};

adService.init({
appId:"testAppId",
redirectURI:"test//redirectURI",
tenant:"TestTenant",
loginPolicy:"testLoginPolicy",
passwordResetPolicy:"testPasswordReset",
profileEditPolicy:"testProfileEditPolicy",
});
onSuccess: jest.fn(),
onFail: jest.fn(),
renderLoading:()=><Text>loading</Text>
};

adService.init(props);

test('renders correctly', () => {
const wrapper = shallow(<EditView {...props} />);
Expand Down Expand Up @@ -182,7 +181,7 @@ describe('EditView',()=>{
await callbackAsync({url:"doesNotMatter"});

expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledTimes(1);
expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledWith(expectedResult.data, true);
expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledWith(expectedResult.data, props.profileEditPolicy);
});

test('calls props.success correctly when fetchAndSetTokenAsync returns valid', async ()=> {
Expand Down
17 changes: 14 additions & 3 deletions tests/LoginView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,27 @@ describe('LoginView',() => {
adService.getLoginFlowResult.mockReturnValue(expectedResult);
});

test('calls fetchAndSetTokenAsync correctly', async ()=> {
test('calls fetchAndSetTokenAsync correctly when login uri', async ()=> {
adService.fetchAndSetTokenAsync = jest.fn();
adService.fetchAndSetTokenAsync.mockResolvedValue({isValid:true});

instance.state.uri = props.loginPolicy;

await callbackAsync({url:"doesNotMatter"});

expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledTimes(1);
expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledWith(expectedResult.data);
expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledWith(expectedResult.data, props.loginPolicy);
});

test('calls fetchAndSetTokenAsync correctly when password reset uri', async ()=> {
adService.fetchAndSetTokenAsync = jest.fn();
adService.fetchAndSetTokenAsync.mockResolvedValue({isValid:true});
instance.state.uri = props.passwordResetPolicy;

await callbackAsync({url:"doesNotMatter"});

expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledTimes(1);
expect(adService.fetchAndSetTokenAsync).toHaveBeenCalledWith(expectedResult.data, props.passwordResetPolicy);
});
test('calls props.success correctly when fetchAndSetTokenAsync returns valid', async ()=> {
props.onSuccess.mockClear();
adService.fetchAndSetTokenAsync = jest.fn();
Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/EditView.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

exports[`EditView renders correctly 1`] = `
<WebView
appId="testAppId"
cacheEnabled={true}
javaScriptEnabled={true}
loginPolicy="testLoginPolicy"
onError={[Function]}
onFail={[MockFunction]}
onNavigationStateChange={[Function]}
Expand All @@ -14,13 +16,17 @@ exports[`EditView renders correctly 1`] = `
"*",
]
}
passwordResetPolicy="testPasswordReset"
profileEditPolicy="testProfileEditPolicy"
redirectURI="test//redirectURI"
renderLoading={[Function]}
source={
Object {
"uri": "https://TestTenant.b2clogin.com/TestTenant.onmicrosoft.com/testProfileEditPolicy/oauth2/v2.0/authorize?client_id=testAppId&response_type=code&redirect_uri=test//redirectURI&response_mode=query&scope=testAppId%20offline_access",
}
}
startInLoadingState={true}
tenant="TestTenant"
useSharedProcessPool={true}
/>
`;

0 comments on commit 8605c63

Please sign in to comment.