Skip to content

Commit

Permalink
ALl test cases apply new AbstractTest to extract configuration initia…
Browse files Browse the repository at this point in the history
…lisation outside
  • Loading branch information
Yuki-YuXin committed Nov 26, 2024
1 parent 242b66c commit bd75314
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.microsoft.identity.client.e2e.shadows.ShadowBaseController
import com.microsoft.identity.client.e2e.utils.assertResult
import com.microsoft.identity.internal.testutils.nativeauth.ConfigType
import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAuthTestConfig
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.statemachine.errors.GetAccessTokenError
import com.microsoft.identity.nativeauth.statemachine.errors.SignInError
import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult
Expand All @@ -41,13 +42,19 @@ class GetTokenTests : NativeAuthPublicClientApplicationAbstractTest() {

private lateinit var resources: List<NativeAuthTestConfig.Resource>

lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

private val defaultConfigType = ConfigType.SIGN_IN_PASSWORD
private val defaultChallengeTypes = listOf("password", "oob")

override fun setup() {
super.setup()
config = getConfig(defaultConfigType)
application = setupPCA(config, defaultChallengeTypes)
resources = config.resources
}

override val defaultConfigType = ConfigType.SIGN_IN_PASSWORD

/**
* Signing in with an invalid scope should make the API and the SDK return an error.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.microsoft.identity.client.Logger
import com.microsoft.identity.client.PublicClientApplication
import com.microsoft.identity.client.e2e.shadows.ShadowAndroidSdkStorageEncryptionManager
import com.microsoft.identity.client.e2e.tests.IPublicClientApplicationTest
Expand Down Expand Up @@ -60,12 +59,12 @@ import org.robolectric.annotation.LooperMode
abstract class NativeAuthPublicClientApplicationAbstractTest : IPublicClientApplicationTest {
companion object{
const val SHARED_PREFERENCES_NAME = "com.microsoft.identity.client.account_credential_cache"
const val INVALID_EMAIL = "invalid_email"
const val INVALID_PASSWORD = "invalid_password"
}

private lateinit var context: Context
private lateinit var activity: Activity
lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

// Remove default Coroutine test timeout of 10 seconds.
private val testDispatcher = StandardTestDispatcher()
Expand All @@ -74,19 +73,13 @@ abstract class NativeAuthPublicClientApplicationAbstractTest : IPublicClientAppl
return "" // Not needed for native auth flows
}

abstract val defaultConfigType: ConfigType

@Before
open fun setup() {
context = ApplicationProvider.getApplicationContext()
activity = Mockito.mock(Activity::class.java)
Mockito.`when`(activity.applicationContext).thenReturn(context)
Logger.getInstance().setEnableLogcatLog(true)
Logger.getInstance().setEnablePII(true)
Logger.getInstance().setLogLevel(Logger.LogLevel.VERBOSE)
CommandDispatcherHelper.clear()
Dispatchers.setMain(testDispatcher)
setupPCA(defaultConfigType)
}

@After
Expand Down Expand Up @@ -114,13 +107,15 @@ abstract class NativeAuthPublicClientApplicationAbstractTest : IPublicClientAppl
return Gson().fromJson(secretValue, type)
}

fun setupPCA(configType: ConfigType) {
fun getConfig(configType: ConfigType): NativeAuthTestConfig.Config {
val secretValue = getConfigsThroughSecretValue()
config = secretValue?.get(configType.stringValue) ?: throw IllegalStateException("Config not $secretValue")
val challengeTypes = listOf("password", "oob")
return secretValue?.get(configType.stringValue)
?: throw IllegalStateException("Config not $secretValue")
}

try {
application = PublicClientApplication.createNativeAuthPublicClientApplication(
fun setupPCA(config: NativeAuthTestConfig.Config, challengeTypes: List<String>): INativeAuthPublicClientApplication {
return try {
PublicClientApplication.createNativeAuthPublicClientApplication(
context,
config.clientId,
config.authorityUrl,
Expand All @@ -129,6 +124,7 @@ abstract class NativeAuthPublicClientApplicationAbstractTest : IPublicClientAppl
)
} catch (e: MsalException) {
Assert.fail(e.message)
throw e
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ package com.microsoft.identity.client.e2e.tests.network.nativeauth
import com.microsoft.identity.client.e2e.utils.assertResult
import com.microsoft.identity.internal.testutils.nativeauth.ConfigType
import com.microsoft.identity.internal.testutils.nativeauth.api.TemporaryEmailService
import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAuthTestConfig
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.statemachine.errors.ResetPasswordError
import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordResult
import com.microsoft.identity.nativeauth.statemachine.results.ResetPasswordStartResult
Expand All @@ -39,7 +41,17 @@ class SSPRTest : NativeAuthPublicClientApplicationAbstractTest() {

private val tempEmailApi = TemporaryEmailService()

override val defaultConfigType = ConfigType.SSPR
lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

private val defaultConfigType = ConfigType.SSPR
private val defaultChallengeTypes = listOf("password", "oob")

override fun setup() {
super.setup()
config = getConfig(defaultConfigType)
application = setupPCA(config, defaultChallengeTypes)
}

@Test
fun testSSPRErrorSimple() = runTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ package com.microsoft.identity.client.e2e.tests.network.nativeauth
import com.microsoft.identity.client.e2e.utils.assertResult
import com.microsoft.identity.internal.testutils.nativeauth.ConfigType
import com.microsoft.identity.internal.testutils.nativeauth.api.TemporaryEmailService
import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAuthTestConfig
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.statemachine.errors.SignInError
import com.microsoft.identity.nativeauth.statemachine.errors.SubmitCodeError
import com.microsoft.identity.nativeauth.statemachine.results.SignInResult
Expand All @@ -38,7 +40,17 @@ class SignInEmailOTPTest : NativeAuthPublicClientApplicationAbstractTest() {

private val tempEmailApi = TemporaryEmailService()

override val defaultConfigType = ConfigType.SIGN_IN_OTP
lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

private val defaultConfigType = ConfigType.SIGN_IN_OTP
private val defaultChallengeTypes = listOf("password", "oob")

override fun setup() {
super.setup()
config = getConfig(defaultConfigType)
application = setupPCA(config, defaultChallengeTypes)
}

/**
* Use valid email and OTP to get token and sign in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package com.microsoft.identity.client.e2e.tests.network.nativeauth

import com.microsoft.identity.client.e2e.utils.assertResult
import com.microsoft.identity.internal.testutils.nativeauth.ConfigType
import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAuthTestConfig
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.NativeAuthPublicClientApplicationConfiguration
import com.microsoft.identity.nativeauth.statemachine.errors.SignInError
import com.microsoft.identity.nativeauth.statemachine.results.MFARequiredResult
Expand All @@ -41,7 +43,17 @@ import org.mockito.kotlin.spy

class SignInEmailPasswordTest : NativeAuthPublicClientApplicationAbstractTest() {

override val defaultConfigType = ConfigType.SIGN_IN_PASSWORD
lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

private val defaultConfigType = ConfigType.SIGN_IN_PASSWORD
private val defaultChallengeTypes = listOf("password", "oob")

override fun setup() {
super.setup()
config = getConfig(defaultConfigType)
application = setupPCA(config, defaultChallengeTypes)
}

/**
* Use valid email and password to get token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.microsoft.identity.client.e2e.utils.assertResult
import com.microsoft.identity.internal.testutils.nativeauth.ConfigType
import com.microsoft.identity.internal.testutils.nativeauth.api.TemporaryEmailService
import com.microsoft.identity.internal.testutils.nativeauth.api.models.NativeAuthTestConfig
import com.microsoft.identity.nativeauth.INativeAuthPublicClientApplication
import com.microsoft.identity.nativeauth.statemachine.errors.MFASubmitChallengeError
import com.microsoft.identity.nativeauth.statemachine.results.GetAccessTokenResult
import com.microsoft.identity.nativeauth.statemachine.results.MFARequiredResult
Expand All @@ -42,12 +43,18 @@ class SignInMFATest : NativeAuthPublicClientApplicationAbstractTest() {

private val tempEmailApi = TemporaryEmailService()

override var defaultConfigType = ConfigType.SIGN_IN_MFA_SINGLE_AUTH

private lateinit var resources: List<NativeAuthTestConfig.Resource>

lateinit var application: INativeAuthPublicClientApplication
lateinit var config: NativeAuthTestConfig.Config

private val defaultConfigType = ConfigType.SIGN_IN_MFA_SINGLE_AUTH
private val defaultChallengeTypes = listOf("password", "oob")

override fun setup() {
super.setup()
config = getConfig(defaultConfigType)
application = setupPCA(config, defaultChallengeTypes)
resources = config.resources
}

Expand Down Expand Up @@ -203,9 +210,6 @@ class SignInMFATest : NativeAuthPublicClientApplicationAbstractTest() {
fun `test selection required, request challenge on specific auth method and complete MFA flow`() {
retryOperation {
runBlocking {
val configType = ConfigType.SIGN_IN_MFA_MULTI_AUTH
setupPCA(configType)

val scopeA = resources[0].scopes[0]
val scopeB = resources[0].scopes[1]

Expand Down
Loading

0 comments on commit bd75314

Please sign in to comment.