Search in sources :

Example 1 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method sendResetPasswordEmailWithRedirect.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void sendResetPasswordEmailWithRedirect() throws IOException {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setEnabled(true);
    userRep.setUsername("user1");
    userRep.setEmail("user1@test.com");
    String id = createUser(userRep);
    UserResource user = realm.users().get(id);
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId("myclient");
    client.setRedirectUris(new LinkedList<>());
    client.getRedirectUris().add("http://myclient.com/*");
    client.setName("myclient");
    client.setEnabled(true);
    Response response = realm.clients().create(client);
    String createdId = ApiUtil.getCreatedId(response);
    assertAdminEvents.assertEvent(realmId, OperationType.CREATE, AdminEventPaths.clientResourcePath(createdId), client, ResourceType.CLIENT);
    List<String> actions = new LinkedList<>();
    actions.add(UserModel.RequiredAction.UPDATE_PASSWORD.name());
    try {
        // test that an invalid redirect uri is rejected.
        user.executeActionsEmail("myclient", "http://unregistered-uri.com/", actions);
        fail("Expected failure");
    } catch (ClientErrorException e) {
        assertEquals(400, e.getResponse().getStatus());
        ErrorRepresentation error = e.getResponse().readEntity(ErrorRepresentation.class);
        Assert.assertEquals("Invalid redirect uri.", error.getErrorMessage());
    }
    user.executeActionsEmail("myclient", "http://myclient.com/home.html", actions);
    assertAdminEvents.assertEvent(realmId, OperationType.ACTION, AdminEventPaths.userResourcePath(id) + "/execute-actions-email", ResourceType.USER);
    Assert.assertEquals(1, greenMail.getReceivedMessages().length);
    MimeMessage message = greenMail.getReceivedMessages()[0];
    String link = MailUtils.getPasswordResetEmailLink(message);
    driver.navigate().to(link);
    proceedPage.assertCurrent();
    assertThat(proceedPage.getInfo(), Matchers.containsString("Update Password"));
    proceedPage.clickProceedLink();
    passwordUpdatePage.assertCurrent();
    passwordUpdatePage.changePassword("new-pass", "new-pass");
    assertEquals("Your account has been updated.", driver.findElement(By.id("kc-page-title")).getText());
    String pageSource = driver.getPageSource();
    // check to make sure the back link is set.
    Assert.assertTrue(pageSource.contains("http://myclient.com/home.html"));
    driver.navigate().to(link);
    assertEquals("We are sorry...", PageUtils.getPageTitle(driver));
}
Also used : Response(javax.ws.rs.core.Response) MimeMessage(javax.mail.internet.MimeMessage) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserResource(org.keycloak.admin.client.resource.UserResource) ClientErrorException(javax.ws.rs.ClientErrorException) LinkedList(java.util.LinkedList) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test)

Example 2 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class UserTest method createDuplicatedUser1.

@Test
public void createDuplicatedUser1() {
    createUser();
    UserRepresentation user = new UserRepresentation();
    user.setUsername("user1");
    try (Response response = realm.users().create(user)) {
        assertEquals(409, response.getStatus());
        assertAdminEvents.assertEmpty();
        // Just to show how to retrieve underlying error message
        ErrorRepresentation error = response.readEntity(ErrorRepresentation.class);
        Assert.assertEquals("User exists with same username", error.getErrorMessage());
    }
}
Also used : Response(javax.ws.rs.core.Response) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 3 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class ImportedRsaKeyProviderTest method assertErrror.

protected void assertErrror(Response response, String error) {
    if (!response.hasEntity()) {
        fail("No error message set");
    }
    ErrorRepresentation errorRepresentation = response.readEntity(ErrorRepresentation.class);
    assertEquals(error, errorRepresentation.getErrorMessage());
    response.close();
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation)

Example 4 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class GeneratedHmacKeyProviderTest method assertErrror.

protected void assertErrror(Response response, String error) {
    if (!response.hasEntity()) {
        fail("No error message set");
    }
    ErrorRepresentation errorRepresentation = response.readEntity(ErrorRepresentation.class);
    assertEquals(error, errorRepresentation.getErrorMessage());
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation)

Example 5 with ErrorRepresentation

use of org.keycloak.representations.idm.ErrorRepresentation in project keycloak by keycloak.

the class GeneratedRsaKeyProviderTest method assertErrror.

protected void assertErrror(Response response, String error) {
    if (!response.hasEntity()) {
        fail("No error message set");
    }
    ErrorRepresentation errorRepresentation = response.readEntity(ErrorRepresentation.class);
    assertEquals(error, errorRepresentation.getErrorMessage());
    response.close();
}
Also used : ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation)

Aggregations

ErrorRepresentation (org.keycloak.representations.idm.ErrorRepresentation)21 Test (org.junit.Test)14 Response (javax.ws.rs.core.Response)11 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)11 ClientErrorException (javax.ws.rs.ClientErrorException)7 UserResource (org.keycloak.admin.client.resource.UserResource)5 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)5 LinkedList (java.util.LinkedList)4 IOException (java.io.IOException)3 List (java.util.List)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Charset (java.nio.charset.Charset)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2