Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
4.1 kB
1
Indexable
Never
package com.impect.iam.keycloak;

import com.impect.iam.core.customer.Customer;
import com.impect.iam.core.customer_account.CustomerAccount;
import com.impect.iam.core.customer_role.CustomerRole;
import com.impect.iam.keycloak.services.KeycloakUserService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.representations.idm.RoleRepresentation;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class KeycloakSyncTest {
    @Container
    @ServiceConnection
    private KeycloakContainer keycloakContainer = new KeycloakContainer("quay.io/keycloak/keycloak:17.0.1-legacy").withRealmImportFile("keycloak/realm-export.json");

    private Keycloak keycloakAdminClient;
    private KeycloakCore keycloakCore;
    private KeycloakUserService keycloakUserService;
    private KeycloakSync keycloakSync;

    @BeforeAll
    public void setup() {
        keycloakContainer.start();
        keycloakAdminClient = KeycloakBuilder.builder().serverUrl(keycloakContainer.getAuthServerUrl()).realm("develop").clientId("admin-cli").username("admin2").password("Pa55w0rdPa55w0rd!").build();
        keycloakCore = new KeycloakCore() {
            @Override
            public String getRealm() {
                return "develop";
            }

            @Override
            public Keycloak getInstance() {
                return keycloakAdminClient;
            }
        };
        keycloakUserService = new KeycloakUserService(keycloakCore);
        keycloakSync = new KeycloakSync(keycloakUserService);
    }

    @Test
    void test_account_sync_lifecycle() {
        // Create a new customer account and sync it
        Customer customer = new Customer("2. FC Mustermann & Söhne", LocalDate.of(2065, 1, 1), true, 1L, "yahoo.com", Customer.Language.DE, 10, 1L, null, null, new HashMap<>());
        CustomerAccount customerAccount = new CustomerAccount(customer, "max.mustermann@yahoo.com", true, "Max", "Mustermann", LocalDate.of(2065, 1, 1), Customer.Language.DE, 1L);
        keycloakSync.syncCustomerAccount(customerAccount);

        // Make sure that the account creation got successfully synchronized
        String accountId = keycloakUserService.getAccountId("max.mustermann@yahoo.com");
        assert accountId != null && !accountId.equals("");

        // Add some roles and sync them
        customerAccount.addCustomerRole(new CustomerRole("Analysis Portal"));
        customerAccount.addCustomerRole(new CustomerRole("Glossary Portal"));
        customerAccount.addCustomerRole(new CustomerRole("Scouting Portal"));
        keycloakUserService.updateAccount(accountId, customerAccount);

        // Ensure that the roles got successfully synced
        List<RoleRepresentation> roleRepresentations =
                keycloakAdminClient
                        .realm(keycloakCore.getRealm())
                        .users()
                        .get(accountId)
                        .roles()
                        .getAll()
                        .getRealmMappings();
        Set<String> roleNames = roleRepresentations.stream().map(x -> x.getName()).collect(Collectors.toSet());
        assert(roleNames.contains("Analysis Portal"));
        assert(roleNames.contains("Glossary Portal"));
        assert(roleNames.contains("Scouting Portal"));

        // Delete the account
        keycloakUserService.deleteCustomerAccount(customerAccount);

        // Ensure that the deletion got successfully synchronized
        accountId = keycloakUserService.getAccountId("max.mustermann@yahoo.com");
        assert accountId.equals("");

    }

}
Leave a Comment