Removed unnecessary api call. Refactored methods to use existing currentDeviceId.

Better string naming
Added more tests
This commit is contained in:
Andre Rosado
2026-04-21 13:09:05 +01:00
parent 7ef963684e
commit 64e2e985e9
23 changed files with 404 additions and 235 deletions

View File

@@ -1,7 +1,6 @@
package com.bitwarden.network.api
import androidx.annotation.Keep
import com.bitwarden.network.model.DeviceResponseJson
import com.bitwarden.network.model.DevicesResponseJson
import com.bitwarden.network.model.NetworkResult
import com.bitwarden.network.model.TrustedDeviceKeysRequestJson
@@ -19,11 +18,6 @@ internal interface AuthenticatedDevicesApi {
@GET("/devices")
suspend fun getDevices(): NetworkResult<DevicesResponseJson>
@GET("/devices/identifier/{deviceIdentifier}")
suspend fun getDeviceByIdentifier(
@Path(value = "deviceIdentifier") deviceIdentifier: String,
): NetworkResult<DeviceResponseJson>
@PUT("/devices/{appId}/keys")
suspend fun updateTrustedDeviceKeys(
@Path(value = "appId") appId: String,

View File

@@ -10,7 +10,7 @@ import java.time.Instant
*
* @property id The unique identifier of the device.
* @property name The name of the device.
* @property identifier The unique identifier of the device.
* @property identifier The unique install identifier of the device.
* @property type The type of the device.
* @property creationDate The date and time on which this device was created.
* @property isTrusted Whether this device is trusted.

View File

@@ -1,6 +1,5 @@
package com.bitwarden.network.service
import com.bitwarden.network.model.DeviceResponseJson
import com.bitwarden.network.model.DevicesResponseJson
import com.bitwarden.network.model.TrustedDeviceKeysResponseJson
@@ -13,11 +12,6 @@ interface DevicesService {
*/
suspend fun getDevices(): Result<DevicesResponseJson>
/**
* Get a device by its client-generated identifier.
*/
suspend fun getDeviceByIdentifier(deviceIdentifier: String): Result<DeviceResponseJson>
/**
* Check whether this device is known (and thus whether Login with Device is available).
*/

View File

@@ -2,7 +2,6 @@ package com.bitwarden.network.service
import com.bitwarden.network.api.AuthenticatedDevicesApi
import com.bitwarden.network.api.UnauthenticatedDevicesApi
import com.bitwarden.network.model.DeviceResponseJson
import com.bitwarden.network.model.DevicesResponseJson
import com.bitwarden.network.model.TrustedDeviceKeysRequestJson
import com.bitwarden.network.model.TrustedDeviceKeysResponseJson
@@ -19,11 +18,6 @@ internal class DevicesServiceImpl(
override suspend fun getDevices(): Result<DevicesResponseJson> =
authenticatedDevicesApi.getDevices().toResult()
override suspend fun getDeviceByIdentifier(
deviceIdentifier: String,
): Result<DeviceResponseJson> =
authenticatedDevicesApi.getDeviceByIdentifier(deviceIdentifier).toResult()
override suspend fun getIsKnownDevice(
emailAddress: String,
deviceId: String,

View File

@@ -4,6 +4,8 @@ import com.bitwarden.core.data.util.asSuccess
import com.bitwarden.network.api.AuthenticatedDevicesApi
import com.bitwarden.network.api.UnauthenticatedDevicesApi
import com.bitwarden.network.base.BaseServiceTest
import com.bitwarden.network.model.DeviceResponseJson
import com.bitwarden.network.model.DevicesResponseJson
import com.bitwarden.network.model.TrustedDeviceKeysResponseJson
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
@@ -22,6 +24,22 @@ class DevicesServiceTest : BaseServiceTest() {
unauthenticatedDevicesApi = unauthenticatedDevicesApi,
)
@Test
fun `getDevices when request response is Failure should return Failure`() = runTest {
val response = MockResponse().setResponseCode(400)
server.enqueue(response)
val actual = service.getDevices()
assertTrue(actual.isFailure)
}
@Test
fun `getDevices when request response is Success should return Success`() = runTest {
val response = MockResponse().setBody(GET_DEVICES_RESPONSE_JSON).setResponseCode(200)
server.enqueue(response)
val actual = service.getDevices()
assertEquals(GET_DEVICES_RESPONSE.asSuccess(), actual)
}
@Test
fun `getIsKnownDevice when request response is Failure should return Failure`() = runTest {
val response = MockResponse().setResponseCode(400)
@@ -65,6 +83,42 @@ class DevicesServiceTest : BaseServiceTest() {
}
}
private val GET_DEVICES_RESPONSE: DevicesResponseJson = DevicesResponseJson(
devices = listOf(
DeviceResponseJson(
id = "0d31b6fb-d282-43c7-b614-b13e0129dbd7",
name = "Pixel 8",
identifier = "ea7c0a13-5ce4-4f96-8e17-4fc7fa54f464",
type = 0,
creationDate = Instant.parse("2024-03-25T18:04:28.23Z"),
lastActivityDate = Instant.parse("2024-03-26T10:00:00.00Z"),
isTrusted = true,
encryptedUserKey = null,
encryptedPublicKey = null,
devicePendingAuthRequest = null,
),
),
)
private const val GET_DEVICES_RESPONSE_JSON: String = """
{
"data": [
{
"id": "0d31b6fb-d282-43c7-b614-b13e0129dbd7",
"name": "Pixel 8",
"identifier": "ea7c0a13-5ce4-4f96-8e17-4fc7fa54f464",
"type": 0,
"creationDate": "2024-03-25T18:04:28.23Z",
"lastActivityDate": "2024-03-26T10:00:00.00Z",
"isTrusted": true,
"encryptedUserKey": null,
"encryptedPublicKey": null,
"devicePendingAuthRequest": null
}
]
}
"""
private val TRUST_DEVICE_RESPONSE: TrustedDeviceKeysResponseJson =
TrustedDeviceKeysResponseJson(
id = "0d31b6fb-d282-43c7-b614-b13e0129dbd7",