Search in sources :

Example 41 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class UserRoleRestControllerTest method testAddRoleToUser.

@Test
public void testAddRoleToUser() throws Exception {
    final UserEntity user = new UserEntity().setId("123").setUsername("user123").setPassword("123456");
    when(roleService.addRoleToUser("user123", "role123")).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(put("/api/users/user123/roles").content("{\"role\": \"role123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-role-create"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(userMapper.toUserData(user)));
    verify(roleService).addRoleToUser("user123", "role123");
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 42 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class PostRestControllerTest method init.

@Before
public void init() {
    when(userDetailsService.loadUserByUsername(USERNAME)).thenReturn(new UserEntity().setId(USER_ID).setUsername(USERNAME));
    mockMvc = MockMvcBuilders.standaloneSetup(new PostRestController(postService, postReactionService)).setCustomArgumentResolvers(new UserArgumentResolver(userDetailsService), new AuthenticationPrincipalArgumentResolver(), new PageableHandlerMethodArgumentResolver()).apply(documentationConfiguration(restDocumentation)).build();
    Authentication authentication = new UsernamePasswordAuthenticationToken(new UserEntity().setId(USER_ID).setUsername(USERNAME), "1234");
    SecurityContextHolder.getContext().setAuthentication(authentication);
    PostServiceTest.DUMMY_POST.setCreatedDate(new Date());
    PostServiceTest.DUMMY_POST.setLastModifiedDate(new Date());
    PostServiceTest.DUMMY_POST2.setCreatedDate(new Date());
    PostServiceTest.DUMMY_POST2.setLastModifiedDate(new Date());
}
Also used : PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) Authentication(org.springframework.security.core.Authentication) UserArgumentResolver(org.codenergic.theskeleton.core.web.UserArgumentResolver) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthenticationPrincipalArgumentResolver(org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver) UserEntity(org.codenergic.theskeleton.user.UserEntity) Date(java.util.Date) Before(org.junit.Before)

Example 43 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class UserFollowerRestControllerTest method testFindUserFollowings.

@Test
public void testFindUserFollowings() throws Exception {
    when(userFollowerService.findUserFollowings(eq(USER_ID), any())).thenReturn(new PageImpl<>(Collections.singletonList(new UserEntity().setId(USERNAME).setUsername(USER_ID).setPictureUrl(USERNAME))));
    MockHttpServletResponse response = mockMvc.perform(get("/api/users/" + USERNAME + "/followings").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    Page<UserFollowerRestData> result = new PageImpl<>(Collections.singletonList(UserFollowerRestData.builder().followingUsername(USER_ID).followingPictureUrl(USERNAME).build()));
    assertThat(response.getContentAsString()).isEqualTo(objectMapper.writeValueAsString(result));
    verify(userFollowerService).findUserFollowings(eq(USER_ID), any());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 44 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class UserFollowerRestControllerTest method testFindUserFollowers.

@Test
public void testFindUserFollowers() throws Exception {
    when(userFollowerService.findUserFollowers(eq(USER_ID), any())).thenReturn(new PageImpl<>(Collections.singletonList(new UserEntity().setId(USERNAME).setUsername(USER_ID).setPictureUrl(USERNAME))));
    MockHttpServletResponse response = mockMvc.perform(get("/api/users/" + USERNAME + "/followers").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    Page<UserFollowerRestData> result = new PageImpl<>(Collections.singletonList(UserFollowerRestData.builder().followerUsername(USER_ID).followerPictureUrl(USERNAME).build()));
    assertThat(response.getContentAsString()).isEqualTo(objectMapper.writeValueAsString(result));
    verify(userFollowerService).findUserFollowers(eq(USER_ID), any());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 45 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class UserFollowerRestControllerTest method testFollowUser.

@Test
public void testFollowUser() throws Exception {
    when(userFollowerService.followUser(USER_ID, USER_ID)).thenReturn(new UserFollowerEntity().setUser(new UserEntity().setUsername(USER_ID).setPictureUrl(USERNAME)).setFollower(new UserEntity().setUsername(USERNAME).setPictureUrl(USER_ID)));
    MockHttpServletResponse response = mockMvc.perform(put("/api/users/" + USERNAME + "/followers").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    UserFollowerRestData result = UserFollowerRestData.builder().followingUsername(USER_ID).followingPictureUrl(USERNAME).followerUsername(USERNAME).followerPictureUrl(USER_ID).build();
    assertThat(response.getContentAsString()).isEqualTo(objectMapper.writeValueAsString(result));
    verify(userFollowerService).followUser(USER_ID, USER_ID);
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

UserEntity (org.codenergic.theskeleton.user.UserEntity)48 Test (org.junit.Test)30 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)14 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)10 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)8 Transactional (org.springframework.transaction.annotation.Transactional)7 WithMockUser (org.springframework.security.test.context.support.WithMockUser)6 Before (org.junit.Before)5 Authentication (org.springframework.security.core.Authentication)5 PageableHandlerMethodArgumentResolver (org.springframework.data.web.PageableHandlerMethodArgumentResolver)4 AuthenticationPrincipalArgumentResolver (org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver)4 InputStream (java.io.InputStream)3 Date (java.util.Date)3 UserArgumentResolver (org.codenergic.theskeleton.core.web.UserArgumentResolver)3 PageImpl (org.springframework.data.domain.PageImpl)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 OAuth2ClientEntity (org.codenergic.theskeleton.client.OAuth2ClientEntity)2 EmailServiceTest (org.codenergic.theskeleton.core.mail.EmailServiceTest)2 RegistrationException (org.codenergic.theskeleton.registration.RegistrationException)2