Search in sources :

Example 1 with Role

use of uk.gov.pay.adminusers.model.Role in project pay-adminusers by alphagov.

the class RoleDaoIT method shouldFindARoleByRoleName.

@Test
public void shouldFindARoleByRoleName() {
    Role role1 = roleDbFixture(databaseHelper).insertRole();
    Role role2 = roleDbFixture(databaseHelper).insertRole();
    Optional<RoleEntity> optionalRole1 = roleDao.findByRoleName(role1.getName());
    assertTrue(optionalRole1.isPresent());
    RoleEntity roleEntity = optionalRole1.get();
    assertThat(roleEntity.toRole(), is(role1));
    Optional<RoleEntity> optionalRole2 = roleDao.findByRoleName(role2.getName());
    assertThat(optionalRole2.get().toRole(), is(role2));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) Test(org.junit.jupiter.api.Test)

Example 2 with Role

use of uk.gov.pay.adminusers.model.Role in project pay-adminusers by alphagov.

the class UserDaoIT method shouldFindUser_ByEmail_caseInsensitive.

@Test
public void shouldFindUser_ByEmail_caseInsensitive() {
    Role role = roleDbFixture(databaseHelper).insertRole();
    int serviceId = serviceDbFixture(databaseHelper).insertService().getId();
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withServiceRole(serviceId, role.getId()).withUsername(username).withEmail(email).insertUser();
    String otpKey = user.getOtpKey();
    Optional<UserEntity> userEntityMaybe = userDao.findByEmail(username + "@EXAMPLE.com");
    assertTrue(userEntityMaybe.isPresent());
    UserEntity foundUser = userEntityMaybe.get();
    assertThat(foundUser.getUsername(), is(username));
    assertThat(foundUser.getEmail(), is(email));
    assertThat(foundUser.getOtpKey(), is(otpKey));
    assertThat(foundUser.getTelephoneNumber(), is("+447700900000"));
    assertThat(foundUser.isDisabled(), is(false));
    assertThat(foundUser.getLoginCounter(), is(0));
    assertThat(foundUser.getSessionVersion(), is(0));
    assertThat(foundUser.getRoles().size(), is(1));
    assertThat(foundUser.getRoles().get(0).getId(), is(role.getId()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) User(uk.gov.pay.adminusers.model.User) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with Role

use of uk.gov.pay.adminusers.model.Role in project pay-adminusers by alphagov.

the class UserDaoIT method shouldFindUsersBy_ExternalIds.

@Test
public void shouldFindUsersBy_ExternalIds() {
    Role role = roleDbFixture(databaseHelper).insertRole();
    int serviceId1 = serviceDbFixture(databaseHelper).insertService().getId();
    int serviceId2 = serviceDbFixture(databaseHelper).insertService().getId();
    String username1 = randomUuid();
    String email1 = username1 + "@example.com";
    User user1 = userDbFixture(databaseHelper).withServiceRole(serviceId1, role.getId()).withServiceRole(serviceId2, role.getId()).withUsername(username1).withEmail(email1).insertUser();
    String username2 = randomUuid();
    String email2 = username2 + "@example.com";
    User user2 = userDbFixture(databaseHelper).withServiceRole(serviceId1, role.getId()).withServiceRole(serviceId2, role.getId()).withUsername(username2).withEmail(email2).insertUser();
    // Add third user to prove we're not just returning all users
    String username3 = randomUuid();
    String email3 = username3 + "@example.com";
    userDbFixture(databaseHelper).withServiceRole(serviceId1, role.getId()).withServiceRole(serviceId2, role.getId()).withUsername(username3).withEmail(email3).insertUser();
    List<String> externalIds = Arrays.asList(user1.getExternalId(), user2.getExternalId());
    List<UserEntity> userEntities = userDao.findByExternalIds(externalIds);
    assertThat(userEntities.size(), is(2));
    UserEntity foundUser1 = userEntities.get(0);
    assertThat(foundUser1.getExternalId(), is(user1.getExternalId()));
    assertThat(foundUser1.getUsername(), is(user1.getUsername()));
    assertThat(foundUser1.getEmail(), is(user1.getEmail()));
    assertThat(foundUser1.getOtpKey(), is(user1.getOtpKey()));
    assertThat(foundUser1.getTelephoneNumber(), is("+447700900000"));
    assertThat(foundUser1.isDisabled(), is(false));
    assertThat(foundUser1.getLoginCounter(), is(0));
    assertThat(foundUser1.getSessionVersion(), is(0));
    assertThat(foundUser1.getRoles().size(), is(1));
    assertThat(foundUser1.toUser().getServiceRoles().size(), is(2));
    assertThat(foundUser1.getRoles().get(0).getId(), is(role.getId()));
    UserEntity foundUser2 = userEntities.get(1);
    assertThat(foundUser2.getExternalId(), is(user2.getExternalId()));
    assertThat(foundUser2.getUsername(), is(user2.getUsername()));
    assertThat(foundUser2.getEmail(), is(user2.getEmail()));
    assertThat(foundUser2.getOtpKey(), is(user2.getOtpKey()));
    assertThat(foundUser2.getTelephoneNumber(), is("+447700900000"));
    assertThat(foundUser2.isDisabled(), is(false));
    assertThat(foundUser2.getLoginCounter(), is(0));
    assertThat(foundUser2.getSessionVersion(), is(0));
    assertThat(foundUser2.getRoles().size(), is(1));
    assertThat(foundUser2.toUser().getServiceRoles().size(), is(2));
    assertThat(foundUser2.getRoles().get(0).getId(), is(role.getId()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) User(uk.gov.pay.adminusers.model.User) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 4 with Role

use of uk.gov.pay.adminusers.model.Role in project pay-adminusers by alphagov.

the class UserDaoIT method shouldCreateAUserSuccessfully.

@Test
public void shouldCreateAUserSuccessfully() {
    Role role = roleDbFixture(databaseHelper).insertRole();
    String gatewayAccountId = randomInt().toString();
    int serviceId = serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId).insertService().getId();
    String username = valueOf(nextInt());
    UserEntity userEntity = new UserEntity();
    userEntity.setExternalId(randomUuid());
    userEntity.setUsername(username);
    userEntity.setPassword("password-" + username);
    userEntity.setDisabled(false);
    userEntity.setEmail(username + "@example.com");
    userEntity.setOtpKey(randomInt().toString());
    userEntity.setTelephoneNumber("+447700900000");
    userEntity.setSecondFactor(SecondFactorMethod.SMS);
    userEntity.setSessionVersion(0);
    ZonedDateTime timeNow = ZonedDateTime.now(ZoneId.of("UTC"));
    userEntity.setCreatedAt(timeNow);
    userEntity.setUpdatedAt(timeNow);
    ServiceEntity serviceEntity = serviceDao.findByGatewayAccountId(gatewayAccountId).get();
    RoleEntity roleEntity = roleDao.findByRoleName(role.getName()).get();
    ServiceRoleEntity serviceRoleEntity = new ServiceRoleEntity(serviceEntity, roleEntity);
    serviceRoleEntity.setUser(userEntity);
    userEntity.addServiceRole(serviceRoleEntity);
    userDao.persist(userEntity);
    assertThat(userEntity.getId(), is(notNullValue()));
    List<Map<String, Object>> savedUserData = databaseHelper.findUser(userEntity.getId());
    assertThat(savedUserData.size(), is(1));
    assertThat((String) savedUserData.get(0).get("external_id"), not(emptyOrNullString()));
    assertThat(((String) savedUserData.get(0).get("external_id")).length(), equalTo(32));
    assertThat(savedUserData.get(0).get("username"), is(userEntity.getUsername()));
    assertThat(savedUserData.get(0).get("password"), is(userEntity.getPassword()));
    assertThat(savedUserData.get(0).get("email"), is(userEntity.getEmail()));
    assertThat(savedUserData.get(0).get("otp_key"), is(userEntity.getOtpKey()));
    assertThat(savedUserData.get(0).get("telephone_number"), is(userEntity.getTelephoneNumber()));
    assertThat(savedUserData.get(0).get("disabled"), is(Boolean.FALSE));
    assertThat(savedUserData.get(0).get("session_version"), is(0));
    assertThat(savedUserData.get(0).get("createdat"), is(java.sql.Timestamp.from(timeNow.toInstant())));
    assertThat(savedUserData.get(0).get("updatedat"), is(java.sql.Timestamp.from(timeNow.toInstant())));
    List<Map<String, Object>> serviceRolesForUser = databaseHelper.findServiceRoleForUser(userEntity.getId());
    assertThat(serviceRolesForUser.size(), is(1));
    assertThat(serviceRolesForUser.get(0).get("id"), is(role.getId()));
    assertThat(serviceRolesForUser.get(0).get("service_id"), is(serviceId));
    assertThat(serviceRolesForUser.get(0).get("name"), is(role.getName()));
    assertThat(serviceRolesForUser.get(0).get("description"), is(role.getDescription()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) ZonedDateTime(java.time.ZonedDateTime) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) Map(java.util.Map) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 5 with Role

use of uk.gov.pay.adminusers.model.Role in project pay-adminusers by alphagov.

the class UserDaoIT method shouldAddServiceRoleOfAnExistingUser_whenSettingANewServiceRole.

@Test
public void shouldAddServiceRoleOfAnExistingUser_whenSettingANewServiceRole() {
    Role role1 = roleDbFixture(databaseHelper).insertRole();
    Role role2 = roleDbFixture(databaseHelper).insertRole();
    String gatewayAccountId1 = randomInt().toString();
    String gatewayAccountId2 = randomInt().toString();
    Service service1 = serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId1).insertService();
    serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId2).insertService();
    String username = randomUuid();
    String email = username + "@example.com";
    userDbFixture(databaseHelper).withServiceRole(service1, role1.getId()).withUsername(username).withEmail(email).insertUser();
    UserEntity existingUser = userDao.findByUsername(username).get();
    assertThat(existingUser.getGatewayAccountId(), is(gatewayAccountId1));
    assertThat(existingUser.getRoles().size(), is(1));
    assertThat(existingUser.getRoles().get(0).getId(), is(role1.getId()));
    ServiceEntity serviceEntity2 = serviceDao.findByGatewayAccountId(gatewayAccountId2).get();
    RoleEntity roleEntity2 = roleDao.findByRoleName(role2.getName()).get();
    ServiceRoleEntity serviceRole = new ServiceRoleEntity(serviceEntity2, roleEntity2);
    serviceRole.setUser(existingUser);
    existingUser.addServiceRole(serviceRole);
    userDao.merge(existingUser);
    UserEntity changedUser = userDao.findByUsername(username).get();
    List<ServiceRoleEntity> servicesRoles = changedUser.getServicesRoles();
    assertThat(servicesRoles.size(), is(2));
    assertThat(servicesRoles.stream().map(sr -> sr.getService().getExternalId()).collect(toUnmodifiableList()), hasItems(service1.getExternalId(), serviceEntity2.getExternalId()));
    assertThat(servicesRoles.stream().map(sr -> sr.getRole().getName()).collect(toUnmodifiableList()), hasItems(role1.getName(), role2.getName()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) Service(uk.gov.pay.adminusers.model.Service) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Role (uk.gov.pay.adminusers.model.Role)30 Test (org.junit.jupiter.api.Test)22 Service (uk.gov.pay.adminusers.model.Service)19 User (uk.gov.pay.adminusers.model.User)19 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)9 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)5 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)4 State (au.com.dius.pact.provider.junit.State)3 ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)3 Map (java.util.Map)2 Matchers.emptyString (org.hamcrest.Matchers.emptyString)2 ServiceName (uk.gov.pay.adminusers.model.ServiceName)2 ZonedDateTime (java.time.ZonedDateTime)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Permission (uk.gov.pay.adminusers.model.Permission)1 ServiceRole (uk.gov.pay.adminusers.model.ServiceRole)1 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)1