diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/api/AuthenticatedAuthRequestsApi.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/api/AuthenticatedAuthRequestsApi.kt index 453db3df1..36b192b12 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/api/AuthenticatedAuthRequestsApi.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/api/AuthenticatedAuthRequestsApi.kt @@ -26,4 +26,12 @@ interface AuthenticatedAuthRequestsApi { */ @GET("/auth-requests") suspend fun getAuthRequests(): Result + + /** + * Retrieves an existing authentication request by ID. + */ + @GET("/auth-requests/{requestId}") + suspend fun getAuthRequest( + @Path("requestId") requestId: String, + ): Result } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsService.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsService.kt index 409e3f8ee..5daa3820c 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsService.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsService.kt @@ -11,6 +11,13 @@ interface AuthRequestsService { */ suspend fun getAuthRequests(): Result + /** + * Retrieves an existing auth request to see if a device has approved it. + */ + suspend fun getAuthRequest( + requestId: String, + ): Result + /** * Updates an approval request. */ diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceImpl.kt index 065135cf7..a6c4f5782 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceImpl.kt @@ -10,6 +10,11 @@ class AuthRequestsServiceImpl( override suspend fun getAuthRequests(): Result = authenticatedAuthRequestsApi.getAuthRequests() + override suspend fun getAuthRequest( + requestId: String, + ): Result = + authenticatedAuthRequestsApi.getAuthRequest(requestId = requestId) + override suspend fun updateAuthRequest( requestId: String, key: String, diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceTest.kt index af6b2a655..2e8f9dd2e 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/AuthRequestsServiceTest.kt @@ -65,32 +65,7 @@ class AuthRequestsServiceTest : BaseServiceTest() { @Test fun `updateAuthRequest 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) + val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200) server.enqueue(response) val actual = service.updateAuthRequest( requestId = "userId", @@ -99,6 +74,49 @@ class AuthRequestsServiceTest : BaseServiceTest() { masterPasswordHash = "verySecureHash", isApproved = true, ) - assertEquals(Result.success(expected), actual) + assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual) + } + + @Test + fun `getAuthRequest when request response is Failure should return Failure`() = runTest { + val response = MockResponse().setResponseCode(400) + server.enqueue(response) + val actual = service.getAuthRequest(requestId = "1") + assertTrue(actual.isFailure) + } + + @Test + fun `getAuthRequest when request response is Success should return Success`() = runTest { + val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200) + server.enqueue(response) + val actual = service.getAuthRequest(requestId = "1") + assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual) } } + +private const val AUTH_REQUEST_RESPONSE_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" +} +""" + +private val AUTH_REQUEST_RESPONSE = 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", +) diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/NewAuthRequestServiceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/NewAuthRequestServiceTest.kt index 635c05a05..16bccce56 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/NewAuthRequestServiceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/network/service/NewAuthRequestServiceTest.kt @@ -34,32 +34,7 @@ class NewAuthRequestServiceTest : BaseServiceTest() { @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) + val response = MockResponse().setBody(AUTH_REQUEST_RESPONSE_JSON).setResponseCode(200) server.enqueue(response) val actual = service.createAuthRequest( email = "test@gmail.com", @@ -68,6 +43,33 @@ class NewAuthRequestServiceTest : BaseServiceTest() { accessCode = "accessCode", fingerprint = "fingerprint", ) - assertEquals(Result.success(expected), actual) + assertEquals(Result.success(AUTH_REQUEST_RESPONSE), actual) } } + +private const val AUTH_REQUEST_RESPONSE_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" +} +""" + +private val AUTH_REQUEST_RESPONSE = 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", +)