|
1 | 1 | package com.onesignal.user.internal.subscriptions
|
2 | 2 |
|
| 3 | +import com.onesignal.common.IDManager.LOCAL_PREFIX |
3 | 4 | import com.onesignal.common.modeling.ModelChangeTags
|
4 | 5 | import com.onesignal.common.modeling.ModelChangedArgs
|
5 | 6 | import com.onesignal.core.internal.application.IApplicationService
|
6 | 7 | import com.onesignal.session.internal.session.ISessionService
|
| 8 | +import com.onesignal.user.internal.Subscription |
7 | 9 | import com.onesignal.user.internal.subscriptions.impl.SubscriptionManager
|
8 | 10 | import com.onesignal.user.subscriptions.ISmsSubscription
|
9 | 11 | import io.kotest.core.spec.style.FunSpec
|
10 | 12 | import io.kotest.matchers.should
|
11 | 13 | import io.kotest.matchers.shouldBe
|
12 | 14 | import io.kotest.matchers.shouldNotBe
|
13 | 15 | import io.kotest.matchers.types.beInstanceOf
|
| 16 | +import io.kotest.matchers.types.shouldBeSameInstanceAs |
14 | 17 | import io.mockk.every
|
15 | 18 | import io.mockk.just
|
16 | 19 | import io.mockk.mockk
|
@@ -328,6 +331,53 @@ class SubscriptionManagerTests : FunSpec({
|
328 | 331 | verify(exactly = 1) { spySubscriptionChangedHandler.onSubscriptionChanged(any(), any()) }
|
329 | 332 | }
|
330 | 333 |
|
| 334 | + // This is a common case where updates (such as optedIn) should |
| 335 | + // still propagate even if we haven't sent the POST /users create |
| 336 | + // call yet. Motivation for this test was a bug was discovered |
| 337 | + // where calling OneSignal.User.pushSubscription.optIn() was not |
| 338 | + // prompting for notification permission if it was called before |
| 339 | + // the create User network call finished. |
| 340 | + test("subscription modified when model updated, but with local-id") { |
| 341 | + // Given |
| 342 | + val pushSubscriptionModel = SubscriptionModel() |
| 343 | + pushSubscriptionModel.id = "${LOCAL_PREFIX}subscription1" |
| 344 | + pushSubscriptionModel.type = SubscriptionType.PUSH |
| 345 | + pushSubscriptionModel.optedIn = true |
| 346 | + pushSubscriptionModel.address = "my_push_token-org" |
| 347 | + |
| 348 | + val mockSubscriptionModelStore = mockk<SubscriptionModelStore>() |
| 349 | + val mockApplicationService = mockk<IApplicationService>() |
| 350 | + val mockSessionService = mockk<ISessionService>(relaxed = true) |
| 351 | + val listOfSubscriptions = listOf(pushSubscriptionModel) |
| 352 | + |
| 353 | + every { mockSubscriptionModelStore.subscribe(any()) } just runs |
| 354 | + every { mockSubscriptionModelStore.list() } returns listOfSubscriptions |
| 355 | + |
| 356 | + val spySubscriptionChangedHandler = spyk<ISubscriptionChangedHandler>() |
| 357 | + |
| 358 | + val subscriptionManager = SubscriptionManager(mockApplicationService, mockSessionService, mockSubscriptionModelStore) |
| 359 | + subscriptionManager.subscribe(spySubscriptionChangedHandler) |
| 360 | + |
| 361 | + // When |
| 362 | + pushSubscriptionModel.address = "my_push_token-new" |
| 363 | + subscriptionManager.onModelUpdated( |
| 364 | + ModelChangedArgs( |
| 365 | + pushSubscriptionModel, |
| 366 | + SubscriptionModel::address.name, |
| 367 | + SubscriptionModel::address.name, |
| 368 | + "my_push_token-org", |
| 369 | + "my_push_token-new", |
| 370 | + ), |
| 371 | + ModelChangeTags.NORMAL, |
| 372 | + ) |
| 373 | + val subscriptions = subscriptionManager.subscriptions |
| 374 | + |
| 375 | + // Then |
| 376 | + (subscriptions.push as Subscription).model shouldBeSameInstanceAs pushSubscriptionModel |
| 377 | + subscriptions.push.token shouldBe "my_push_token-new" |
| 378 | + verify(exactly = 1) { spySubscriptionChangedHandler.onSubscriptionChanged(any(), any()) } |
| 379 | + } |
| 380 | + |
331 | 381 | test("subscription removed when model removed") {
|
332 | 382 | // Given
|
333 | 383 | val smsSubscription = SubscriptionModel()
|
|
0 commit comments