33import static io .f1 .backend .domain .user .constants .SessionKeys .USER ;
44
55import static org .assertj .core .api .Assertions .assertThat ;
6+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
67import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
78import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
89
910import com .github .database .rider .core .api .dataset .DataSet ;
1011
1112import io .f1 .backend .domain .user .dto .AuthenticationUser ;
13+ import io .f1 .backend .domain .user .dto .UserPrincipal ;
1214import io .f1 .backend .global .template .BrowserTestTemplate ;
1315
1416import org .junit .jupiter .api .DisplayName ;
1517import org .junit .jupiter .api .Test ;
1618import org .springframework .mock .web .MockHttpSession ;
19+ import org .springframework .security .core .context .SecurityContext ;
1720import org .springframework .test .web .servlet .ResultActions ;
1821
1922public class TestUserBrowserTest extends BrowserTestTemplate {
2023
2124 @ Test
2225 @ DataSet ("datasets/user.yml" )
23- @ DisplayName ("테스트 유저가 로그인하면 세션에 유저 정보가 저장된다" )
26+ @ DisplayName ("테스트 유저가 로그인하면 세션에 SecurityContext가 저장된다" )
2427 void testUserLogin () throws Exception {
2528 // given
2629 MockHttpSession session = new MockHttpSession ();
@@ -30,11 +33,30 @@ void testUserLogin() throws Exception {
3033
3134 // then
3235 result .andExpect (status ().isOk ());
33- assertThat (session .getAttribute (USER )).isNotNull ();
36+ assertThat (session .getAttribute ("SPRING_SECURITY_CONTEXT" )).isNotNull ();
37+
38+ SecurityContext context = (SecurityContext ) session .getAttribute ("SPRING_SECURITY_CONTEXT" );
39+ assertThat (context .getAuthentication ().getPrincipal ()).isInstanceOf (UserPrincipal .class );
40+
41+ UserPrincipal userPrincipal = (UserPrincipal ) context .getAuthentication ().getPrincipal ();
42+ assertThat (userPrincipal .getUserId ()).isEqualTo (1L );
43+ assertThat (userPrincipal .getUserNickname ()).isEqualTo ("USER1" );
44+ }
45+
46+ @ Test
47+ @ DataSet ("datasets/stat/one-user-stat.yml" )
48+ @ DisplayName ("테스트 유저가 로그인하면 마이페이지에 접근이 가능하다" )
49+ void testUserLoginSecurityContext () throws Exception {
50+ // given
51+ MockHttpSession session = new MockHttpSession ();
52+
53+ // when
54+ ResultActions beforeLogin = mockMvc .perform (get ("/user/me" ).session (session ));
55+ mockMvc .perform (post ("/user/test/login/1" ).session (session ));
56+ ResultActions afterLogin = mockMvc .perform (get ("/user/me" ).session (session ));
3457
35- AuthenticationUser authenticationUser = (AuthenticationUser ) session .getAttribute (USER );
36- assertThat (authenticationUser .userId ()).isEqualTo (1L );
37- assertThat (authenticationUser .nickname ()).isEqualTo ("USER1" );
38- assertThat (authenticationUser .providerId ()).isEqualTo ("kakao1" );
58+ // then
59+ beforeLogin .andExpect (status ().isUnauthorized ());
60+ afterLogin .andExpect (status ().isOk ());
3961 }
4062}
0 commit comments