Search in sources :

Example 16 with TempFileResource

use of org.keycloak.testsuite.util.TempFileResource in project keycloak by keycloak.

the class KcAdmTest method testCustomConfigLoginCreateDelete.

@Test
public void testCustomConfigLoginCreateDelete() throws IOException {
    /*
         *  Test user login, create, delete session using a custom config file
         */
    // prepare for loading a config file
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        KcAdmExec exe = KcAdmExec.execute("config credentials --server " + serverUrl + " --realm master --user admin --password admin --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        // remember the state of config file
        ConfigData config1 = handler.loadConfig();
        exe = KcAdmExec.execute("create --config '" + configFile.getName() + "' clients -s clientId=test-client -o");
        assertExitCodeAndStdErrSize(exe, 0, 0);
        // check changes to config file
        ConfigData config2 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config1, config2);
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("clientId", "test-client", client.getClientId());
        exe = KcAdmExec.execute("delete clients/" + client.getId() + " --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
        // check changes to config file
        ConfigData config3 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config2, config3);
    }
}
Also used : FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) ConfigData(org.keycloak.client.admin.cli.config.ConfigData) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 17 with TempFileResource

use of org.keycloak.testsuite.util.TempFileResource in project keycloak by keycloak.

the class KcRegTest method testCreateWithAllowedHostsWithoutAuthentication.

private void testCreateWithAllowedHostsWithoutAuthentication(String realm, boolean useConfig) throws IOException {
    addLocalhostToAllowedHosts(realm);
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        KcRegExec exe = execute("create " + (useConfig ? ("--config '" + configFile.getAbsolutePath()) + "'" : "--no-config") + " --server " + serverUrl + " --realm " + realm + " -s clientId=test-client -o");
        assertExitCodeAndStdErrSize(exe, 0, 0);
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("clientId", "test-client", client.getClientId());
        Assert.assertNotNull("registrationAccessToken", client.getRegistrationAccessToken());
        exe = execute("delete test-client " + (useConfig ? ("--config '" + configFile.getAbsolutePath()) + "'" : "--no-config") + " --server " + serverUrl + " --realm " + realm + " -t " + client.getRegistrationAccessToken());
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
    }
}
Also used : FileConfigHandler(org.keycloak.client.registration.cli.config.FileConfigHandler) KcRegExec(org.keycloak.testsuite.cli.KcRegExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 18 with TempFileResource

use of org.keycloak.testsuite.util.TempFileResource in project keycloak by keycloak.

the class KcRegTest method testCustomConfigLoginCreateDelete.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void testCustomConfigLoginCreateDelete() throws IOException {
    /*
         *  Test user login, create, delete session using a custom config file
         */
    // prepare for loading a config file
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        KcRegExec exe = execute("config credentials --server " + serverUrl + " --realm master --user admin --password admin --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        // remember the state of config file
        ConfigData config1 = handler.loadConfig();
        exe = execute("create --config '" + configFile.getName() + "' -s clientId=test-client -o");
        assertExitCodeAndStdErrSize(exe, 0, 0);
        // check changes to config file
        ConfigData config2 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config1, config2, "endpoints." + serverUrl + ".master.clients.test-client");
        // check that registration access token is now set
        Assert.assertNotNull(config2.sessionRealmConfigData().getClients().get("test-client"));
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("clientId", "test-client", client.getClientId());
        Assert.assertNotNull("registrationAccessToken", client.getRegistrationAccessToken());
        Assert.assertEquals("registrationAccessToken in returned json same as in config", config2.sessionRealmConfigData().getClients().get("test-client"), client.getRegistrationAccessToken());
        exe = execute("delete test-client --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
        // check changes to config file
        ConfigData config3 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config2, config3, "endpoints." + serverUrl + ".master.clients.test-client");
        // check that registration access token is no longer there
        Assert.assertTrue("clients empty", config3.sessionRealmConfigData().getClients().isEmpty());
    }
}
Also used : FileConfigHandler(org.keycloak.client.registration.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) KcRegExec(org.keycloak.testsuite.cli.KcRegExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Example 19 with TempFileResource

use of org.keycloak.testsuite.util.TempFileResource in project keycloak by keycloak.

the class KcRegTest method testCreateDeleteWithInitialAndRegistrationTokensWithUnsecureOption.

private void testCreateDeleteWithInitialAndRegistrationTokensWithUnsecureOption(boolean useConfig) throws IOException {
    Assume.assumeTrue(AUTH_SERVER_SSL_REQUIRED);
    // prepare for loading a config file
    // only used when useConfig is true
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        String token = issueInitialAccessToken("master");
        final String realm = "master";
        KcRegExec exe = execute("create " + (useConfig ? ("--config '" + configFile.getAbsolutePath()) + "'" : "--no-config") + " --insecure --server " + oauth.AUTH_SERVER_ROOT + " --realm " + realm + " -s clientId=test-client2 -o -t " + token);
        Assert.assertEquals("exitCode == 0", 0, exe.exitCode());
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("clientId", "test-client2", client.getClientId());
        Assert.assertNotNull("registrationAccessToken", client.getRegistrationAccessToken());
        if (useConfig) {
            ConfigData config = handler.loadConfig();
            Assert.assertEquals("Registration Access Token in config file", client.getRegistrationAccessToken(), config.ensureRealmConfigData(oauth.AUTH_SERVER_ROOT, realm).getClients().get("test-client2"));
        } else {
            Assert.assertFalse("There should be no config file", configFile.isFile());
        }
        exe = execute("delete test-client2 " + (useConfig ? ("--config '" + configFile.getAbsolutePath()) + "'" : "--no-config") + " --insecure --server " + oauth.AUTH_SERVER_ROOT + " --realm " + realm + " -t " + client.getRegistrationAccessToken());
        assertExitCodeAndStreamSizes(exe, 0, 0, 2);
    }
}
Also used : FileConfigHandler(org.keycloak.client.registration.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.registration.cli.config.RealmConfigData) ConfigData(org.keycloak.client.registration.cli.config.ConfigData) KcRegExec(org.keycloak.testsuite.cli.KcRegExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Aggregations

TempFileResource (org.keycloak.testsuite.util.TempFileResource)19 Test (org.junit.Test)17 FileConfigHandler (org.keycloak.client.registration.cli.config.FileConfigHandler)10 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)10 KcRegExec (org.keycloak.testsuite.cli.KcRegExec)10 FileConfigHandler (org.keycloak.client.admin.cli.config.FileConfigHandler)9 KcAdmExec (org.keycloak.testsuite.cli.KcAdmExec)9 File (java.io.File)6 ConfigData (org.keycloak.client.registration.cli.config.ConfigData)6 RealmResource (org.keycloak.admin.client.resource.RealmResource)3 RealmConfigData (org.keycloak.client.registration.cli.config.RealmConfigData)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Assert (org.junit.Assert)2 ConfigData (org.keycloak.client.admin.cli.config.ConfigData)2 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)2 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1