From b0b64178f722ca750b67dcfb7fd946856e1a1a5f Mon Sep 17 00:00:00 2001 From: Andrew Haisting <142518658+ahaisting-livefront@users.noreply.github.com> Date: Mon, 16 Oct 2023 09:35:44 -0500 Subject: [PATCH] BIT-326 Use correct value for deviceName on get token request (#114) --- .../network/service/IdentityServiceImpl.kt | 5 +++-- .../data/platform/util/DeviceModelProvider.kt | 14 ++++++++++++++ .../network/service/IdentityServiceTest.kt | 7 +++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/x8bit/bitwarden/data/platform/util/DeviceModelProvider.kt diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt index b84495e69..0aae1893c 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceImpl.kt @@ -5,6 +5,7 @@ import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJs import com.x8bit.bitwarden.data.platform.datasource.network.model.toBitwardenError import com.x8bit.bitwarden.data.platform.datasource.network.util.base64UrlEncode import com.x8bit.bitwarden.data.platform.datasource.network.util.parseErrorBodyOrNull +import com.x8bit.bitwarden.data.platform.util.DeviceModelProvider import kotlinx.serialization.json.Json import java.net.HttpURLConnection.HTTP_BAD_REQUEST import java.util.UUID @@ -14,6 +15,7 @@ class IdentityServiceImpl constructor( private val json: Json, // TODO: use correct base URL here BIT-328 private val baseUrl: String = "https://vault.bitwarden.com", + private val deviceModelProvider: DeviceModelProvider = DeviceModelProvider(), ) : IdentityService { override suspend fun getToken( @@ -29,8 +31,7 @@ class IdentityServiceImpl constructor( authEmail = email.base64UrlEncode(), // TODO: use correct device identifier here BIT-325 deviceIdentifier = UUID.randomUUID().toString(), - // TODO: use correct values for deviceName and deviceType BIT-326 - deviceName = "Pixel 6", + deviceName = deviceModelProvider.deviceModel, deviceType = "0", grantType = "password", passwordHash = passwordHash, diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/util/DeviceModelProvider.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/util/DeviceModelProvider.kt new file mode 100644 index 000000000..c594af2f2 --- /dev/null +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/util/DeviceModelProvider.kt @@ -0,0 +1,14 @@ +package com.x8bit.bitwarden.data.platform.util + +import android.os.Build + +/** + * Provides device model string. Useful for mocking static [Build.model] call tests. + */ +class DeviceModelProvider { + + /** + * Device model. + */ + val deviceModel: String = Build.MODEL +} diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt index 89ce9fd45..9aae29a6b 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/IdentityServiceTest.kt @@ -3,6 +3,9 @@ package com.x8bit.bitwarden.data.auth.datasource.network.service import com.x8bit.bitwarden.data.auth.datasource.network.api.IdentityApi import com.x8bit.bitwarden.data.auth.datasource.network.model.GetTokenResponseJson import com.x8bit.bitwarden.data.platform.base.BaseServiceTest +import com.x8bit.bitwarden.data.platform.util.DeviceModelProvider +import io.mockk.every +import io.mockk.mockk import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json import okhttp3.mockwebserver.MockResponse @@ -14,11 +17,15 @@ import retrofit2.create class IdentityServiceTest : BaseServiceTest() { private val identityApi: IdentityApi = retrofit.create() + private val deviceModelProvider = mockk() { + every { deviceModel } returns "Test Device" + } private val identityService = IdentityServiceImpl( api = identityApi, json = Json, baseUrl = server.url("/").toString(), + deviceModelProvider = deviceModelProvider, ) @Test