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

The following examples show how to use org.springframework.security.test.context.support.WithMockUser. These examples are extracted from open source projects. 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
@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 2
@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 3
Source Project: Spring-5.0-Projects   Source File: AccountResourceIntTest.java    License: 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 4
@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 5
Source Project: java-master   Source File: ExamControllerTest.java    License: 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 6
Source Project: java-master   Source File: UserControllerTest.java    License: 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 7
Source Project: java-master   Source File: UserControllerTest.java    License: 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 8
Source Project: java-master   Source File: UserControllerTest.java    License: 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 9
Source Project: Spring-5.0-Projects   Source File: AccountResourceIntTest.java    License: 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 10
@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 11
Source Project: Spring-5.0-Projects   Source File: AccountResourceIntTest.java    License: 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 12
Source Project: Spring-5.0-Projects   Source File: AccountResourceIntTest.java    License: 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
@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 14
@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 15
Source Project: alchemy   Source File: AccountResourceIT.java    License: 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 16
Source Project: alchemy   Source File: AccountResourceIT.java    License: 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 17
Source Project: alchemy   Source File: AccountResourceIT.java    License: 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 18
Source Project: alchemy   Source File: AccountResourceIT.java    License: 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 19
Source Project: alchemy   Source File: AccountResourceIT.java    License: 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 20
Source Project: TeamDojo   Source File: AccountResourceIntTest.java    License: 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 21
Source Project: cubeai   Source File: AccountResourceIntTest.java    License: 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 22
Source Project: cubeai   Source File: AccountResourceIntTest.java    License: 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 23
Source Project: TeamDojo   Source File: AccountResourceIntTest.java    License: 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 24
Source Project: heimdall   Source File: AccessTokenResourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@WithMockUser(username="tester", authorities={"CREATE_ACCESSTOKEN"})
public void testSavingAccessTokenWithoutDefaultValues() throws Exception {
     
     AccessToken recoverAt = new AccessToken();
     recoverAt.setId(10L);
     
     Mockito.when(service.save(Mockito.any(AccessTokenPersist.class))).thenReturn(recoverAt);
     
     mockMvc.perform(MockMvcRequestBuilders.post(ConstantsPath.PATH_ACCESS_TOKENS, 10L)
            .content("{\"code\":\"[email protected]!##1212\",\"app\":{\"id\":10}, \"plans\": [{\"id\": 5}]}")
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResultMatchers.status().isCreated());
}
 
Example 25
@Test
@WithMockUser(username = "test")
public void homeWithAuth() throws Exception {
  Mockito.when(crowdAuthenticationManager.getUserPassword()).thenReturn("logged_in");
  defaultController.setCustomAuthenticationManager(crowdAuthenticationManager);
  mockMvc
      .perform(get("/home"))
      .andExpect(MockMvcResultMatchers.status().isOk())
      .andDo(MockMvcResultHandlers.print());
}
 
Example 26
Source Project: oauth2-blog   Source File: HelloMockUserTest.java    License: MIT License 5 votes vote down vote up
@Test
@WithMockUser(username = "[email protected]")
public void shouldAllowAnyAuthenticatedUser() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/api/hello?name=Seb")
            .accept(MediaType.ALL))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.greetings", is("Welcome Seb ([email protected])!")));
}
 
Example 27
Source Project: gaia   Source File: ModuleRestControllerIT.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@Test
@WithMockUser("Mary J")
void findAllModules_shouldReturnLimitedModules_forStandardUsers() throws Exception {
    mockMvc.perform(get("/api/modules"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$", hasSize(1)))
            .andExpect(jsonPath("$[0]name", is("terraform-docker-mongo-limited")))
            .andExpect(jsonPath("$[0]authorizedTeams..id", contains("Not Ze Team")));
}
 
Example 28
Source Project: Spring-5.0-Projects   Source File: AccountResourceIntTest.java    License: MIT License 5 votes vote down vote up
@Test
@Transactional
@WithMockUser("save-invalid-email")
public void testSaveInvalidEmail() throws Exception {
    User user = new User();
    user.setLogin("save-invalid-email");
    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("invalid email");
    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());

    assertThat(userRepository.findOneByEmailIgnoreCase("invalid email")).isNotPresent();
}
 
Example 29
Source Project: gaia   Source File: ModuleRestControllerIT.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@Test
@WithMockUser("Mary J")
void saveModule_shouldNotBeAccessible_forStandardUsers() throws Exception {
    mockMvc.perform(put("/api/modules/e01f9925-a559-45a2-8a55-f93dc434c676")
            .with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content("{\"name\":\"module-test\"," +
                    "\"terraformImage\":{\"repository\":\"hashicorp/terraform\",\"tag\":\"latest\"}," +
                    "\"gitRepositoryUrl\":\"https://github.com/juwit/terraform-docker-mongo.git\"}"))
            .andExpect(status().isForbidden());
}
 
Example 30
Source Project: gaia   Source File: StackRestControllerIT.java    License: Mozilla Public License 2.0 5 votes vote down vote up
@Test
@WithMockUser("Mary J")
void getStacks_shouldReturnStack_forStandardUsers() throws Exception {
    mockMvc.perform(get("/api/stacks/845543d0-20a5-466c-8978-33c9a4661606"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.name", is("mongo-instance-limited")))
            .andExpect(jsonPath("$.ownerTeam.id", is("Not Ze Team")))
            .andExpect(jsonPath("$.estimatedRunningCost", is(0)));
}