Skip to content

Commit bd9bf0f

Browse files
authored
test: 테스트 환경 설정 (#327)
* test: 테스트 환경 설정 * test: 테스트 환경 설정 * test: 테스트 환경 설정
1 parent c822e06 commit bd9bf0f

File tree

9 files changed

+195
-6
lines changed

9 files changed

+195
-6
lines changed

.github/workflows/ci_gradle.yml

+17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,24 @@ jobs:
1717
with:
1818
java-version: '17'
1919
distribution: 'temurin'
20+
21+
## create application.yml
22+
- name: make application-secret.yml
23+
run: |
24+
mkdir -p ./api/src/main/resources
25+
touch ./api/src/main/resources/application-secret.yml
26+
shell: bash
27+
- name: deliver application-secret.yml
28+
run: echo "${{ secrets.APPLICATION_SECRET }}" > ./api/src/main/resources/application-secret.yml
29+
shell: bash
30+
31+
- name: Start Docker Compose
32+
run: docker-compose -f docker-compose-test.yml up -d
33+
2034
- name: Grant execute permission for gradlew
2135
run: chmod +x gradlew
2236
- name: Test with Gradle
2337
run: ./gradlew test
38+
39+
- name: Stop Docker Compose
40+
run: docker-compose -f docker-compose-test.yml down

api/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414

1515
// Validation
1616
implementation 'org.springframework.boot:spring-boot-starter-validation'
17+
testImplementation 'org.projectlombok:lombok:1.18.22'
1718
}
1819
bootJar { enabled = true }
1920
jar { enabled = false }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sponus.sponusbe.domain.organization;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.ActiveProfiles;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import com.sponus.coredomain.domain.organization.Organization;
11+
import com.sponus.coredomain.domain.organization.enums.OrganizationType;
12+
import com.sponus.coredomain.domain.organization.enums.ProfileStatus;
13+
import com.sponus.coredomain.domain.organization.repository.OrganizationRepository;
14+
15+
@SpringBootTest
16+
@Transactional
17+
@ActiveProfiles("test")
18+
class OrganizationRepositoryTest {
19+
20+
@Autowired
21+
OrganizationRepository organizationRepository;
22+
23+
@Test
24+
void saveTest() {
25+
// given
26+
Organization entity = Organization.builder()
27+
28+
.name("sponus_company")
29+
.password("sponus_company1234#")
30+
.organizationType(OrganizationType.COMPANY)
31+
.profileStatus(ProfileStatus.ACTIVE)
32+
.build();
33+
34+
// when
35+
Organization savedEntity = organizationRepository.save(entity);
36+
37+
// then
38+
Assertions.assertThat(entity.getEmail()).isEqualTo(savedEntity.getEmail());
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.sponus.sponusbe.domain.organization;
2+
3+
import java.util.List;
4+
5+
import org.assertj.core.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.MediaType;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.MvcResult;
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
15+
import org.springframework.transaction.annotation.Transactional;
16+
17+
import com.sponus.coredomain.domain.organization.Organization;
18+
import com.sponus.coredomain.domain.organization.enums.OrganizationType;
19+
import com.sponus.coredomain.domain.organization.repository.OrganizationRepository;
20+
import com.sponus.sponusbe.domain.organization.dto.OrganizationCreateRequest;
21+
import com.sponus.sponusbe.domain.organization.service.OrganizationService;
22+
23+
@SpringBootTest
24+
@Transactional
25+
@AutoConfigureMockMvc
26+
class SecurityAuthTest {
27+
28+
@Autowired
29+
MockMvc mockMvc;
30+
31+
@Autowired
32+
OrganizationRepository organizationRepository;
33+
@Autowired
34+
OrganizationService organizationService;
35+
36+
@Test
37+
void joinTest() {
38+
39+
// given
40+
OrganizationCreateRequest request = new OrganizationCreateRequest("[email protected]",
41+
"sponus_company1234#",
42+
"sponus_company", OrganizationType.COMPANY);
43+
44+
// when
45+
Long organizationId = organizationService.createOrganization(request);
46+
47+
// then
48+
List<Organization> organizationList = organizationRepository.findAll();
49+
Assertions.assertThat(organizationList.size()).isEqualTo(1);
50+
}
51+
52+
@Test
53+
void loginTest() throws Exception {
54+
55+
// given
56+
OrganizationCreateRequest request = new OrganizationCreateRequest("[email protected]",
57+
"sponus_club1234#",
58+
"sponus_club", OrganizationType.COMPANY);
59+
60+
organizationService.createOrganization(request);
61+
62+
// when
63+
String jsonRequest = "{"
64+
+ "\"email\": \"[email protected]\", "
65+
+ "\"password\": \"sponus_club1234#\", "
66+
+ "\"fcmToken\": \"fcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmTokenFcmToken\""
67+
+ "}";
68+
69+
MvcResult response = mockMvc.perform(
70+
MockMvcRequestBuilders.post("/api/v2/auth/login")
71+
.contentType(MediaType.APPLICATION_JSON)
72+
.content(jsonRequest)) // JSON 형식의 데이터를 본문에 추가
73+
.andReturn();
74+
75+
// then
76+
Assertions.assertThat(response.getResponse().getStatus()).isEqualTo(HttpStatus.CREATED.value());
77+
}
78+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
profiles:
6+
group:
7+
"test": "db, secret, s3, redis, email, firebase, security"
8+
default: test
9+
10+
servlet:
11+
multipart:
12+
max-file-size: 20MB
13+
max-request-size: 20MB

core/core-infra-db/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ dependencies {
88
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
99
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
1010
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
11+
12+
runtimeOnly 'com.h2database:h2'
1113
}
1214

1315
bootJar { enabled = false }

core/core-infra-db/src/main/resources/application-db.yml

+20
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ spring:
3737
ddl-auto: update
3838
open-in-view: false
3939
show-sql: true
40+
41+
---
42+
spring.config.activate.on-profile: test
43+
44+
spring:
45+
datasource:
46+
driver-class-name: org.h2.Driver
47+
url: jdbc:h2:mem:sponus;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
48+
username: sa
49+
password:
50+
jpa:
51+
hibernate:
52+
ddl-auto: create-drop
53+
open-in-view: false
54+
show-sql: true
55+
56+
data:
57+
redis:
58+
host: localhost
59+
port: 6379

core/core-infra-security/src/main/java/com/sponus/coreinfrasecurity/jwt/filter/CustomLoginFilter.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,22 @@ public Authentication attemptAuthentication(
5858
String password = (String)requestBody.get("password");
5959
String fcmToken = (String)requestBody.get("fcmToken");
6060

61-
redisUtil.saveAsValue(
62-
email + "_fcm_token",
63-
fcmToken,
64-
999999999L,
65-
TimeUnit.MILLISECONDS
66-
);
61+
if (fcmToken != null && !fcmToken.isEmpty()) {
62+
redisUtil.saveAsValue(
63+
email + "_fcm_token",
64+
fcmToken,
65+
999999999L,
66+
TimeUnit.MILLISECONDS
67+
);
68+
} else {
69+
// TODO FcmToken이 없는 경우 처리
70+
redisUtil.saveAsValue(
71+
email + "_fcm_token",
72+
"fcmTokenFcmTokenFcmTokenFcmToken",
73+
999999999L,
74+
TimeUnit.MILLISECONDS
75+
);
76+
}
6777

6878
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(email, password, null);
6979

docker-compose-test.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3'
2+
3+
services:
4+
redis:
5+
image: redis:latest
6+
container_name: sponus-redis
7+
ports:
8+
- "6379:6379"

0 commit comments

Comments
 (0)