@Test @Transactional public void testRegisterDuplicateLogin() throws Exception { // Good UserDTO u = new UserDTO( "alice", // login "password", // password "Alice", // firstName "Something", // lastName "*****@*****.**", // e-mail true, // activated "en", // langKey new HashSet<>(Arrays.asList(AuthoritiesConstants.USER))); // Duplicate login, different e-mail UserDTO dup = new UserDTO( u.getLogin(), u.getPassword(), u.getLogin(), u.getLastName(), "*****@*****.**", true, u.getLangKey(), u.getAuthorities()); // Good user restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isCreated()); // Duplicate login restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(dup))) .andExpect(status().is4xxClientError()); Optional<User> userDup = userRepository.findOneByEmail("*****@*****.**"); assertThat(userDup.isPresent()).isFalse(); }
@Test @Transactional public void testRegisterInvalidLogin() throws Exception { UserDTO u = new UserDTO( "funky-log!n", // login <-- invalid "password", // password "Funky", // firstName "One", // lastName "*****@*****.**", // e-mail true, // activated "en", // langKey new HashSet<>(Arrays.asList(AuthoritiesConstants.USER))); restUserMockMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isBadRequest()); Optional<User> user = userRepository.findOneByEmail("*****@*****.**"); assertThat(user.isPresent()).isFalse(); }