Skip to content

Commit 310c020

Browse files
authored
fix user info not persists when reset (#179)
* fix reset not persisted in disk * add unit tests * update sample apps permission
1 parent 980ae87 commit 310c020

File tree

7 files changed

+77
-13
lines changed

7 files changed

+77
-13
lines changed

android/src/test/java/com/segment/analytics/kotlin/android/StorageTests.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ class StorageTests {
9595
assertEquals("{}", traits)
9696
}
9797

98+
@Test
99+
fun `userInfo reset action removes userInfo`() = runTest {
100+
store.dispatch(UserInfo.ResetAction(), UserInfo::class)
101+
102+
val userId = androidStorage.read(Storage.Constants.UserId)
103+
val anonId = androidStorage.read(Storage.Constants.AnonymousId)
104+
val traits = androidStorage.read(Storage.Constants.Traits)
105+
106+
assertNotNull(anonId)
107+
assertEquals(null, userId)
108+
assertEquals(null, traits)
109+
}
110+
98111
@Test
99112
fun `system update calls write for settings`() = runTest {
100113
val action = object : Action<System> {
@@ -137,6 +150,20 @@ class StorageTests {
137150
)
138151
}
139152

153+
@Test
154+
fun `system reset action removes system`() = runTest {
155+
val action = object : Action<System> {
156+
override fun reduce(state: System): System {
157+
return System(state.configuration, null, state.running, state.initialSettingsDispatched, state.enabled)
158+
}
159+
}
160+
store.dispatch(action, System::class)
161+
162+
val settings = androidStorage.read(Storage.Constants.Settings)
163+
164+
assertEquals(null, settings)
165+
}
166+
140167
@Nested
141168
inner class KeyValueStorage {
142169
val map = getWorkingMap(mockContext.getSharedPreferences("", 0))

core/src/main/java/com/segment/analytics/kotlin/core/Storage.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,25 @@ interface Storage {
5757

5858
suspend fun userInfoUpdate(userInfo: UserInfo) {
5959
write(Constants.AnonymousId, userInfo.anonymousId)
60-
userInfo.userId?.let { write(Constants.UserId, it) }
60+
61+
userInfo.userId?.let {
62+
write(Constants.UserId, it)
63+
} ?: run {
64+
remove(Constants.UserId)
65+
}
66+
6167
userInfo.traits?.let {
62-
write(
63-
Constants.Traits,
64-
Json.encodeToString(JsonObject.serializer(), it)
65-
)
68+
write(Constants.Traits, Json.encodeToString(JsonObject.serializer(), it))
69+
} ?: run {
70+
remove(Constants.Traits)
6671
}
6772
}
6873

6974
suspend fun systemUpdate(system: System) {
7075
system.settings?.let {
71-
write(
72-
Constants.Settings,
73-
Json.encodeToString(Settings.serializer(), it)
74-
)
76+
write(Constants.Settings, Json.encodeToString(Settings.serializer(), it))
77+
} ?: run {
78+
remove(Constants.Settings)
7579
}
7680
}
7781
}

core/src/test/kotlin/com/segment/analytics/kotlin/core/AnalyticsTests.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,14 @@ class AnalyticsTests {
704704

705705
analytics.identify("oldUserId",
706706
buildJsonObject { put("behaviour", "bad") })
707-
assertEquals(analytics.userIdAsync(), "oldUserId")
708-
assertEquals(analytics.traitsAsync(), buildJsonObject { put("behaviour", "bad") })
707+
assertEquals(analytics.userId(), "oldUserId")
708+
assertEquals(analytics.traits(), buildJsonObject { put("behaviour", "bad") })
709709

710710
analytics.reset()
711-
assertEquals(analytics.userIdAsync(), null)
712-
assertEquals(analytics.traitsAsync(), null)
711+
assertEquals(analytics.userId(), null)
712+
assertEquals(analytics.traits(), null)
713+
assertEquals(analytics.storage.read(Storage.Constants.UserId), null)
714+
assertEquals(analytics.storage.read(Storage.Constants.Traits), null)
713715
verify { plugin.reset() }
714716
}
715717
}

core/src/test/kotlin/com/segment/analytics/kotlin/core/compat/JavaAnalyticsTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ internal class JavaAnalyticsTest {
528528
analytics.reset()
529529
assertEquals(analytics.userId(), null)
530530
assertEquals(analytics.traits(), null)
531+
assertEquals(analytics.storage.read(Storage.Constants.UserId), null)
532+
assertEquals(analytics.storage.read(Storage.Constants.Traits), null)
531533
verify { plugin.reset() }
532534
}
533535
}

core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/StorageImplTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ internal class StorageImplTest {
9191
assertEquals("{}", traits)
9292
}
9393

94+
@Test
95+
fun `userInfo reset action removes userInfo`() = runTest {
96+
store.dispatch(UserInfo.ResetAction(), UserInfo::class)
97+
98+
val userId = storage.read(Storage.Constants.UserId)
99+
val anonId = storage.read(Storage.Constants.AnonymousId)
100+
val traits = storage.read(Storage.Constants.Traits)
101+
102+
assertNotNull(anonId)
103+
assertEquals(null, userId)
104+
assertEquals(null, traits)
105+
}
106+
94107
@Test
95108
fun `system update calls write for settings`() = runTest {
96109
val action = object : Action<System> {
@@ -135,6 +148,20 @@ internal class StorageImplTest {
135148
)
136149
}
137150

151+
@Test
152+
fun `system reset action removes system`() = runTest {
153+
val action = object : Action<System> {
154+
override fun reduce(state: System): System {
155+
return System(state.configuration, null, state.running, state.initialSettingsDispatched, state.enabled)
156+
}
157+
}
158+
store.dispatch(action, System::class)
159+
160+
val settings = storage.read(Storage.Constants.Settings)
161+
162+
assertEquals(null, settings)
163+
}
164+
138165
@Nested
139166
inner class EventsStorage() {
140167

samples/kotlin-android-app-destinations/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- Allows location to be tracked. -->
1414
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
1515
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
16+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1617

1718
<application
1819
android:name=".MainApplication"

samples/kotlin-android-app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- Allows location to be tracked. -->
1414
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
1515
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
16+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1617

1718
<application
1819
android:name=".MainApplication"

0 commit comments

Comments
 (0)