Skip to content
Merged
18 changes: 18 additions & 0 deletions .github/ci_setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE USER 'ci_user'@'localhost' IDENTIFIED BY 'ci_user_pass';
DROP DATABASE IF EXISTS ci; CREATE DATABASE ci;

GRANT LOCK TABLES ON `ci`.* TO `ci_user`@`localhost`;
GRANT SELECT, INSERT, UPDATE, CREATE, REFERENCES, ALTER ON `ci`.`flyway_schema_history` TO `ci_user`@`localhost`;

create table ci.invite(is_disabled INT, used INT);
GRANT SELECT, INSERT, UPDATE (`is_disabled`, `used`), CREATE, REFERENCES, ALTER ON `ci`.`invite` TO `ci_user`@`localhost`;
drop table ci.invite;

GRANT SELECT, INSERT, CREATE, REFERENCES, ALTER ON `ci`.`target_app` TO `ci_user`@`localhost`;

GRANT SELECT, INSERT, CREATE, REFERENCES, ALTER ON `ci`.`invite_discord` TO `ci_user`@`localhost`;
GRANT SELECT, INSERT, CREATE, REFERENCES, ALTER ON `ci`.`invite_discord_joined_user` TO `ci_user`@`localhost`;
GRANT SELECT, INSERT, DELETE, CREATE, REFERENCES, ALTER ON `ci`.`invite_discord_state` TO `ci_user`@`localhost`;

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, REFERENCES, ALTER ON `ci`.`user` TO `ci_user`@`localhost`;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, REFERENCES, ALTER ON `ci`.`user_authority` TO `ci_user`@`localhost`;
54 changes: 54 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Test

on:
pull_request:
push:
branches: [ develop ]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
if: github.actor != 'dependabot[bot]'
runs-on: ubuntu-latest
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up database
uses: shogo82148/actions-setup-mysql@v1
with:
mysql-version: "8.0"

- name: Prepare database
run: |
mysql -u root < ${{ github.workspace }}/.github/ci_setup.sql

- name: Set up JDK
uses: actions/setup-java@v4
with:
distribution: 'oracle'
java-version: '21'

- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-

- name: Create config file
run: |
cat << 'EOF' > src/main/resources/application-test.yml
${{ secrets.APPLICATION_TEST_YML }}
EOF

- name: Test
run: mvn integration-test
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<version>0.0.1-SNAPSHOT</version>
<name>TechUni Invite System</name>
<description>TechUni Invitation System.</description>
<organization>
<name>IT Student Org, Tech.Uni</name>
<url>https://techuni.org</url>
</organization>
<url>https://invite.techuni.org</url>
<developers>
<developer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public ResponseEntity<LoginSuccessResponse> login(@Validated @NotNull @RequestBo
}

try {
final var authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getUser(), loginRequest.getPassword()));
final var authentication =
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUser(), loginRequest.getPassword()));

final var token = tokenProvider.generateToken(authentication);
return ResponseEntity.ok().body(new LoginSuccessResponse(token));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface InviteWithDiscordStateMapper {
Invite getInviteByState(@Param("state") String state);

void cleanState(@Param("nowTime") LocalDateTime nowTime, @Param("stateExpireTime") LocalDateTime stateExpireTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public Invite intoDB() {
public <M extends AbstractInviteModel<?>> M intoModel(Class<M> modelClass) {
final var _modelClass = targetApplication.getModelClass();
if (!modelClass.equals(_modelClass)) {
throw ErrorCode.UNEXPECTED_ERROR.exception(
"Model class is not matched. (Expected: %s, Selected: %s)".formatted(_modelClass.getName(), modelClass.getName()));
throw ErrorCode.UNEXPECTED_ERROR
.exception("Model class is not matched. (Expected: %s, Selected: %s)".formatted(_modelClass.getName(), modelClass.getName()));
}

return modelClass.cast(intoModel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public enum ErrorCode {
/**
* OTHER
*/
UNEXPECTED_ERROR(ErrorSource.OTHER, 1, HttpStatus.INTERNAL_SERVER_ERROR, ErrorMessage.INTERNAL_UNEXPECTED_ERROR,
ErrorMessage.UNEXPECTED_ERROR), //
UNEXPECTED_ERROR(ErrorSource.OTHER, 1, HttpStatus.INTERNAL_SERVER_ERROR, ErrorMessage.INTERNAL_UNEXPECTED_ERROR, ErrorMessage.UNEXPECTED_ERROR), //

/**
* LOGIN
Expand Down Expand Up @@ -51,8 +50,7 @@ public enum ErrorCode {
ErrorMessage.DISCORD_LOGIN_DENIED), //
DISCORD_AUTHENTICATED_VALIDATION_ERROR(ErrorSource.INVITATION, 108, HttpStatus.UNAUTHORIZED, null, ErrorMessage.DISCORD_LOGIN_FAILED), //
DISCORD_CREATE_JOIN_DM_ERROR(ErrorSource.INVITATION, 109, HttpStatus.INTERNAL_SERVER_ERROR, null, ErrorMessage.DISCORD_UNEXPECTED_ERROR), //
DISCORD_CREATE_INVITE_GUILD_ACCESS_ERROR(ErrorSource.INVITATION, 110, HttpStatus.BAD_REQUEST,
ErrorMessage.INTERNAL_DISCORD_GUILD_ACCESS_ERROR), //
DISCORD_CREATE_INVITE_GUILD_ACCESS_ERROR(ErrorSource.INVITATION, 110, HttpStatus.BAD_REQUEST, ErrorMessage.INTERNAL_DISCORD_GUILD_ACCESS_ERROR), //
DISCORD_GUILD_ACCESS_ERROR(ErrorSource.INVITATION, 111, HttpStatus.NOT_FOUND, ErrorMessage.INTERNAL_DISCORD_GUILD_ACCESS_ERROR,
ErrorMessage.INVITATION_INVALID), //
DISCORD_LACK_GUILD_PERMISSION(ErrorSource.INVITATION, 112, HttpStatus.NOT_FOUND, ErrorMessage.INTERNAL_LACK_GUILD_PERMISSION,
Expand Down Expand Up @@ -123,8 +121,8 @@ public String getInternalMessage() {
}

public String getInternalMessage(String... args) {
final var output = Optional.ofNullable(internalMessage).map(ErrorMessage::getMessage).orElse(status.getReasonPhrase())
.formatted((Object[]) args);
final var output =
Optional.ofNullable(internalMessage).map(ErrorMessage::getMessage).orElse(status.getReasonPhrase()).formatted((Object[]) args);
return "\"%s\" - ErrorCode.%s".formatted(output, this.name());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public enum ErrorMessage {
INTERNAL_INVITATION_INVALID("Request Invalid Invitation Code. (Code: %s)"), //

/* DISCORD INVITATION */
INTERNAL_DISCORD_ALREADY_JOINED(
"The Discord Invitation target already joined to the guild. (DbId: %s, InvitationCode: %s, Guild: %s, User: %s)"), //
DISCORD_ALREADY_JOINED(
"Your account have already joined to the discord server. If you think this is a mistake, please contact to the inviter."), //
INTERNAL_DISCORD_ALREADY_JOINED("The Discord Invitation target already joined to the guild. (DbId: %s, InvitationCode: %s, Guild: %s, User: %s)"), //
DISCORD_ALREADY_JOINED("Your account have already joined to the discord server. If you think this is a mistake, please contact to the inviter."), //
INTERNAL_DISCORD_LOGIN_FAILED("Discord login failed."), //
DISCORD_LOGIN_FAILED("Login to Your account is failed. If you think this is a mistake, please try again."), //
INTERNAL_DISCORD_LOGIN_UNEXPECTED_ERROR("Unexpected error occurred in Discord login sequence."), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

@AllArgsConstructor
public enum MessageType {
JOIN_SERVER_DM("join-server", JoinServerDMVariable.class),
;
JOIN_SERVER_DM("join-server", JoinServerDMVariable.class),;

@Getter
private final String templateName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.techuni.TechUniInviteSystem.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Profile("!test")
@Component
public class MyPasswordEncoder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private void sendDM(CreateDMData createDMData) {
if (attachment.forceLoad) { // isForceLoadAttachment
throw new RuntimeException("Failed to load attachment: %s".formatted(attachment.name), e);
} else {
final var message = "Failed to load attachment. But the DM will sent(because it is not forced attachment). : %s".formatted(
attachment.name);
final var message = "Failed to load attachment. But the DM will sent(because it is not forced attachment). : %s"
.formatted(attachment.name);
log.warn(message, e);
return null;
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/resources/db/mapper_test/TestUserMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.techuni.TechUniInviteSystem.db.TestUserRepository">

<!-- getUserByNameセレクトクエリ -->
<insert id="insertUser" parameterType="org.techuni.TechUniInviteSystem.db.TestUser" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user (uuid, isEnable, name, passHash) VALUES (#{insertUser.uuid}, #{insertUser.isEnable}, #{insertUser.name}, #{insertUser.passHash})
</insert>

<insert id="insertUserAuthority">
INSERT INTO user_authority (id, authority) VALUES
<foreach item="authority" collection="authorities" separator=",">
(#{id}, #{authority})
</foreach>
</insert>

<delete id="resetUser">
DELETE FROM user;
</delete>

<delete id="resetUserAuthority">
DELETE FROM user_authority;
</delete>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class TechUniInviteSystemApplicationTests {

@Test
Expand Down
Loading