Authentication integration test

- add end-to-end test AuthenticationEndpoint
- add user-token.data
- add admin-token.data
- add .data to .editorconfig
This commit is contained in:
2025-02-07 13:50:37 +01:00
parent 5951af7d44
commit c7eb3484e6
6 changed files with 123 additions and 1 deletions

View File

@@ -17,6 +17,10 @@ tab_width = 2
[*.bat]
end_of_line = crlf
[*.data]
max_line_length = 1024
insert_final_newline = false
[*.pem]
max_line_length = 64
insert_final_newline = false

View File

@@ -8,6 +8,7 @@ plugins {
}
dependencies {
implementation(hlaeja.fasterxml.jackson.module.kotlin)
implementation(hlaeja.kotlin.logging)
implementation(hlaeja.kotlin.reflect)
implementation(hlaeja.kotlinx.coroutines)

View File

@@ -195,7 +195,6 @@ class AccountEndpoint {
// then
result.expectStatus().isNotFound
}
}
@Nested

View File

@@ -0,0 +1,116 @@
package ltd.hlaeja.controller
import org.assertj.core.api.Assertions.assertThat
import ltd.hlaeja.library.accountRegistry.Authentication
import ltd.hlaeja.test.compareToFile
import ltd.hlaeja.test.container.PostgresContainer
import org.assertj.core.api.SoftAssertions
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.http.HttpStatus
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.reactive.server.expectBody
@PostgresContainer
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(SoftAssertionsExtension::class)
class AuthenticationEndpoint {
@InjectSoftAssertions
lateinit var softly: SoftAssertions
@LocalServerPort
var port: Int = 0
lateinit var webClient: WebTestClient
@BeforeEach
fun setup() {
webClient = WebTestClient.bindToServer().baseUrl("http://localhost:$port").build()
}
@Test
fun `login as admin`() {
// given
val request = Authentication.Request(
username = "admin",
password = "pass",
)
// when
val result = webClient.post().uri("/authenticate").bodyValue(request).exchange()
// then
result.expectStatus().isOk()
.expectBody<Authentication.Response>()
.consumeWith { assertThat(it.responseBody?.token).compareToFile("authenticate/admin-token.data") }
}
@Test
fun `login as user`() {
// given
val request = Authentication.Request(
username = "user",
password = "pass",
)
// when
val result = webClient.post().uri("/authenticate").bodyValue(request).exchange()
// then
result.expectStatus().isOk()
.expectBody<Authentication.Response>()
.consumeWith { assertThat(it.responseBody?.token).compareToFile("authenticate/user-token.data") }
}
@Test
fun `login as disabled user`() {
// given
val request = Authentication.Request(
username = "disabled",
password = "pass",
)
// when
val result = webClient.post().uri("/authenticate").bodyValue(request).exchange()
// then
result.expectStatus().isEqualTo(HttpStatus.LOCKED)
}
@Test
fun `login as non-existent `() {
// given
val request = Authentication.Request(
username = "username",
password = "pass",
)
// when
val result = webClient.post().uri("/authenticate").bodyValue(request).exchange()
// then
result.expectStatus().isNotFound
}
@Test
fun `login as user bad password`() {
// given
val request = Authentication.Request(
username = "user",
password = "password",
)
// when
val result = webClient.post().uri("/authenticate").bodyValue(request).exchange()
// then
result.expectStatus().isUnauthorized
}
}

View File

@@ -0,0 +1 @@
eyJhbGciOiJSUzI1NiJ9.eyJpZCI6IjAwMDAwMDAwLTAwMDAtNzAwMC0wMDAwLTAwMDAwMDAwMDAwMSIsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiUk9MRV9BRE1JTiJ9.D6pK86XPWcdu1imV_y_4nAM6R4WEZvJpQ7oGaPAYe0_rg3UWdmVMa8Iw7L21bRgFoyIa7FQBwb_0AXojFVdb2mdOVDeGOwxQZAx23dwqeicOGd8yUMnuBaRSnd7z4P65KPMbbf0NOTQtho0Iv5mBAwFMJoF67sw-yntfx3cD_bfrI-Rf4oZaZsVn38Y2HJBe2sO2QI4e5_7s82ikxac416OX7PcIEgaf3IeEK1fSzSjRG_dyBGT_Jq_vAzVURsSu4ep976kI-k5ZXNE9EMxKu1S-n5c5eiaqo96ObnaSl4eWFik5q8vLhNLYIYO-bQi1xlJKnStwZqtUwlR763Gd5w

View File

@@ -0,0 +1 @@
eyJhbGciOiJSUzI1NiJ9.eyJpZCI6IjAwMDAwMDAwLTAwMDAtNzAwMC0wMDAwLTAwMDAwMDAwMDAwMiIsInVzZXJuYW1lIjoidXNlciIsInJvbGUiOiJST0xFX1VTRVIifQ.GvZIq0VF9xB8UY3PUGdnc6JNeUXtv4LzHJ56hWSeqUS6BXH0M_QJ5Lu9ndh9_P85CECp3eKrW4fKymGYe-NUXCtrzhr9-SSZLF6D7GRzAJ4yZjVRCOa_dgqe1RGuIZyZpli36z4NPqeBFqtHJ3Cs5rAI-WdvxGfWPgtM2kzpSJ_0zFihp9mVcZBlWP57HlN7-oKzDJWVpO2E17fWZTy-y4pdrIUsff63c256Cy8NhiAgux9aqZTdzaqp9TsXw59bRsS5d0YH7-gJuBd4xctZwgy_41BOcRk2q-nLyLZgWJs1wmCa_zaW0Fj6fjAsYvpdPNegkpIqrHJcQpGd7nE0KQ