diff --git a/src/integration-test/kotlin/ltd/hlaeja/controller/AccountsEndpoint.kt b/src/integration-test/kotlin/ltd/hlaeja/controller/AccountsEndpoint.kt index deb7aa1..89ea12a 100644 --- a/src/integration-test/kotlin/ltd/hlaeja/controller/AccountsEndpoint.kt +++ b/src/integration-test/kotlin/ltd/hlaeja/controller/AccountsEndpoint.kt @@ -1,16 +1,10 @@ package ltd.hlaeja.controller -import java.util.UUID import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.test.container.PostgresContainer import org.assertj.core.api.Assertions.assertThat -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.Nested import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.CsvSource import org.springframework.boot.test.context.SpringBootTest @@ -21,7 +15,6 @@ import org.springframework.test.web.reactive.server.expectBody @PostgresContainer @SpringBootTest(webEnvironment = RANDOM_PORT) -@ExtendWith(SoftAssertionsExtension::class) class AccountsEndpoint { @LocalServerPort @@ -52,7 +45,7 @@ class AccountsEndpoint { value = [ "1,3", "2,0", - ] + ], ) fun `get accounts with pages`(page: Int, expected: Int) { // when @@ -83,7 +76,7 @@ class AccountsEndpoint { "3,2,0", "1,5,3", "2,5,0", - ] + ], ) fun `get accounts with pages and size to show`(page: Int, show: Int, expected: Int) { // when @@ -106,7 +99,7 @@ class AccountsEndpoint { "1,-1", "-1,1", "-1,-1", - ] + ], ) fun `get accounts with bad pages or bad size to show`(page: Int, show: Int) { // when diff --git a/src/integration-test/resources/application.yml b/src/integration-test/resources/application.yml index 3eb9709..0e36cfe 100644 --- a/src/integration-test/resources/application.yml +++ b/src/integration-test/resources/application.yml @@ -6,7 +6,3 @@ spring: url: r2dbc:pool:postgresql://localhost:5432/test username: test password: test - -test: - postgres: - init-script: postgres/schema.sql diff --git a/src/integration-test/resources/postgres/data.sql b/src/integration-test/resources/postgres/data.sql new file mode 100644 index 0000000..16e7c7e --- /dev/null +++ b/src/integration-test/resources/postgres/data.sql @@ -0,0 +1,5 @@ +-- Test data +insert into public.accounts (id, created_at, updated_at, enabled, username, password, roles) +values ('00000000-0000-7000-0000-000000000001'::uuid, '2000-01-01 00:00:00.000001 +00:00', '2000-01-01 00:00:01.000001 +00:00', true, 'admin', '$2a$12$KoXBoLOANMK11J4xeJHPA.Sy0FG.m8KWk7P4XFsMO.ZbFmFI2DckK', 'ROLE_ADMIN'), + ('00000000-0000-7000-0000-000000000002'::uuid, '2000-01-01 00:00:00.000001 +00:00', '2000-01-01 00:00:01.000001 +00:00', true, 'user', '$2a$12$KoXBoLOANMK11J4xeJHPA.Sy0FG.m8KWk7P4XFsMO.ZbFmFI2DckK', 'ROLE_USER'), + ('00000000-0000-7000-0000-000000000003'::uuid, '2000-01-01 00:00:00.000001 +00:00', '2000-01-01 00:00:01.000001 +00:00', false, 'disabled', '$2a$12$KoXBoLOANMK11J4xeJHPA.Sy0FG.m8KWk7P4XFsMO.ZbFmFI2DckK', 'ROLE_USER'); diff --git a/src/integration-test/resources/postgres/reset.sql b/src/integration-test/resources/postgres/reset.sql new file mode 100644 index 0000000..99fc858 --- /dev/null +++ b/src/integration-test/resources/postgres/reset.sql @@ -0,0 +1,11 @@ +-- Disable triggers on the tables +ALTER TABLE accounts DISABLE TRIGGER ALL; +ALTER TABLE accounts_audit DISABLE TRIGGER ALL; + +-- Truncate tables +TRUNCATE TABLE accounts_audit; +TRUNCATE TABLE accounts; + +-- Enable triggers on the account table +ALTER TABLE accounts ENABLE TRIGGER ALL; +ALTER TABLE accounts_audit ENABLE TRIGGER ALL; diff --git a/src/integration-test/resources/postgres/schema.sql b/src/integration-test/resources/postgres/schema.sql index be82893..9ed483a 100644 --- a/src/integration-test/resources/postgres/schema.sql +++ b/src/integration-test/resources/postgres/schema.sql @@ -72,7 +72,6 @@ CREATE OR REPLACE TRIGGER accounts_audit_trigger ON public.accounts FOR EACH ROW EXECUTE FUNCTION public.accounts_audit(); - -- Test data insert into public.accounts (id, created_at, updated_at, enabled, username, password, roles) values ('00000000-0000-7000-0000-000000000001'::uuid, '2000-01-01 00:00:00.000001 +00:00', '2000-01-01 00:00:01.000001 +00:00', true, 'admin', '$2a$12$KoXBoLOANMK11J4xeJHPA.Sy0FG.m8KWk7P4XFsMO.ZbFmFI2DckK', 'ROLE_ADMIN'), diff --git a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt index cd0d326..d7af26b 100644 --- a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt @@ -1,12 +1,10 @@ package ltd.hlaeja.service import io.github.oshai.kotlinlogging.KotlinLogging -import java.lang.IllegalArgumentException import java.util.UUID import ltd.hlaeja.entity.AccountEntity import ltd.hlaeja.repository.AccountRepository import org.springframework.dao.DuplicateKeyException -import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus.BAD_REQUEST import org.springframework.http.HttpStatus.CONFLICT import org.springframework.http.HttpStatus.NOT_FOUND diff --git a/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt b/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt index 52ec38f..d872c99 100644 --- a/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt +++ b/src/main/kotlin/ltd/hlaeja/validator/AccountValidator.kt @@ -13,7 +13,7 @@ class AccountValidator : ConstraintValidator { } } - private fun Account.Request.validate(): Boolean = username.isNotBlank() - && password?.isNotBlank() ?: true - && roles.isNotEmpty() + private fun Account.Request.validate(): Boolean = username.isNotBlank() && + password?.isNotBlank() ?: true && + roles.isNotEmpty() } diff --git a/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt b/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt index 3803c1f..89a6803 100644 --- a/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt +++ b/src/main/kotlin/ltd/hlaeja/validator/ValidAccount.kt @@ -11,5 +11,5 @@ import kotlin.reflect.KClass annotation class ValidAccount( val message: String = "Roles must not be empty", val groups: Array> = [], - val payload: Array> = [] + val payload: Array> = [], ) diff --git a/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt b/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt index 6a36c49..4037a3f 100644 --- a/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt +++ b/src/test/kotlin/ltd/hlaeja/validator/AccountValidatorTest.kt @@ -1,6 +1,5 @@ package ltd.hlaeja.validator - import io.mockk.mockk import ltd.hlaeja.library.accountRegistry.Account import org.assertj.core.api.Assertions.assertThat @@ -17,7 +16,7 @@ class AccountValidatorTest { username = "validUser", password = "strongPassword", enabled = true, - roles = listOf("USER", "TEST") + roles = listOf("USER", "TEST"), ) // when @@ -34,7 +33,7 @@ class AccountValidatorTest { username = "validUser", password = null, enabled = true, - roles = listOf("USER") + roles = listOf("USER"), ) // when @@ -51,7 +50,7 @@ class AccountValidatorTest { username = "", password = "strongPassword", enabled = true, - roles = listOf("USER") + roles = listOf("USER"), ) // when @@ -68,7 +67,7 @@ class AccountValidatorTest { username = "validUser", password = "", enabled = true, - roles = listOf("USER") + roles = listOf("USER"), ) // when @@ -85,7 +84,7 @@ class AccountValidatorTest { username = "validUser", password = "", enabled = true, - roles = emptyList() + roles = emptyList(), ) // when