11package com.onesignal.user.internal.operations
22
3+ import com.onesignal.common.TimeUtils
34import com.onesignal.common.exceptions.BackendException
45import com.onesignal.common.modeling.ModelChangeTags
56import com.onesignal.core.internal.operations.ExecutionResult
@@ -27,7 +28,9 @@ import io.mockk.coVerify
2728import io.mockk.every
2829import io.mockk.just
2930import io.mockk.mockk
31+ import io.mockk.mockkObject
3032import io.mockk.runs
33+ import io.mockk.unmockkObject
3134
3235class RefreshUserOperationExecutorTests : FunSpec ({
3336 val appId = " appId"
@@ -37,13 +40,24 @@ class RefreshUserOperationExecutorTests : FunSpec({
3740 val remoteSubscriptionId1 = " remote-subscriptionId1"
3841 val remoteSubscriptionId2 = " remote-subscriptionId2"
3942
40- test("refresh user is successful") {
43+ test("refresh user is successful and models are hydrated properly ") {
4144 // Given
45+ val localTimeZone = " Europe/Local"
46+ val remoteTimeZone = " Europe/Remote"
47+ mockkObject(TimeUtils )
48+ every { TimeUtils .getTimeZoneId() } returns localTimeZone
49+
50+ val localCountry = " US"
51+ val remoteCountry = " VT"
52+ val localLanguage = " fr"
53+ val remoteLanguage = " it"
54+ val remoteTags = mapOf("tagKey1" to "remote-1", "tagKey2" to "remote-2")
55+
4256 val mockUserBackendService = mockk<IUserBackendService >()
4357 coEvery { mockUserBackendService.getUser(appId, IdentityConstants .ONESIGNAL_ID , remoteOneSignalId) } returns
4458 CreateUserResponse (
4559 mapOf(IdentityConstants .ONESIGNAL_ID to remoteOneSignalId, "aliasLabel1" to "aliasValue1"),
46- PropertiesObject (country = " US " ),
60+ PropertiesObject (country = remoteCountry, language = remoteLanguage, timezoneId = remoteTimeZone, tags = remoteTags ),
4761 listOf(
4862 SubscriptionObject (existingSubscriptionId1, SubscriptionObjectType .ANDROID_PUSH , enabled = true, token = "on-backend-push-token"),
4963 SubscriptionObject (remoteSubscriptionId1, SubscriptionObjectType .ANDROID_PUSH , enabled = true, token = "pushToken2"),
@@ -61,8 +75,8 @@ class RefreshUserOperationExecutorTests : FunSpec({
6175 val mockPropertiesModelStore = MockHelper .propertiesModelStore()
6276 val mockPropertiesModel = PropertiesModel ()
6377 mockPropertiesModel.onesignalId = remoteOneSignalId
64- mockPropertiesModel.country = " VT "
65- mockPropertiesModel.language = " language "
78+ mockPropertiesModel.country = localCountry
79+ mockPropertiesModel.language = localLanguage
6680 every { mockPropertiesModelStore.model } returns mockPropertiesModel
6781 every { mockPropertiesModelStore.replace(any(), any()) } just runs
6882
@@ -84,7 +98,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
8498
8599 val mockBuildUserService = mockk<IRebuildUserService >()
86100
87- val loginUserOperationExecutor =
101+ val refreshUserOperationExecutor =
88102 RefreshUserOperationExecutor (
89103 mockUserBackendService,
90104 mockIdentityModelStore,
@@ -97,40 +111,49 @@ class RefreshUserOperationExecutorTests : FunSpec({
97111
98112 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
99113
100- // When
101- val response = loginUserOperationExecutor.execute(operations)
102-
103- // Then
104- response.result shouldBe ExecutionResult .SUCCESS
105- coVerify(exactly = 1) {
106- mockUserBackendService.getUser(appId, IdentityConstants .ONESIGNAL_ID , remoteOneSignalId)
107- mockIdentityModelStore.replace(
108- withArg {
109- it["aliasLabel1"] shouldBe "aliasValue1"
110- },
111- ModelChangeTags .HYDRATE ,
112- )
113- mockPropertiesModelStore.replace(
114- withArg {
115- it.country shouldBe "US "
116- it.language shouldBe null
117- },
118- ModelChangeTags .HYDRATE ,
119- )
120- mockSubscriptionsModelStore.replaceAll(
121- withArg {
122- it.count() shouldBe 2
123- it[0].id shouldBe remoteSubscriptionId2
124- it[0].type shouldBe SubscriptionType .EMAIL
125- it[0].optedIn shouldBe true
126- it[0].address shouldBe "name@company.com"
127- it[1].id shouldBe existingSubscriptionId1
128- it[1].type shouldBe SubscriptionType .PUSH
129- it[1].optedIn shouldBe true
130- it[1].address shouldBe onDevicePushToken
131- },
132- ModelChangeTags .HYDRATE ,
133- )
114+ try {
115+ // When
116+ val response = refreshUserOperationExecutor.execute(operations)
117+
118+ // Then
119+ response.result shouldBe ExecutionResult .SUCCESS
120+ coVerify(exactly = 1) {
121+ mockUserBackendService.getUser(appId, IdentityConstants .ONESIGNAL_ID , remoteOneSignalId)
122+ mockIdentityModelStore.replace(
123+ withArg {
124+ it["aliasLabel1"] shouldBe "aliasValue1"
125+ },
126+ ModelChangeTags .HYDRATE ,
127+ )
128+ // The properties model should be set with appropriate remote and local values
129+ mockPropertiesModelStore.replace(
130+ withArg {
131+ it.onesignalId shouldBe remoteOneSignalId
132+ it.country shouldBe remoteCountry
133+ it.language shouldBe remoteLanguage
134+ it.tags shouldBe remoteTags
135+ it.timezone shouldBe localTimeZone // timezone is set locally
136+ },
137+ ModelChangeTags .HYDRATE ,
138+ )
139+ mockSubscriptionsModelStore.replaceAll(
140+ withArg {
141+ it.count() shouldBe 2
142+ it[0].id shouldBe remoteSubscriptionId2
143+ it[0].type shouldBe SubscriptionType .EMAIL
144+ it[0].optedIn shouldBe true
145+ it[0].address shouldBe "name@company.com"
146+ it[1].id shouldBe existingSubscriptionId1
147+ it[1].type shouldBe SubscriptionType .PUSH
148+ it[1].optedIn shouldBe true
149+ it[1].address shouldBe onDevicePushToken
150+ },
151+ ModelChangeTags .HYDRATE ,
152+ )
153+ }
154+ } finally {
155+ // Clean up the mock
156+ unmockkObject(TimeUtils )
134157 }
135158 }
136159
@@ -159,7 +182,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
159182 val mockSubscriptionsModelStore = mockk<SubscriptionModelStore >()
160183 val mockBuildUserService = mockk<IRebuildUserService >()
161184
162- val loginUserOperationExecutor =
185+ val refreshUserOperationExecutor =
163186 RefreshUserOperationExecutor (
164187 mockUserBackendService,
165188 mockIdentityModelStore,
@@ -173,7 +196,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
173196 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
174197
175198 // When
176- val response = loginUserOperationExecutor .execute(operations)
199+ val response = refreshUserOperationExecutor .execute(operations)
177200
178201 // Then
179202 response.result shouldBe ExecutionResult .SUCCESS
@@ -198,7 +221,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
198221 val mockSubscriptionsModelStore = mockk<SubscriptionModelStore >()
199222 val mockBuildUserService = mockk<IRebuildUserService >()
200223
201- val loginUserOperationExecutor =
224+ val refreshUserOperationExecutor =
202225 RefreshUserOperationExecutor (
203226 mockUserBackendService,
204227 mockIdentityModelStore,
@@ -212,7 +235,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
212235 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
213236
214237 // When
215- val response = loginUserOperationExecutor .execute(operations)
238+ val response = refreshUserOperationExecutor .execute(operations)
216239
217240 // Then
218241 response.result shouldBe ExecutionResult .FAIL_RETRY
@@ -233,7 +256,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
233256 val mockSubscriptionsModelStore = mockk<SubscriptionModelStore >()
234257 val mockBuildUserService = mockk<IRebuildUserService >()
235258
236- val loginUserOperationExecutor =
259+ val refreshUserOperationExecutor =
237260 RefreshUserOperationExecutor (
238261 mockUserBackendService,
239262 mockIdentityModelStore,
@@ -247,7 +270,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
247270 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
248271
249272 // When
250- val response = loginUserOperationExecutor .execute(operations)
273+ val response = refreshUserOperationExecutor .execute(operations)
251274
252275 // Then
253276 response.result shouldBe ExecutionResult .FAIL_NORETRY
@@ -268,7 +291,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
268291 val mockBuildUserService = mockk<IRebuildUserService >()
269292 every { mockBuildUserService.getRebuildOperationsIfCurrentUser(any(), any()) } returns null
270293
271- val loginUserOperationExecutor =
294+ val refreshUserOperationExecutor =
272295 RefreshUserOperationExecutor (
273296 mockUserBackendService,
274297 mockIdentityModelStore,
@@ -282,7 +305,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
282305 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
283306
284307 // When
285- val response = loginUserOperationExecutor .execute(operations)
308+ val response = refreshUserOperationExecutor .execute(operations)
286309
287310 // Then
288311 response.result shouldBe ExecutionResult .FAIL_NORETRY
@@ -305,7 +328,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
305328 val mockConfigModelStore = MockHelper .configModelStore().also { it.model.opRepoPostCreateRetryUpTo = 1_000 }
306329 val newRecordState = getNewRecordState(mockConfigModelStore).also { it.add(remoteOneSignalId) }
307330
308- val loginUserOperationExecutor =
331+ val refreshUserOperationExecutor =
309332 RefreshUserOperationExecutor (
310333 mockUserBackendService,
311334 mockIdentityModelStore,
@@ -319,7 +342,7 @@ class RefreshUserOperationExecutorTests : FunSpec({
319342 val operations = listOf<Operation >(RefreshUserOperation (appId, remoteOneSignalId))
320343
321344 // When
322- val response = loginUserOperationExecutor .execute(operations)
345+ val response = refreshUserOperationExecutor .execute(operations)
323346
324347 // Then
325348 response.result shouldBe ExecutionResult .FAIL_RETRY
0 commit comments