Skip to content

Commit

Permalink
fix: Update membership revocation to retain user
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
anarsultanov committed Aug 30, 2023
1 parent 35ca8dc commit 110d267
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TenantMembershipEntity {
@JoinColumn(name = "TENANT_ID")
private TenantEntity tenant;

@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private UserEntity user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.microsoft.playwright.Playwright;
import dasniko.testcontainers.keycloak.KeycloakContainer;
import dev.sultanov.keycloak.multitenancy.resource.representation.TenantInvitationRepresentation;
import dev.sultanov.keycloak.multitenancy.resource.representation.TenantMembershipRepresentation;
import dev.sultanov.keycloak.multitenancy.resource.representation.TenantRepresentation;
import dev.sultanov.keycloak.multitenancy.support.api.KeycloakClient;
import dev.sultanov.keycloak.multitenancy.support.api.KeycloakClientFactory;
Expand All @@ -17,6 +18,7 @@
import dev.sultanov.keycloak.multitenancy.support.data.TestDataFactory;
import dev.sultanov.keycloak.multitenancy.support.data.UserData;
import java.util.List;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -130,6 +132,39 @@ void user_shouldBePromptedToSelectTenant_whenTheyAcceptMultipleInvitations() {
assertThat(((AccountPage) nextPage).getLoggedInUser()).contains(user.getFullName());
}

@Test
void admin_shouldBeAbleToRevokeMembership_whenUserAcceptsInvitation() {
// given
var admin = createVerifiedUser(TestDataFactory.userData());
var user = createVerifiedUser(TestDataFactory.userData());

var tenantData = createInvitationFor(admin, user);
var nextPage = AccountPage.open(browser, keycloakUrl)
.signIn()
.fillCredentials(user.getEmail(), user.getPassword())
.signIn();
assertThat(nextPage).isInstanceOf(ReviewInvitationsPage.class);
((ReviewInvitationsPage) nextPage).accept();

var adminTenantResource = clientFactory.createUserClient(admin).tenantResource(tenantData.getName());
var userMembership = adminTenantResource.memberships().listMemberships("", null, null).stream()
.filter(membership -> membership.getUser().getEmail().equalsIgnoreCase(user.getEmail()))
.findFirst()
.orElseThrow();

// when
try (var response = adminTenantResource.memberships().revokeMembership(userMembership.getId())) {

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
assertThat(adminTenantResource.memberships().listMemberships("", null, null))
.extracting(TenantMembershipRepresentation::getUser)
.extracting(UserRepresentation::getEmail)
.extracting(String::toLowerCase)
.containsExactly(admin.getEmail().toLowerCase());
}
}

private UserData createVerifiedUser(UserData userData) {
var userRepresentation = new UserRepresentation();
userRepresentation.setFirstName(userData.getFirstName());
Expand All @@ -148,8 +183,12 @@ private UserData createVerifiedUser(UserData userData) {
return userData;
}

private TenantData createInvitationFor(UserData user) {
private TenantData createInvitationFor(UserData invitee) {
var inviter = createVerifiedUser(TestDataFactory.userData());
return createInvitationFor(inviter, invitee);
}

private TenantData createInvitationFor(UserData inviter, UserData invitee) {
var tenant = TestDataFactory.tenantData();
((CreateTenantPage) AccountPage.open(browser, keycloakUrl)
.signIn()
Expand All @@ -166,7 +205,7 @@ private TenantData createInvitationFor(UserData user) {
.orElseThrow();

var invitation = new TenantInvitationRepresentation();
invitation.setEmail(user.getEmail());
invitation.setEmail(invitee.getEmail());
try (var response = tenantResource.invitations().createInvitation(invitation)) {
assertThat(CreatedResponseUtil.getCreatedId(response)).isNotNull();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ public RealmResource realmResource() {
public TenantsResource tenantsResource() {
return keycloak.proxy(TenantsResource.class, URI.create(keycloakUrl + "/realms/" + REALM_NAME));
}

public TenantResource tenantResource(String tenantName) {
var tenantsResource = tenantsResource();
var matchingTenants = tenantsResource.listTenants(tenantName, null, null);
if (matchingTenants == null || matchingTenants.isEmpty()) {
throw new IllegalArgumentException("No tenants found with name: " + tenantName);
} else if (matchingTenants.size() > 1) {
throw new IllegalArgumentException("More than 1 tenant found with name matching: " + tenantName);
}
return tenantsResource.getTenantResource(matchingTenants.get(0).getId());
}
}

0 comments on commit 110d267

Please sign in to comment.