test: Add initial test for token

This commit is contained in:
oSumAtrIX 2024-01-31 11:58:52 +01:00
parent 1dba4923c8
commit 673efc8038
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
4 changed files with 55 additions and 16 deletions

View File

@ -53,6 +53,7 @@ dependencies {
implementation(libs.picocli)
implementation(libs.kotlinx.datetime)
testImplementation(libs.mockk)
testImplementation(libs.ktor.server.tests)
testImplementation(libs.kotlin.test.junit)
}

View File

@ -9,6 +9,7 @@ ktor = "2.3.7"
ktoml = "0.5.1"
picocli = "4.7.3"
datetime = "0.5.0"
mockk = "1.13.9"
[libraries]
ktor-client-core = { module = "io.ktor:ktor-client-core" }
@ -41,6 +42,7 @@ ktoml-core = { module = "com.akuleshov7:ktoml-core", version.ref = "ktoml" }
ktoml-file = { module = "com.akuleshov7:ktoml-file", version.ref = "ktoml" }
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
[plugins]
serilization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

View File

@ -29,14 +29,14 @@ val globalModule = module {
Dotenv.load()
}
single {
val configFilePath = get<Dotenv>().get("CONFIG_FILE_PATH")!!
val configFilePath = get<Dotenv>()["CONFIG_FILE_PATH"]
Toml.decodeFromStream<APIConfiguration>(File(configFilePath).inputStream())
}
}
val gitHubBackendModule = module {
single {
val token = get<Dotenv>().get("GITHUB_TOKEN")
val token = get<Dotenv>()["GITHUB_TOKEN"]
GitHubBackend(token)
} bind Backend::class
}
@ -46,9 +46,9 @@ val databaseModule = module {
val dotenv = get<Dotenv>()
Database.connect(
url = dotenv.get("DB_URL"),
user = dotenv.get("DB_USER"),
password = dotenv.get("DB_PASSWORD"),
url = dotenv["DB_URL"],
user = dotenv["DB_USER"],
password = dotenv["DB_PASSWORD"],
driver = "org.h2.Driver"
)
}
@ -61,12 +61,12 @@ val authModule = module {
single {
val dotenv = get<Dotenv>()
val jwtSecret = dotenv.get("JWT_SECRET")!!
val issuer = dotenv.get("JWT_ISSUER")!!
val validityInMin = dotenv.get("JWT_VALIDITY_IN_MIN")!!.toInt()
val jwtSecret = dotenv["JWT_SECRET"]
val issuer = dotenv["JWT_ISSUER"]
val validityInMin = dotenv["JWT_VALIDITY_IN_MIN"].toInt()
val basicUsername = dotenv.get("BASIC_USERNAME")!!
val basicPassword = dotenv.get("BASIC_PASSWORD")!!
val basicUsername = dotenv["BASIC_USERNAME"]
val basicPassword = dotenv["BASIC_PASSWORD"]
AuthService(issuer, validityInMin, jwtSecret, basicUsername, basicPassword)
}

View File

@ -1,21 +1,57 @@
package app.revanced
import app.revanced.api.modules.configureRouting
import app.revanced.api.modules.*
import app.revanced.api.schema.APIConfiguration
import com.akuleshov7.ktoml.Toml
import io.github.cdimascio.dotenv.Dotenv
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import io.ktor.util.*
import io.mockk.every
import io.mockk.mockk
import kotlinx.serialization.encodeToString
import kotlin.test.*
class ApplicationTest {
@Test
fun testRoot() = testApplication {
fun `successfully create a token`() = testApplication {
val apiConfigurationFile = kotlin.io.path.createTempFile().toFile().apply {
Toml.encodeToString(
APIConfiguration(
organization = "ReVanced",
patchesRepository = "",
integrationsRepositoryNames = setOf(),
contributorsRepositoryNames = setOf()
)
).let(::writeText)
deleteOnExit()
}
val dotenv = mockk<Dotenv>()
every { dotenv[any()] } returns "ReVanced"
every { dotenv["JWT_VALIDITY_IN_MIN"] } returns "5"
every { dotenv["CONFIG_FILE_PATH"] } returns apiConfigurationFile.absolutePath
application {
configureDependencies()
configureHTTP()
configureSerialization()
configureSecurity()
configureRouting()
}
client.get("/").apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals("Hello World!", bodyAsText())
}
val token = client.get("/v1/token") {
headers {
append(
HttpHeaders.Authorization,
"Basic ${"${dotenv["BASIC_USERNAME"]}:${dotenv["BASIC_PASSWORD"]}".encodeBase64()}"
)
}
}.bodyAsText()
assert(token.isNotEmpty())
}
}