org.springframework.security.test.context.support.WithMockUser Java Examples

The following examples show how to use org.springframework.security.test.context.support.WithMockUser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AccountResourceIntTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-long");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, RandomStringUtils.random(101)))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #2
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password"))))
        .andExpect(status().isOk());

    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
 
Example #3
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-long");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    String newPassword = RandomStringUtils.random(ManagedUserVM.PASSWORD_MAX_LENGTH + 1);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #4
Source File: AccountResourceIntTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-wrong-existing-password")
public void testChangePasswordWrongExistingPassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-wrong-existing-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8)
    .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1"+currentPassword,"new password"))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
    assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
}
 
Example #5
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-small")
public void testChangePasswordTooSmall() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-small");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    String newPassword = RandomStringUtils.random(ManagedUserVM.PASSWORD_MIN_LENGTH - 1);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-small").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #6
Source File: AccountResourceIntTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword,"new password"))))
            .andExpect(status().isOk());

    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
 
Example #7
Source File: AccountResourceIntTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password"))))
        .andExpect(status().isOk());

    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
 
Example #8
Source File: AccountResourceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password")
public void testChangePassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password"))))
        .andExpect(status().isOk());

    User updatedUser = userRepository.findOneByLogin("change-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
}
 
Example #9
Source File: AccountResourceIntTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-wrong-existing-password")
public void testChangePasswordWrongExistingPassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-wrong-existing-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1"+currentPassword, "new password"))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
    assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
}
 
Example #10
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-wrong-existing-password")
public void testChangePasswordWrongExistingPassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-wrong-existing-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1"+currentPassword, "new password"))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
    assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
}
 
Example #11
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-empty")
public void testChangePasswordEmpty() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-empty");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, ""))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-empty").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #12
Source File: AccountResourceIntTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-long");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword,RandomStringUtils.random(101)))))
            .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #13
Source File: UserControllerTest.java    From java-master with Apache License 2.0 6 votes vote down vote up
@Test
@WithMockUser(username = "1050106266",
        password = "123456",
        authorities = "ROLE_ADMIN")
public void testChangeUserStatus() throws Exception {
    ChangeUserStatusReqVo reqVo = new ChangeUserStatusReqVo();
    reqVo.setUsername("1050106158");
    reqVo.setEnabled(false);
    System.out.println(objectMapper.writeValueAsString(reqVo));
    mockMvc.perform(MockMvcRequestBuilders.post("/admin/user/changeUserStatus")
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(reqVo))
            .accept(MediaType.APPLICATION_JSON))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}
 
Example #14
Source File: ExamControllerTest.java    From java-master with Apache License 2.0 6 votes vote down vote up
@Test
@WithMockUser(username = "1050106266",
        password = "123456",
        authorities = "ROLE_ADMIN")
public void testGetExamList() throws Exception {
    Map<String, Object> reqVo = new HashMap<>();
    reqVo.put("examType", 1);
    System.out.println(objectMapper.writeValueAsString(reqVo));
    mockMvc.perform(MockMvcRequestBuilders.post("/json/exam/getExamList")
            .session(session)
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(reqVo))
            .accept(MediaType.APPLICATION_JSON))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}
 
Example #15
Source File: AccountResourceIntTest.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("invalidate-session")
public void testInvalidateSession() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("invalidate-session");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    PersistentToken token = new PersistentToken();
    token.setSeries("invalidate-session");
    token.setUser(user);
    token.setTokenValue("invalidate-data");
    token.setTokenDate(LocalDate.of(2017, 3, 23));
    token.setIpAddress("127.0.0.1");
    token.setUserAgent("Test agent");
    persistentTokenRepository.saveAndFlush(token);

    assertThat(persistentTokenRepository.findByUser(user)).hasSize(1);

    restMvc.perform(delete("/api/account/sessions/invalidate-session"))
        .andExpect(status().isOk());

    assertThat(persistentTokenRepository.findByUser(user)).isEmpty();
}
 
Example #16
Source File: AccountResourceIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-small")
public void testChangePasswordTooSmall() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("change-password-too-small");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").content("new"))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-small").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #17
Source File: AccountResourceIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("change-password-too-long");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").content(RandomStringUtils.random(101)))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #18
Source File: AccountResourceIntTest.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-wrong-existing-password")
public void testChangePasswordWrongExistingPassword() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-wrong-existing-password");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password").contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1" + currentPassword, "new password"))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password").orElse(null);
    assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
    assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
}
 
Example #19
Source File: AccountResourceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("current-sessions")
public void testGetCurrentSessions() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("current-sessions");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    PersistentToken token = new PersistentToken();
    token.setSeries("current-sessions");
    token.setUser(user);
    token.setTokenValue("current-session-data");
    token.setTokenDate(LocalDate.of(2017, 3, 23));
    token.setIpAddress("127.0.0.1");
    token.setUserAgent("Test agent");
    persistentTokenRepository.saveAndFlush(token);

    restMvc.perform(get("/api/account/sessions"))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.[*].series").value(hasItem(token.getSeries())))
        .andExpect(jsonPath("$.[*].ipAddress").value(hasItem(token.getIpAddress())))
        .andExpect(jsonPath("$.[*].userAgent").value(hasItem(token.getUserAgent())))
        .andExpect(jsonPath("$.[*].tokenDate").value(hasItem(token.getTokenDate().toString())));
}
 
Example #20
Source File: AccountResourceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("invalidate-session")
public void testInvalidateSession() throws Exception {
    User user = new User();
    user.setPassword(RandomStringUtils.random(60));
    user.setLogin("invalidate-session");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    PersistentToken token = new PersistentToken();
    token.setSeries("invalidate-session");
    token.setUser(user);
    token.setTokenValue("invalidate-data");
    token.setTokenDate(LocalDate.of(2017, 3, 23));
    token.setIpAddress("127.0.0.1");
    token.setUserAgent("Test agent");
    persistentTokenRepository.saveAndFlush(token);

    assertThat(persistentTokenRepository.findByUser(user)).hasSize(1);

    restMvc.perform(delete("/api/account/sessions/invalidate-session"))
        .andExpect(status().isOk());

    assertThat(persistentTokenRepository.findByUser(user)).isEmpty();
}
 
Example #21
Source File: UserControllerTest.java    From java-master with Apache License 2.0 6 votes vote down vote up
@Test
@WithMockUser(username = "1050106266",
        password = "123456",
        authorities = "ROLE_ADMIN")
public void testUpdatePassword() throws Exception {
    UpdatePasswordReqVo reqVo = new UpdatePasswordReqVo();
    reqVo.setUsername("1050106158");
    reqVo.setNewPassword("654321");
    System.out.println(objectMapper.writeValueAsString(reqVo));
    mockMvc.perform(MockMvcRequestBuilders.post("/admin/user/updatePassword")
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(reqVo))
            .accept(MediaType.APPLICATION_JSON))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}
 
Example #22
Source File: UserControllerTest.java    From java-master with Apache License 2.0 6 votes vote down vote up
@Test
@WithMockUser(username = "1050106266",
        password = "123456",
        authorities = "ROLE_ADMIN")
public void testFindUsers() throws Exception {
    FindUsersReqVo reqVo = new FindUsersReqVo();
    SysUser user = new SysUser();
    user.setEnabled(true);
    reqVo.setSysUser(user);
    Page page = new Page();
    page.setPageNum(0);
    page.setPageSize(10);
    reqVo.setPage(page);
    System.out.println(objectMapper.writeValueAsString(reqVo));
    mockMvc.perform(MockMvcRequestBuilders.post("/admin/user/findUsers")
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(reqVo))
            .accept(MediaType.APPLICATION_JSON))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}
 
Example #23
Source File: AccountResourceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
@Transactional
@WithMockUser("change-password-too-long")
public void testChangePasswordTooLong() throws Exception {
    User user = new User();
    String currentPassword = RandomStringUtils.random(60);
    user.setPassword(passwordEncoder.encode(currentPassword));
    user.setLogin("change-password-too-long");
    user.setEmail("[email protected]");
    userRepository.saveAndFlush(user);

    restMvc.perform(post("/api/account/change-password")
        .contentType(TestUtil.APPLICATION_JSON_UTF8)
        .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, RandomStringUtils.random(101)))))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("change-password-too-long").orElse(null);
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
}
 
Example #24
Source File: AccountResourceIT.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Test
@Transactional
@WithMockUser("save-existing-email")
public void testSaveExistingEmail() throws Exception {
    User user = new User();
    user.setLogin("save-existing-email");
    user.setEmail("[email protected]");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);

    userRepository.saveAndFlush(user);

    User anotherUser = new User();
    anotherUser.setLogin("save-existing-email2");
    anotherUser.setEmail("[email protected]");
    anotherUser.setPassword(RandomStringUtils.random(60));
    anotherUser.setActivated(true);

    userRepository.saveAndFlush(anotherUser);

    UserDTO userDTO = new UserDTO();
    userDTO.setLogin("not-used");
    userDTO.setFirstName("firstname");
    userDTO.setLastName("lastname");
    userDTO.setEmail("[email protected]");
    userDTO.setActivated(false);
    userDTO.setImageUrl("http://placehold.it/50x50");
    userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

    restMvc.perform(
        post("/api/account")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(userDTO)))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("save-existing-email").orElse(null);
    assertThat(updatedUser.getEmail()).isEqualTo("[email protected]");
}
 
Example #25
Source File: AccountResourceIntTest.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
@Test
@Transactional
@WithMockUser("save-account")
public void testSaveAccount() throws Exception {
    User user = new User();
    user.setLogin("save-account");
    user.setEmail("[email protected]");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);

    userRepository.saveAndFlush(user);

    UserDTO userDTO = new UserDTO();
    userDTO.setLogin("not-used");
    userDTO.setFirstName("firstname");
    userDTO.setLastName("lastname");
    userDTO.setEmail("[email protected]");
    userDTO.setActivated(false);
    userDTO.setImageUrl("http://placehold.it/50x50");
    userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

    restMvc.perform(
        post("/api/account")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(userDTO)))
        .andExpect(status().isOk());

    User updatedUser = userRepository.findOneByLogin(user.getLogin()).orElse(null);
    assertThat(updatedUser.getFirstName()).isEqualTo(userDTO.getFirstName());
    assertThat(updatedUser.getLastName()).isEqualTo(userDTO.getLastName());
    assertThat(updatedUser.getEmail()).isEqualTo(userDTO.getEmail());
    assertThat(updatedUser.getLangKey()).isEqualTo(userDTO.getLangKey());
    assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
    assertThat(updatedUser.getImageUrl()).isEqualTo(userDTO.getImageUrl());
    assertThat(updatedUser.getActivated()).isEqualTo(true);
    assertThat(updatedUser.getAuthorities()).isEmpty();
}
 
Example #26
Source File: MethodProtectedRestControllerTest.java    From spring-react-boilerplate with MIT License 5 votes vote down vote up
@Test
@WithMockUser(roles = "USER")
public void shouldGetForbiddenWithUserRole() throws Exception{
    this.mvc
            .perform(get("/protected"))
            .andExpect(status().isForbidden());
}
 
Example #27
Source File: IntegrationResourceTest.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Test
@WithMockUser(username="tester", authorities={"CREATE_ACCESSTOKEN"})
public void testSavingDeveloperWithDefaultValues() throws Exception {        
     Mockito.when(developerService.save(Mockito.any(DeveloperDTO.class))).thenReturn(new Developer());
     
     mockMvc.perform(MockMvcRequestBuilders.post(ConstantsPath.PATH_INTEGRATION_RESOURCES+"/developer/callback")
            .content("{\"name\":\"markmark\",\"email\":\"[email protected]\"}")
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResultMatchers.status().isOk());
}
 
Example #28
Source File: AuditSecurityConfigurationTest.java    From spring-security-samples with MIT License 5 votes vote down vote up
@Test
@Transactional
@WithMockUser("test-user")
void testSaveWhileLoggedInAsExistingUser() {
	// Create and save a blogpost
	Blogpost blogpost = new Blogpost();
	blogpost.setTitle("Auditing Spring Data Entities");
	Long id = blogpostRepo.save(blogpost).getId();

	// Verify that author was set by JPA
	Blogpost found = em.find(Blogpost.class, id);
	assertThat(found.getCreatedBy()).hasValueSatisfying(a -> assertThat(a.getName()).isEqualTo("test-user"));
}
 
Example #29
Source File: AccountResourceIntTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
@Transactional
@WithMockUser("save-existing-email")
public void testSaveExistingEmail() throws Exception {
    User user = new User();
    user.setLogin("save-existing-email");
    user.setEmail("[email protected]");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);

    userRepository.saveAndFlush(user);

    User anotherUser = new User();
    anotherUser.setLogin("save-existing-email2");
    anotherUser.setEmail("[email protected]");
    anotherUser.setPassword(RandomStringUtils.random(60));
    anotherUser.setActivated(true);

    userRepository.saveAndFlush(anotherUser);

    UserDTO userDTO = new UserDTO();
    userDTO.setLogin("not-used");
    userDTO.setFullName("fullname");
    userDTO.setPhone("phone");
    userDTO.setEmail("[email protected]");
    userDTO.setActivated(false);
    userDTO.setImageUrl("http://placehold.it/50x50");
    userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

    restMvc.perform(
        post("/api/account")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(userDTO)))
        .andExpect(status().isBadRequest());

    User updatedUser = userRepository.findOneByLogin("save-existing-email").orElse(null);
    assertThat(updatedUser.getEmail()).isEqualTo("[email protected]");
}
 
Example #30
Source File: PreferencesRepositoryIT.java    From spring-security-samples with MIT License 5 votes vote down vote up
@Test
@WithMockUser("eve")
void testEveNotAllowedToSaveAlicesPreferences() {
	// Try to save preferences for Alice
	Preferences lightpreferences = new Preferences();
	lightpreferences.setUser(userAlice);
	lightpreferences.setDarkMode(true);
	Assertions.assertThrows(AccessDeniedException.class, () -> preferencesRepo.save(lightpreferences));
}