From c7eb3484e6553d32247626402cfa89c2fb7869b7 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Fri, 7 Feb 2025 13:50:37 +0100 Subject: [PATCH] Authentication integration test - add end-to-end test AuthenticationEndpoint - add user-token.data - add admin-token.data - add .data to .editorconfig --- .editorconfig | 4 + build.gradle.kts | 1 + .../ltd/hlaeja/controller/AccountEndpoint.kt | 1 - .../controller/AuthenticationEndpoint.kt | 116 ++++++++++++++++++ .../resources/authenticate/admin-token.data | 1 + .../resources/authenticate/user-token.data | 1 + 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/integration-test/kotlin/ltd/hlaeja/controller/AuthenticationEndpoint.kt create mode 100644 src/integration-test/resources/authenticate/admin-token.data create mode 100644 src/integration-test/resources/authenticate/user-token.data diff --git a/.editorconfig b/.editorconfig index 1de69b6..1b4230f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/build.gradle.kts b/build.gradle.kts index 18fc380..aec946c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,6 +8,7 @@ plugins { } dependencies { + implementation(hlaeja.fasterxml.jackson.module.kotlin) implementation(hlaeja.kotlin.logging) implementation(hlaeja.kotlin.reflect) implementation(hlaeja.kotlinx.coroutines) diff --git a/src/integration-test/kotlin/ltd/hlaeja/controller/AccountEndpoint.kt b/src/integration-test/kotlin/ltd/hlaeja/controller/AccountEndpoint.kt index c6f77cc..ee020a4 100644 --- a/src/integration-test/kotlin/ltd/hlaeja/controller/AccountEndpoint.kt +++ b/src/integration-test/kotlin/ltd/hlaeja/controller/AccountEndpoint.kt @@ -195,7 +195,6 @@ class AccountEndpoint { // then result.expectStatus().isNotFound } - } @Nested diff --git a/src/integration-test/kotlin/ltd/hlaeja/controller/AuthenticationEndpoint.kt b/src/integration-test/kotlin/ltd/hlaeja/controller/AuthenticationEndpoint.kt new file mode 100644 index 0000000..6d834f1 --- /dev/null +++ b/src/integration-test/kotlin/ltd/hlaeja/controller/AuthenticationEndpoint.kt @@ -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() + .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() + .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 + } +} diff --git a/src/integration-test/resources/authenticate/admin-token.data b/src/integration-test/resources/authenticate/admin-token.data new file mode 100644 index 0000000..021ddea --- /dev/null +++ b/src/integration-test/resources/authenticate/admin-token.data @@ -0,0 +1 @@ +eyJhbGciOiJSUzI1NiJ9.eyJpZCI6IjAwMDAwMDAwLTAwMDAtNzAwMC0wMDAwLTAwMDAwMDAwMDAwMSIsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiUk9MRV9BRE1JTiJ9.D6pK86XPWcdu1imV_y_4nAM6R4WEZvJpQ7oGaPAYe0_rg3UWdmVMa8Iw7L21bRgFoyIa7FQBwb_0AXojFVdb2mdOVDeGOwxQZAx23dwqeicOGd8yUMnuBaRSnd7z4P65KPMbbf0NOTQtho0Iv5mBAwFMJoF67sw-yntfx3cD_bfrI-Rf4oZaZsVn38Y2HJBe2sO2QI4e5_7s82ikxac416OX7PcIEgaf3IeEK1fSzSjRG_dyBGT_Jq_vAzVURsSu4ep976kI-k5ZXNE9EMxKu1S-n5c5eiaqo96ObnaSl4eWFik5q8vLhNLYIYO-bQi1xlJKnStwZqtUwlR763Gd5w \ No newline at end of file diff --git a/src/integration-test/resources/authenticate/user-token.data b/src/integration-test/resources/authenticate/user-token.data new file mode 100644 index 0000000..f28e904 --- /dev/null +++ b/src/integration-test/resources/authenticate/user-token.data @@ -0,0 +1 @@ +eyJhbGciOiJSUzI1NiJ9.eyJpZCI6IjAwMDAwMDAwLTAwMDAtNzAwMC0wMDAwLTAwMDAwMDAwMDAwMiIsInVzZXJuYW1lIjoidXNlciIsInJvbGUiOiJST0xFX1VTRVIifQ.GvZIq0VF9xB8UY3PUGdnc6JNeUXtv4LzHJ56hWSeqUS6BXH0M_QJ5Lu9ndh9_P85CECp3eKrW4fKymGYe-NUXCtrzhr9-SSZLF6D7GRzAJ4yZjVRCOa_dgqe1RGuIZyZpli36z4NPqeBFqtHJ3Cs5rAI-WdvxGfWPgtM2kzpSJ_0zFihp9mVcZBlWP57HlN7-oKzDJWVpO2E17fWZTy-y4pdrIUsff63c256Cy8NhiAgux9aqZTdzaqp9TsXw59bRsS5d0YH7-gJuBd4xctZwgy_41BOcRk2q-nLyLZgWJs1wmCa_zaW0Fj6fjAsYvpdPNegkpIqrHJcQpGd7nE0KQ \ No newline at end of file