forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAuthenticationEntryPointTest.java
More file actions
52 lines (41 loc) · 1.99 KB
/
CustomAuthenticationEntryPointTest.java
File metadata and controls
52 lines (41 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.solidconnection.common.exception;
import com.example.solidconnection.common.response.ErrorResponse;
import com.example.solidconnection.support.TestContainerSpringBootTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import java.io.IOException;
import static com.example.solidconnection.common.exception.ErrorCode.AUTHENTICATION_FAILED;
import static org.assertj.core.api.Assertions.assertThat;
@TestContainerSpringBootTest
@DisplayName("커스텀 인증 예외 처리 테스트")
class CustomAuthenticationEntryPointTest {
@Autowired
private CustomAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private ObjectMapper objectMapper;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
@BeforeEach
void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
@Test
void 인증되지_않은_사용자_접근시_401_예외가_발생한다() throws IOException {
// given
AuthenticationException authException = new AuthenticationServiceException(AUTHENTICATION_FAILED.getMessage());
// when
authenticationEntryPoint.commence(request, response, authException);
// then
ErrorResponse errorResponse = objectMapper.readValue(response.getContentAsString(), ErrorResponse.class);
assertThat(response.getStatus()).isEqualTo(AUTHENTICATION_FAILED.getCode());
assertThat(errorResponse.message()).isEqualTo(AUTHENTICATION_FAILED.getMessage());
}
}