app/src/androidTest/java/com/geekorum/ttrss/accounts/AndroidTinyrssAccountManagerTest.kt
author Da Risk <da_risk@geekorum.com>
Wed, 12 Jan 2022 17:29:59 -0400
changeset 882 7a74abf66c49
parent 846 ac0863af5ef6
child 943 298742859784
permissions -rw-r--r--
update license headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
     1
/*
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     2
 * Geekttrss is a RSS feed reader application on the Android Platform.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     3
 *
882
7a74abf66c49 update license headers
Da Risk <da_risk@geekorum.com>
parents: 846
diff changeset
     4
 * Copyright (C) 2017-2022 by Frederic-Charles Barthelery.
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     5
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     6
 * This file is part of Geekttrss.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     7
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     8
 * Geekttrss is free software: you can redistribute it and/or modify
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
     9
 * it under the terms of the GNU General Public License as published by
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    10
 * the Free Software Foundation, either version 3 of the License, or
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    11
 * (at your option) any later version.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    12
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    13
 * Geekttrss is distributed in the hope that it will be useful,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    16
 * GNU General Public License for more details.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    17
 *
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    19
 * along with Geekttrss.  If not, see <http://www.gnu.org/licenses/>.
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    20
 */
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    21
package com.geekorum.ttrss.accounts
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    22
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    23
import android.accounts.AccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    24
import android.content.ContentResolver
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    25
import android.content.PeriodicSync
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    26
import android.os.Bundle
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    27
import androidx.test.core.app.ApplicationProvider
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    28
import androidx.work.testing.WorkManagerTestInitHelper
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    29
import com.geekorum.geekdroid.security.SecretCipher
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    30
import com.geekorum.geekdroid.security.SecretEncryption
265
bc4d82c760f6 Move BackgroundJobManager into its own package
Da Risk <da_risk@geekorum.com>
parents: 137
diff changeset
    31
import com.geekorum.ttrss.background_job.BackgroundJobManager
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    32
import com.geekorum.ttrss.providers.ArticlesContract
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    33
import com.geekorum.ttrss.sync.SyncContract
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    34
import com.google.common.truth.Truth.assertThat
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    35
import dagger.hilt.android.testing.HiltAndroidRule
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    36
import dagger.hilt.android.testing.HiltAndroidTest
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    37
import kotlinx.coroutines.delay
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    38
import kotlinx.coroutines.runBlocking
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    39
import org.junit.After
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    40
import org.junit.Before
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    41
import org.junit.Rule
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    42
import org.junit.Test
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    43
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    44
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    45
@HiltAndroidTest
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    46
class AndroidTinyrssAccountManagerTest {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    47
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    48
    @get:Rule
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    49
    val hiltRule = HiltAndroidRule(this)
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    50
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    51
    lateinit var accountManager: AndroidTinyrssAccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    52
    lateinit var androidAccountManager: AccountManager
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    53
    lateinit var secretCipher: SecretCipher
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    54
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    55
    private val modelAccount = Account("test", "https://exemple.com")
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    56
    private val androidAccount = android.accounts.Account("test", AndroidTinyrssAccountManager.ACCOUNT_TYPE)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    57
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    58
    @Before
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    59
    fun setup() {
744
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    60
        // sync get triggered during test and try to access WorkManager from HiltTestApplication
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    61
        // provide a TestWorkManager
90fd7d86d034 app: make the rest of instrumented tests work with hilt
Da Risk <da_risk@geekorum.com>
parents: 611
diff changeset
    62
        WorkManagerTestInitHelper.initializeTestWorkManager(ApplicationProvider.getApplicationContext())
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    63
        androidAccountManager = AccountManager.get(ApplicationProvider.getApplicationContext())
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    64
        secretCipher = SecretEncryption().getSecretCipher("instrumented test")
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    65
        accountManager = AndroidTinyrssAccountManager(androidAccountManager, secretCipher)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    66
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    67
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    68
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    69
    @After
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    70
    fun tearDown() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    71
        androidAccountManager.removeAccountExplicitly(androidAccount)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    72
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    73
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    74
    @Test
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    75
    fun testThatAddAccountWorks() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    76
        val account = modelAccount
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    77
        val password = "password"
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    78
        val result = accountManager.addAccount(account, password)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    79
        assertThat(result).isTrue()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    80
        assertThat(androidAccountManager.getAccountsByType(AndroidTinyrssAccountManager.ACCOUNT_TYPE)).asList().contains(
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    81
            androidAccount)
80
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    82
        val retrievedPassword = accountManager.getPassword(account)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    83
        assertThat(retrievedPassword).isEqualTo(password)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    84
    }
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    85
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    86
    @Test
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    87
    fun testThatAccountWithLongPasswordWorks() {
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    88
        val account = modelAccount
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    89
        val password = "9a266fc8b42966fb624d852bafa241d8fd05b47d36153ff6684ab344bd1ae57bba96a7de8fc12ec0bb016583735d7f5bca6dd7d9bc6482c2a3ac6bf6f9ec323f"
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    90
        val result = accountManager.addAccount(account, password)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    91
        assertThat(result).isTrue()
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    92
        assertThat(androidAccountManager.getAccountsByType(AndroidTinyrssAccountManager.ACCOUNT_TYPE)).asList().contains(
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    93
            androidAccount)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    94
        val retrievedPassword = accountManager.getPassword(account)
a4353ceef1e7 Fix bug with long password.
Da Risk <da_risk@geekorum.com>
parents: 0
diff changeset
    95
        assertThat(retrievedPassword).isEqualTo(password)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    96
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    97
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    98
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
    99
    @Test
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   100
    fun testThatInitializeAccountSyncWorks() {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   101
        val urlBundle = Bundle().apply {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   102
            putString(AccountAuthenticator.USERDATA_URL, "https://exemple.com")
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   103
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   104
        androidAccountManager.addAccountExplicitly(androidAccount, "password", urlBundle)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   105
        val periodicRefreshSync = PeriodicSync(androidAccount, ArticlesContract.AUTHORITY, Bundle.EMPTY,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   106
            BackgroundJobManager.PERIODIC_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   107
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   108
        val fullExtra = Bundle().apply {
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   109
            putInt(SyncContract.EXTRA_NUMBER_OF_LATEST_ARTICLES_TO_REFRESH, -1)
501
caf68bb56590 AndroidTinyrssAccountManagerTest: fix test for sync initialization
Da Risk <da_risk@geekorum.com>
parents: 265
diff changeset
   110
            putBoolean(SyncContract.EXTRA_UPDATE_FEED_ICONS, true)
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   111
        }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   112
        val periodicFullRefreshSync = PeriodicSync(androidAccount, ArticlesContract.AUTHORITY, fullExtra,
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   113
            BackgroundJobManager.PERIODIC_FULL_REFRESH_JOB_INTERVAL_S)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   114
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   115
        accountManager.initializeAccountSync(modelAccount)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   116
        // the system needs some time to initialize
783
e0df91e957c8 Fix instrumentation tests
Da Risk <da_risk@geekorum.com>
parents: 744
diff changeset
   117
        runBlocking{ delay(1000) }
0
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   118
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   119
        val syncAutomatically = ContentResolver.getSyncAutomatically(androidAccount, ArticlesContract.AUTHORITY)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   120
        assertThat(syncAutomatically).isTrue()
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   121
        val periodicSyncs = ContentResolver.getPeriodicSyncs(androidAccount, ArticlesContract.AUTHORITY)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   122
        assertThat(periodicSyncs).hasSize(2)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   123
        assertThat(periodicSyncs).containsExactly(periodicFullRefreshSync, periodicRefreshSync)
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   124
    }
14443efede32 Initial commit
Da Risk <da_risk@geekorum.com>
parents:
diff changeset
   125
}