aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/androidTest/java/com/zeapo/pwdstore/MigrationsTest.kt
blob: c66bb2156139a42ff948a0f1a616f1a485c30513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

@file:Suppress("DEPRECATION")
package com.zeapo.pwdstore

import android.content.Context
import androidx.core.content.edit
import com.zeapo.pwdstore.git.config.AuthMode
import com.zeapo.pwdstore.git.config.Protocol
import com.zeapo.pwdstore.utils.PreferenceKeys
import com.zeapo.pwdstore.utils.getString
import com.zeapo.pwdstore.utils.sharedPrefs
import org.junit.Test

import org.junit.Assert.*

class MigrationsTest {

    private fun checkOldKeysAreRemoved(context: Context) = with(context.sharedPrefs) {
        assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT))
        assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME))
        assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER))
        assertNull(getString(PreferenceKeys.GIT_REMOTE_LOCATION))
        assertNull(getString(PreferenceKeys.GIT_REMOTE_PROTOCOL))
    }

    @Test
    fun verifySshWithCustomPortMigration() {
        val context = Application.instance.applicationContext
        context.sharedPrefs.edit {
            clear()
            putString(PreferenceKeys.GIT_REMOTE_PORT, "2200")
            putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
            putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo")
            putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102")
            putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref)
            putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref)
        }
        runMigrations(context)
        checkOldKeysAreRemoved(context)
        assertEquals(
            context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
            "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo"
        )
    }

    @Test
    fun verifySshWithDefaultPortMigration() {
        val context = Application.instance.applicationContext
        context.sharedPrefs.edit {
            clear()
            putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
            putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo")
            putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102")
            putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref)
            putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref)
        }
        runMigrations(context)
        checkOldKeysAreRemoved(context)
        assertEquals(
            context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
            "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo"
        )
    }

    @Test
    fun verifyHttpsWithGitHubMigration() {
        val context = Application.instance.applicationContext
        context.sharedPrefs.edit {
            clear()
            putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis")
            putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test")
            putString(PreferenceKeys.GIT_REMOTE_SERVER, "github.com")
            putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref)
            putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref)
        }
        runMigrations(context)
        checkOldKeysAreRemoved(context)
        assertEquals(
            context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL),
            "https://github.com/Android-Password-Store/pass-test"
        )
    }
}