Skip to content

Commit 05337e9

Browse files
committed
Test/Chore : 테스트 추가, 업로드요청 모두 허용하던부분 수정
1 parent a99a6bc commit 05337e9

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

back/src/main/java/com/back/global/security/SecurityConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3030
.requestMatchers("/auth/**").permitAll()
3131
.requestMatchers("/actuator/**").permitAll()
3232
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**").permitAll()
33-
.requestMatchers("/videos/upload").permitAll()
3433
.requestMatchers("/ws-chat/**").permitAll()
3534
.anyRequest().authenticated()
3635
)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.back.domain.file.video.controller;
2+
3+
import com.back.domain.file.video.dto.service.PresignedUrlResponse;
4+
import com.back.domain.file.video.service.FileManager;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mockito;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.http.MediaType;
12+
import org.springframework.security.test.context.support.WithMockUser;
13+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
16+
import java.net.URL;
17+
import java.time.LocalDateTime;
18+
19+
import static org.mockito.ArgumentMatchers.anyString;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
23+
24+
@SpringBootTest
25+
@AutoConfigureMockMvc
26+
class VideoControllerTest {
27+
28+
@Autowired
29+
private MockMvc mockMvc;
30+
31+
@MockitoBean
32+
private FileManager fileManager;
33+
34+
@Test
35+
@DisplayName("업로드 URL 요청 - 성공")
36+
@WithMockUser(roles = "ADMIN")
37+
void testGetUploadUrl() throws Exception {
38+
URL mockUrl = new URL("https://bucket.s3.amazonaws.com/123e4567-e89b-12d3-a456-426614174000");
39+
LocalDateTime expiresAt = LocalDateTime.now().plusMinutes(10);
40+
41+
Mockito.when(fileManager.getUploadUrl(anyString()))
42+
.thenReturn(new PresignedUrlResponse(mockUrl, expiresAt));
43+
44+
mockMvc.perform(get("/videos/upload")
45+
.param("filename", "test.mp4")
46+
.contentType(MediaType.APPLICATION_JSON))
47+
.andExpect(status().isOk())
48+
.andExpect(jsonPath("$.resultCode").value("200"))
49+
.andExpect(jsonPath("$.msg").value("업로드용 URL 요청완료"))
50+
.andExpect(jsonPath("$.data.url").value(mockUrl.toString()))
51+
.andExpect(jsonPath("$.data.uuid").value("123e4567-e89b-12d3-a456-426614174000"));
52+
}
53+
54+
@Test
55+
@DisplayName("업로드 URL 요청 - 권한 부족")
56+
@WithMockUser(roles = "USER")
57+
// ADMIN 아님
58+
void testGetUploadUrl_Unauthorized() throws Exception {
59+
mockMvc.perform(get("/videos/upload")
60+
.param("filename", "test.mp4")
61+
.contentType(MediaType.APPLICATION_JSON))
62+
.andExpect(status().isForbidden()); // 403 권한 부족
63+
}
64+
65+
@Test
66+
@DisplayName("업로드 URL 요청 - 파일명에 확장자가 없는 경우")
67+
@WithMockUser(roles = "ADMIN")
68+
void testGetUploadUrl_NoExtension() throws Exception {
69+
Mockito.when(fileManager.getUploadUrl(anyString()))
70+
.thenThrow(new com.back.global.exception.ServiceException("400", "지원하지 않는 동영상 파일 형식입니다: testfile"));
71+
72+
mockMvc.perform(get("/videos/upload")
73+
.param("filename", "testfile")
74+
.contentType(MediaType.APPLICATION_JSON))
75+
.andExpect(status().isBadRequest())
76+
.andExpect(jsonPath("$.resultCode").value("400"))
77+
.andExpect(jsonPath("$.msg").value("지원하지 않는 동영상 파일 형식입니다: testfile"));
78+
}
79+
80+
@Test
81+
@DisplayName("다운로드 URL 요청 - 성공")
82+
@WithMockUser
83+
void testGetDownloadUrls() throws Exception {
84+
URL mockUrl = new URL("https://bucket.s3.amazonaws.com/test.mp4");
85+
LocalDateTime expiresAt = LocalDateTime.now().plusMinutes(10);
86+
87+
Mockito.when(fileManager.getDownloadUrl(anyString(), anyString()))
88+
.thenReturn(new PresignedUrlResponse(mockUrl, expiresAt));
89+
90+
mockMvc.perform(get("/videos/download")
91+
.param("uuid", "123e4567-e89b-12d3-a456-426614174000")
92+
.param("resolution", "1080p")
93+
.contentType(MediaType.APPLICATION_JSON))
94+
.andExpect(status().isOk())
95+
.andExpect(jsonPath("$.resultCode").value("200"))
96+
.andExpect(jsonPath("$.msg").value("다운로드용 URL 요청완료"))
97+
.andExpect(jsonPath("$.data.url").value(mockUrl.toString()));
98+
}
99+
100+
}

0 commit comments

Comments
 (0)