Implement observing authentication statuses

This commit is contained in:
Kirill Kamakin
2021-11-14 10:27:45 +03:00
parent 1a136b6ade
commit 698d93b351
9 changed files with 84 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_TOKEN
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_USERNAME
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.enqueueSuccessfulAuthResponse
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.enqueueUnsuccessfulAuthResponse
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Test
import retrofit2.HttpException
@@ -18,15 +19,15 @@ class AuthRepoImplTest : MockServerTest() {
lateinit var subject: AuthRepoImpl
@Test
fun `when not authenticated then isAuthenticated false`() = runBlocking {
assertThat(subject.isAuthenticated()).isFalse()
fun `when not authenticated then first auth status is false`() = runBlocking {
assertThat(subject.authenticationStatuses().first()).isFalse()
}
@Test
fun `when authenticated then isAuthenticated true`() = runBlocking {
fun `when authenticated then first auth status is true`() = runBlocking {
mockServer.enqueueSuccessfulAuthResponse()
subject.authenticate(TEST_USERNAME, TEST_PASSWORD, serverUrl)
assertThat(subject.isAuthenticated()).isTrue()
assertThat(subject.authenticationStatuses().first()).isTrue()
}
@Test(expected = HttpException::class)

View File

@@ -4,10 +4,13 @@ import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_TOKEN
import gq.kirmanak.mealie.data.auth.impl.AuthImplTestData.TEST_URL
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Test
import javax.inject.Inject
@ExperimentalCoroutinesApi
@HiltAndroidTest
class AuthStorageImplTest : HiltRobolectricTest() {
@Inject
@@ -39,4 +42,15 @@ class AuthStorageImplTest : HiltRobolectricTest() {
fun `when reading url without storing data then returns null`() = runBlocking {
assertThat(subject.getBaseUrl()).isNull()
}
@Test
fun `when didn't store auth data then first token is null`() = runBlocking {
assertThat(subject.tokenObservable().first()).isNull()
}
@Test
fun `when stored auth data then first token is correct`() = runBlocking {
subject.storeAuthData(TEST_TOKEN, TEST_URL)
assertThat(subject.tokenObservable().first()).isEqualTo(TEST_TOKEN)
}
}