use data and reset sql files and some clean up

This commit is contained in:
2025-02-05 12:05:25 +01:00
parent c08c3cb880
commit 5951af7d44
9 changed files with 28 additions and 27 deletions

View File

@@ -1,16 +1,10 @@
package ltd.hlaeja.controller package ltd.hlaeja.controller
import java.util.UUID
import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.test.container.PostgresContainer import ltd.hlaeja.test.container.PostgresContainer
import org.assertj.core.api.Assertions.assertThat 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.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource import org.junit.jupiter.params.provider.CsvSource
import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.SpringBootTest
@@ -21,7 +15,6 @@ import org.springframework.test.web.reactive.server.expectBody
@PostgresContainer @PostgresContainer
@SpringBootTest(webEnvironment = RANDOM_PORT) @SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(SoftAssertionsExtension::class)
class AccountsEndpoint { class AccountsEndpoint {
@LocalServerPort @LocalServerPort
@@ -52,7 +45,7 @@ class AccountsEndpoint {
value = [ value = [
"1,3", "1,3",
"2,0", "2,0",
] ],
) )
fun `get accounts with pages`(page: Int, expected: Int) { fun `get accounts with pages`(page: Int, expected: Int) {
// when // when
@@ -83,7 +76,7 @@ class AccountsEndpoint {
"3,2,0", "3,2,0",
"1,5,3", "1,5,3",
"2,5,0", "2,5,0",
] ],
) )
fun `get accounts with pages and size to show`(page: Int, show: Int, expected: Int) { fun `get accounts with pages and size to show`(page: Int, show: Int, expected: Int) {
// when // when
@@ -106,7 +99,7 @@ class AccountsEndpoint {
"1,-1", "1,-1",
"-1,1", "-1,1",
"-1,-1", "-1,-1",
] ],
) )
fun `get accounts with bad pages or bad size to show`(page: Int, show: Int) { fun `get accounts with bad pages or bad size to show`(page: Int, show: Int) {
// when // when

View File

@@ -6,7 +6,3 @@ spring:
url: r2dbc:pool:postgresql://localhost:5432/test url: r2dbc:pool:postgresql://localhost:5432/test
username: test username: test
password: test password: test
test:
postgres:
init-script: postgres/schema.sql

View File

@@ -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');

View File

@@ -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;

View File

@@ -72,7 +72,6 @@ CREATE OR REPLACE TRIGGER accounts_audit_trigger
ON public.accounts ON public.accounts
FOR EACH ROW FOR EACH ROW
EXECUTE FUNCTION public.accounts_audit(); EXECUTE FUNCTION public.accounts_audit();
-- Test data -- Test data
insert into public.accounts (id, created_at, updated_at, enabled, username, password, roles) 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'), 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'),

View File

@@ -1,12 +1,10 @@
package ltd.hlaeja.service package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
import java.lang.IllegalArgumentException
import java.util.UUID import java.util.UUID
import ltd.hlaeja.entity.AccountEntity import ltd.hlaeja.entity.AccountEntity
import ltd.hlaeja.repository.AccountRepository import ltd.hlaeja.repository.AccountRepository
import org.springframework.dao.DuplicateKeyException import org.springframework.dao.DuplicateKeyException
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatus.BAD_REQUEST import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.CONFLICT import org.springframework.http.HttpStatus.CONFLICT
import org.springframework.http.HttpStatus.NOT_FOUND import org.springframework.http.HttpStatus.NOT_FOUND

View File

@@ -13,7 +13,7 @@ class AccountValidator : ConstraintValidator<ValidAccount, Any> {
} }
} }
private fun Account.Request.validate(): Boolean = username.isNotBlank() private fun Account.Request.validate(): Boolean = username.isNotBlank() &&
&& password?.isNotBlank() ?: true password?.isNotBlank() ?: true &&
&& roles.isNotEmpty() roles.isNotEmpty()
} }

View File

@@ -11,5 +11,5 @@ import kotlin.reflect.KClass
annotation class ValidAccount( annotation class ValidAccount(
val message: String = "Roles must not be empty", val message: String = "Roles must not be empty",
val groups: Array<KClass<out Any>> = [], val groups: Array<KClass<out Any>> = [],
val payload: Array<KClass<out Payload>> = [] val payload: Array<KClass<out Payload>> = [],
) )

View File

@@ -1,6 +1,5 @@
package ltd.hlaeja.validator package ltd.hlaeja.validator
import io.mockk.mockk import io.mockk.mockk
import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Account
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
@@ -17,7 +16,7 @@ class AccountValidatorTest {
username = "validUser", username = "validUser",
password = "strongPassword", password = "strongPassword",
enabled = true, enabled = true,
roles = listOf("USER", "TEST") roles = listOf("USER", "TEST"),
) )
// when // when
@@ -34,7 +33,7 @@ class AccountValidatorTest {
username = "validUser", username = "validUser",
password = null, password = null,
enabled = true, enabled = true,
roles = listOf("USER") roles = listOf("USER"),
) )
// when // when
@@ -51,7 +50,7 @@ class AccountValidatorTest {
username = "", username = "",
password = "strongPassword", password = "strongPassword",
enabled = true, enabled = true,
roles = listOf("USER") roles = listOf("USER"),
) )
// when // when
@@ -68,7 +67,7 @@ class AccountValidatorTest {
username = "validUser", username = "validUser",
password = "", password = "",
enabled = true, enabled = true,
roles = listOf("USER") roles = listOf("USER"),
) )
// when // when
@@ -85,7 +84,7 @@ class AccountValidatorTest {
username = "validUser", username = "validUser",
password = "", password = "",
enabled = true, enabled = true,
roles = emptyList() roles = emptyList(),
) )
// when // when