|
| 1 | +/* |
| 2 | + * Copyright (c) 2025-present, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Redistribution and use of this software in source and binary forms, with or |
| 5 | + * without modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * - Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer in the documentation |
| 11 | + * and/or other materials provided with the distribution. |
| 12 | + * - Neither the name of salesforce.com, inc. nor the names of its contributors |
| 13 | + * may be used to endorse or promote products derived from this software without |
| 14 | + * specific prior written permission of salesforce.com, inc. |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +package com.salesforce.androidsdk.util.test |
| 29 | + |
| 30 | +import android.content.Intent |
| 31 | +import android.content.Intent.FLAG_ACTIVITY_NEW_TASK |
| 32 | +import android.os.Bundle |
| 33 | +import androidx.appcompat.app.AppCompatActivity |
| 34 | +import com.salesforce.androidsdk.accounts.UserAccountBuilder |
| 35 | +import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_DEFAULT |
| 36 | +import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_FIRST_LOGIN |
| 37 | +import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_LOGIN |
| 38 | +import com.salesforce.androidsdk.app.SalesforceSDKManager |
| 39 | +import com.salesforce.androidsdk.rest.ClientManager.AccMgrAuthTokenProvider |
| 40 | +import com.salesforce.androidsdk.util.test.TestCredentials.ACCOUNT_NAME |
| 41 | +import com.salesforce.androidsdk.util.test.TestCredentials.CLIENT_ID |
| 42 | +import com.salesforce.androidsdk.util.test.TestCredentials.COMMUNITY_URL |
| 43 | +import com.salesforce.androidsdk.util.test.TestCredentials.IDENTITY_URL |
| 44 | +import com.salesforce.androidsdk.util.test.TestCredentials.INSTANCE_URL |
| 45 | +import com.salesforce.androidsdk.util.test.TestCredentials.LANGUAGE |
| 46 | +import com.salesforce.androidsdk.util.test.TestCredentials.LOCALE |
| 47 | +import com.salesforce.androidsdk.util.test.TestCredentials.LOGIN_URL |
| 48 | +import com.salesforce.androidsdk.util.test.TestCredentials.ORG_ID |
| 49 | +import com.salesforce.androidsdk.util.test.TestCredentials.PHOTO_URL |
| 50 | +import com.salesforce.androidsdk.util.test.TestCredentials.REFRESH_TOKEN |
| 51 | +import com.salesforce.androidsdk.util.test.TestCredentials.USER_ID |
| 52 | + |
| 53 | +/** |
| 54 | + * An activity that authenticates using a provided set of credentials rather |
| 55 | + * than user interaction. This is intended only for tests automation in debug |
| 56 | + * build variants. This class should not be used in release builds. |
| 57 | + */ |
| 58 | +class TestAuthenticationActivity : AppCompatActivity() { |
| 59 | + |
| 60 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 61 | + super.onCreate(savedInstanceState) |
| 62 | + |
| 63 | + val credentials = intent.getStringExtra("creds") |
| 64 | + if (credentials == null) { |
| 65 | + finish() |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + TestCredentials.init(credentials, this) |
| 70 | + |
| 71 | + val account = UserAccountBuilder.getInstance() |
| 72 | + .refreshToken(REFRESH_TOKEN) |
| 73 | + .instanceServer(INSTANCE_URL) |
| 74 | + .idUrl(IDENTITY_URL) |
| 75 | + .orgId(ORG_ID) |
| 76 | + .userId(USER_ID) |
| 77 | + .communityUrl(COMMUNITY_URL) |
| 78 | + .accountName(ACCOUNT_NAME) |
| 79 | + .clientId(CLIENT_ID) |
| 80 | + .photoUrl(PHOTO_URL) |
| 81 | + .language(LANGUAGE) |
| 82 | + .locale(LOCALE) |
| 83 | + .loginServer(LOGIN_URL) |
| 84 | + .authToken("Nothing yet") |
| 85 | + .firstName("User") |
| 86 | + .lastName("Test") |
| 87 | + .displayName("Test user") |
| 88 | + .username("username") |
| 89 | + .build() |
| 90 | + |
| 91 | + val authTokenProvider = AccMgrAuthTokenProvider( |
| 92 | + SalesforceSDKManager.getInstance().clientManager, |
| 93 | + INSTANCE_URL, |
| 94 | + null, |
| 95 | + REFRESH_TOKEN |
| 96 | + ) |
| 97 | + authTokenProvider.newAuthToken |
| 98 | + account.downloadProfilePhoto() |
| 99 | + |
| 100 | + val userAccountManager = SalesforceSDKManager.getInstance().userAccountManager |
| 101 | + // Send user switch intent, create and switch to user. |
| 102 | + val numAuthenticatedUsers = userAccountManager.authenticatedUsers?.size ?: 0 |
| 103 | + val userSwitchType = when { |
| 104 | + // We've already authenticated the first user, so there should be one. |
| 105 | + numAuthenticatedUsers == 1 -> USER_SWITCH_TYPE_FIRST_LOGIN |
| 106 | + |
| 107 | + // Otherwise we're logging in with an additional user. |
| 108 | + numAuthenticatedUsers > 1 -> USER_SWITCH_TYPE_LOGIN |
| 109 | + |
| 110 | + // This should never happen but if it does, pass in the "unknown" value. |
| 111 | + else -> USER_SWITCH_TYPE_DEFAULT |
| 112 | + } |
| 113 | + userAccountManager.sendUserSwitchIntent(userSwitchType, null) |
| 114 | + userAccountManager.createAccount(account) |
| 115 | + userAccountManager.switchToUser(account) |
| 116 | + |
| 117 | + startActivity(Intent(this, SalesforceSDKManager.getInstance().mainActivityClass).apply { |
| 118 | + setPackage(SalesforceSDKManager.getInstance().appContext.packageName) |
| 119 | + flags = FLAG_ACTIVITY_NEW_TASK |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments