Search in sources :

Example 6 with KcRegExec

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

the class AbstractRegCliTest method loginAsUser.

void loginAsUser(File configFile, String server, String realm, String user, String password) {
    KcRegExec exe = execute("config credentials --server " + server + " --realm " + realm + " --user " + user + " --password " + password + " --config " + configFile.getAbsolutePath());
    assertExitCodeAndStreamSizes(exe, 0, 0, 1);
}
Also used : KcRegExec(org.keycloak.testsuite.cli.KcRegExec)

Example 7 with KcRegExec

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

the class AbstractRegCliTest method testCRUDWithOnTheFlyAuth.

void testCRUDWithOnTheFlyAuth(String serverUrl, String credentials, String extraOptions, String loginMessage) throws IOException {
    File configFile = getDefaultConfigFilePath();
    long lastModified = configFile.exists() ? configFile.lastModified() : 0;
    // This test assumes it is the only user of any instance of on the system
    KcRegExec exe = execute("create --no-config --server " + serverUrl + " --realm test " + credentials + " " + extraOptions + " -s clientId=test-client -o");
    Assert.assertEquals("exitCode == 0", 0, exe.exitCode());
    Assert.assertEquals("login message", loginMessage, exe.stderrLines().get(0));
    ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
    Assert.assertEquals("clientId", "test-client", client.getClientId());
    Assert.assertNotNull("registrationAccessToken not null", client.getRegistrationAccessToken());
    long lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    exe = execute("get test-client --no-config --server " + serverUrl + " --realm test " + credentials + " " + extraOptions);
    assertExitCodeAndStdErrSize(exe, 0, 1);
    ClientRepresentation client2 = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
    Assert.assertEquals("clientId", "test-client", client2.getClientId());
    // we did not provide a token, thus no registrationAccessToken is present
    Assert.assertNull("registrationAccessToken is null", client2.getRegistrationAccessToken());
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    // the token works even though an intermediary invocation was performed,
    // because the previous invocation didn't use a registration access token
    exe = execute("get test-client --no-config --server " + serverUrl + " --realm test " + extraOptions + " -t " + client.getRegistrationAccessToken());
    assertExitCodeAndStdErrSize(exe, 0, 0);
    ClientRepresentation client3 = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
    Assert.assertEquals("clientId", "test-client", client3.getClientId());
    Assert.assertEquals("registrationAccessToken in returned json is different than one returned by create", client.getRegistrationAccessToken(), client3.getRegistrationAccessToken());
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    exe = execute("update test-client --no-config --server " + serverUrl + " --realm test " + credentials + " " + extraOptions + " -s enabled=false -o");
    assertExitCodeAndStdErrSize(exe, 0, 1);
    ClientRepresentation client4 = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
    Assert.assertEquals("clientId", "test-client", client4.getClientId());
    Assert.assertFalse("enabled", client4.isEnabled());
    Assert.assertNull("registrationAccessToken in null", client4.getRegistrationAccessToken());
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    exe = execute("update test-client --no-config --server " + serverUrl + " --realm test " + extraOptions + " -s enabled=true -o -t " + client3.getRegistrationAccessToken());
    assertExitCodeAndStdErrSize(exe, 0, 0);
    ClientRepresentation client5 = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
    Assert.assertEquals("clientId", "test-client", client5.getClientId());
    Assert.assertTrue("enabled", client5.isEnabled());
    Assert.assertNotEquals("registrationAccessToken in returned json is different than one returned by get", client3.getRegistrationAccessToken(), client5.getRegistrationAccessToken());
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    exe = execute("delete test-client --no-config --server " + serverUrl + " --realm test " + credentials + " " + extraOptions);
    assertExitCodeAndStreamSizes(exe, 0, 0, 1);
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
    // subsequent delete should fail
    exe = execute("delete test-client --no-config --server " + serverUrl + " --realm test " + credentials + " " + extraOptions);
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("error message", "Client not found [invalid_request]", exe.stderrLines().get(1));
    lastModified2 = configFile.exists() ? configFile.lastModified() : 0;
    Assert.assertEquals("config file not modified", lastModified, lastModified2);
}
Also used : KcRegExec(org.keycloak.testsuite.cli.KcRegExec) File(java.io.File) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 8 with KcRegExec

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

the class KcRegConfigTest method testRegistrationToken.

@Test
public void testRegistrationToken() throws IOException {
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        // without --server
        KcRegExec exe = execute("config registration-token --config '" + configFile.getName() + "' ");
        assertExitCodeAndStreamSizes(exe, 1, 0, 2);
        Assert.assertEquals("error message", "Required option not specified: --server", exe.stderrLines().get(0));
        Assert.assertEquals("try help", "Try '" + CMD + " help config registration-token' for more information", exe.stderrLines().get(1));
        // without --realm
        exe = execute("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth");
        assertExitCodeAndStreamSizes(exe, 1, 0, 2);
        Assert.assertEquals("error message", "Required option not specified: --realm", exe.stderrLines().get(0));
        Assert.assertEquals("try help", "Try '" + CMD + " help config registration-token' for more information", exe.stderrLines().get(1));
        // without --client
        exe = execute("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth --realm test");
        assertExitCodeAndStreamSizes(exe, 1, 0, 2);
        Assert.assertEquals("error message", "Required option not specified: --client", exe.stderrLines().get(0));
        Assert.assertEquals("try help", "Try '" + CMD + " help config registration-token' for more information", exe.stderrLines().get(1));
        // specify token on cmdline
        exe = execute("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth --realm test --client my_client NEWTOKEN");
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
        if (runIntermittentlyFailingTests()) {
            // don't specify token - must be prompted for it
            exe = KcRegExec.newBuilder().argsLine("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth --realm test --client my_client").executeAsync();
            exe.waitForStdout("Enter Registration Access Token:");
            exe.sendToStdin("NEWTOKEN" + EOL);
            exe.waitCompletion();
            assertExitCodeAndStreamSizes(exe, 0, 1, 0);
        } else {
            System.out.println("TEST SKIPPED PARTIALLY - This test currently suffers from intermittent failures. Use -Dtest.intermittent=true to run it in full.");
        }
        // delete non-existent token
        exe = execute("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth --realm test --client nonexistent --delete");
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
        // delete token
        exe = execute("config registration-token --config '" + configFile.getName() + "' --server http://localhost:8080/auth --realm test --client my_client --delete");
        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) Test(org.junit.Test)

Example 9 with KcRegExec

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

the class KcRegTest method testNoArgs.

@Test
public void testNoArgs() {
    /*
         *  Test (sub)commands without any arguments
         */
    KcRegExec exe = execute("");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    List<String> lines = exe.stdoutLines();
    Assert.assertTrue("stdout output not empty", lines.size() > 0);
    Assert.assertEquals("stdout first line", "Keycloak Client Registration CLI", lines.get(0));
    Assert.assertEquals("stdout one but last line", "Use '" + KcRegExec.CMD + " help <command>' for more information about a given command.", lines.get(lines.size() - 2));
    Assert.assertEquals("stdout last line", "", lines.get(lines.size() - 1));
    /*
         * Test commands without arguments
         */
    exe = execute("config");
    assertExitCodeAndStreamSizes(exe, 1, 0, 1);
    Assert.assertEquals("error message", "Sub-command required by '" + CMD + " config' - one of: 'credentials', 'truststore', 'initial-token', 'registration-token'", exe.stderrLines().get(0));
    exe = execute("config credentials");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " config credentials --server SERVER_URL --realm REALM [ARGUMENTS]", exe.stdoutLines().get(0));
    exe = execute("config initial-token");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " config initial-token --server SERVER --realm REALM [--delete | TOKEN] [ARGUMENTS]", exe.stdoutLines().get(0));
    exe = execute("config registration-token");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " config registration-token --server SERVER --realm REALM --client CLIENT [--delete | TOKEN] [ARGUMENTS]", exe.stdoutLines().get(0));
    exe = execute("config truststore");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " config truststore [TRUSTSTORE | --delete] [--trustpass PASSWORD] [ARGUMENTS]", exe.stdoutLines().get(0));
    exe = execute("create");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " create [ARGUMENTS]", exe.stdoutLines().get(0));
    // Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
    exe = execute("get");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " get CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
    // Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
    exe = execute("update");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " update CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
    // Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
    exe = execute("delete");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " delete CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
    // Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
    exe = execute("attrs");
    Assert.assertEquals("exit code", 0, exe.exitCode());
    Assert.assertTrue("stdout has response", exe.stdoutLines().size() > 10);
    Assert.assertEquals("first line", "Attributes for default format:", exe.stdoutLines().get(0));
    exe = execute("update-token");
    assertExitCodeAndStdErrSize(exe, 1, 0);
    Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
    Assert.assertEquals("help message", "Usage: " + CMD + " update-token CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
    // Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
    exe = execute("help");
    assertExitCodeAndStdErrSize(exe, 0, 0);
    lines = exe.stdoutLines();
    Assert.assertTrue("stdout output not empty", lines.size() > 0);
    Assert.assertEquals("stdout first line", "Keycloak Client Registration CLI", lines.get(0));
    Assert.assertEquals("stdout one but last line", "Use '" + KcRegExec.CMD + " help <command>' for more information about a given command.", lines.get(lines.size() - 2));
    Assert.assertEquals("stdout last line", "", lines.get(lines.size() - 1));
}
Also used : KcRegExec(org.keycloak.testsuite.cli.KcRegExec) Test(org.junit.Test)

Example 10 with KcRegExec

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

the class KcRegTest method testBadCommand.

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

Aggregations

KcRegExec (org.keycloak.testsuite.cli.KcRegExec)28 Test (org.junit.Test)24 FileConfigHandler (org.keycloak.client.registration.cli.config.FileConfigHandler)11 TempFileResource (org.keycloak.testsuite.util.TempFileResource)10 File (java.io.File)8 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)8 ConfigData (org.keycloak.client.registration.cli.config.ConfigData)7 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)5 RealmConfigData (org.keycloak.client.registration.cli.config.RealmConfigData)4 FileOutputStream (java.io.FileOutputStream)2 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Matchers (org.hamcrest.Matchers)1 Assert (org.junit.Assert)1 Assume (org.junit.Assume)1 Before (org.junit.Before)1 OAuth2Constants (org.keycloak.OAuth2Constants)1