test: update MigrationsTest to run on JVM

Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
This commit is contained in:
Aditya Wasan 2021-05-22 17:26:31 +05:30 committed by Harsh Shandilya
parent c3f8de99be
commit d45bb753cf
No known key found for this signature in database
GPG Key ID: 366D7BBAD1031E80

View File

@ -2,12 +2,12 @@
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only * SPDX-License-Identifier: GPL-3.0-only
*/ */
@file:Suppress("DEPRECATION") @file:Suppress("DEPRECATION")
package dev.msfjarvis.aps.util.settings package dev.msfjarvis.aps.util.settings
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit import androidx.core.content.edit
import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder
import dev.msfjarvis.aps.util.extensions.getString import dev.msfjarvis.aps.util.extensions.getString
@ -15,12 +15,32 @@ import dev.msfjarvis.aps.util.extensions.sharedPrefs
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.TemporaryFolder
class MigrationsTest { class MigrationsTest {
private fun checkOldKeysAreRemoved(context: Context) = @get:Rule val tempFolder = TemporaryFolder()
with(context.sharedPrefs) {
private lateinit var context: Context
private lateinit var filesDir: String
private lateinit var sharedPrefs: SharedPreferences
private lateinit var encryptedSharedPreferences: SharedPreferences
private lateinit var proxySharedPreferences: SharedPreferences
@Before
fun setup() {
context = SPMockBuilder().createContext()
filesDir = tempFolder.root.path
sharedPrefs = SPMockBuilder().createSharedPreferences()
encryptedSharedPreferences = SPMockBuilder().createSharedPreferences()
proxySharedPreferences = SPMockBuilder().createSharedPreferences()
}
private fun checkOldKeysAreRemoved() =
with(sharedPrefs) {
assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT)) assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT))
assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME)) assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME))
assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER)) assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER))
@ -30,8 +50,7 @@ class MigrationsTest {
@Test @Test
fun verifySshWithCustomPortMigration() { fun verifySshWithCustomPortMigration() {
val context = SPMockBuilder().createContext() sharedPrefs.edit {
context.sharedPrefs.edit {
clear() clear()
putString(PreferenceKeys.GIT_REMOTE_PORT, "2200") putString(PreferenceKeys.GIT_REMOTE_PORT, "2200")
putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
@ -40,18 +59,17 @@ class MigrationsTest {
putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref)
putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref) putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref)
} }
runMigrations(context) runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
checkOldKeysAreRemoved(context) checkOldKeysAreRemoved()
assertEquals( assertEquals(
context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
"ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo" "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo"
) )
} }
@Test @Test
fun verifySshWithDefaultPortMigration() { fun verifySshWithDefaultPortMigration() {
val context = SPMockBuilder().createContext() sharedPrefs.edit {
context.sharedPrefs.edit {
clear() clear()
putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo")
@ -59,18 +77,17 @@ class MigrationsTest {
putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref)
putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref) putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref)
} }
runMigrations(context) runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
checkOldKeysAreRemoved(context) checkOldKeysAreRemoved(context)
assertEquals( assertEquals(
context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
"msfjarvis@192.168.0.102:/mnt/disk3/pass-repo" "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo"
) )
} }
@Test @Test
fun verifyHttpsWithGitHubMigration() { fun verifyHttpsWithGitHubMigration() {
val context = SPMockBuilder().createContext() sharedPrefs.edit {
context.sharedPrefs.edit {
clear() clear()
putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test") putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test")
@ -78,47 +95,44 @@ class MigrationsTest {
putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref) putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref)
putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref) putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref)
} }
runMigrations(context) runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
checkOldKeysAreRemoved(context) checkOldKeysAreRemoved(context)
assertEquals( assertEquals(
context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
"https://github.com/Android-Password-Store/pass-test" "https://github.com/Android-Password-Store/pass-test"
) )
} }
@Test @Test
fun verifyHiddenFoldersMigrationIfDisabled() { fun verifyHiddenFoldersMigrationIfDisabled() {
val context = SPMockBuilder().createContext() sharedPrefs.edit { clear() }
context.sharedPrefs.edit { clear() } runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
runMigrations(context) assertEquals(true, sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true))
assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)) assertEquals(false, sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false))
assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false))
} }
@Test @Test
fun verifyHiddenFoldersMigrationIfEnabled() { fun verifyHiddenFoldersMigrationIfEnabled() {
val context = SPMockBuilder().createContext() sharedPrefs.edit {
context.sharedPrefs.edit {
clear() clear()
putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true) putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)
} }
runMigrations(context) runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)) assertEquals(false, sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false))
assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) assertEquals(true, sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false))
} }
@Test @Test
fun verifyClearClipboardHistoryMigration() { fun verifyClearClipboardHistoryMigration() {
val context = SPMockBuilder().createContext() sharedPrefs.edit {
context.sharedPrefs.edit {
clear() clear()
putBoolean(PreferenceKeys.CLEAR_CLIPBOARD_20X, true) putBoolean(PreferenceKeys.CLEAR_CLIPBOARD_20X, true)
} }
runMigrations(context) runMigrations(filesDir, sharedPrefs, GitSettings(sharedPrefs, encryptedSharedPreferences, proxySharedPreferences, filesDir))
assertEquals( assertEquals(
true, true,
context.sharedPrefs.getBoolean(PreferenceKeys.CLEAR_CLIPBOARD_HISTORY, false) sharedPrefs.getBoolean(PreferenceKeys.CLEAR_CLIPBOARD_HISTORY, false)
) )
assertFalse(context.sharedPrefs.contains(PreferenceKeys.CLEAR_CLIPBOARD_20X)) assertFalse(sharedPrefs.contains(PreferenceKeys.CLEAR_CLIPBOARD_20X))
} }
} }