Add From24AuthMigrationExecutorTest tests

This commit is contained in:
Kirill Kamakin
2022-12-11 18:33:45 +01:00
parent 7a5cbbb5a3
commit fd89dbad3b
4 changed files with 85 additions and 4 deletions

View File

@@ -8,9 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealient.data.auth.AuthStorage
import gq.kirmanak.mealient.data.auth.impl.AuthStorageImpl.Companion.AUTH_HEADER_KEY
import gq.kirmanak.mealient.logging.Logger
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_AUTH_HEADER
import gq.kirmanak.mealient.test.FakeLogger
import gq.kirmanak.mealient.test.HiltRobolectricTest
import io.mockk.MockKAnnotations
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -28,8 +26,6 @@ class AuthStorageImplTest : HiltRobolectricTest() {
@ApplicationContext
lateinit var context: Context
private val logger: Logger = FakeLogger()
lateinit var subject: AuthStorage
lateinit var sharedPreferences: SharedPreferences

View File

@@ -11,6 +11,7 @@ import javax.inject.Inject
@OptIn(ExperimentalCoroutinesApi::class)
@HiltAndroidTest
class DisclaimerStorageImplTest : HiltRobolectricTest() {
@Inject
lateinit var subject: DisclaimerStorageImpl

View File

@@ -0,0 +1,81 @@
package gq.kirmanak.mealient.data.migration
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealient.data.auth.AuthRepo
import gq.kirmanak.mealient.test.HiltRobolectricTest
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import java.io.IOException
import javax.inject.Inject
@OptIn(ExperimentalCoroutinesApi::class)
@HiltAndroidTest
class From24AuthMigrationExecutorTest : HiltRobolectricTest() {
@Inject
@ApplicationContext
lateinit var context: Context
@MockK(relaxUnitFun = true)
lateinit var authRepo: AuthRepo
private lateinit var subject: MigrationExecutor
private lateinit var sharedPreferences: SharedPreferences
@Before
fun setUp() {
MockKAnnotations.init(this)
sharedPreferences = context.getSharedPreferences("test", Context.MODE_PRIVATE)
subject = From24AuthMigrationExecutor(sharedPreferences, authRepo, logger)
}
@Test
fun `when there were email and password expect authentication`() = runTest {
sharedPreferences.edit(commit = true) {
putString("email", "email_value")
putString("password", "pass_value")
}
subject.executeMigration()
coVerify { authRepo.authenticate(eq("email_value"), eq("pass_value")) }
}
@Test
fun `when there were email and password expect them gone`() = runTest {
sharedPreferences.edit(commit = true) {
putString("email", "email_value")
putString("password", "pass_value")
}
subject.executeMigration()
assertThat(sharedPreferences.getString("email", null)).isNull()
assertThat(sharedPreferences.getString("password", null)).isNull()
}
@Test
fun `when there is email and password but authenticate fails expect values gone`() = runTest {
sharedPreferences.edit(commit = true) {
putString("email", "email_value")
putString("password", "pass_value")
}
coEvery { authRepo.authenticate(any(), any()) } throws IOException()
subject.executeMigration()
assertThat(sharedPreferences.getString("email", null)).isNull()
assertThat(sharedPreferences.getString("password", null)).isNull()
}
@Test
fun `when there was no email and password expect no authentication`() = runTest {
subject.executeMigration()
coVerify(inverse = true) { authRepo.authenticate(any(), any()) }
}
}

View File

@@ -3,6 +3,7 @@ package gq.kirmanak.mealient.test
import androidx.test.ext.junit.runners.AndroidJUnit4
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltTestApplication
import gq.kirmanak.mealient.logging.Logger
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
@@ -15,6 +16,8 @@ abstract class HiltRobolectricTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
protected val logger: Logger = FakeLogger()
@Before
fun inject() {
hiltRule.inject()