This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlinx Kover test coverage calculator (#199)
* Add Kotlin Kover * Add AuthKtorConfiguration tests * Ensure at least 25% code coverage * Exclude Previews from code coverage * Specify Kover report path for SonarQube * Add Kover xml report task * Extract sonar to a separate step * Add some exclusions and minimum coverage * Exclude Hilt-generated classes * Add shopping list view model tests * Reduce the coverage requirement
- Loading branch information
Showing
12 changed files
with
425 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
datasource/src/test/kotlin/gq/kirmanak/mealient/datasource/AuthKtorConfigurationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package gq.kirmanak.mealient.datasource | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import gq.kirmanak.mealient.datasource.ktor.AuthKtorConfiguration | ||
import gq.kirmanak.mealient.test.BaseUnitTest | ||
import io.ktor.client.plugins.auth.providers.BearerTokens | ||
import io.ktor.client.plugins.auth.providers.RefreshTokensParams | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.http.HttpStatusCode | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
private const val AUTH_TOKEN = "token" | ||
|
||
internal class AuthKtorConfigurationTest : BaseUnitTest() { | ||
|
||
@MockK(relaxUnitFun = true) | ||
lateinit var authenticationProvider: AuthenticationProvider | ||
|
||
private lateinit var subject: AuthKtorConfiguration | ||
|
||
@Before | ||
override fun setUp() { | ||
super.setUp() | ||
coEvery { authenticationProvider.getAuthToken() } returns AUTH_TOKEN | ||
subject = AuthKtorConfiguration(FakeProvider(authenticationProvider), logger) | ||
} | ||
|
||
@Test | ||
fun `getTokens returns BearerTokens with auth token`() = runTest { | ||
val bearerTokens = subject.getTokens() | ||
assertThat(bearerTokens?.accessToken).isEqualTo(AUTH_TOKEN) | ||
} | ||
|
||
@Test | ||
fun `getTokens returns BearerTokens without refresh token`() = runTest { | ||
val bearerTokens = subject.getTokens() | ||
assertThat(bearerTokens?.refreshToken).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `refreshTokens returns new auth token if it doesn't match old`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.Unauthorized, "old token") | ||
val actual = with(subject) { refreshTokensParams.refreshTokens() } | ||
assertThat(actual?.accessToken).isEqualTo(AUTH_TOKEN) | ||
} | ||
|
||
@Test | ||
fun `refreshTokens returns empty refresh token if auth token doesn't match old`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.Unauthorized, "old token") | ||
val actual = with(subject) { refreshTokensParams.refreshTokens() } | ||
assertThat(actual?.refreshToken).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `refreshTokens returns null if auth token matches old`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.Unauthorized, AUTH_TOKEN) | ||
val actual = with(subject) { refreshTokensParams.refreshTokens() } | ||
assertThat(actual).isNull() | ||
} | ||
|
||
@Test | ||
fun `refreshTokens calls logout if auth token matches old`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.Unauthorized, AUTH_TOKEN) | ||
with(subject) { refreshTokensParams.refreshTokens() } | ||
coVerify { authenticationProvider.logout() } | ||
} | ||
|
||
@Test | ||
fun `refreshTokens does not logout if status code is not found`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.NotFound, AUTH_TOKEN) | ||
with(subject) { refreshTokensParams.refreshTokens() } | ||
coVerify(inverse = true) { authenticationProvider.logout() } | ||
} | ||
|
||
@Test | ||
fun `refreshTokens returns same access token if status code is not found`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.NotFound, AUTH_TOKEN) | ||
val actual = with(subject) { refreshTokensParams.refreshTokens() } | ||
assertThat(actual?.accessToken).isEqualTo(AUTH_TOKEN) | ||
} | ||
|
||
@Test | ||
fun `refreshTokens returns empty refresh token if status code is not found`() = runTest { | ||
val refreshTokensParams = mockRefreshTokenParams(HttpStatusCode.NotFound, AUTH_TOKEN) | ||
val actual = with(subject) { refreshTokensParams.refreshTokens() } | ||
assertThat(actual?.refreshToken).isEmpty() | ||
} | ||
|
||
private fun mockRefreshTokenParams( | ||
responseStatusCode: HttpStatusCode, | ||
oldAccessToken: String, | ||
): RefreshTokensParams { | ||
val notFoundResponse = mockk<HttpResponse> { | ||
every { status } returns responseStatusCode | ||
} | ||
val refreshTokensParams = mockk<RefreshTokensParams> { | ||
every { response } returns notFoundResponse | ||
every { oldTokens } returns BearerTokens(oldAccessToken, "") | ||
} | ||
return refreshTokensParams | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
datasource/src/test/kotlin/gq/kirmanak/mealient/datasource/FakeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package gq.kirmanak.mealient.datasource | ||
|
||
import javax.inject.Provider | ||
|
||
data class FakeProvider<T>( | ||
val value: T, | ||
) : Provider<T> { | ||
|
||
override fun get(): T = value | ||
} |
Oops, something went wrong.