@@ -8,10 +8,12 @@ package com.nextcloud.talk.users
88
99import com.nextcloud.talk.data.user.UsersRepository
1010import com.nextcloud.talk.data.user.model.User
11+ import io.reactivex.Maybe
1112import io.reactivex.Single
1213import org.junit.Assert.assertEquals
1314import org.junit.Assert.assertFalse
1415import org.junit.Assert.assertTrue
16+ import org.junit.Before
1517import org.junit.Test
1618import org.mockito.kotlin.mock
1719import org.mockito.kotlin.verify
@@ -25,6 +27,14 @@ class UserManagerTest {
2527 private fun user (id : Long , username : String , baseUrl : String , current : Boolean = false) =
2628 User (id = id, username = username, baseUrl = baseUrl, current = current)
2729
30+ @Before
31+ fun setUp () {
32+ // No row resolves as "the" active user unless a test overrides this, so
33+ // scheduleDuplicateAccountsForDeletion() falls back to the `current` flag / oldest row,
34+ // matching the behavior asserted by the tests below that don't care about this priority.
35+ whenever(usersRepository.getActiveUser()).thenReturn(Maybe .empty())
36+ }
37+
2838 @Test
2939 fun `keeps the current user among duplicates and schedules the rest for deletion` () {
3040 val current = user(id = 2 , username = " userA" , baseUrl = " https://example.com" , current = true )
@@ -135,4 +145,23 @@ class UserManagerTest {
135145
136146 assertEquals(0 , scheduledCount)
137147 }
148+
149+ @Test
150+ fun `keeps whichever row getActiveUser resolves to, even over a different row flagged current` () {
151+ // Simulates a past bug leaving two rows marked current=true for the same account: the
152+ // active-user lookup (deterministically) resolves to one of them, but the other still
153+ // carries the current flag too. The actively-resolved row must win, since it may be the
154+ // one a live session/background sync is still bound to.
155+ val staleCurrentFlag = user(id = 1 , username = " userA" , baseUrl = " https://example.com" , current = true )
156+ val actuallyActive = user(id = 2 , username = " userA" , baseUrl = " https://example.com" , current = true )
157+ whenever(usersRepository.getUsers()).thenReturn(Single .just(listOf (staleCurrentFlag, actuallyActive)))
158+ whenever(usersRepository.getActiveUser()).thenReturn(Maybe .just(actuallyActive))
159+
160+ val scheduledCount = userManager.scheduleDuplicateAccountsForDeletion().blockingGet()
161+
162+ assertEquals(1 , scheduledCount)
163+ assertTrue(staleCurrentFlag.scheduledForDeletion)
164+ assertFalse(actuallyActive.scheduledForDeletion)
165+ verify(usersRepository).updateUser(staleCurrentFlag)
166+ }
138167}
0 commit comments