mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-1291: Initiate Login with Device flow (#791)
This commit is contained in:
committed by
Álison Fernandes
parent
52acc2fa47
commit
0818638273
+73
@@ -0,0 +1,73 @@
|
||||
package com.x8bit.bitwarden.data.auth.datasource.network.service
|
||||
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.api.AuthRequestsApi
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.model.AuthRequestsResponseJson
|
||||
import com.x8bit.bitwarden.data.platform.base.BaseServiceTest
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import retrofit2.create
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
class NewAuthRequestServiceTest : BaseServiceTest() {
|
||||
|
||||
private val authRequestsApi: AuthRequestsApi = retrofit.create()
|
||||
private val service = NewAuthRequestServiceImpl(
|
||||
authRequestsApi = authRequestsApi,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `createAuthRequest when request response is Failure should return Failure`() = runTest {
|
||||
val response = MockResponse().setResponseCode(400)
|
||||
server.enqueue(response)
|
||||
val actual = service.createAuthRequest(
|
||||
email = "test@gmail.com",
|
||||
publicKey = "1234",
|
||||
deviceId = "4321",
|
||||
accessCode = "accessCode",
|
||||
fingerprint = "fingerprint",
|
||||
)
|
||||
assertTrue(actual.isFailure)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `createAuthRequest when request response is Success should return Success`() = runTest {
|
||||
val json = """
|
||||
{
|
||||
"id": "1",
|
||||
"publicKey": "2",
|
||||
"requestDeviceType": "Android",
|
||||
"requestIpAddress": "1.0.0.1",
|
||||
"key": "key",
|
||||
"masterPasswordHash": "verySecureHash",
|
||||
"creationDate": "2024-09-13T01:00:00.00Z",
|
||||
"requestApproved": true,
|
||||
"origin": "www.bitwarden.com"
|
||||
}
|
||||
"""
|
||||
val expected = AuthRequestsResponseJson.AuthRequest(
|
||||
id = "1",
|
||||
publicKey = "2",
|
||||
platform = "Android",
|
||||
ipAddress = "1.0.0.1",
|
||||
key = "key",
|
||||
masterPasswordHash = "verySecureHash",
|
||||
creationDate = ZonedDateTime.parse("2024-09-13T01:00:00.00Z"),
|
||||
responseDate = null,
|
||||
requestApproved = true,
|
||||
originUrl = "www.bitwarden.com",
|
||||
)
|
||||
val response = MockResponse().setBody(json).setResponseCode(200)
|
||||
server.enqueue(response)
|
||||
val actual = service.createAuthRequest(
|
||||
email = "test@gmail.com",
|
||||
publicKey = "1234",
|
||||
deviceId = "4321",
|
||||
accessCode = "accessCode",
|
||||
fingerprint = "fingerprint",
|
||||
)
|
||||
assertEquals(Result.success(expected), actual)
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import com.x8bit.bitwarden.data.auth.datasource.network.service.AuthRequestsServ
|
||||
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
|
||||
import com.x8bit.bitwarden.data.auth.datasource.network.service.NewAuthRequestService
|
||||
import com.x8bit.bitwarden.data.auth.datasource.sdk.AuthSdkSource
|
||||
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_0
|
||||
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_1
|
||||
@@ -36,6 +37,7 @@ import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL
|
||||
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.AuthRequestResult
|
||||
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
|
||||
@@ -98,6 +100,7 @@ class AuthRepositoryTest {
|
||||
private val devicesService: DevicesService = mockk()
|
||||
private val identityService: IdentityService = mockk()
|
||||
private val haveIBeenPwnedService: HaveIBeenPwnedService = mockk()
|
||||
private val newAuthRequestService: NewAuthRequestService = mockk()
|
||||
private val mutableVaultStateFlow = MutableStateFlow(VAULT_STATE)
|
||||
private val vaultRepository: VaultRepository = mockk {
|
||||
every { vaultStateFlow } returns mutableVaultStateFlow
|
||||
@@ -114,6 +117,11 @@ class AuthRepositoryTest {
|
||||
every { setDefaultsIfNecessary(any()) } just runs
|
||||
}
|
||||
private val authSdkSource = mockk<AuthSdkSource> {
|
||||
coEvery {
|
||||
getNewAuthRequest(
|
||||
email = EMAIL,
|
||||
)
|
||||
} returns Result.success(AUTH_REQUEST_RESPONSE)
|
||||
coEvery {
|
||||
hashPassword(
|
||||
email = EMAIL,
|
||||
@@ -151,6 +159,7 @@ class AuthRepositoryTest {
|
||||
devicesService = devicesService,
|
||||
identityService = identityService,
|
||||
haveIBeenPwnedService = haveIBeenPwnedService,
|
||||
newAuthRequestService = newAuthRequestService,
|
||||
authSdkSource = authSdkSource,
|
||||
authDiskSource = fakeAuthDiskSource,
|
||||
environmentRepository = fakeEnvironmentRepository,
|
||||
@@ -1605,6 +1614,93 @@ class AuthRepositoryTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `createAuthRequest should return failure when service returns failure`() = runTest {
|
||||
val accessCode = "accessCode"
|
||||
val fingerprint = "fingerprint"
|
||||
coEvery {
|
||||
newAuthRequestService.createAuthRequest(
|
||||
email = EMAIL,
|
||||
publicKey = PUBLIC_KEY,
|
||||
deviceId = UNIQUE_APP_ID,
|
||||
accessCode = accessCode,
|
||||
fingerprint = fingerprint,
|
||||
)
|
||||
} returns Throwable("Fail").asFailure()
|
||||
|
||||
val result = repository.createAuthRequest(
|
||||
email = EMAIL,
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
newAuthRequestService.createAuthRequest(
|
||||
email = EMAIL,
|
||||
publicKey = PUBLIC_KEY,
|
||||
deviceId = UNIQUE_APP_ID,
|
||||
accessCode = accessCode,
|
||||
fingerprint = fingerprint,
|
||||
)
|
||||
}
|
||||
assertEquals(AuthRequestResult.Error, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `createAuthRequest should return success when service returns success`() = runTest {
|
||||
val accessCode = "accessCode"
|
||||
val fingerprint = "fingerprint"
|
||||
|
||||
val responseJson = AuthRequestsResponseJson.AuthRequest(
|
||||
id = "1",
|
||||
publicKey = PUBLIC_KEY,
|
||||
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 = AuthRequestResult.Success(
|
||||
authRequest = AuthRequest(
|
||||
id = "1",
|
||||
publicKey = PUBLIC_KEY,
|
||||
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 {
|
||||
newAuthRequestService.createAuthRequest(
|
||||
email = EMAIL,
|
||||
publicKey = PUBLIC_KEY,
|
||||
deviceId = UNIQUE_APP_ID,
|
||||
accessCode = accessCode,
|
||||
fingerprint = fingerprint,
|
||||
)
|
||||
} returns responseJson.asSuccess()
|
||||
|
||||
val result = repository.createAuthRequest(
|
||||
email = EMAIL,
|
||||
)
|
||||
|
||||
coVerify(exactly = 1) {
|
||||
newAuthRequestService.createAuthRequest(
|
||||
email = EMAIL,
|
||||
publicKey = PUBLIC_KEY,
|
||||
deviceId = UNIQUE_APP_ID,
|
||||
accessCode = accessCode,
|
||||
fingerprint = fingerprint,
|
||||
)
|
||||
}
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getAuthRequests should return failure when service returns failure`() = runTest {
|
||||
coEvery {
|
||||
@@ -1869,6 +1965,12 @@ class AuthRepositoryTest {
|
||||
private val PRE_LOGIN_SUCCESS = PreLoginResponseJson(
|
||||
kdfParams = PreLoginResponseJson.KdfParams.Pbkdf2(iterations = 1u),
|
||||
)
|
||||
private val AUTH_REQUEST_RESPONSE = AuthRequestResponse(
|
||||
privateKey = PRIVATE_KEY,
|
||||
publicKey = PUBLIC_KEY,
|
||||
accessCode = "accessCode",
|
||||
fingerprint = "fingerprint",
|
||||
)
|
||||
private val REFRESH_TOKEN_RESPONSE_JSON = RefreshTokenResponseJson(
|
||||
accessToken = ACCESS_TOKEN_2,
|
||||
expiresIn = 3600,
|
||||
|
||||
+13
-1
@@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.AuthRequestResult
|
||||
import com.x8bit.bitwarden.data.auth.repository.model.UserFingerprintResult
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
@@ -20,6 +21,9 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
|
||||
coEvery {
|
||||
getFingerprintPhrase(EMAIL)
|
||||
} returns UserFingerprintResult.Success("initialFingerprint")
|
||||
coEvery {
|
||||
createAuthRequest(EMAIL)
|
||||
} returns mockk<AuthRequestResult.Success>()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -28,12 +32,17 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE, awaitItem())
|
||||
}
|
||||
coVerify { authRepository.createAuthRequest(EMAIL) }
|
||||
coVerify { authRepository.getFingerprintPhrase(EMAIL) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct when set`() = runTest {
|
||||
val newEmail = "newEmail@gmail.com"
|
||||
|
||||
coEvery {
|
||||
authRepository.createAuthRequest(newEmail)
|
||||
} returns mockk<AuthRequestResult.Success>()
|
||||
coEvery {
|
||||
authRepository.getFingerprintPhrase(newEmail)
|
||||
} returns UserFingerprintResult.Success("initialFingerprint")
|
||||
@@ -47,7 +56,10 @@ class LoginWithDeviceViewModelTest : BaseViewModelTest() {
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(state, awaitItem())
|
||||
}
|
||||
coVerify { authRepository.getFingerprintPhrase(newEmail) }
|
||||
coVerify {
|
||||
authRepository.createAuthRequest(newEmail)
|
||||
authRepository.getFingerprintPhrase(newEmail)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user