use of uk.gov.pay.adminusers.persistence.entity.RoleEntity 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));
}
use of uk.gov.pay.adminusers.persistence.entity.RoleEntity 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()));
}
use of uk.gov.pay.adminusers.persistence.entity.RoleEntity 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()));
}
use of uk.gov.pay.adminusers.persistence.entity.RoleEntity in project pay-adminusers by alphagov.
the class InviteDaoIT method create_shouldCreateAnInvite.
@Test
public void create_shouldCreateAnInvite() {
Role role = roleDbFixture(databaseHelper).insertRole();
int serviceId = serviceDbFixture(databaseHelper).insertService().getId();
String username = randomUuid();
String email = username + "@example.com";
User sender = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
RoleEntity roleEntity = roleDao.findByRoleName(role.getName()).get();
ServiceEntity serviceEntity = serviceDao.findById(serviceId).get();
UserEntity userSenderEntity = userDao.findById(sender.getId()).get();
String code = randomAlphanumeric(10);
String otpKey = randomAlphanumeric(10);
InviteEntity invite = new InviteEntity("USER@example.com", code, otpKey, roleEntity);
invite.setService(serviceEntity);
invite.setSender(userSenderEntity);
inviteDao.persist(invite);
List<Map<String, Object>> savedInvite = databaseHelper.findInviteById(invite.getId());
assertThat(savedInvite.size(), is(1));
assertThat(savedInvite.get(0).get("sender_id"), is(userSenderEntity.getId()));
assertThat(savedInvite.get(0).get("email"), is("user@example.com"));
assertThat(savedInvite.get(0).get("role_id"), is(roleEntity.getId()));
assertThat(savedInvite.get(0).get("service_id"), is(serviceId));
assertThat(savedInvite.get(0).get("code"), is(code));
assertThat(savedInvite.get(0).get("otp_key"), is(notNullValue()));
assertThat(savedInvite.get(0).get("otp_key"), is(invite.getOtpKey()));
assertThat(savedInvite.get(0).get("telephone_number"), is(nullValue()));
assertThat(savedInvite.get(0).get("date"), is(from(invite.getDate().toInstant())));
assertThat(savedInvite.get(0).get("disabled"), is(Boolean.FALSE));
assertThat(savedInvite.get(0).get("login_counter"), is(0));
}
use of uk.gov.pay.adminusers.persistence.entity.RoleEntity in project pay-adminusers by alphagov.
the class ServiceRoleCreator method doCreate.
@Transactional
public Optional<User> doCreate(String userExternalId, String serviceExternalId, String roleName) {
Optional<UserEntity> userMaybe = userDao.findByExternalId(userExternalId);
if (!userMaybe.isPresent()) {
return Optional.empty();
}
Optional<ServiceEntity> serviceMaybe = serviceDao.findByExternalId(serviceExternalId);
if (!serviceMaybe.isPresent()) {
throw serviceDoesNotExistError(serviceExternalId);
}
Optional<RoleEntity> roleMaybe = roleDao.findByRoleName(roleName);
if (!roleMaybe.isPresent()) {
throw undefinedRoleException(roleName);
}
UserEntity userEntity = userMaybe.get();
userEntity.getServicesRole(serviceExternalId).ifPresent(serviceRoleEntity -> {
throw conflictingServiceRoleForUser(userExternalId, serviceExternalId);
});
userEntity.addServiceRole(new ServiceRoleEntity(serviceMaybe.get(), roleMaybe.get()));
userDao.merge(userEntity);
return Optional.of(linksBuilder.decorate(userEntity.toUser()));
}
Aggregations