feat: implement tests for api
This commit is contained in:
1 parent
5d4e855b8e
commit
1aa87f33c4
13 files changed
+218
-18
No files matched your search
@@ -6,7 +6,28 @@ on:
|
||||
- "main"
|
||||
|
||||
jobs:
|
||||
test-backend:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: "21"
|
||||
cache: gradle
|
||||
cache-dependency-path: |
|
||||
private/minis-backend/*.gradle.kts
|
||||
private/minis-backend/gradle/wrapper/gradle-wrapper.properties
|
||||
|
||||
- name: Run backend tests
|
||||
working-directory: private/minis-backend
|
||||
run: ./gradlew test --no-daemon
|
||||
|
||||
build:
|
||||
needs: test-backend
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
FROM gradle:7.5.1 as build_backend
|
||||
FROM gradle:8.11-jdk-21-and-23 as build_backend
|
||||
|
||||
COPY --chown=gradle:gradle /private/minis-backend /home/gradle/src
|
||||
WORKDIR /home/gradle/src
|
||||
|
||||
@@ -52,6 +52,21 @@ dependencies {
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
||||
}
|
||||
|
||||
ktor {
|
||||
openApi {
|
||||
enabled = true
|
||||
codeInferenceEnabled = true
|
||||
onlyCommented = false
|
||||
}
|
||||
}
|
||||
|
||||
docker {
|
||||
name = "${project.name}:${project.version}"
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
environment("SECRET", "testsecret")
|
||||
environment("ADMIN", "admin123")
|
||||
environment("PORT", "8080")
|
||||
systemProperty("io.ktor.development", "true")
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
ktor_version=3.5.1
|
||||
kotlin_version=2.3.21
|
||||
kotlin_version=2.4.10
|
||||
logback_version=1.4.14
|
||||
kotlin.code.style=official
|
||||
exposed_version=0.43.0
|
||||
|
||||
@@ -3,11 +3,16 @@ package de.walamana
|
||||
import de.walamana.plugins.*
|
||||
import de.walamana.views.*
|
||||
import io.github.cdimascio.dotenv.dotenv
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.openapi.OpenApiInfo
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.http.content.*
|
||||
import io.ktor.server.netty.*
|
||||
import io.ktor.server.plugins.swagger.swaggerUI
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.OpenApiDocSource
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
|
||||
//fun main() {
|
||||
// embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
|
||||
@@ -23,6 +28,7 @@ fun main(args: Array<String>): Unit {
|
||||
EngineMain.main(args)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalKtorApi::class)
|
||||
fun Application.module() {
|
||||
configureSecurity()
|
||||
configureHTTP()
|
||||
@@ -31,7 +37,7 @@ fun Application.module() {
|
||||
|
||||
routing {
|
||||
singlePageApplication {
|
||||
vue(System.getenv("FRONTEND_PATH"))
|
||||
vue(System.getenv("FRONTEND_PATH") ?: "")
|
||||
}
|
||||
route("/api") {
|
||||
configureMinistrantenRoutes()
|
||||
@@ -40,5 +46,12 @@ fun Application.module() {
|
||||
configurePlanRoutes()
|
||||
configureAuthenticationRoutes()
|
||||
}
|
||||
swaggerUI("/docs") {
|
||||
info = OpenApiInfo("Ministranten App API", "1.0")
|
||||
remotePath = "openapi.json"
|
||||
source = OpenApiDocSource.Routing(ContentType.Application.Json) {
|
||||
routingRoot.descendants()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.walamana.models
|
||||
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt
|
||||
import de.walamana.plugins.*
|
||||
import kotlinx.serialization.Contextual
|
||||
import kotlinx.serialization.KSerializer
|
||||
@@ -101,13 +102,13 @@ object MinistrantenDao {
|
||||
) = dbQuery {
|
||||
val statement = Ministranten.insert {
|
||||
it[Ministranten.username] = username
|
||||
it[Ministranten.passwordHash] = ""
|
||||
it[Ministranten.passwordHash] = BCrypt.withDefaults()
|
||||
.hashToString(SALT_ROUNDS, passwordHash.toCharArray())
|
||||
it[Ministranten.firstname] = firstname
|
||||
it[Ministranten.lastname] = lastname
|
||||
it[Ministranten.birthday] = birthday?.time
|
||||
it[Ministranten.privileges] = privileges.joinToString(",")
|
||||
}
|
||||
Security.setPassword(username, passwordHash)
|
||||
statement.resultedValues?.singleOrNull()?.let(::resultRowToMinistrant)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.describe
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.util.*
|
||||
import kotlin.math.max
|
||||
@@ -44,6 +46,7 @@ data class PasswordResetRequest(
|
||||
val username: String
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalKtorApi::class)
|
||||
fun Route.configureAuthenticationRoutes() {
|
||||
route("/auth") {
|
||||
post {
|
||||
@@ -122,5 +125,7 @@ fun Route.configureAuthenticationRoutes() {
|
||||
call.respond(AuthenticationResult(true, newToken, ministrant.privileges))
|
||||
}
|
||||
}
|
||||
}.describe {
|
||||
tag("Authentication")
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,11 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.describe
|
||||
import io.ktor.server.util.*
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
|
||||
@OptIn(ExperimentalKtorApi::class)
|
||||
fun Route.configureGottesdiensteRoutes() {
|
||||
authenticate {
|
||||
route("/gottesdienste") {
|
||||
@@ -69,6 +72,8 @@ fun Route.configureGottesdiensteRoutes() {
|
||||
part.dispose()
|
||||
}
|
||||
}
|
||||
}.describe {
|
||||
tag("Gottesdienste")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,15 +13,19 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.describe
|
||||
import io.ktor.server.util.*
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
|
||||
|
||||
@OptIn(ExperimentalKtorApi::class)
|
||||
fun Route.configureMarksView() {
|
||||
authenticate {
|
||||
route("/marks") {
|
||||
get {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@get call.respond(HttpStatusCode.Unauthorized)
|
||||
val planId = call.receiveParameters().getOrFail("id").toInt()
|
||||
val planId = call.request.queryParameters["id"]?.toInt()
|
||||
?: return@get call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Missing id parameter"))
|
||||
val plan = PlanDao.constructPlan(planId, isAnonymous = false)
|
||||
val data = MarksDao.allMarksForPlan(planId)
|
||||
call.respond(data)
|
||||
@@ -64,6 +68,8 @@ fun Route.configureMarksView() {
|
||||
MarksDao.removeMark(ministerId, gottesdienstId)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}.describe {
|
||||
tag("Marks")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,11 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.describe
|
||||
import io.ktor.server.util.*
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
|
||||
@ExperimentalKtorApi
|
||||
fun Route.configureMinistrantenRoutes() {
|
||||
authenticate {
|
||||
route("/ministranten") {
|
||||
@@ -69,6 +72,8 @@ fun Route.configureMinistrantenRoutes() {
|
||||
MinistrantenDao.deleteMinistrant(id)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}.describe {
|
||||
tag("Ministranten")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,11 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.routing.openapi.describe
|
||||
import io.ktor.server.util.*
|
||||
import io.ktor.utils.io.ExperimentalKtorApi
|
||||
|
||||
@OptIn(ExperimentalKtorApi::class)
|
||||
fun Route.configurePlanRoutes() {
|
||||
route("/plan") {
|
||||
authenticate {
|
||||
@@ -25,6 +28,8 @@ fun Route.configurePlanRoutes() {
|
||||
call.respond(plan)
|
||||
}
|
||||
}
|
||||
}.describe {
|
||||
tag("Plan")
|
||||
}
|
||||
route("/groups") {
|
||||
authenticate {
|
||||
@@ -58,5 +63,7 @@ fun Route.configurePlanRoutes() {
|
||||
call.respond(hashMapOf("success" to res))
|
||||
}
|
||||
}
|
||||
}.describe {
|
||||
tag("Plan")
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,118 @@
|
||||
package de.walamana
|
||||
|
||||
import de.walamana.plugins.*
|
||||
import de.walamana.models.Gottesdienst
|
||||
import de.walamana.models.GottesdienstGroup
|
||||
import de.walamana.models.Mark
|
||||
import de.walamana.models.Ministrant
|
||||
import de.walamana.views.AuthenticationRequest
|
||||
import de.walamana.views.AuthenticationResult
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.testing.*
|
||||
import kotlin.test.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import java.util.Date
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ApplicationTest {
|
||||
// @Test
|
||||
// fun testRoot() = testApplication {
|
||||
// application {
|
||||
// configureRouting()
|
||||
// }
|
||||
// client.get("/").apply {
|
||||
// assertEquals(HttpStatusCode.OK, status)
|
||||
// assertEquals("Hello World!", bodyAsText())
|
||||
// }
|
||||
// }
|
||||
private val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
@Test
|
||||
fun `admin can manage a plan and regular users remain restricted`() = testApplication {
|
||||
environment {
|
||||
config = io.ktor.server.config.ApplicationConfig("application.conf")
|
||||
}
|
||||
|
||||
fun authenticated(token: String) = "Bearer $token"
|
||||
|
||||
val adminToken = client.post("/api/auth") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(AuthenticationRequest("admin", "admin123")))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
val result = json.decodeFromString<AuthenticationResult>(response.bodyAsText())
|
||||
assertTrue(result.success)
|
||||
assertNotNull(result.token)
|
||||
}
|
||||
|
||||
client.get("/api/groups").also {
|
||||
assertEquals(HttpStatusCode.Unauthorized, it.status)
|
||||
}
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
val group = client.post("/api/groups") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(GottesdienstGroup(-1, Date(now), Date(now + 86_400_000))))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
json.decodeFromString<GottesdienstGroup>(response.bodyAsText())
|
||||
}
|
||||
assertTrue(group.id > 0)
|
||||
|
||||
val ministrantId = client.put("/api/ministranten") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(Ministrant(-1, "testuser", "password123", "Test", "User", null, emptyList())))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
json.parseToJsonElement(response.bodyAsText()).jsonObject["id"]!!.toString().toInt()
|
||||
}
|
||||
|
||||
val userToken = client.post("/api/auth") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(AuthenticationRequest("testuser", "password123")))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertNotNull(json.decodeFromString<AuthenticationResult>(response.bodyAsText()).token)
|
||||
}
|
||||
client.get("/api/groups") {
|
||||
header(HttpHeaders.Authorization, authenticated(userToken))
|
||||
}.also {
|
||||
assertEquals(HttpStatusCode.Forbidden, it.status)
|
||||
}
|
||||
|
||||
val service = client.put("/api/gottesdienste") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(Gottesdienst(0, "Test service", Date(now), Date(now + 1_800_000), group.id)))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
json.parseToJsonElement(response.bodyAsText()).jsonObject["id"]!!.toString().toInt()
|
||||
}
|
||||
|
||||
client.patch("/api/marks") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(json.encodeToString(listOf(Mark(ministrantId, service, 1))))
|
||||
}.also {
|
||||
assertEquals(HttpStatusCode.OK, it.status)
|
||||
}
|
||||
|
||||
client.get("/api/marks?id=${group.id}") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
}.let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertTrue(json.decodeFromString<List<Mark>>(response.bodyAsText()).contains(Mark(ministrantId, service, 1)))
|
||||
}
|
||||
|
||||
client.get("/api/plan?id=${group.id}") {
|
||||
header(HttpHeaders.Authorization, authenticated(adminToken))
|
||||
}.also {
|
||||
assertEquals(HttpStatusCode.OK, it.status)
|
||||
}
|
||||
|
||||
client.get("/docs/openapi.json").let { response ->
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
val specification = response.bodyAsText()
|
||||
assertTrue(specification.contains("\"requestBody\""))
|
||||
assertTrue(specification.contains("\"responses\""))
|
||||
assertTrue(specification.contains("AuthenticationRequest"))
|
||||
assertTrue(specification.contains("AuthenticationResult"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
ktor {
|
||||
development = true
|
||||
deployment {
|
||||
port = 8080
|
||||
}
|
||||
application {
|
||||
modules = [ de.walamana.ApplicationKt.module ]
|
||||
}
|
||||
}
|
||||
|
||||
jwt {
|
||||
secret = "testsecret"
|
||||
issuer = "http://0.0.0.0:8080/"
|
||||
audience = "http://0.0.0.0:8080/"
|
||||
realm = "mini-data"
|
||||
}
|
||||
|
||||
database {
|
||||
url = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"
|
||||
driver = "org.h2.Driver"
|
||||
}
|
||||
|
||||
admin {
|
||||
password = "admin123"
|
||||
}
|
||||
Reference in new issue
Block a user