Prevent crash when logging in #81

Merged
AmineB merged 3 commits from davidoskky/ReaderForSelfoss-multiplatform:login_crash into master 2022-10-22 19:54:01 +00:00
3 changed files with 64 additions and 74 deletions

View File

@ -93,6 +93,9 @@ class LoginActivity : AppCompatActivity(), DIAware {
}
private fun goToMain() {
CoroutineScope(Dispatchers.Main).launch {
repository.updateApiVersion()
}
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()

View File

@ -41,13 +41,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
private var fetchedTags = false
init {
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
connectivityStatus.start()
runBlocking {
updateApiVersion()
dateUtils = DateUtils(appSettingsService)
reloadBadges()
}
}
AmineB marked this conversation as resolved Outdated

getBaseUrl should never return an empty string.

There is a verification done before even logging-in (and saving the settings), so this should never happen.

`getBaseUrl` should never return an empty string. There is a verification done before even logging-in (and saving the settings), so this should never happen.

The repository is initialized before logging in, at that point appSettingsService returns an empty string

The repository is initialized before logging in, at that point appSettingsService returns an empty string

Ok, so this is a workaround.

The issue is that the runBlocking block is called even when the user is'nt updated. This should be fixed.

Ok, so this is a workaround. The issue is that the `runBlocking` block is called even when the user is'nt updated. This should be fixed.

I could remove this from the init method and make a new public method which is called by the login activity after it checks that the user is correctly set.

I could remove this from the init method and make a new public method which is called by the login activity after it checks that the user is correctly set.

that could be better, yes !

that could be better, yes !
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
@ -383,9 +377,6 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
try {
val response = api.login()
result = response.isSuccess == true
if (result) {
updateApiVersion()
}
} catch (cause: Throwable) {
Napier.e(cause.stackTraceToString(), tag = "RepositoryImpl.updateRemote")
}
@ -399,7 +390,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
api.refreshLoginInformation()
}
private suspend fun updateApiVersion() {
suspend fun updateApiVersion() {
val apiMajorVersion = appSettingsService.getApiVersion()
if (isNetworkAvailable()) {
@ -408,6 +399,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
appSettingsService.updateApiVersion(fetchedVersion.data.getApiMajorVersion())
}
}
dateUtils = DateUtils(appSettingsService)
}
fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride

View File

@ -24,6 +24,7 @@ class RepositoryTest() {
private val NUMBER_ARTICLES = 100
private val NUMBER_UNREAD = 50
private val NUMBER_STARRED = 20
private lateinit var repository: Repository
@BeforeTest
fun setup() {
@ -48,10 +49,9 @@ class RepositoryTest() {
@Test
fun `Instantiate repository`() {
Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
coVerify(exactly = 1) { api.version() }
coVerify(exactly = 1) { api.stats() }
}
@Test
@ -59,25 +59,12 @@ class RepositoryTest() {
every { appSettingsService.getApiVersion() } returns -1
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
coVerify(exactly = 0) { api.version() }
coVerify(exactly = 0) { api.stats() }
}
@Test
fun `Instantiate repository with negative stats`() {
coEvery { api.stats() } returns SelfossModel.StatusAndData(success = true, data = SelfossModel.Stats(-100, -50, -20))
val repository = Repository(api, appSettingsService, connectivityStatus, db)
assertEquals(0, repository.badgeAll)
assertEquals(0, repository.badgeStarred)
assertEquals(0, repository.badgeUnread)
coVerify(exactly = 1) { api.version() }
coVerify(exactly = 1) { api.stats() }
}
@Test
fun `Get api 4 date with api 1 version stored`() {
every { appSettingsService.getApiVersion() } returns 1
@ -85,7 +72,7 @@ class RepositoryTest() {
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
every { appSettingsService.updateApiVersion(any()) } returns Unit
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
repository.getNewerItems()
}
@ -103,7 +90,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem(itemParameters))
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
repository.getNewerItems()
}
@ -116,7 +103,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
repository.getNewerItems()
}
@ -131,7 +118,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.displayedItems = ItemType.ALL
runBlocking {
repository.getNewerItems()
@ -147,7 +134,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.displayedItems = ItemType.STARRED
runBlocking {
repository.getNewerItems()
@ -163,7 +150,7 @@ class RepositoryTest() {
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
repository.getNewerItems()
}
@ -189,7 +176,7 @@ class RepositoryTest() {
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.tagFilter = SelfossModel.Tag("Test", "red", 3)
runBlocking {
repository.getNewerItems()
@ -216,7 +203,7 @@ class RepositoryTest() {
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.sourceFilter = SelfossModel.Source(1, "Test", listOf("tags"),"spouts\\rss\\fulltextrss", "", "b3aa8a664d08eb15d6ff1db2fa83e0d9.png")
runBlocking {
repository.getNewerItems()
@ -232,7 +219,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.items = ArrayList(generateTestApiItem())
runBlocking {
repository.getOlderItems()
@ -248,7 +235,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.items = ArrayList(generateTestApiItem())
repository.displayedItems = ItemType.ALL
runBlocking {
@ -265,7 +252,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = true, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.displayedItems = ItemType.STARRED
repository.items = ArrayList(generateTestApiItem())
runBlocking {
@ -281,7 +268,7 @@ class RepositoryTest() {
fun `Reload badges`() {
var success = false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
success = repository.reloadBadges()
}
@ -300,7 +287,7 @@ class RepositoryTest() {
var success = false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
success = repository.reloadBadges()
}
@ -321,7 +308,7 @@ class RepositoryTest() {
var success = false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
success = repository.reloadBadges()
}
@ -342,7 +329,7 @@ class RepositoryTest() {
var success = false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
runBlocking {
success = repository.reloadBadges()
}
@ -367,7 +354,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns true
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag>? = null
runBlocking {
testTags = repository.getTags()
@ -390,7 +377,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -416,7 +403,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns true
every { appSettingsService.isItemCachingEnabled() } returns false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -439,7 +426,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
every { appSettingsService.isItemCachingEnabled() } returns false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -465,7 +452,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns true
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -490,7 +477,7 @@ class RepositoryTest() {
every { appSettingsService.isItemCachingEnabled() } returns false
every { appSettingsService.isUpdateSourcesEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -514,7 +501,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
every { appSettingsService.isItemCachingEnabled() } returns true
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -539,7 +526,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
every { appSettingsService.isItemCachingEnabled() } returns false
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testTags: List<SelfossModel.Tag> = emptyList()
runBlocking {
testTags = repository.getTags()
@ -559,7 +546,7 @@ class RepositoryTest() {
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -581,7 +568,7 @@ class RepositoryTest() {
every { appSettingsService.isItemCachingEnabled() } returns true
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -606,7 +593,7 @@ class RepositoryTest() {
every { appSettingsService.isItemCachingEnabled() } returns false
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -628,7 +615,7 @@ class RepositoryTest() {
every { appSettingsService.isItemCachingEnabled() } returns false
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -649,7 +636,7 @@ class RepositoryTest() {
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -672,7 +659,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns true
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -695,7 +682,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -718,7 +705,7 @@ class RepositoryTest() {
every { appSettingsService.isUpdateSourcesEnabled() } returns false
coEvery { api.sources() } returns SelfossModel.StatusAndData(success = true, data = sources)
every { db.sourcesQueries.sources().executeAsList() } returns sourcesDB
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var testSources: List<SelfossModel.Source>? = null
runBlocking {
testSources = repository.getSources()
@ -734,7 +721,7 @@ class RepositoryTest() {
coEvery { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) } returns
SelfossModel.SuccessResponse(true)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
@ -749,7 +736,7 @@ class RepositoryTest() {
coEvery { api.createSourceForVersion(any(), any(), any(), any(), any(), any()) } returns
SelfossModel.SuccessResponse(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
@ -765,7 +752,7 @@ class RepositoryTest() {
SelfossModel.SuccessResponse(true)
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.createSource("test", "https://test.com/feed", "spouts\\rss\\fulltextrss", "Test, New", "")
@ -779,7 +766,7 @@ class RepositoryTest() {
fun `delete source`() {
coEvery { api.deleteSource(any())} returns SelfossModel.SuccessResponse(true)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.deleteSource(5)
@ -793,7 +780,7 @@ class RepositoryTest() {
fun `delete source but response fails`() {
coEvery { api.deleteSource(any())} returns SelfossModel.SuccessResponse(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.deleteSource(5)
@ -808,7 +795,7 @@ class RepositoryTest() {
coEvery { api.deleteSource(any())} returns SelfossModel.SuccessResponse(false)
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.deleteSource(5)
@ -822,7 +809,7 @@ class RepositoryTest() {
fun `update remote`() {
coEvery { api.update()} returns SelfossModel.StatusAndData(success = true, data = "finished")
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.updateRemote()
@ -836,7 +823,7 @@ class RepositoryTest() {
fun `update remote but response fails`() {
coEvery { api.update()} returns SelfossModel.StatusAndData(success = false, data = "unallowed access")
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.updateRemote()
@ -850,7 +837,7 @@ class RepositoryTest() {
fun `update remote with unallowed access`() {
coEvery { api.update()} returns SelfossModel.StatusAndData(success = true, data = "unallowed access")
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.updateRemote()
@ -865,7 +852,7 @@ class RepositoryTest() {
coEvery { api.update()} returns SelfossModel.StatusAndData(success = true, data = "undocumented...")
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.updateRemote()
@ -879,7 +866,7 @@ class RepositoryTest() {
fun login() {
coEvery { api.login() } returns SelfossModel.SuccessResponse(success = true)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.login()
@ -893,7 +880,7 @@ class RepositoryTest() {
fun `login but response fails`() {
coEvery { api.login() } returns SelfossModel.SuccessResponse(success = false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.login()
@ -908,7 +895,7 @@ class RepositoryTest() {
coEvery { api.login() } returns SelfossModel.SuccessResponse(success = true)
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
var response = false
runBlocking {
response = repository.login()
@ -923,7 +910,7 @@ class RepositoryTest() {
coEvery { api.refreshLoginInformation() } returns Unit
coEvery { appSettingsService.refreshLoginInformation(any(), any(), any()) } returns Unit
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.refreshLoginInformation("https://test.com/selfoss/", "login", "password")
coVerify(exactly = 1) { api.refreshLoginInformation() }
@ -943,7 +930,7 @@ class RepositoryTest() {
SelfossModel.StatusAndData(success = true, data = generateTestApiItem(itemParameter1)),
)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.tagFilter = SelfossModel.Tag("Tag", "read", 0)
repository.sourceFilter = SelfossModel.Source(
1,
@ -966,7 +953,7 @@ class RepositoryTest() {
coEvery { api.getItems(any(), any(), any(), any(), any(), any(), any()) } returns
SelfossModel.StatusAndData(success = false, data = generateTestApiItem())
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.tagFilter = SelfossModel.Tag("Tag", "read", 0)
repository.sourceFilter = SelfossModel.Source(
1,
@ -990,7 +977,7 @@ class RepositoryTest() {
SelfossModel.StatusAndData(success = false, data = generateTestApiItem())
every { connectivityStatus.isNetworkConnected } returns MutableStateFlow(false)
val repository = Repository(api, appSettingsService, connectivityStatus, db)
initializeRepository()
repository.tagFilter = SelfossModel.Tag("Tag", "read", 0)
repository.sourceFilter = SelfossModel.Source(
1,
@ -1007,6 +994,14 @@ class RepositoryTest() {
coVerify(exactly = 0) { api.getItems(any(), 0, null, null, null, null, 200) }
}
fun initializeRepository() {
repository = Repository(api, appSettingsService, connectivityStatus, db)
runBlocking {
repository.updateApiVersion()
}
}
}
fun generateTestDBItems(item : FakeItemParameters = FakeItemParameters()) : List<ITEM> {