test: Add initial test for token

This commit is contained in:
oSumAtrIX
2024-01-31 11:58:52 +01:00
parent 1dba4923c8
commit 673efc8038
4 changed files with 55 additions and 16 deletions

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())
}
}