|
1 | 1 | package com.onesignal.user.internal
|
2 | 2 |
|
| 3 | +import com.onesignal.IUserJwtInvalidatedListener |
3 | 4 | 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 |
4 | 8 | 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 |
5 | 16 | import com.onesignal.user.internal.subscriptions.ISubscriptionManager
|
6 | 17 | import com.onesignal.user.internal.subscriptions.SubscriptionList
|
| 18 | +import com.onesignal.user.internal.subscriptions.SubscriptionModelStore |
7 | 19 | import io.kotest.core.spec.style.FunSpec
|
8 | 20 | import io.kotest.matchers.shouldBe
|
9 | 21 | import io.kotest.matchers.shouldNotBe
|
| 22 | +import io.mockk.coEvery |
10 | 23 | import io.mockk.every
|
11 | 24 | import io.mockk.just
|
12 | 25 | import io.mockk.mockk
|
13 | 26 | import io.mockk.runs
|
14 | 27 | import io.mockk.slot
|
| 28 | +import io.mockk.spyk |
15 | 29 | import io.mockk.verify
|
16 | 30 |
|
17 | 31 | class UserManagerTests : FunSpec({
|
@@ -141,7 +155,8 @@ class UserManagerTests : FunSpec({
|
141 | 155 | it.tags["my-tag-key1"] = "my-tag-value1"
|
142 | 156 | }
|
143 | 157 |
|
144 |
| - val userManager = UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext()) |
| 158 | + val userManager = |
| 159 | + UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext()) |
145 | 160 |
|
146 | 161 | // When
|
147 | 162 | val tagSnapshot1 = userManager.getTags()
|
@@ -191,4 +206,58 @@ class UserManagerTests : FunSpec({
|
191 | 206 | verify(exactly = 1) { mockSubscriptionManager.addSmsSubscription("+15558675309") }
|
192 | 207 | verify(exactly = 1) { mockSubscriptionManager.removeSmsSubscription("+15558675309") }
|
193 | 208 | }
|
| 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 | + } |
194 | 263 | })
|
0 commit comments