-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Bird
committed
Apr 5, 2024
1 parent
8b17816
commit 1f945d4
Showing
7 changed files
with
88 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,15 +44,16 @@ class UID2ClientTest { | |
private val logger: Logger = mock() | ||
|
||
private val url = "https://test.dev" | ||
private val apiPublicKey = "an api key" | ||
private val apiSubscriptionId = "subscription id" | ||
private val refreshToken = "RefreshToken" | ||
private val refreshKey = "RefreshKey" | ||
|
||
private val keyPair: KeyPair = mock() | ||
private val keyPairPublic: PublicKey = mock() | ||
private val keyPairPublicEncoded = ByteArray(12) | ||
|
||
private val SUBSCRIPTION_ID = "subscription_id" | ||
private val PUBLIC_KEY = "public_key" | ||
|
||
@Before | ||
fun before() { | ||
// By default, don't encrypt the data. Just convert it directly to a ByteArray. | ||
|
@@ -77,7 +78,11 @@ class UID2ClientTest { | |
@Test | ||
fun `test generate with invalid api url`() = runTest(testDispatcher) { | ||
testInvalidClientApi { client -> | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
|
||
|
@@ -91,7 +96,11 @@ class UID2ClientTest { | |
// Verify the expected CryptoException is thrown. | ||
assertThrows(CryptoException::class.java) { | ||
runTest(testDispatcher) { | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -106,7 +115,11 @@ class UID2ClientTest { | |
// Verify the expected CryptoException is thrown. | ||
assertThrows(CryptoException::class.java) { | ||
runTest(testDispatcher) { | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -121,7 +134,11 @@ class UID2ClientTest { | |
// Verify the expected CryptoException is thrown. | ||
assertThrows(CryptoException::class.java) { | ||
runTest(testDispatcher) { | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -136,15 +153,23 @@ class UID2ClientTest { | |
// Verify the expected CryptoException is thrown. | ||
assertThrows(CryptoException::class.java) { | ||
runTest(testDispatcher) { | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `test generate with network failure`() = runTest(testDispatcher) { | ||
testNetworkFailure { client -> | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
|
||
|
@@ -159,7 +184,11 @@ class UID2ClientTest { | |
// Verify the expected CryptoException is thrown. | ||
assertThrows(PayloadDecryptException::class.java) { | ||
runTest(testDispatcher) { | ||
client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -174,7 +203,11 @@ class UID2ClientTest { | |
) | ||
whenever(networkSession.loadData(any(), any())).thenReturn(NetworkResponse(200, "some data")) | ||
|
||
val response = client.generateIdentity(IdentityRequest.Email("[email protected]")) | ||
val response = client.generateIdentity( | ||
IdentityRequest.Email("[email protected]"), | ||
SUBSCRIPTION_ID, | ||
PUBLIC_KEY, | ||
) | ||
assertNotNull(response) | ||
|
||
// Verify that the returned package has an identity that matches what we included in the body of the response. | ||
|
@@ -288,8 +321,6 @@ class UID2ClientTest { | |
private fun testInvalidClientApi(callback: suspend (client: UID2Client) -> Unit) { | ||
val client = UID2Client( | ||
"this is not a url", | ||
apiPublicKey, | ||
apiSubscriptionId, | ||
networkSession, | ||
packageName, | ||
dataEnvelope, | ||
|
@@ -347,8 +378,6 @@ class UID2ClientTest { | |
|
||
private fun withClient() = UID2Client( | ||
url, | ||
apiPublicKey, | ||
apiSubscriptionId, | ||
networkSession, | ||
packageName, | ||
dataEnvelope, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,18 +142,23 @@ class UID2ManagerTest { | |
|
||
@Test | ||
fun `generates identity for different requests`() = runTest(testDispatcher) { | ||
val subscriptionId = "sub" | ||
val publicKey = "pub" | ||
|
||
listOf( | ||
IdentityRequest.Email("[email protected]"), | ||
IdentityRequest.EmailHash("a-hash"), | ||
IdentityRequest.Phone("+00000000000"), | ||
IdentityRequest.PhoneHash("another-hash"), | ||
).forEach { request -> | ||
val generated = withRandomIdentity() | ||
whenever(client.generateIdentity(request)).thenReturn(ResponsePackage(generated, ESTABLISHED, "")) | ||
whenever(client.generateIdentity(request, subscriptionId, publicKey)).thenReturn( | ||
ResponsePackage(generated, ESTABLISHED, ""), | ||
) | ||
|
||
// Request a new identity should be generated. | ||
var result: GenerateIdentityResult? = null | ||
manager.generateIdentity(request) { result = it } | ||
manager.generateIdentity(request, subscriptionId, publicKey) { result = it } | ||
testDispatcher.scheduler.advanceUntilIdle() | ||
|
||
// Verify that the identity is updated from the one provided via the Client. | ||
|
@@ -166,15 +171,20 @@ class UID2ManagerTest { | |
|
||
@Test | ||
fun `existing identity untouched if generation fails`() = runTest(testDispatcher) { | ||
val subscriptionId = "sub" | ||
val publicKey = "pub" | ||
|
||
val request = IdentityRequest.Email("[email protected]") | ||
whenever(client.generateIdentity(request)).thenThrow(PayloadDecryptException::class.java) | ||
whenever(client.generateIdentity(request, subscriptionId, publicKey)).thenThrow( | ||
PayloadDecryptException::class.java, | ||
) | ||
|
||
// Verify that the manager has a known (existing) identity. | ||
assertEquals(manager.currentIdentity, initialIdentity) | ||
|
||
// Request a new identity is generated, knowing that this will fail. | ||
var result: GenerateIdentityResult? = null | ||
manager.generateIdentity(request) { result = it } | ||
manager.generateIdentity(request, subscriptionId, publicKey) { result = it } | ||
testDispatcher.scheduler.advanceUntilIdle() | ||
|
||
// Verify that after the failure, the existing identity is still present. | ||
|