From 3530e9e28d74f1f92d809b5c389b250cd94536d1 Mon Sep 17 00:00:00 2001 From: Leonid Date: Sun, 2 Mar 2025 11:06:11 +0300 Subject: [PATCH] =?UTF-8?q?-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D1=8C=D1=8E=20Testconta?= =?UTF-8?q?iners=20-=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D1=81=D0=BE=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=91=D0=94,=20=D1=81=20=D0=BD=D0=B8=D0=BC=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BD=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8F.=20-=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3=D0=B8=D1=8E=20=D0=B3?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20Id=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D1=81=20'AUTO'=20=D0=BD=D0=B0=20`IDENTITY`.=20=D0=91?= =?UTF-8?q?=D0=B5=D0=B7=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE=20=D1=80=D1=83?= =?UTF-8?q?=D0=B3=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=BD=D0=B0=20=D0=B4?= =?UTF-8?q?=D1=83=D0=B1=D0=BB=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20id=20=D0=BF=D1=80=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BD=D0=BE=D0=B2=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D1=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.youtube.com/watch?v=Lnc3o8cCwZY&list=PLU2ftbIeotGpAYRP9Iv2KLIwK36-o_qYk&index=26 --- pom.xml | 25 ++++- .../sweater/config/WebSecurityConfig.java | 6 +- .../sytes/kashey/sweater/domain/Message.java | 2 +- src/main/resources/application.properties | 3 - .../resources/db/migration/V1__Init_DB.sql | 69 ++++++------ src/main/resources/templates/main.ftl | 4 +- src/main/resources/templates/parts/common.ftl | 6 +- .../templates/parts/messageEditor.ftl | 4 +- .../resources/templates/parts/messageList.ftl | 6 +- src/main/resources/templates/parts/navbar.ftl | 2 +- src/main/resources/templates/userEdit.ftl | 8 +- .../net/sytes/kashey/sweater/LoginTest.java | 80 ++++++++++++++ .../kashey/sweater/MainControllerTest.java | 102 ++++++++++++++++++ .../resources/application-test.properties | 19 ++++ src/test/resources/import.sql | 49 +++++++++ 15 files changed, 323 insertions(+), 62 deletions(-) create mode 100644 src/test/java/net/sytes/kashey/sweater/LoginTest.java create mode 100644 src/test/java/net/sytes/kashey/sweater/MainControllerTest.java create mode 100644 src/test/resources/application-test.properties create mode 100644 src/test/resources/import.sql diff --git a/pom.xml b/pom.xml index 96bf85a..9358797 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,26 @@ spring-boot-starter-test test + + org.testcontainers + mysql + test + + + org.testcontainers + junit-jupiter + test + + + org.springframework.boot + spring-boot-testcontainers + + + org.springframework.security + spring-security-test + RELEASE + test + org.springframework.boot spring-boot-starter-data-jpa @@ -81,12 +101,7 @@ org.springframework.boot spring-boot-starter-validation - - org.springframework.session - spring-session-jdbc - - diff --git a/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java b/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java index c13c214..8335f76 100644 --- a/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java +++ b/src/main/java/net/sytes/kashey/sweater/config/WebSecurityConfig.java @@ -2,7 +2,6 @@ package net.sytes.kashey.sweater.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -20,18 +19,19 @@ public class WebSecurityConfig { public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(8); } + @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests((requests) -> requests - .requestMatchers("/", "/registration","/static/**", "/activate/*").permitAll() + .requestMatchers("/", "/registration", "/static/**", "/activate/*").permitAll() .anyRequest().authenticated() ) .formLogin((form) -> form .loginPage("/login") .defaultSuccessUrl("/main", true) .permitAll() - ).rememberMe(Customizer.withDefaults()) + ) .logout(LogoutConfigurer::permitAll); return http.build(); diff --git a/src/main/java/net/sytes/kashey/sweater/domain/Message.java b/src/main/java/net/sytes/kashey/sweater/domain/Message.java index fd95fae..81b8510 100644 --- a/src/main/java/net/sytes/kashey/sweater/domain/Message.java +++ b/src/main/java/net/sytes/kashey/sweater/domain/Message.java @@ -7,7 +7,7 @@ import org.hibernate.validator.constraints.Length; @Entity public class Message { @Id - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank(message = "Please fill in the field.") diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f0eed7e..c873ed4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -19,6 +19,3 @@ spring.mail.protocol=smtps mail.debug=true recaptcha.secret= -spring.session.jdbc.serialize=java -spring.session.jdbc.initialize-schema=always -spring.session.jdbc.table-name=SPRING_SESSION diff --git a/src/main/resources/db/migration/V1__Init_DB.sql b/src/main/resources/db/migration/V1__Init_DB.sql index ce9f7ca..905bd21 100644 --- a/src/main/resources/db/migration/V1__Init_DB.sql +++ b/src/main/resources/db/migration/V1__Init_DB.sql @@ -1,48 +1,47 @@ -create table message +CREATE TABLE IF NOT EXISTS message ( - id bigint not null, - user_id bigint, - filename varchar(255), - tag varchar(255), - text varchar(2048) not null, - primary key (id) -) engine = InnoDB; + id BIGINT AUTO_INCREMENT PRIMARY KEY, + user_id BIGINT, + filename VARCHAR(255), + tag VARCHAR(255), + text VARCHAR(2048) NOT NULL +) ENGINE = InnoDB; -create table message_seq +CREATE TABLE message_seq ( - next_val bigint -) engine = InnoDB; + next_val BIGINT +) ENGINE = InnoDB; -insert into message_seq -values (1); +INSERT INTO message_seq +VALUES (1); -create table user_role +CREATE TABLE user_role ( - user_id bigint not null, - roles enum ('ADMIN','USER') -) engine = InnoDB; + user_id BIGINT NOT NULL, + roles ENUM ('ADMIN','USER') +) ENGINE = InnoDB; -create table usr +CREATE TABLE usr ( - enabled bit not null, - id bigint not null, - activation_code varchar(255), - email varchar(255), - password varchar(255) not null, - username varchar(255) not null, - primary key (id) -) engine = InnoDB; + enabled BIT NOT NULL, + id BIGINT NOT NULL, + activation_code VARCHAR(255), + email VARCHAR(255), + password VARCHAR(255) NOT NULL, + username VARCHAR(255) NOT NULL, + PRIMARY KEY (id) +) ENGINE = InnoDB; -create table usr_seq +CREATE TABLE usr_seq ( - next_val bigint -) engine = InnoDB; + next_val BIGINT +) ENGINE = InnoDB; -insert into usr_seq -values (1); +INSERT INTO usr_seq +VALUES (1); -alter table message - add constraint message_user_fk foreign key (user_id) references usr (id); +ALTER TABLE message + ADD CONSTRAINT message_user_fk FOREIGN KEY (user_id) REFERENCES usr (id); -alter table user_role - add constraint user_role_user_fk foreign key (user_id) references usr (id); +ALTER TABLE user_role + ADD CONSTRAINT user_role_user_fk FOREIGN KEY (user_id) REFERENCES usr (id); diff --git a/src/main/resources/templates/main.ftl b/src/main/resources/templates/main.ftl index 500159a..809a3d1 100644 --- a/src/main/resources/templates/main.ftl +++ b/src/main/resources/templates/main.ftl @@ -1,9 +1,9 @@ <#import "parts/common.ftl" as com> <@com.page> - +
- +
diff --git a/src/main/resources/templates/parts/common.ftl b/src/main/resources/templates/parts/common.ftl index 4fb35ab..6abe296 100644 --- a/src/main/resources/templates/parts/common.ftl +++ b/src/main/resources/templates/parts/common.ftl @@ -4,9 +4,9 @@ Sweater - - - + + + diff --git a/src/main/resources/templates/parts/messageEditor.ftl b/src/main/resources/templates/parts/messageEditor.ftl index c3e0798..1bd7a1a 100644 --- a/src/main/resources/templates/parts/messageEditor.ftl +++ b/src/main/resources/templates/parts/messageEditor.ftl @@ -18,7 +18,7 @@
+ value="<#if message??>${message.tag}" name="tag" placeholder="Tag"/> <#if tagError??>
${tagError} @@ -27,7 +27,7 @@
- +
diff --git a/src/main/resources/templates/parts/messageList.ftl b/src/main/resources/templates/parts/messageList.ftl index 5eeff32..e2359e3 100644 --- a/src/main/resources/templates/parts/messageList.ftl +++ b/src/main/resources/templates/parts/messageList.ftl @@ -1,14 +1,14 @@ <#include "security.ftl"> -
+
<#list messages as message>
-
+
<#if message.filename??>
- ${message.text}
+ ${message.text}
#${message.tag}