Skip to content
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

Port Some UI Tests from ADAL to MSAL, Fixes AB#3139992 #2251

Merged
merged 14 commits into from
Feb 23, 2025
Merged
2 changes: 1 addition & 1 deletion common
Submodule common updated 21 files
+2 −0 LabApiUtilities/src/main/com/microsoft/identity/labapi/utilities/constants/LabConstants.java
+26 −63 LabApiUtilities/src/test/com/microsoft/identity/labapi/utilities/client/LabClientTest.java
+1 −0 changelog.txt
+23 −0 common/src/main/java/com/microsoft/identity/common/adal/internal/AuthenticationConstants.java
+6 −2 common/src/main/java/com/microsoft/identity/common/internal/providers/oauth2/AuthorizationActivityFactory.java
+42 −4 common/src/main/java/com/microsoft/identity/common/internal/ui/webview/AzureActiveDirectoryWebViewClient.java
+56 −0 ...src/main/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserChallenge.kt
+106 −0 ...n/src/main/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserHandler.kt
+125 −0 ...src/main/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserUriHelper.kt
+85 −0 ...c/test/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserHandlerTest.kt
+93 −0 ...test/java/com/microsoft/identity/common/internal/ui/webview/challengehandlers/SwitchBrowserUriHelperTest.kt
+5 −0 common4j/src/main/com/microsoft/identity/common/java/exception/ClientException.java
+1 −1 ...n/com/microsoft/identity/common/java/providers/microsoft/microsoftsts/MicrosoftStsAuthorizationRequest.java
+13 −0 uiautomationutilities/build.gradle
+17 −18 ...tilities/src/main/java/com/microsoft/identity/client/ui/automation/broker/BrokerMicrosoftAuthenticator.java
+12 −0 ...tionutilities/src/main/java/com/microsoft/identity/client/ui/automation/device/settings/GoogleSettings.java
+6 −0 uiautomationutilities/src/main/java/com/microsoft/identity/client/ui/automation/device/settings/ISettings.java
+5 −0 ...ionutilities/src/main/java/com/microsoft/identity/client/ui/automation/device/settings/SamsungSettings.java
+7 −0 uiautomationutilities/src/main/java/com/microsoft/identity/client/ui/automation/rules/RetryTestRule.java
+2 −1 uiautomationutilities/src/main/java/com/microsoft/identity/client/ui/automation/utils/CommonUtils.java
+15 −0 uiautomationutilities/src/main/java/com/microsoft/identity/client/ui/automation/utils/UiAutomatorUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.microsoft.identity.client.msal.automationapp.testpass.broker.crosscloud;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.microsoft.identity.client.Prompt;
import com.microsoft.identity.client.msal.automationapp.R;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthResult;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalAuthTestParams;
import com.microsoft.identity.client.msal.automationapp.sdk.MsalSdk;
import com.microsoft.identity.client.msal.automationapp.testpass.broker.AbstractMsalBrokerTest;
import com.microsoft.identity.client.ui.automation.TokenRequestTimeout;
import com.microsoft.identity.client.ui.automation.annotations.RetryOnFailure;
import com.microsoft.identity.labapi.utilities.client.LabGuestAccount;
import com.microsoft.identity.labapi.utilities.client.LabQuery;
import com.microsoft.identity.labapi.utilities.constants.LabConstants;
import com.microsoft.identity.labapi.utilities.constants.TempUserType;
import com.microsoft.identity.labapi.utilities.constants.UserType;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

// [Joined] Guest Support: Interactive and Silent Auth with MSAL Test app (Authenticator or Company Portal)
// https://identitydivision.visualstudio.com/Engineering/_workitems/edit/1400731/
@RetryOnFailure(retryCount = 2)
public class TestCase1400731 extends AbstractMsalBrokerTest {

@Test
public void test_1400731() throws Throwable {
// load a guest user account from the Lab
final LabGuestAccount labGuest = mLabClient.loadGuestAccountFromLab(getLabQuery());

final String username = "gcidlab@msidlab4.onmicrosoft.com";
final String password = mLabClient.getPasswordForGuestUser(labGuest);

//perform device registration
mBroker.performDeviceRegistration(username, password);

final MsalSdk msalSdk = new MsalSdk();

final MsalAuthTestParams authTestParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.scopes(Arrays.asList(mScopes))
.promptParameter(Prompt.SELECT_ACCOUNT)
.authority(LabConstants.MSID_LAB3)
.msalConfigResourceId(getConfigFileResourceId())
.build();

// start interactive acquire token request in MSAL (should succeed)
final MsalAuthResult authResult = msalSdk.acquireTokenInteractive(authTestParams, new com.microsoft.identity.client.ui.automation.interaction.OnInteractionRequired() {
@Override
public void handleUserInteraction() {
// Should be silent
}
}, TokenRequestTimeout.MEDIUM);

Assert.assertFalse(TextUtils.isEmpty(authResult.getAccessToken()));

final MsalAuthTestParams authTestParams2 = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.scopes(Arrays.asList(mScopes))
.promptParameter(Prompt.SELECT_ACCOUNT)
.authority(LabConstants.MSID_LAB4)
.msalConfigResourceId(getConfigFileResourceId())
.build();

// start interactive acquire token request in MSAL for msidlab4 (should succeed and be silent)
final MsalAuthResult authResult2 = msalSdk.acquireTokenInteractive(authTestParams2, new com.microsoft.identity.client.ui.automation.interaction.OnInteractionRequired() {
@Override
public void handleUserInteraction() {
// Should be silent
}
}, TokenRequestTimeout.MEDIUM);

authResult2.assertSuccess();

// advance clock by more than an hour to expire AT in cache
getSettingsScreen().forwardDeviceTimeForOneDay();

final MsalAuthTestParams silentParams = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.authority(LabConstants.MSID_LAB3)
.forceRefresh(true)
.scopes(Arrays.asList(mScopes))
.msalConfigResourceId(getConfigFileResourceId())
.build();

// get a token silently for msidlab3
final MsalAuthResult silentAuthResult = msalSdk.acquireTokenSilent(silentParams, TokenRequestTimeout.SILENT);
silentAuthResult.assertSuccess();

final MsalAuthTestParams silentParams2 = MsalAuthTestParams.builder()
.activity(mActivity)
.loginHint(username)
.authority(LabConstants.MSID_LAB4)
.forceRefresh(true)
.scopes(Arrays.asList(mScopes))
.msalConfigResourceId(getConfigFileResourceId())
.build();

// get a token silently for msidlab4
final MsalAuthResult silentAuthResult2 = msalSdk.acquireTokenSilent(silentParams2, TokenRequestTimeout.SILENT);
silentAuthResult2.assertSuccess();
}

@Override
public LabQuery getLabQuery() {
return LabQuery.builder()
.userType(UserType.GUEST)
.build();
}

@Override
public TempUserType getTempUserType() {
return null;
}

@Override
public String[] getScopes() {
return new String[]{"User.read"};
}
@Override
public String getAuthority() {
return "https://login.microsoftonline.us/common";
}

@Override
public int getConfigFileResourceId() {
return R.raw.msal_config_default;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package com.microsoft.identity.client.msal.automationapp.testpass.broker.foci;

import androidx.test.uiautomator.UiObject;

import com.microsoft.identity.client.msal.automationapp.R;
import com.microsoft.identity.client.msal.automationapp.testpass.broker.AbstractMsalBrokerTest;
import com.microsoft.identity.client.ui.automation.annotations.LongUIAutomationTest;
import com.microsoft.identity.client.ui.automation.annotations.RetryOnFailure;
import com.microsoft.identity.client.ui.automation.annotations.SupportedBrokers;
import com.microsoft.identity.client.ui.automation.app.AzureSampleApp;
import com.microsoft.identity.client.ui.automation.app.OutlookApp;
import com.microsoft.identity.client.ui.automation.app.WordApp;
import com.microsoft.identity.client.ui.automation.broker.BrokerMicrosoftAuthenticator;
import com.microsoft.identity.client.ui.automation.logging.Logger;
import com.microsoft.identity.client.ui.automation.installer.LocalApkInstaller;
import com.microsoft.identity.client.ui.automation.interaction.FirstPartyAppPromptHandlerParameters;
import com.microsoft.identity.client.ui.automation.interaction.PromptParameter;
import com.microsoft.identity.client.ui.automation.utils.CommonUtils;
import com.microsoft.identity.client.ui.automation.utils.UiAutomatorUtils;
import com.microsoft.identity.common.java.util.ThreadUtils;
import com.microsoft.identity.labapi.utilities.client.ILabAccount;
import com.microsoft.identity.labapi.utilities.client.LabQuery;
import com.microsoft.identity.labapi.utilities.constants.AzureEnvironment;
import com.microsoft.identity.labapi.utilities.constants.FederationProvider;
import com.microsoft.identity.labapi.utilities.constants.TempUserType;
import com.microsoft.identity.labapi.utilities.constants.UserType;
import com.microsoft.identity.labapi.utilities.exception.LabApiException;

import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

// [Non-joined][FoCl] FoCl (Multi-users) with Outlook and Word
// https://identitydivision.visualstudio.com/DevEx/_workitems/edit/833544
@SupportedBrokers(brokers = {BrokerMicrosoftAuthenticator.class})
@RetryOnFailure
@LongUIAutomationTest
public class TestCase833544 extends AbstractMsalBrokerTest {

@Test
public void test_833544() throws LabApiException {
// Recent build of authenticator seems to produce a notification popup on device, this blocks some ui we rely on to validate account presence. Disabling notifications will work.
getSettingsScreen().toggleNotificationsThroughSettings(mBroker.getPackageName());

final String username = mLabAccount.getUsername();
final String password = mLabAccount.getPassword();

final OutlookApp outlook = new OutlookApp(new LocalApkInstaller());

outlook.install();
outlook.launch();
outlook.handleFirstRun();

final FirstPartyAppPromptHandlerParameters promptHandlerParameters = FirstPartyAppPromptHandlerParameters.builder()
.prompt(PromptParameter.SELECT_ACCOUNT)
.loginHint(username)
.broker(mBroker)
.registerPageExpected(false)
.enrollPageExpected(false)
.consentPageExpected(false)
.speedBumpExpected(false)
.sessionExpected(false)
.expectingLoginPageAccountPicker(false)
.expectingBrokerAccountChooserActivity(false)
.build();

// add first account to Outlook
outlook.addFirstAccount(username, password, promptHandlerParameters);
outlook.onAccountAdded();
outlook.confirmAccount(username);

final WordApp wordApp = new WordApp(new LocalApkInstaller());

// open word
wordApp.install();
wordApp.launch();
wordApp.handleFirstRun();

// Word auto signs the user into with the account that was in Outlook
// Sometimes, it might take a bit longer to see this UI page in word app
final UiObject fileFetchScreen = UiAutomatorUtils.obtainUiObjectWithText("Fetching your files", TimeUnit.SECONDS.toMillis(45));
Assert.assertTrue(fileFetchScreen.exists());

// Make sure the account exists in Word
wordApp.confirmAccount(username);

// Steps from 833519
// Make sure a Non-FOCI app (Azure sample in this case) can't see the account
AzureSampleApp azureSample = new AzureSampleApp();
azureSample.install();
azureSample.launch();

// sign in silently into Azure Sample App, should see account picker and not get signed in
azureSample.signInSilentlyWithSingleAccountFragment(mBrowser, mBroker, false);

// Confirm that the account picker did show up
final UiObject accountPicker = UiAutomatorUtils.obtainUiObjectWithResourceId(CommonUtils.getResourceId(mBroker.getPackageName(), "account_chooser_listView"));
Assert.assertTrue(accountPicker.exists());

// Confirm that no account is logged in to AzureSampleApp
azureSample.forceStop();
azureSample.launch();
azureSample.confirmSignedIn("None");

// fetch another account from lab - someone from a different tenant
final LabQuery queryForAdfsV3Account = LabQuery.builder()
.userType(UserType.FEDERATED)
.federationProvider(FederationProvider.ADFS_V3)
.build();

final ILabAccount labAccountAdfsV3 = mLabClient.getLabAccount(queryForAdfsV3Account);

final String usernameV3 = labAccountAdfsV3.getUsername();
final String passwordV3 = labAccountAdfsV3.getPassword();

// relaunch Outlook
outlook.forceStop();
outlook.launch();

final FirstPartyAppPromptHandlerParameters outlookPromptParameters =
FirstPartyAppPromptHandlerParameters.builder()
.expectingNonZeroAccountsInTSL(true)
.prompt(PromptParameter.SELECT_ACCOUNT)
.broker(mBroker)
.consentPageExpected(false)
.enrollPageExpected(false)
.registerPageExpected(false)
.isFederated(true)
.expectingBrokerAccountChooserActivity(true)
.expectingLoginPageAccountPicker(false)
.howWouldYouLikeToSignInExpected(true)
.loginHint(usernameV3)
.sessionExpected(false)
.speedBumpExpected(false)
.build();

// add another account in Outlook
outlook.addAnotherAccount(usernameV3, passwordV3, outlookPromptParameters);

// Relaunching word right after outlook sign in is pressed leads to issues, sometimes the user is not signed in
ThreadUtils.sleepSafely(5000, "Sleep failed", "Interrupted");

// relaunch Word app
wordApp.forceStop();
wordApp.launch();

// We used to check for a flag to expect what new, which occasionally appears in our testing based on word version
// Let's just ignore any AssertionErrors that get thrown here, we don't know what to expect before hand anyway
try {
// Word shows a Whats New Dialog when the app is launched NEXT TIME after adding first account
final UiObject whatsNewDialog = UiAutomatorUtils.obtainUiObjectWithResourceId(
"com.microsoft.office.word:id/WhatsNewDialogTitleTextView"
);

Assert.assertTrue(whatsNewDialog.exists());

// Click the close btn to close this dialog
UiAutomatorUtils.handleButtonClick("android:id/button2");
} catch (AssertionError e){
Logger.i(TestCase833544.class.getSimpleName(), "What's New Page did not appear: " + e.getMessage());
}

final FirstPartyAppPromptHandlerParameters wordPromptParameters =
FirstPartyAppPromptHandlerParameters.builder()
.expectingNonZeroAccountsInTSL(true)
.prompt(PromptParameter.SELECT_ACCOUNT)
.broker(mBroker)
.consentPageExpected(false)
.enrollPageExpected(false)
.registerPageExpected(false)
.isFederated(true)
.expectingBrokerAccountChooserActivity(true)
.expectingLoginPageAccountPicker(false)
.loginHint(usernameV3)
.sessionExpected(true)
.speedBumpExpected(false)
.build();

// add another account in Word
wordApp.addAnotherAccount(usernameV3, passwordV3, wordPromptParameters);

// make sure this other account is in Word
wordApp.confirmAccount(usernameV3);
}

@Override
public LabQuery getLabQuery() {
return LabQuery.builder()
.azureEnvironment(AzureEnvironment.AZURE_CLOUD)
.build();
}

@Override
public TempUserType getTempUserType() {
return null;
}

@Override
public String[] getScopes() {
return new String[]{"User.read"};
}

@Override
public String getAuthority() {
return mApplication.getConfiguration().getDefaultAuthority().toString();
}

@Override
public int getConfigFileResourceId() {
return R.raw.msal_config_default;
}
}
Loading