@Test @Transactional public void testRegisterAdminIsIgnored() throws Exception { UserDTO u = new UserDTO( "badguy", // login "password", // password "Bad", // firstName "Guy", // lastName "*****@*****.**", // e-mail true, // activated "en", // langKey new HashSet<>( Arrays.asList( AuthoritiesConstants.ADMIN)) // <-- only admin should be able to do that ); restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isCreated()); Optional<User> userDup = userRepository.findOneByLogin("badguy"); assertThat(userDup.isPresent()).isTrue(); assertThat(userDup.get().getAuthorities()) .hasSize(1) .containsExactly(authorityRepository.findOne(AuthoritiesConstants.USER)); }
@Test @Transactional public void testRegisterDuplicateEmail() throws Exception { // Good UserDTO u = new UserDTO( "john", // login "password", // password "John", // firstName "Doe", // lastName "*****@*****.**", // e-mail true, // activated "en", // langKey new HashSet<>(Arrays.asList(AuthoritiesConstants.USER))); // Duplicate e-mail, different login UserDTO dup = new UserDTO( "johnjr", u.getPassword(), u.getLogin(), u.getLastName(), u.getEmail(), 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 e-mail restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(dup))) .andExpect(status().is4xxClientError()); Optional<User> userDup = userRepository.findOneByLogin("johnjr"); assertThat(userDup.isPresent()).isFalse(); }
@Test @Transactional public void testRegisterInvalidEmail() throws Exception { UserDTO u = new UserDTO( "bob", // login "password", // password "Bob", // firstName "Green", // lastName "invalid", // e-mail <-- invalid 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.findOneByLogin("bob"); assertThat(user.isPresent()).isFalse(); }
@Test @Transactional public void testRegisterValid() throws Exception { UserDTO u = new UserDTO( "joe", // login "password", // password "Joe", // firstName "Shmoe", // lastName "*****@*****.**", // e-mail true, // activated "en", // langKey new HashSet<>(Arrays.asList(AuthoritiesConstants.USER))); restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isCreated()); Optional<User> user = userRepository.findOneByLogin("joe"); assertThat(user.isPresent()).isTrue(); }