Skip to content

Commit 47afee3

Browse files
committed
Add: new test case for anonymous login with no create subscription and fix some other test cases
1 parent 28e1e24 commit 47afee3

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/operations/OperationRepoTests.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class OperationRepoTests : FunSpec({
215215
// Given
216216
val mocks = Mocks()
217217
val opRepo = mocks.operationRepo
218-
coEvery { opRepo.delayBeforeNextExecution(any(), any(), any()) } just runs
218+
coEvery { opRepo.delayBeforeNextExecution(any(), any()) } just runs
219219
coEvery {
220220
mocks.executor.execute(any())
221221
} returns ExecutionResponse(ExecutionResult.FAIL_RETRY) andThen ExecutionResponse(ExecutionResult.SUCCESS)
@@ -239,7 +239,7 @@ class OperationRepoTests : FunSpec({
239239
it[0] shouldBe operation
240240
},
241241
)
242-
opRepo.delayBeforeNextExecution(1, null, 0)
242+
opRepo.delayBeforeNextExecution(1, null)
243243
mocks.executor.execute(
244244
withArg {
245245
it.count() shouldBe 1
@@ -649,6 +649,32 @@ class OperationRepoTests : FunSpec({
649649
}
650650
}
651651

652+
// operations not removed from the queue may get stuck in the queue if app is force closed within the delay
653+
test("execution of an operation with translation IDs removes the operation from queue before delay") {
654+
// Given
655+
val mocks = Mocks()
656+
mocks.configModelStore.model.opRepoPostCreateDelay = 100
657+
val operation = mockOperation(groupComparisonType = GroupComparisonType.NONE)
658+
val opId = operation.id
659+
val idTranslation = mapOf("local-id1" to "id1")
660+
coEvery {
661+
mocks.executor.execute(listOf(operation))
662+
} returns ExecutionResponse(ExecutionResult.SUCCESS, idTranslation)
663+
664+
// When
665+
mocks.operationRepo.start()
666+
val response = mocks.operationRepo.enqueueAndWait(operation)
667+
668+
// Then
669+
response shouldBe true
670+
coVerifyOrder {
671+
// ensure the order: IDs are translated, operation removed from the store, then delay for postCreateDelay
672+
operation.translateIds(idTranslation)
673+
mocks.operationModelStore.remove(opId)
674+
mocks.operationRepo.delayBeforeNextExecution(any(), any())
675+
}
676+
}
677+
652678
// We want to prevent a misbehaving app stuck in a loop from continuously
653679
// sending updates every opRepoExecutionInterval (5 seconds currently).
654680
// By waiting for the dust to settle we ensure the app is done making

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/operations/LoginUserOperationExecutorTests.kt

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class LoginUserOperationExecutorTests : FunSpec({
3939
val localSubscriptionId2 = "local-subscriptionId2"
4040
val remoteSubscriptionId1 = "remote-subscriptionId1"
4141
val remoteSubscriptionId2 = "remote-subscriptionId2"
42+
val createSubscriptionOperation =
43+
CreateSubscriptionOperation(
44+
appId,
45+
localOneSignalId,
46+
"subscriptionId1",
47+
SubscriptionType.PUSH,
48+
true,
49+
"pushToken1",
50+
SubscriptionStatus.SUBSCRIBED,
51+
)
4252

4353
test("login anonymous user successfully creates user") {
4454
// Given
@@ -58,7 +68,7 @@ class LoginUserOperationExecutorTests : FunSpec({
5868
val loginUserOperationExecutor =
5969
LoginUserOperationExecutor(
6070
mockIdentityOperationExecutor,
61-
MockHelper.applicationService(),
71+
AndroidMockHelper.applicationService(),
6272
MockHelper.deviceService(),
6373
mockUserBackendService,
6474
mockIdentityModelStore,
@@ -67,7 +77,11 @@ class LoginUserOperationExecutorTests : FunSpec({
6777
MockHelper.configModelStore(),
6878
MockHelper.languageContext(),
6979
)
70-
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))
80+
val operations =
81+
listOf<Operation>(
82+
LoginUserOperation(appId, localOneSignalId, null, null),
83+
createSubscriptionOperation,
84+
)
7185

7286
// When
7387
val response = loginUserOperationExecutor.execute(operations)
@@ -98,7 +112,7 @@ class LoginUserOperationExecutorTests : FunSpec({
98112
val loginUserOperationExecutor =
99113
LoginUserOperationExecutor(
100114
mockIdentityOperationExecutor,
101-
MockHelper.applicationService(),
115+
AndroidMockHelper.applicationService(),
102116
MockHelper.deviceService(),
103117
mockUserBackendService,
104118
mockIdentityModelStore,
@@ -107,7 +121,11 @@ class LoginUserOperationExecutorTests : FunSpec({
107121
MockHelper.configModelStore(),
108122
MockHelper.languageContext(),
109123
)
110-
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))
124+
val operations =
125+
listOf<Operation>(
126+
LoginUserOperation(appId, localOneSignalId, null, null),
127+
createSubscriptionOperation,
128+
)
111129

112130
// When
113131
val response = loginUserOperationExecutor.execute(operations)
@@ -130,8 +148,12 @@ class LoginUserOperationExecutorTests : FunSpec({
130148
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
131149

132150
val loginUserOperationExecutor =
133-
LoginUserOperationExecutor(mockIdentityOperationExecutor, MockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
134-
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))
151+
LoginUserOperationExecutor(mockIdentityOperationExecutor, AndroidMockHelper.applicationService(), MockHelper.deviceService(), mockUserBackendService, mockIdentityModelStore, mockPropertiesModelStore, mockSubscriptionsModelStore, MockHelper.configModelStore(), MockHelper.languageContext())
152+
val operations =
153+
listOf<Operation>(
154+
LoginUserOperation(appId, localOneSignalId, null, null),
155+
createSubscriptionOperation,
156+
)
135157

136158
// When
137159
val response = loginUserOperationExecutor.execute(operations)
@@ -679,4 +701,42 @@ class LoginUserOperationExecutorTests : FunSpec({
679701
)
680702
}
681703
}
704+
705+
test("ensure anonymous login with no other operations will fail with FAIL_NORETRY") {
706+
// Given
707+
val mockUserBackendService = mockk<IUserBackendService>()
708+
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
709+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
710+
711+
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
712+
713+
val mockIdentityModelStore = MockHelper.identityModelStore()
714+
val mockPropertiesModelStore = MockHelper.propertiesModelStore()
715+
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
716+
717+
val loginUserOperationExecutor =
718+
LoginUserOperationExecutor(
719+
mockIdentityOperationExecutor,
720+
MockHelper.applicationService(),
721+
MockHelper.deviceService(),
722+
mockUserBackendService,
723+
mockIdentityModelStore,
724+
mockPropertiesModelStore,
725+
mockSubscriptionsModelStore,
726+
MockHelper.configModelStore(),
727+
MockHelper.languageContext(),
728+
)
729+
// anonymous Login request
730+
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, null, null))
731+
732+
// When
733+
val response = loginUserOperationExecutor.execute(operations)
734+
735+
// Then
736+
response.result shouldBe ExecutionResult.FAIL_NORETRY
737+
// ensure user is not created by the bad request
738+
coVerify(
739+
exactly = 0,
740+
) { mockUserBackendService.createUser(appId, any(), any(), any()) }
741+
}
682742
})

0 commit comments

Comments
 (0)