From 41a55981039f7b7e1b54f581ff5c7ceb44c277b2 Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Tue, 6 Dec 2016 08:52:38 +0100 Subject: [PATCH] KEYCLOAK-3655 Tests for adding duplicate auth flow --- .../admin/authentication/FlowTest.java | 13 +++- .../org/keycloak/testsuite/util/Matchers.java | 77 +++++++++++++++++++ .../util/matchers/ResponseBodyMatcher.java | 52 +++++++++++++ .../util/matchers/ResponseHeaderMatcher.java | 47 +++++++++++ .../matchers/ResponseStatusCodeMatcher.java | 46 +++++++++++ 5 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/Matchers.java create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseBodyMatcher.java create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseHeaderMatcher.java create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseStatusCodeMatcher.java diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/authentication/FlowTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/authentication/FlowTest.java index bce911703f..990175dcbf 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/authentication/FlowTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/authentication/FlowTest.java @@ -19,6 +19,7 @@ package org.keycloak.testsuite.admin.authentication; import org.junit.Assert; import org.junit.Test; + import org.keycloak.events.admin.OperationType; import org.keycloak.events.admin.ResourceType; import org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation; @@ -31,6 +32,10 @@ import javax.ws.rs.core.Response; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.ws.rs.core.Response.Status; + +import static org.hamcrest.Matchers.*; +import static org.keycloak.testsuite.util.Matchers.*; /** * @author Marko Strukelj @@ -195,7 +200,9 @@ public class FlowTest extends AbstractAuthenticationTest { // copy using existing alias as new name Response response = authMgmtResource.copy("browser", params); try { - Assert.assertEquals("Copy flow using the new alias of existing flow should fail", 409, response.getStatus()); + Assert.assertThat("Copy flow using the new alias of existing flow should fail", response, statusCodeIs(Status.CONFLICT)); + Assert.assertThat("Copy flow using the new alias of existing flow should fail", response, body(containsString("already exists"))); + Assert.assertThat("Copy flow using the new alias of existing flow should fail", response, body(containsString("flow alias"))); } finally { response.close(); } @@ -204,7 +211,7 @@ public class FlowTest extends AbstractAuthenticationTest { params.clear(); response = authMgmtResource.copy("non-existent", params); try { - Assert.assertEquals("Copy non-existing flow", 404, response.getStatus()); + Assert.assertThat("Copy non-existing flow", response, statusCodeIs(Status.NOT_FOUND)); } finally { response.close(); } @@ -214,7 +221,7 @@ public class FlowTest extends AbstractAuthenticationTest { response = authMgmtResource.copy("browser", params); assertAdminEvents.assertEvent(REALM_NAME, OperationType.CREATE, AdminEventPaths.authCopyFlowPath("browser"), params, ResourceType.AUTH_FLOW); try { - Assert.assertEquals("Copy flow", 201, response.getStatus()); + Assert.assertThat("Copy flow", response, statusCodeIs(Status.CREATED)); } finally { response.close(); } diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/Matchers.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/Matchers.java new file mode 100644 index 0000000000..b66e728962 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/Matchers.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.util; + +import org.keycloak.testsuite.util.matchers.ResponseBodyMatcher; +import org.keycloak.testsuite.util.matchers.ResponseHeaderMatcher; +import org.keycloak.testsuite.util.matchers.ResponseStatusCodeMatcher; + +import java.util.Map; +import javax.ws.rs.core.Response; +import org.hamcrest.Matcher; + +/** + * Additional hamcrest matchers for use in {@link org.junit.Assert#assertThat}. + * @author hmlnarik + */ +public class Matchers { + + /** + * Matcher on HTTP status code of a {@link Response} instance. + * @param matcher + * @return + */ + public static Matcher body(Matcher matcher) { + return new ResponseBodyMatcher(matcher); + } + + /** + * Matcher on HTTP status code of a {@link Response} instance. + * @param matcher + * @return + */ + public static Matcher statusCode(Matcher matcher) { + return new ResponseStatusCodeMatcher(matcher); + } + + /** + * Matches when the HTTP status code of a {@link Response} instance is equal to the given code. + * @param expectedStatusCode + * @return + */ + public static Matcher statusCodeIs(Response.Status expectedStatusCode) { + return new ResponseStatusCodeMatcher(org.hamcrest.Matchers.is(expectedStatusCode.getStatusCode())); + } + + /** + * Matches when the HTTP status code of a {@link Response} instance is equal to the given code. + * @param expectedStatusCode + * @return + */ + public static Matcher statusCodeIs(int expectedStatusCode) { + return new ResponseStatusCodeMatcher(org.hamcrest.Matchers.is(expectedStatusCode)); + } + + /** + * Matches when the HTTP status code of a {@link Response} instance is equal to the given code. + * @param expectedStatusCode + * @return + */ + public static Matcher header(Matcher> matcher) { + return new ResponseHeaderMatcher(matcher); + } +} diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseBodyMatcher.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseBodyMatcher.java new file mode 100644 index 0000000000..4a9788fa93 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseBodyMatcher.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.util.matchers; + +import javax.ws.rs.core.Response; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +/** + * Matcher for matching status code of {@link Response} instance. + * @author hmlnarik + */ +public class ResponseBodyMatcher extends BaseMatcher { + + private final Matcher matcher; + + public ResponseBodyMatcher(Matcher matcher) { + this.matcher = matcher; + } + + @Override + public boolean matches(Object item) { + if (item instanceof Response) { + final Response rItem = (Response) item; + rItem.bufferEntity(); + return this.matcher.matches(rItem.readEntity(String.class)); + } else { + return false; + } + } + + @Override + public void describeTo(Description description) { + description.appendText("response body matches ").appendDescriptionOf(this.matcher); + } + +} diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseHeaderMatcher.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseHeaderMatcher.java new file mode 100644 index 0000000000..e2a3bb5b71 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseHeaderMatcher.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.util.matchers; + +import java.util.Map; +import javax.ws.rs.core.Response; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +/** + * Matcher for matching status code of {@link Response} instance. + * @author hmlnarik + */ +public class ResponseHeaderMatcher extends BaseMatcher { + + private final Matcher> matcher; + + public ResponseHeaderMatcher(Matcher> matcher) { + this.matcher = matcher; + } + + @Override + public boolean matches(Object item) { + return (item instanceof Response) && this.matcher.matches(((Response) item).getHeaders()); + } + + @Override + public void describeTo(Description description) { + description.appendText("response headers match ").appendDescriptionOf(this.matcher); + } + +} diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseStatusCodeMatcher.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseStatusCodeMatcher.java new file mode 100644 index 0000000000..a4e1885b98 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/util/matchers/ResponseStatusCodeMatcher.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.util.matchers; + +import javax.ws.rs.core.Response; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +/** + * Matcher for matching status code of {@link Response} instance. + * @author hmlnarik + */ +public class ResponseStatusCodeMatcher extends BaseMatcher { + + private final Matcher matcher; + + public ResponseStatusCodeMatcher(Matcher matcher) { + this.matcher = matcher; + } + + @Override + public boolean matches(Object item) { + return (item instanceof Response) && this.matcher.matches(((Response) item).getStatus()); + } + + @Override + public void describeTo(Description description) { + description.appendText("response status code matches ").appendDescriptionOf(this.matcher); + } + +}