Skip to content

Commit

Permalink
MODCON-42. Not create shadow admin and dummy user tenant for central …
Browse files Browse the repository at this point in the history
…tenant (folio-org#48)

* MODCON-42. Not create shadow admin and dummy user tenant for central tenant

Co-authored-by: Azizbek <[email protected]>
  • Loading branch information
SerhiiNosko and azizbekxm authored May 31, 2023
1 parent d1b3c49 commit 79791e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class TenantServiceImpl implements TenantService {
private static final String TENANTS_IDS_NOT_MATCHED_ERROR_MSG = "Request body tenantId and path param tenantId should be identical";
private static final String TENANT_HAS_ACTIVE_USER_ASSOCIATIONS_ERROR_MSG = "Cannot delete tenant with ID {tenantId} because it has an association with a user. "
+ "Please remove the user association before attempting to delete the tenant.";
private static final String DUMMY_USERNAME = "*-dummy_user-*";
private static final String DUMMY_USERNAME = "dummy_user";
private final TenantRepository tenantRepository;
private final UserTenantRepository userTenantRepository;
private final ConversionService converter;
Expand Down Expand Up @@ -88,31 +88,39 @@ public TenantEntity getByTenantId(String tenantId) {
@Override
@Transactional
public Tenant save(UUID consortiumId, UUID adminUserId, Tenant tenantDto) {
log.debug("save:: Trying to save a tenant by consortiumId '{}', tenant object with id '{}' and isCentral={}", consortiumId,
log.info("save:: Trying to save a tenant by consortiumId '{}', tenant object with id '{}' and isCentral={}", consortiumId,
tenantDto.getId(), tenantDto.getIsCentral());
FolioExecutionContext currentTenantContext = (FolioExecutionContext) folioExecutionContext.getInstance();
String centralTenantId;
consortiumService.checkConsortiumExistsOrThrow(consortiumId);

// validation part
checkTenantNotExistsAndConsortiumExistsOrThrow(consortiumId, tenantDto.getId());
if (tenantDto.getIsCentral()) {
checkCentralTenantExistsOrThrow();
centralTenantId = tenantDto.getId();
} else {
centralTenantId = getCentralTenantId();
}

checkTenantNotExistsAndConsortiumExistsOrThrow(consortiumId, tenantDto.getId());
// save tenant to db
TenantEntity savedTenantEntity = saveTenantEntity(consortiumId, tenantDto);
var savedTenant = converter.convert(savedTenantEntity, Tenant.class);

User shadowAdminUser = userService.prepareShadowUser(adminUserId, currentTenantContext.getTenantId());
userTenantRepository.save(createUserTenantEntity(consortiumId, shadowAdminUser, tenantDto));
// save admin user tenant association for non-central tenant
String centralTenantId;
User shadowAdminUser = null;
if (tenantDto.getIsCentral()) {
centralTenantId = tenantDto.getId();
} else {
centralTenantId = getCentralTenantId();
shadowAdminUser = userService.prepareShadowUser(adminUserId, currentTenantContext.getTenantId());
userTenantRepository.save(createUserTenantEntity(consortiumId, shadowAdminUser, tenantDto));
}

// switch to context of the desired tenant and apply all necessary setup
try (var context = new FolioExecutionContextSetter(contextHelper.getSystemUserFolioExecutionContext(tenantDto.getId()))) {
configurationClient.saveConfiguration(createConsortiaConfigurationBody(centralTenantId));
createUserTenantWithDummyUser(tenantDto.getId());
createShadowAdminUserWithPermissions(shadowAdminUser);
createPrimaryUserAffiliationsAsync.createPrimaryUserAffiliationsAsync(consortiumId, savedTenantEntity, tenantDto);
if (!tenantDto.getIsCentral()) {
createUserTenantWithDummyUser(tenantDto.getId());
createShadowAdminUserWithPermissions(shadowAdminUser); //NOSONAR
}
}
log.info("save:: saved consortia configuration with centralTenantId={} by tenantId={} context", centralTenantId, tenantDto.getId());
return savedTenant;
Expand Down
29 changes: 22 additions & 7 deletions src/test/java/org/folio/consortia/service/TenantServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
Expand Down Expand Up @@ -124,7 +126,7 @@ void shouldGetTenantList() {
}

@Test
void shouldSaveTenantWithNewUserAndPermissions() {
void shouldSaveNotCentralTenantWithNewUserAndPermissions() {
UUID consortiumId = UUID.fromString(CONSORTIUM_ID);
TenantEntity tenantEntity1 = createTenantEntity("ABC1", "TestName1");
Tenant tenant = createTenant("TestID", "Test");
Expand All @@ -151,13 +153,19 @@ void shouldSaveTenantWithNewUserAndPermissions() {
when(folioExecutionContext.getInstance()).thenReturn(folioExecutionContext);

var tenant1 = tenantService.save(consortiumId, UUID.randomUUID(), tenant);
Mockito.verify(userTenantsClient, Mockito.times(1)).postUserTenant(any());
Mockito.verify(configurationClient, Mockito.times(1)).saveConfiguration(any());

verify(userService).prepareShadowUser(any(), any());
verify(userTenantRepository).save(any());
verify(configurationClient).saveConfiguration(any());
verify(userAffiliationAsyncService).createPrimaryUserAffiliationsAsync(any(), any(), any());
verify(userTenantsClient).postUserTenant(any());
verify(userService).createUser(any());

Assertions.assertEquals(tenant, tenant1);
}

@Test
void shouldSaveTenantWithExistingAndPermissions() throws JsonProcessingException {
void shouldSaveCentralTenantWithExistingAndPermissions() throws JsonProcessingException {
UUID consortiumId = UUID.fromString(CONSORTIUM_ID);
TenantEntity tenantEntity1 = createTenantEntity("ABC1", "TestName1");
Tenant tenant = createTenant("TestID", "Test", true);
Expand All @@ -173,7 +181,6 @@ void shouldSaveTenantWithExistingAndPermissions() throws JsonProcessingException

when(consortiumRepository.existsById(consortiumId)).thenReturn(true);
when(userService.prepareShadowUser(any(), any())).thenReturn(user);
when(userService.getById(any())).thenReturn(user);
when(permissionsClient.get(any())).thenReturn(permissionUserCollection);
doNothing().when(permissionsClient).addPermission(any(), any());
when(tenantRepository.existsById(any())).thenReturn(false);
Expand All @@ -191,8 +198,16 @@ void shouldSaveTenantWithExistingAndPermissions() throws JsonProcessingException
when(usersClient.getUserCollection(anyString(), anyInt(), anyInt())).thenReturn(userCollection);

var tenant1 = tenantService.save(consortiumId, UUID.randomUUID(), tenant);
Mockito.verify(userTenantsClient, Mockito.times(1)).postUserTenant(any());
Mockito.verify(configurationClient, Mockito.times(1)).saveConfiguration(any());

verify(configurationClient).saveConfiguration(any());
verify(userAffiliationAsyncService).createPrimaryUserAffiliationsAsync(any(), any(), any());

verify(userService, never()).prepareShadowUser(any(), any());
verify(userTenantRepository, never()).save(any());
verify(userTenantsClient, never()).postUserTenant(any());
verify(userService, never()).createUser(any());
verify(permissionUserService, never()).createWithPermissionsFromFile(any(), any());

Assertions.assertEquals(tenant, tenant1);
}

Expand Down

0 comments on commit 79791e5

Please sign in to comment.