Search in sources :

Example 36 with UserEntity

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

the class RoleServiceTest method testFindRolesByUserUsername.

@Test
public void testFindRolesByUserUsername() {
    Set<UserRoleEntity> dbResult = new HashSet<>(Collections.singletonList(new UserRoleEntity().setRole(new RoleEntity().setCode("role")).setUser(new UserEntity())));
    when(userRoleRepository.findByUserUsername("user")).thenReturn(dbResult);
    Set<RoleEntity> result = roleService.findRolesByUserUsername("user");
    assertThat(result.size()).isEqualTo(1);
    assertThat(result.iterator().next()).isEqualTo(dbResult.iterator().next().getRole());
    verify(userRoleRepository).findByUserUsername("user");
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with UserEntity

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

the class TokenStoreServiceTest method testSendEmailActivation.

@Test
public void testSendEmailActivation() {
    UserEntity user = new UserEntity().setId(UUID.randomUUID().toString()).setUsername("user").setPassword(passwordEncoder.encode("user")).setEmail("user@codenergic.org");
    user.setLastModifiedDate(new Date());
    TokenStoreRestData activationTokenData = tokenStoreService.sendTokenNotification(TokenStoreType.USER_ACTIVATION, user);
    TokenStoreRestData changePasswordTokenData = tokenStoreService.sendTokenNotification(TokenStoreType.CHANGE_PASSWORD, user);
    assertThat(activationTokenData.getSignedToken()).isNotBlank();
    assertThat(changePasswordTokenData.getSignedToken()).isNotBlank();
    assertThat(!activationTokenData.isExpired() && !changePasswordTokenData.isExpired()).isTrue();
    assertThat(greenMail.waitForIncomingEmail(1000, 2)).isTrue();
    MimeMessage messageActivation = greenMail.getReceivedMessages()[0];
    assertThat(GreenMailUtil.getBody(messageActivation)).contains("activate?at=");
    MimeMessage messageChangePassword = greenMail.getReceivedMessages()[1];
    assertThat(GreenMailUtil.getBody(messageChangePassword)).contains("update?rt=");
    when(userRepository.findById(user.getId())).thenReturn(Optional.of(user));
    TokenStoreRestData verifiedData = tokenStoreService.findAndVerifyToken(changePasswordTokenData.getSignedToken());
    assertThat(verifiedData.getUuid()).isEqualTo(changePasswordTokenData.getUuid());
    verify(userRepository).findById(user.getId());
    user.setEmail("@codenergic.org");
    assertThatThrownBy(() -> tokenStoreService.sendTokenNotification(TokenStoreType.USER_ACTIVATION, user)).isInstanceOf(RegistrationException.class);
    assertThatThrownBy(() -> tokenStoreService.sendTokenNotification(TokenStoreType.CHANGE_PASSWORD, user)).isInstanceOf(RegistrationException.class);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) UserEntity(org.codenergic.theskeleton.user.UserEntity) Date(java.util.Date) EmailServiceTest(org.codenergic.theskeleton.core.mail.EmailServiceTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 38 with UserEntity

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

the class PostReactionServiceTest method testFindUserByPostReaction.

@Test
public void testFindUserByPostReaction() throws Exception {
    List<PostReactionEntity> reactions = Collections.singletonList(new PostReactionEntity().setUser(new UserEntity().setId(USER_ID)));
    when(postReactionRepository.findByPostIdAndReactionType(eq(POST_ID), eq(DISLIKE), any())).thenReturn(new PageImpl<>(reactions));
    Page<UserEntity> users = postReactionService.findUserByPostReaction(POST_ID, DISLIKE, null);
    assertThat(users).hasSize(1);
    assertThat(users).first().hasFieldOrPropertyWithValue("id", USER_ID);
    verify(postReactionRepository).findByPostIdAndReactionType(eq(POST_ID), eq(DISLIKE), any());
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) Test(org.junit.Test)

Example 39 with UserEntity

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

the class RegistrationRestControllerTest method testRegister.

@Test
public void testRegister() throws Exception {
    final RegistrationForm form = new RegistrationForm().setUsername("test123").setPassword("12345678").setEmail("test@email.com");
    final UserEntity user = new UserEntity().setId("123");
    when(registrationService.registerUser(any())).thenReturn(user);
    MockHttpServletRequestBuilder request = post("/api/register").content("{\"username\": \"test123\", \"password\": \"12345678\", " + "\"email\": \"test@email.com\"}").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andExpect(status().isOk()).andDo(document("registration")).andReturn().getResponse();
    assertThat(response.getContentAsString()).isEqualTo(objectMapper.writeValueAsString(form));
    verify(registrationService).registerUser(any());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) 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 40 with UserEntity

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

the class UserRoleRestControllerTest method testRemoveRoleFromUser.

@Test
public void testRemoveRoleFromUser() throws Exception {
    final UserEntity user = new UserEntity().setId("user123");
    when(roleService.removeRoleFromUser("user123", "role123")).thenReturn(user);
    ResultActions resultActions = mockMvc.perform(delete("/api/users/user123/roles").content("{\"role\": \"role123\"}").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andDo(document("user-role-delete"));
    MockHttpServletResponse response = resultActions.andReturn().getResponse();
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(userMapper.toUserData(user)));
    verify(roleService).removeRoleFromUser("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)

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