示例#1
0
  @Test
  public void testAddUserRole() throws Exception {
    User user = dao.get(-1L);
    assertEquals(1, user.getRoles().size());

    Role role = rdao.getRoleByName(Constants.ADMIN_ROLE);
    user.addRole(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals(2, user.getRoles().size());

    // add the same role twice - should result in no additional role
    user.addRole(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals("more than 2 roles", 2, user.getRoles().size());

    user.getRoles().remove(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals(1, user.getRoles().size());
  }
示例#2
0
  public void testUpdateUser() throws Exception {
    User user = dao.get(1L);

    Address address = user.getAddress();
    address.setAddress1("new address");
    user.setTimeZone("US/Central");

    dao.saveUser(user);

    user = dao.get(1L);
    assertEquals(address, user.getAddress());
    assertEquals("new address", user.getAddress().getAddress1());
    assertEquals("US/Central", user.getTimeZone());

    // verify that violation occurs when adding new user with same username
    user.setId(null);

    endTransaction();

    try {
      dao.saveUser(user);
      // flush();
      fail("saveUser didn't throw DataIntegrityViolationException");
    } catch (DataIntegrityViolationException e) {
      assertNotNull(e);
      log.debug("expected exception: " + e.getMessage());
    }
  }
示例#3
0
  @Test
  @ExpectedException(DataIntegrityViolationException.class)
  public void testUpdateUser() throws Exception {
    User user = dao.get(-1L);

    Address address = user.getAddress();
    address.setAddress("new address");

    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals(address, user.getAddress());
    assertEquals("new address", user.getAddress().getAddress());

    // verify that violation occurs when adding new user with same username
    User user2 = new User();
    user2.setAddress(user.getAddress());
    user2.setConfirmPassword(user.getConfirmPassword());
    user2.setEmail(user.getEmail());
    user2.setFirstName(user.getFirstName());
    user2.setLastName(user.getLastName());
    user2.setPassword(user.getPassword());
    user2.setPasswordHint(user.getPasswordHint());
    user2.setRoles(user.getRoles());
    user2.setUsername(user.getUsername());
    user2.setWebsite(user.getWebsite());

    // should throw DataIntegrityViolationException
    dao.saveUser(user2);
  }
示例#4
0
  public void testAddUserRole() throws Exception {
    User user = dao.get(2L);
    assertEquals(6, user.getRoles().size());

    Role role = rdao.getRoleByName("ROLE_EXTERNAL");
    assertEquals((Long) 4L, (Long) role.getId());
    user.addRole(role);
    user = dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals(7, user.getRoles().size());

    // add the same role twice - should result in no additional role
    user.addRole(role);
    dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals("more than 7 roles", 7, user.getRoles().size());

    user.getRoles().remove(role);
    dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals(6, user.getRoles().size());
  }
示例#5
0
  @Test
  @ExpectedException(DataAccessException.class)
  public void testAddAndRemoveUser() throws Exception {
    User user = new User("testuser");
    user.setPassword("testpass");
    user.setFirstName("Test");
    user.setLastName("Last");
    Address address = new Address();
    address.setCity("Denver");
    address.setProvince("CO");
    address.setCountry("USA");
    address.setPostalCode("80210");
    user.setAddress(address);
    user.setEmail("*****@*****.**");
    user.setWebsite("http://raibledesigns.com");

    Role role = rdao.getRoleByName(Constants.USER_ROLE);
    assertNotNull(role.getId());
    user.addRole(role);

    user = dao.saveUser(user);
    flush();

    assertNotNull(user.getId());
    user = dao.get(user.getId());
    assertEquals("testpass", user.getPassword());

    dao.remove(user);
    flush();

    // should throw DataAccessException
    dao.get(user.getId());
  }
示例#6
0
  public void testChangeUserRole() throws Exception {
    User user = dao.get(2L);
    assertEquals(6, user.getRoles().size());

    assertTrue(user.hasRole("ROLE_ADMIN"));

    user.getRoles().clear();
    user.addRole(rdao.getRoleByName("ROLE_EXTERNAL"));
    dao.saveUser(user);

    assertEquals(1, user.getRoles().size());

    assertTrue(user.getRoles().iterator().next().getName().equals("ROLE_EXTERNAL"));
  }
示例#7
0
  public void testAddAndRemoveUser() throws Exception {
    User user = new User("testuser");
    user.setPassword("testpass");
    user.setFirstName("Test");
    user.setLastName("Last");
    Address address = new Address();
    address.setCity("Denver");
    address.setProvince("CO");
    address.setCountry("USA");
    address.setPostalCode("80210");
    user.setAddress(address);
    user.setEmail("*****@*****.**");
    user.setWebsite("http://tek42.com");
    user.setTimeZone("US/Central");

    // Here we are creating an org that should already be in the database...
    // Ideally, we somehow have an org object... from the other dao or whatever...
    /*
     * Account org = new Account(); org.setName("Tek42"); org.setId(1L);
     */
    Account org = adao.get(2L);
    System.out.println("Have org: " + org);
    user.setAccount(org);

    Role role = rdao.getRoleByName(Constants.USER_ROLE);
    assertNotNull(role.getId());
    user.addRole(role);

    user = dao.saveUser(user);

    assertNotNull(user.getId());

    user = dao.get(user.getId());

    assertEquals("Vigilant", user.getAccount().getName());
    assertEquals("testpass", user.getPassword());
    assertEquals("US/Central", user.getTimeZone());

    dao.remove(user.getId());

    try {
      dao.get(user.getId());
      fail("getUser didn't throw DataAccessException");
    } catch (DataAccessException d) {
      assertNotNull(d);
    }
  }
示例#8
0
  @Test
  public void testUserSearch() throws Exception {
    // reindex all the data
    dao.reindex();

    List<User> found = dao.search("Matt");
    assertEquals(1, found.size());
    User user = found.get(0);
    assertEquals("Matt", user.getFirstName());

    // test mirroring
    user = dao.get(-2L);
    user.setFirstName("MattX");
    dao.saveUser(user);
    flush();
    flushSearchIndexes();

    // now verify it is reflected in the index
    found = dao.search("MattX");
    assertEquals(1, found.size());
    user = found.get(0);
    assertEquals("MattX", user.getFirstName());
  }