Skip to content

Commit f8abc51

Browse files
committed
Add and adjust some test units for JWT related change
1 parent f29d0df commit f8abc51

File tree

7 files changed

+199
-6
lines changed

7 files changed

+199
-6
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ import java.util.UUID
3434
private class Mocks {
3535
val configModelStore = MockHelper.configModelStore()
3636

37+
val identityModelStore =
38+
MockHelper.identityModelStore {
39+
it.jwtToken = ""
40+
it.externalId = "externalId1"
41+
}
42+
3743
val operationModelStore: OperationModelStore =
3844
run {
3945
val operationStoreList = mutableListOf<Operation>()
@@ -63,6 +69,7 @@ private class Mocks {
6369
listOf(executor),
6470
operationModelStore,
6571
configModelStore,
72+
identityModelStore,
6673
Time(),
6774
getNewRecordState(configModelStore),
6875
),
@@ -685,6 +692,48 @@ class OperationRepoTests : FunSpec({
685692
response2 shouldBe true
686693
opRepo.forceExecuteOperations()
687694
}
695+
696+
test("operations that need to be identity verified cannot execute until JWT is provided") {
697+
// Given
698+
val mocks = Mocks()
699+
val waiter = Waiter()
700+
701+
every { mocks.configModelStore.model.useIdentityVerification } returns true // set identity verification on
702+
every { mocks.identityModelStore.model.jwtToken } returns null // jwt is initially unset
703+
every { mocks.operationModelStore.remove(any()) } answers {} andThenAnswer { waiter.wake() }
704+
705+
val operation1 = mockOperation("operationId1")
706+
val operation2 = mockOperation("operationId2")
707+
708+
operation1.setStringProperty("externalId", "externalId1")
709+
operation2.setStringProperty("externalId", "externalId1")
710+
711+
// When
712+
mocks.operationRepo.enqueue(operation1)
713+
mocks.operationRepo.enqueue(operation2)
714+
mocks.operationRepo.start()
715+
716+
waiter.waitForWake()
717+
718+
// Then
719+
coVerifyOrder {
720+
mocks.operationModelStore.add(operation1)
721+
mocks.operationModelStore.add(operation2)
722+
}
723+
724+
//
725+
coVerify(exactly = 0) {
726+
mocks.executor.execute(
727+
withArg {
728+
it.count() shouldBe 2
729+
it[0] shouldBe operation1
730+
it[1] shouldBe operation2
731+
},
732+
)
733+
mocks.operationModelStore.remove("operationId1")
734+
mocks.operationModelStore.remove("operationId2")
735+
}
736+
}
688737
}) {
689738
companion object {
690739
private fun mockOperation(

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.onesignal.internal
22

3+
import android.content.Context
34
import com.onesignal.debug.LogLevel
45
import com.onesignal.debug.internal.logging.Logging
56
import io.kotest.assertions.throwables.shouldThrowUnit
67
import io.kotest.core.spec.style.FunSpec
78
import io.kotest.matchers.shouldBe
9+
import io.mockk.mockk
810

911
class OneSignalImpTests : FunSpec({
1012
beforeAny {
@@ -86,4 +88,16 @@ class OneSignalImpTests : FunSpec({
8688
}
8789
}
8890
}
91+
92+
test("When identity verification is on and no user is created, calling initWithContext will create a new user") {
93+
// Given
94+
val os = OneSignalImp()
95+
val appId = "tempAppId"
96+
val context = mockk<Context>()
97+
98+
// When
99+
os.initWithContext(context, appId)
100+
101+
// TODO
102+
}
89103
})

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.onesignal.user.internal
22

3+
import com.onesignal.IUserJwtInvalidatedListener
34
import com.onesignal.core.internal.language.ILanguageContext
5+
import com.onesignal.core.internal.operations.ExecutionResponse
6+
import com.onesignal.core.internal.operations.ExecutionResult
7+
import com.onesignal.core.internal.operations.Operation
48
import com.onesignal.mocks.MockHelper
9+
import com.onesignal.user.internal.backend.CreateUserResponse
10+
import com.onesignal.user.internal.backend.IUserBackendService
11+
import com.onesignal.user.internal.backend.IdentityConstants
12+
import com.onesignal.user.internal.backend.PropertiesObject
13+
import com.onesignal.user.internal.operations.LoginUserOperation
14+
import com.onesignal.user.internal.operations.impl.executors.IdentityOperationExecutor
15+
import com.onesignal.user.internal.operations.impl.executors.LoginUserOperationExecutor
516
import com.onesignal.user.internal.subscriptions.ISubscriptionManager
617
import com.onesignal.user.internal.subscriptions.SubscriptionList
18+
import com.onesignal.user.internal.subscriptions.SubscriptionModelStore
719
import io.kotest.core.spec.style.FunSpec
820
import io.kotest.matchers.shouldBe
921
import io.kotest.matchers.shouldNotBe
22+
import io.mockk.coEvery
1023
import io.mockk.every
1124
import io.mockk.just
1225
import io.mockk.mockk
1326
import io.mockk.runs
1427
import io.mockk.slot
28+
import io.mockk.spyk
1529
import io.mockk.verify
1630

1731
class UserManagerTests : FunSpec({
@@ -141,7 +155,8 @@ class UserManagerTests : FunSpec({
141155
it.tags["my-tag-key1"] = "my-tag-value1"
142156
}
143157

144-
val userManager = UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())
158+
val userManager =
159+
UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())
145160

146161
// When
147162
val tagSnapshot1 = userManager.getTags()
@@ -191,4 +206,58 @@ class UserManagerTests : FunSpec({
191206
verify(exactly = 1) { mockSubscriptionManager.addSmsSubscription("+15558675309") }
192207
verify(exactly = 1) { mockSubscriptionManager.removeSmsSubscription("+15558675309") }
193208
}
209+
210+
test("login user with jwt calls onUserJwtInvalidated() when the jwt is unauthorized") {
211+
// Given
212+
val appId = "appId"
213+
val localOneSignalId = "local-onesignalId"
214+
val remoteOneSignalId = "remote-onesignalId"
215+
216+
// mock components
217+
val mockSubscriptionManager = mockk<ISubscriptionManager>()
218+
val mockIdentityModelStore = MockHelper.identityModelStore()
219+
val mockPropertiesModelStore = MockHelper.propertiesModelStore()
220+
val mockSubscriptionsModelStore = mockk<SubscriptionModelStore>()
221+
val mockLanguageContext = MockHelper.languageContext()
222+
223+
// mock backend service
224+
val mockUserBackendService = mockk<IUserBackendService>()
225+
coEvery { mockUserBackendService.createUser(any(), any(), any(), any()) } returns
226+
CreateUserResponse(mapOf(IdentityConstants.ONESIGNAL_ID to remoteOneSignalId), PropertiesObject(), listOf())
227+
228+
// mock operation for login user
229+
val mockIdentityOperationExecutor = mockk<IdentityOperationExecutor>()
230+
coEvery { mockIdentityOperationExecutor.execute(any()) } returns
231+
ExecutionResponse(ExecutionResult.FAIL_UNAUTHORIZED)
232+
val loginUserOperationExecutor =
233+
LoginUserOperationExecutor(
234+
mockIdentityOperationExecutor,
235+
MockHelper.applicationService(),
236+
MockHelper.deviceService(),
237+
mockUserBackendService,
238+
mockIdentityModelStore,
239+
mockPropertiesModelStore,
240+
mockSubscriptionsModelStore,
241+
MockHelper.configModelStore(),
242+
mockLanguageContext,
243+
)
244+
val operations = listOf<Operation>(LoginUserOperation(appId, localOneSignalId, "externalId", "existingOneSignalId"))
245+
246+
// mock user manager with jwtInvalidatedListener added
247+
val userManager =
248+
UserManager(mockSubscriptionManager, mockIdentityModelStore, mockPropertiesModelStore, mockLanguageContext)
249+
mockIdentityModelStore.subscribe(userManager)
250+
val spyJwtInvalidatedListener = spyk<IUserJwtInvalidatedListener>()
251+
userManager.addUserJwtInvalidatedListener(spyJwtInvalidatedListener)
252+
253+
// When
254+
val response = loginUserOperationExecutor.execute(operations)
255+
256+
// Then
257+
userManager.jwtInvalidatedCallback.hasSubscribers shouldBe true
258+
response.result shouldBe ExecutionResult.FAIL_UNAUTHORIZED
259+
verify(exactly = 1) { mockIdentityModelStore.invalidateJwt() }
260+
// Note: set the default value of useIdentityVerification in OneSignalImp.kt to pass the test
261+
verify(exactly = 1) { spyJwtInvalidatedListener.onUserJwtInvalidated(any()) }
262+
}
194263
})

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/migrations/RecoverFromDroppedLoginBugTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private class Mocks {
3636
listOf(),
3737
operationModelStore,
3838
configModelStore,
39+
MockHelper.identityModelStore(),
3940
Time(),
4041
ExecutorMocks.getNewRecordState(configModelStore),
4142
),

0 commit comments

Comments
 (0)