mirror of
https://github.com/bitwarden/android.git
synced 2026-11-20 11:37:52 -06:00
BIT-1559: Implement GET auth-requests (#752)
This commit is contained in:
committed by
Álison Fernandes
parent
89fda64baa
commit
d66c3be2a6
+48
@@ -0,0 +1,48 @@
|
||||
package com.x8bit.bitwarden.data.auth.datasource.network.service
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.api.AuthRequestsApi
|
||||
import com.x8bit.bitwarden.data.platform.base.BaseServiceTest
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import retrofit2.create
|
||||
|
||||
class AuthRequestsServiceTest : BaseServiceTest() {
|
||||
|
||||
private val authRequestsApi: AuthRequestsApi = retrofit.create()
|
||||
private val service = AuthRequestsServiceImpl(
|
||||
authRequestsApi = authRequestsApi,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `getAuthRequests when request response is Failure should return Failure`() = runTest {
|
||||
val response = MockResponse().setResponseCode(400)
|
||||
server.enqueue(response)
|
||||
val actual = service.getAuthRequests()
|
||||
assertTrue(actual.isFailure)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAuthRequests when request response is Success should return Success`() = runTest {
|
||||
val json = """
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"id": "1",
|
||||
"publicKey": "2",
|
||||
"requestDeviceType": "Android",
|
||||
"requestIpAddress": "1.0.0.1",
|
||||
"creationDate": "2024-09-13T01:00:00.00Z",
|
||||
"requestApproved": true,
|
||||
"origin": "www.bitwarden.com"
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
val response = MockResponse().setBody(json).setResponseCode(200)
|
||||
server.enqueue(response)
|
||||
val actual = service.getAuthRequests()
|
||||
assertTrue(actual.isSuccess)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.disk.util.FakeAuthDiskSource
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.AuthRequestsResponseJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.KdfTypeJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.PasswordHintResponseJson
|
||||
@@ -18,6 +19,7 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.RefreshTokenRespon
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.RegisterRequestJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.RegisterResponseJson
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.AccountsService
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.AuthRequestsService
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.DevicesService
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.HaveIBeenPwnedService
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.IdentityService
|
||||
@@ -28,6 +30,8 @@ import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL
|
||||
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_3
|
||||
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_4
|
||||
import com.x8bit.bitwarden.data.auth.manager.UserLogoutManager
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthRequest
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthRequestsResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthState
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.BreachCountResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.DeleteAccountResult
|
||||
@@ -58,7 +62,6 @@ import com.x8bit.bitwarden.data.vault.datasource.network.model.createMockOrganiz
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultState
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
|
||||
import io.mockk.clearMocks
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
@@ -77,12 +80,14 @@ import org.junit.jupiter.api.Assertions.assertNull
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
@Suppress("LargeClass")
|
||||
class AuthRepositoryTest {
|
||||
|
||||
private val dispatcherManager: DispatcherManager = FakeDispatcherManager()
|
||||
private val accountsService: AccountsService = mockk()
|
||||
private val authRequestsService: AuthRequestsService = mockk()
|
||||
private val devicesService: DevicesService = mockk()
|
||||
private val identityService: IdentityService = mockk()
|
||||
private val haveIBeenPwnedService: HaveIBeenPwnedService = mockk()
|
||||
@@ -135,6 +140,7 @@ class AuthRepositoryTest {
|
||||
|
||||
private val repository = AuthRepositoryImpl(
|
||||
accountsService = accountsService,
|
||||
authRequestsService = authRequestsService,
|
||||
devicesService = devicesService,
|
||||
identityService = identityService,
|
||||
haveIBeenPwnedService = haveIBeenPwnedService,
|
||||
@@ -150,7 +156,6 @@ class AuthRepositoryTest {
|
||||
|
||||
@BeforeEach
|
||||
fun beforeEach() {
|
||||
clearMocks(identityService, accountsService, haveIBeenPwnedService)
|
||||
mockkStatic(
|
||||
GetTokenResponseJson.Success::toUserState,
|
||||
RefreshTokenResponseJson::toUserStateJson,
|
||||
@@ -1235,6 +1240,66 @@ class AuthRepositoryTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAuthRequests should return failure when service returns failure`() = runTest {
|
||||
coEvery {
|
||||
authRequestsService.getAuthRequests()
|
||||
} returns Throwable("Fail").asFailure()
|
||||
|
||||
val result = repository.getAuthRequests()
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
authRequestsService.getAuthRequests()
|
||||
}
|
||||
assertEquals(AuthRequestsResult.Error, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAuthRequests should return success when service returns success`() = runTest {
|
||||
val responseJson = AuthRequestsResponseJson(
|
||||
authRequests = listOf(
|
||||
AuthRequestsResponseJson.AuthRequest(
|
||||
id = "1",
|
||||
publicKey = "2",
|
||||
platform = "Android",
|
||||
ipAddress = "192.168.0.1",
|
||||
key = "public",
|
||||
masterPasswordHash = "verySecureHash",
|
||||
creationDate = ZonedDateTime.parse("2024-09-13T00:00Z"),
|
||||
responseDate = null,
|
||||
requestApproved = true,
|
||||
originUrl = "www.bitwarden.com",
|
||||
),
|
||||
),
|
||||
)
|
||||
val expected = AuthRequestsResult.Success(
|
||||
authRequests = listOf(
|
||||
AuthRequest(
|
||||
id = "1",
|
||||
publicKey = "2",
|
||||
platform = "Android",
|
||||
ipAddress = "192.168.0.1",
|
||||
key = "public",
|
||||
masterPasswordHash = "verySecureHash",
|
||||
creationDate = ZonedDateTime.parse("2024-09-13T00:00Z"),
|
||||
responseDate = null,
|
||||
requestApproved = true,
|
||||
originUrl = "www.bitwarden.com",
|
||||
),
|
||||
),
|
||||
)
|
||||
coEvery {
|
||||
authRequestsService.getAuthRequests()
|
||||
} returns responseJson.asSuccess()
|
||||
|
||||
val result = repository.getAuthRequests()
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
authRequestsService.getAuthRequests()
|
||||
}
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getIsKnownDevice should return failure when service returns failure`() = runTest {
|
||||
coEvery {
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ class PendingRequestsScreenTest : BaseComposeTest() {
|
||||
|
||||
companion object {
|
||||
val DEFAULT_STATE: PendingRequestsState = PendingRequestsState(
|
||||
viewState = PendingRequestsState.ViewState.Empty,
|
||||
viewState = PendingRequestsState.ViewState.Loading,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+121
-9
@@ -2,31 +2,137 @@ package com.x8bit.bitwarden.ui.platform.feature.settings.accountsecurity.pending
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthRequest
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthRequestsResult
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.TimeZone
|
||||
|
||||
class PendingRequestsViewModelTest : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct when not set`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
|
||||
private val authRepository = mockk<AuthRepository>()
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
// Setting the timezone so the tests pass consistently no matter the environment.
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
// Clearing the timezone after the test.
|
||||
TimeZone.setDefault(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct when set`() {
|
||||
val state = DEFAULT_STATE.copy(
|
||||
viewState = PendingRequestsState.ViewState.Loading,
|
||||
fun `initial state should be correct and trigger a getAuthRequests call`() {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Success(
|
||||
authRequests = emptyList(),
|
||||
)
|
||||
val viewModel = createViewModel(state = state)
|
||||
assertEquals(state, viewModel.stateFlow.value)
|
||||
val viewModel = createViewModel(state = null)
|
||||
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
|
||||
coVerify {
|
||||
authRepository.getAuthRequests()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getPendingResults success with content should update state`() {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Success(
|
||||
authRequests = listOf(
|
||||
AuthRequest(
|
||||
id = "1",
|
||||
publicKey = "pantry-overdue-survive-sleep-jab",
|
||||
platform = "Android",
|
||||
ipAddress = "192.168.0.1",
|
||||
key = "publicKey",
|
||||
masterPasswordHash = "verySecureHash",
|
||||
creationDate = ZonedDateTime.parse("2023-08-24T17:11Z"),
|
||||
responseDate = null,
|
||||
requestApproved = true,
|
||||
originUrl = "www.bitwarden.com",
|
||||
),
|
||||
AuthRequest(
|
||||
id = "2",
|
||||
publicKey = "erupt-anew-matchbook-disk-student",
|
||||
platform = "iOS",
|
||||
ipAddress = "192.168.0.2",
|
||||
key = "publicKey",
|
||||
masterPasswordHash = "verySecureHash",
|
||||
creationDate = ZonedDateTime.parse("2023-08-21T15:43Z"),
|
||||
responseDate = null,
|
||||
requestApproved = false,
|
||||
originUrl = "www.bitwarden.com",
|
||||
),
|
||||
),
|
||||
)
|
||||
val expected = DEFAULT_STATE.copy(
|
||||
viewState = PendingRequestsState.ViewState.Content(
|
||||
requests = listOf(
|
||||
PendingRequestsState.ViewState.Content.PendingLoginRequest(
|
||||
fingerprintPhrase = "pantry-overdue-survive-sleep-jab",
|
||||
platform = "Android",
|
||||
timestamp = "8/24/23 05:11 PM",
|
||||
),
|
||||
PendingRequestsState.ViewState.Content.PendingLoginRequest(
|
||||
fingerprintPhrase = "erupt-anew-matchbook-disk-student",
|
||||
platform = "iOS",
|
||||
timestamp = "8/21/23 03:43 PM",
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
val viewModel = createViewModel()
|
||||
assertEquals(expected, viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getPendingResults success with empty list should update state`() {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Success(
|
||||
authRequests = emptyList(),
|
||||
)
|
||||
val expected = DEFAULT_STATE.copy(
|
||||
viewState = PendingRequestsState.ViewState.Empty,
|
||||
)
|
||||
val viewModel = createViewModel()
|
||||
assertEquals(expected, viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getPendingResults failure with error should update state`() {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Error
|
||||
val expected = DEFAULT_STATE.copy(
|
||||
viewState = PendingRequestsState.ViewState.Error,
|
||||
)
|
||||
val viewModel = createViewModel()
|
||||
assertEquals(expected, viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on CloseClick should emit NavigateBack`() = runTest {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Success(
|
||||
authRequests = emptyList(),
|
||||
)
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(PendingRequestsAction.CloseClick)
|
||||
@@ -36,6 +142,11 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `on DeclineAllRequestsClick should send ShowToast event`() = runTest {
|
||||
coEvery {
|
||||
authRepository.getAuthRequests()
|
||||
} returns AuthRequestsResult.Success(
|
||||
authRequests = emptyList(),
|
||||
)
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(PendingRequestsAction.DeclineAllRequestsClick)
|
||||
@@ -50,6 +161,7 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
||||
private fun createViewModel(
|
||||
state: PendingRequestsState? = DEFAULT_STATE,
|
||||
): PendingRequestsViewModel = PendingRequestsViewModel(
|
||||
authRepository = authRepository,
|
||||
savedStateHandle = SavedStateHandle().apply { set("state", state) },
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user