Search in sources :

Example 1 with KcAdmExec

use of org.keycloak.testsuite.cli.KcAdmExec in project keycloak by keycloak.

the class KcAdmCreateTest method testCreateIDPWithoutSyncMode.

@Test
public void testCreateIDPWithoutSyncMode() throws IOException {
    final String realm = "test";
    final RealmResource realmResource = adminClient.realm(realm);
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        loginAsUser(configFile.getFile(), serverUrl, realm, "user1", "userpass");
        final File idpJson = new File("target/test-classes/cli/idp-keycloak-without-sync-mode.json");
        KcAdmExec exe = execute("create identity-provider/instances/ -r " + realm + " -f " + idpJson.getAbsolutePath() + " --config " + configFile.getFile());
        assertExitCodeAndStdErrSize(exe, 0, 1);
    }
    // If the sync mode is not present on creating the idp, it will never be added automatically. However, the model will always assume "LEGACY", so no errors should occur.
    Assert.assertNull(realmResource.identityProviders().get("idpAlias").toRepresentation().getConfig().get(IdentityProviderModel.SYNC_MODE));
}
Also used : FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) RealmResource(org.keycloak.admin.client.resource.RealmResource) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) File(java.io.File) TempFileResource(org.keycloak.testsuite.util.TempFileResource) Test(org.junit.Test)

Example 2 with KcAdmExec

use of org.keycloak.testsuite.cli.KcAdmExec in project keycloak by keycloak.

the class KcAdmTest method testUserLoginWithDefaultConfigInteractive.

@Test
public void testUserLoginWithDefaultConfigInteractive() throws IOException {
    if (!runIntermittentlyFailingTests()) {
        System.out.println("TEST SKIPPED - This test currently suffers from intermittent failures. Use -Dtest.intermittent=true to run it.");
        return;
    }
    KcAdmExec exe = KcAdmExec.newBuilder().argsLine("config credentials --server " + serverUrl + " --realm master --user admin").executeAsync();
    exe.waitForStdout("Enter password: ");
    exe.sendToStdin("admin" + EOL);
    exe.waitCompletion();
    assertExitCodeAndStreamSizes(exe, 0, 1, 1);
    Assert.assertEquals("stderr first line", "Logging into " + serverUrl + " as user admin of realm master", exe.stderrLines().get(0));
    /*
         *  Run the test one more time with stdin redirect
         */
    File tmpFile = new File(KcAdmExec.WORK_DIR + "/" + UUID.randomUUID().toString() + ".tmp");
    try {
        FileOutputStream tmpos = new FileOutputStream(tmpFile);
        tmpos.write("admin".getBytes());
        tmpos.write(EOL.getBytes());
        tmpos.close();
        exe = KcAdmExec.execute("config credentials --server " + serverUrl + " --realm master --user admin < '" + tmpFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 1, 1);
        Assert.assertTrue("Enter password prompt", exe.stdoutLines().get(0).startsWith("Enter password: "));
        Assert.assertEquals("stderr first line", "Logging into " + serverUrl + " as user admin of realm master", exe.stderrLines().get(0));
    } finally {
        tmpFile.delete();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) File(java.io.File) Test(org.junit.Test)

Example 3 with KcAdmExec

use of org.keycloak.testsuite.cli.KcAdmExec in project keycloak by keycloak.

the class KcAdmTest method testBadCommand.

@Test
public void testBadCommand() {
    /*
         *  Test most basic execution with non-existent command
         */
    KcAdmExec exe = execute("nonexistent");
    assertExitCodeAndStreamSizes(exe, 1, 0, 1);
    Assert.assertEquals("stderr first line", "Unknown command: nonexistent", exe.stderrLines().get(0));
}
Also used : KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) Test(org.junit.Test)

Example 4 with KcAdmExec

use of org.keycloak.testsuite.cli.KcAdmExec in project keycloak by keycloak.

the class KcAdmTest method testUserLoginWithDefaultConfig.

@Test
public void testUserLoginWithDefaultConfig() {
    /*
         *  Test most basic user login, using the default admin-cli as a client
         */
    KcAdmExec exe = KcAdmExec.execute("config credentials --server " + serverUrl + " --realm master --user admin --password admin");
    assertExitCodeAndStreamSizes(exe, 0, 0, 1);
    Assert.assertEquals("stderr first line", "Logging into " + serverUrl + " as user admin of realm master", exe.stderrLines().get(0));
}
Also used : KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) Test(org.junit.Test)

Example 5 with KcAdmExec

use of org.keycloak.testsuite.cli.KcAdmExec in project keycloak by keycloak.

the class KcAdmTest method testCRUDWithOnTheFlyUserAuthWithClientSecret.

@Test
public void testCRUDWithOnTheFlyUserAuthWithClientSecret() throws IOException {
    /*
         *  Test create, get, update, and delete using on-the-fly authentication - without using any config file.
         *  Login is performed by each operation again, and again using username, password, and client secret.
         */
    // try client without direct grants enabled
    KcAdmExec exe = KcAdmExec.execute("get clients --no-config --server " + serverUrl + " --realm test" + " --user user1 --password userpass --client admin-cli-secret --secret password");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("login message", "Logging into " + serverUrl + " as user user1 of realm test", exe.stderrLines().get(0));
    Assert.assertEquals("error message", "Client not allowed for direct access grants [unauthorized_client]", exe.stderrLines().get(1));
    // try wrong user password
    exe = KcAdmExec.execute("get clients --no-config --server " + serverUrl + " --realm test" + " --user user1 --password wrong --client admin-cli-secret-direct --secret password");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("login message", "Logging into " + serverUrl + " as user user1 of realm test", exe.stderrLines().get(0));
    Assert.assertEquals("error message", "Invalid user credentials [invalid_grant]", exe.stderrLines().get(1));
    // try wrong client secret
    exe = KcAdmExec.execute("get clients --no-config --server " + serverUrl + " --realm test" + " --user user1 --password userpass --client admin-cli-secret-direct --secret wrong");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("login message", "Logging into " + serverUrl + " as user user1 of realm test", exe.stderrLines().get(0));
    Assert.assertEquals("error message", "Invalid client secret [unauthorized_client]", exe.stderrLines().get(1));
    // try whole CRUD
    testCRUDWithOnTheFlyAuth(serverUrl, "--user user1 --password userpass --client admin-cli-secret-direct --secret password", "", "Logging into " + serverUrl + " as user user1 of realm test");
}
Also used : KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) Test(org.junit.Test)

Aggregations

KcAdmExec (org.keycloak.testsuite.cli.KcAdmExec)27 Test (org.junit.Test)25 FileConfigHandler (org.keycloak.client.admin.cli.config.FileConfigHandler)10 TempFileResource (org.keycloak.testsuite.util.TempFileResource)9 File (java.io.File)8 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 ConfigData (org.keycloak.client.admin.cli.config.ConfigData)3 FileOutputStream (java.io.FileOutputStream)2 RealmResource (org.keycloak.admin.client.resource.RealmResource)2 RealmConfigData (org.keycloak.client.admin.cli.config.RealmConfigData)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1