Skip to content

Commit a381e93

Browse files
Ian BirdIanDBird
authored andcommitted
Add unit tests to cover opt-out user for generate requests
1 parent fc10821 commit a381e93

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

sdk/src/test/java/com/uid2/UID2ClientTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ class UID2ClientTest {
220220
assertEquals(expectedIdentity, response.identity)
221221
}
222222

223+
@Test
224+
fun `test generate opt-out`() = runTest(testDispatcher) {
225+
val client = withClient()
226+
227+
// Configure the network request to return a status which indicates the user has opted-out.
228+
val unencrypted = JSONObject(TestData.REFRESH_TOKEN_OPT_OUT_DECRYPTED)
229+
every { dataEnvelope.decrypt(any<ByteArray>(), any<String>(), any<Boolean>()) }.returns(
230+
unencrypted.toString().toByteArray(),
231+
)
232+
every { networkSession.loadData(any(), any()) }.returns(NetworkResponse(200, "some data"))
233+
234+
val response = client.generateIdentity(
235+
IdentityRequest.Email("[email protected]"),
236+
SUBSCRIPTION_ID,
237+
PUBLIC_KEY,
238+
)
239+
assertNotNull(response)
240+
241+
// Verify that we don't receive any identity but the status explicitly tells us that they have opted-out.
242+
assertNull(response.identity)
243+
assertEquals(IdentityStatus.OPT_OUT, response.status)
244+
}
245+
223246
//endregion
224247

225248
//region refreshIdentity

sdk/src/test/java/com/uid2/UID2ManagerTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,29 @@ class UID2ManagerTest {
197197
assertTrue(result is GenerateIdentityResult.Error)
198198
}
199199

200+
@Test
201+
fun `generates for opt-out identity`() = runTest(testDispatcher) {
202+
val subscriptionId = "sub"
203+
val publicKey = "pub"
204+
205+
val request = IdentityRequest.Email("[email protected]")
206+
coEvery { client.generateIdentity(request, subscriptionId, publicKey) }.returns(
207+
ResponsePackage(null, OPT_OUT, ""),
208+
)
209+
210+
// Request a new identity should be generated.
211+
var result: GenerateIdentityResult? = null
212+
manager.generateIdentity(request, subscriptionId, publicKey) { result = it }
213+
testDispatcher.scheduler.advanceUntilIdle()
214+
215+
// Verify that after generating the identity, there is no current identity but the expected OPT_OUT status.
216+
assertNull(manager.currentIdentity)
217+
assertEquals(OPT_OUT, manager.currentIdentityStatus)
218+
219+
// Verify that the result callback was invoked.
220+
assertTrue(result is GenerateIdentityResult.Success)
221+
}
222+
200223
@Test
201224
fun `resets identity`() = runTest(testDispatcher) {
202225
// Verify that the initial state of the manager reflects the restored Identity.

0 commit comments

Comments
 (0)