Move AuthDataSourceImplTest
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
package gq.kirmanak.mealient.data.auth.impl
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.data.network.NetworkError.*
|
||||
import gq.kirmanak.mealient.data.network.ServiceFactory
|
||||
import gq.kirmanak.mealient.di.NetworkModule
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_PASSWORD
|
||||
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_TOKEN
|
||||
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_USERNAME
|
||||
import gq.kirmanak.mealient.test.toJsonResponseBody
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.impl.annotations.MockK
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import retrofit2.Response
|
||||
import java.io.IOException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class AuthDataSourceImplTest {
|
||||
@MockK
|
||||
lateinit var authService: AuthService
|
||||
|
||||
@MockK
|
||||
lateinit var authServiceFactory: ServiceFactory<AuthService>
|
||||
|
||||
@MockK(relaxUnitFun = true)
|
||||
lateinit var logger: Logger
|
||||
|
||||
lateinit var subject: AuthDataSourceImpl
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
subject = AuthDataSourceImpl(authServiceFactory, NetworkModule.createJson(), logger)
|
||||
coEvery { authServiceFactory.provideService(any()) } returns authService
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authentication is successful then token is correct`() = runTest {
|
||||
val token = authenticate(Response.success(GetTokenResponse(TEST_TOKEN)))
|
||||
assertThat(token).isEqualTo(TEST_TOKEN)
|
||||
}
|
||||
|
||||
@Test(expected = Unauthorized::class)
|
||||
fun `when authenticate receives 401 and Unauthorized then throws Unauthorized`() = runTest {
|
||||
val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody()
|
||||
authenticate(Response.error(401, body))
|
||||
}
|
||||
|
||||
@Test(expected = NotMealie::class)
|
||||
fun `when authenticate receives 401 but not Unauthorized then throws NotMealie`() = runTest {
|
||||
val body = "{\"detail\":\"Something\"}".toJsonResponseBody()
|
||||
authenticate(Response.error(401, body))
|
||||
}
|
||||
|
||||
@Test(expected = NotMealie::class)
|
||||
fun `when authenticate receives 404 and empty body then throws NotMealie`() = runTest {
|
||||
authenticate(Response.error(401, "".toJsonResponseBody()))
|
||||
}
|
||||
|
||||
@Test(expected = NotMealie::class)
|
||||
fun `when authenticate receives 200 and null then throws NotMealie`() = runTest {
|
||||
authenticate(Response.success<GetTokenResponse>(200, null))
|
||||
}
|
||||
|
||||
@Test(expected = NoServerConnection::class)
|
||||
fun `when authenticate and getToken throws then throws NoServerConnection`() = runTest {
|
||||
coEvery { authService.getToken(any(), any()) } throws IOException("Server not found")
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@Test(expected = MalformedUrl::class)
|
||||
fun `when authenticate and provideService throws then MalformedUrl`() = runTest {
|
||||
coEvery {
|
||||
authServiceFactory.provideService(any())
|
||||
} throws MalformedUrl(RuntimeException())
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
private suspend fun authenticate(response: Response<GetTokenResponse>): String {
|
||||
coEvery { authService.getToken(eq(TEST_USERNAME), eq(TEST_PASSWORD)) } returns response
|
||||
return callAuthenticate()
|
||||
}
|
||||
|
||||
private suspend fun callAuthenticate() = subject.authenticate(TEST_USERNAME, TEST_PASSWORD)
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.datasource.models.GetTokenResponse
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.models.VersionResponse
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
@@ -16,6 +17,7 @@ import org.junit.Before
|
||||
import org.junit.Test
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
import java.io.IOException
|
||||
import java.net.ConnectException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@@ -63,7 +65,53 @@ class MealieDataSourceImplTest {
|
||||
assertThat(subject.getVersionInfo(TEST_BASE_URL)).isSameInstanceAs(versionResponse)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authentication is successful then token is correct`() = runTest {
|
||||
coEvery { service.getToken(any(), any(), any()) } returns GetTokenResponse(TEST_TOKEN)
|
||||
assertThat(callAuthenticate()).isEqualTo(TEST_TOKEN)
|
||||
}
|
||||
|
||||
@Test(expected = NetworkError.Unauthorized::class)
|
||||
fun `when authenticate receives 401 and Unauthorized then throws Unauthorized`() = runTest {
|
||||
val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@Test(expected = HttpException::class)
|
||||
fun `when authenticate receives 401 but not Unauthorized then throws NotMealie`() = runTest {
|
||||
val body = "{\"detail\":\"Something\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@Test(expected = SerializationException::class)
|
||||
fun `when authenticate receives 404 and empty body then throws NotMealie`() = runTest {
|
||||
val body = "".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@Test(expected = IOException::class)
|
||||
fun `when authenticate and getToken throws then throws NoServerConnection`() = runTest {
|
||||
coEvery { service.getToken(any(), any(), any()) } throws IOException("Server not found")
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
private suspend fun callAuthenticate(): String =
|
||||
subject.authenticate(TEST_USERNAME, TEST_PASSWORD, TEST_BASE_URL)
|
||||
|
||||
companion object {
|
||||
private const val TEST_BASE_URL = ""
|
||||
const val TEST_USERNAME = "TEST_USERNAME"
|
||||
const val TEST_PASSWORD = "TEST_PASSWORD"
|
||||
const val TEST_BASE_URL = "https://example.com/"
|
||||
const val TEST_TOKEN = "TEST_TOKEN"
|
||||
const val TEST_AUTH_HEADER = "Bearer TEST_TOKEN"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user