|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amplifyframework.ui.authenticator |
| 17 | + |
| 18 | +import android.app.Application |
| 19 | +import app.cash.turbine.test |
| 20 | +import com.amplifyframework.auth.result.step.AuthSignInStep |
| 21 | +import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep |
| 22 | +import com.amplifyframework.ui.authenticator.util.AmplifyResult |
| 23 | +import com.amplifyframework.ui.authenticator.util.AuthProvider |
| 24 | +import com.amplifyframework.ui.testing.CoroutineTestRule |
| 25 | +import io.kotest.matchers.shouldBe |
| 26 | +import io.mockk.coEvery |
| 27 | +import io.mockk.coVerify |
| 28 | +import io.mockk.mockk |
| 29 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 30 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 31 | +import kotlinx.coroutines.test.runTest |
| 32 | +import org.junit.Rule |
| 33 | +import org.junit.Test |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit tests for the [AuthenticatorViewModel] class |
| 37 | + */ |
| 38 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 39 | +class AuthenticatorViewModelTest { |
| 40 | + |
| 41 | + @get:Rule |
| 42 | + val coroutineRule = CoroutineTestRule() |
| 43 | + |
| 44 | + private val application = mockk<Application>(relaxed = true) |
| 45 | + private val authProvider = mockk<AuthProvider>(relaxed = true) |
| 46 | + |
| 47 | + private val viewModel = AuthenticatorViewModel(application, authProvider) |
| 48 | + |
| 49 | +//region start tests |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `start only executes once`() = runTest { |
| 53 | + viewModel.start(mockAuthConfiguration()) |
| 54 | + viewModel.start(mockAuthConfiguration()) |
| 55 | + advanceUntilIdle() |
| 56 | + |
| 57 | + // fetchAuthSession only called by the first start |
| 58 | + coVerify(exactly = 1) { |
| 59 | + authProvider.fetchAuthSession() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `missing configuration results in an error`() = runTest { |
| 65 | + coEvery { authProvider.getConfiguration() } returns null |
| 66 | + |
| 67 | + viewModel.start(mockAuthConfiguration()) |
| 68 | + advanceUntilIdle() |
| 69 | + |
| 70 | + coVerify(exactly = 0) { authProvider.fetchAuthSession() } |
| 71 | + viewModel.currentStep shouldBe AuthenticatorStep.Error |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `fetchAuthSession error during start results in an error`() = runTest { |
| 76 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Error(mockAuthException()) |
| 77 | + |
| 78 | + viewModel.start(mockAuthConfiguration()) |
| 79 | + advanceUntilIdle() |
| 80 | + |
| 81 | + coVerify(exactly = 1) { authProvider.fetchAuthSession() } |
| 82 | + viewModel.currentStep shouldBe AuthenticatorStep.Error |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `getCurrentUser error during start results in an error`() = runTest { |
| 87 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = true)) |
| 88 | + coEvery { authProvider.getCurrentUser() } returns AmplifyResult.Error(mockAuthException()) |
| 89 | + |
| 90 | + viewModel.start(mockAuthConfiguration()) |
| 91 | + advanceUntilIdle() |
| 92 | + |
| 93 | + coVerify(exactly = 1) { |
| 94 | + authProvider.fetchAuthSession() |
| 95 | + authProvider.getCurrentUser() |
| 96 | + } |
| 97 | + viewModel.currentStep shouldBe AuthenticatorStep.Error |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `when already signed in during start the initial state should be signed in`() = runTest { |
| 102 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = true)) |
| 103 | + coEvery { authProvider.getCurrentUser() } returns AmplifyResult.Success(mockAuthUser()) |
| 104 | + |
| 105 | + viewModel.start(mockAuthConfiguration()) |
| 106 | + advanceUntilIdle() |
| 107 | + |
| 108 | + coVerify(exactly = 1) { |
| 109 | + authProvider.fetchAuthSession() |
| 110 | + authProvider.getCurrentUser() |
| 111 | + } |
| 112 | + viewModel.currentStep shouldBe AuthenticatorStep.SignedIn |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `initial step is SignIn`() = runTest { |
| 117 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = false)) |
| 118 | + |
| 119 | + viewModel.start(mockAuthConfiguration(initialStep = AuthenticatorStep.SignIn)) |
| 120 | + advanceUntilIdle() |
| 121 | + |
| 122 | + viewModel.currentStep shouldBe AuthenticatorStep.SignIn |
| 123 | + } |
| 124 | + |
| 125 | +//endregion |
| 126 | +//region signIn tests |
| 127 | + |
| 128 | + @Test |
| 129 | + fun `TOTPSetup next step is unsupported`() = runTest { |
| 130 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = false)) |
| 131 | + coEvery { authProvider.signIn(any(), any()) } returns AmplifyResult.Success( |
| 132 | + mockSignInResult(signInStep = AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP) |
| 133 | + ) |
| 134 | + |
| 135 | + viewModel.start(mockAuthConfiguration(initialStep = AuthenticatorStep.SignIn)) |
| 136 | + |
| 137 | + viewModel.events.test { |
| 138 | + viewModel.signIn("username", "password") |
| 139 | + awaitItem().shouldBeError(causeMessage = "Authenticator does not yet support TOTP workflows.") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun `TOTP Code next step is unsupported`() = runTest { |
| 145 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = false)) |
| 146 | + coEvery { authProvider.signIn(any(), any()) } returns AmplifyResult.Success( |
| 147 | + mockSignInResult(signInStep = AuthSignInStep.CONFIRM_SIGN_IN_WITH_TOTP_CODE) |
| 148 | + ) |
| 149 | + |
| 150 | + viewModel.start(mockAuthConfiguration(initialStep = AuthenticatorStep.SignIn)) |
| 151 | + |
| 152 | + viewModel.events.test { |
| 153 | + viewModel.signIn("username", "password") |
| 154 | + awaitItem().shouldBeError(causeMessage = "Authenticator does not yet support TOTP workflows.") |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun `MFA Selection next step is unsupported`() = runTest { |
| 160 | + coEvery { authProvider.fetchAuthSession() } returns AmplifyResult.Success(mockAuthSession(isSignedIn = false)) |
| 161 | + coEvery { authProvider.signIn(any(), any()) } returns AmplifyResult.Success( |
| 162 | + mockSignInResult(signInStep = AuthSignInStep.CONTINUE_SIGN_IN_WITH_MFA_SELECTION) |
| 163 | + ) |
| 164 | + |
| 165 | + viewModel.start(mockAuthConfiguration(initialStep = AuthenticatorStep.SignIn)) |
| 166 | + |
| 167 | + viewModel.events.test { |
| 168 | + viewModel.signIn("username", "password") |
| 169 | + awaitItem().shouldBeError(causeMessage = "Authenticator does not yet support TOTP workflows.") |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +//endregion |
| 174 | +//region helpers |
| 175 | + private val AuthenticatorViewModel.currentStep: AuthenticatorStep |
| 176 | + get() = stepState.value.step |
| 177 | +//endregion |
| 178 | +} |
0 commit comments