1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-1235 Auth | Testing | Added test to make sure mnemonic is deleted on logout (#19)

This commit is contained in:
Evgenii Kozlov 2023-06-05 16:52:08 +02:00 committed by GitHub
parent 747e0312ad
commit 44d253b0ba
Signed by: github
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 0 deletions

View file

@ -29,4 +29,5 @@ dependencies {
testImplementation libs.mockitoKotlin
testImplementation libs.robolectric
testImplementation libs.archCoreTesting
testImplementation libs.coroutineTesting
}

View file

@ -0,0 +1,75 @@
package com.anytypeio.anytype.persistence
import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.room.Room
import androidx.test.platform.app.InstrumentationRegistry
import com.anytypeio.anytype.persistence.db.AnytypeDatabase
import com.anytypeio.anytype.persistence.repo.DefaultAuthCache
import com.anytypeio.anytype.test_utils.MockDataFactory
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class DefaultAuthCacheTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
private val database = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getInstrumentation().context,
AnytypeDatabase::class.java
).allowMainThreadQueries().build()
private val encryptedPrefs = RuntimeEnvironment.getApplication().getSharedPreferences(
"Encrypted prefs",
Context.MODE_PRIVATE
)
private val defaultPrefs = RuntimeEnvironment.getApplication().getSharedPreferences(
"Default prefs",
Context.MODE_PRIVATE
)
@After
fun after() {
database.close()
}
@Test
fun `should save mnemonic and clear mnemonic on logout`() = runTest {
val givenMnemonic = MockDataFactory.randomString()
val cache = DefaultAuthCache(
db = database,
encryptedPrefs = encryptedPrefs,
defaultPrefs = defaultPrefs
)
cache.saveMnemonic(mnemonic = givenMnemonic)
assertTrue { encryptedPrefs.contains(DefaultAuthCache.MNEMONIC_KEY) }
assertEquals(
expected = givenMnemonic,
actual = cache.getMnemonic()
)
cache.logout()
assertFalse { encryptedPrefs.contains(DefaultAuthCache.MNEMONIC_KEY) }
assertFalse { defaultPrefs.contains(DefaultAuthCache.MNEMONIC_KEY) }
}
}