@Test @Transactional public void testRegisterAdminIsIgnored() throws Exception { UserDTO u = new UserDTO( "badguy", // login "password", // password "Bad", // firstName "Guy", // lastName "*****@*****.**", // e-mail "en", // langKey Arrays.asList(AuthoritiesConstants.ADMIN), "01" // <-- 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 "en", // langKey Arrays.asList(AuthoritiesConstants.USER), "01"); // Duplicate e-mail, different login UserDTO dup = new UserDTO( "johnjr", u.getPassword(), u.getLogin(), u.getLastName(), u.getEmail(), u.getLangKey(), u.getRoles(), u.getPhoneNumber()); // 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 "en", // langKey Arrays.asList(AuthoritiesConstants.USER), "01"); 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 "en", // langKey Arrays.asList(AuthoritiesConstants.USER), "01"); 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(); }