@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) // <-- 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()); User userDup = userRepository.findOneByLogin("badguy"); assertThat(userDup).isNotNull(); assertThat(userDup.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)); // Duplicate e-mail, different login UserDTO dup = new UserDTO( "johnjr", u.getPassword(), u.getLogin(), u.getLastName(), u.getEmail(), u.getLangKey(), u.getRoles()); // 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()); User userDup = userRepository.findOneByLogin("johnjr"); assertThat(userDup).isNull(); }
@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)); restUserMockMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isBadRequest()); User user = userRepository.findOneByLogin("bob"); assertThat(user).isNull(); }
@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)); restMvc .perform( post("/api/register") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(TestUtil.convertObjectToJsonBytes(u))) .andExpect(status().isCreated()); User user = userRepository.findOneByLogin("joe"); assertThat(user).isNotNull(); }