Repository navigation
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
...arquillian/tests/base/src/test/java/org/keycloak/testframework/events/EventAssertion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| package org.keycloak.testframework.events; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import org.keycloak.events.Details; | ||
| import org.keycloak.events.EventType; | ||
| import org.keycloak.representations.idm.EventRepresentation; | ||
|
|
||
| import org.hamcrest.MatcherAssert; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.jupiter.api.Assertions; | ||
|
|
||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| /** | ||
| * Helper to assert login events | ||
| */ | ||
| public class EventAssertion { | ||
|
|
||
| private final EventRepresentation event; | ||
|
|
||
| private static final String DEFAULT_IP_ADDRESS = "127.0.0.1"; | ||
| private static final String DEFAULT_IP_ADDRESS_V6 = "0:0:0:0:0:0:0:1"; | ||
| private static final String DEFAULT_IP_ADDRESS_V6_SHORT = "::1"; | ||
|
|
||
| protected EventAssertion(EventRepresentation event) { | ||
| Assertions.assertNotNull(event, "Event was null"); | ||
| Assertions.assertNotNull(event.getId(), "Event id was null"); | ||
| this.event = event; | ||
| } | ||
|
|
||
| /** | ||
| * Assert an expected successfull event | ||
| * | ||
| * @param event the event to assert | ||
| * @return | ||
| */ | ||
| public static EventAssertion assertSuccess(EventRepresentation event) { | ||
| Assertions.assertFalse(event.getType().endsWith("_ERROR"), "Expected successful event"); | ||
| return new EventAssertion(event); | ||
| } | ||
|
|
||
| /** | ||
| * Assert an expected error event | ||
| * | ||
| * @param event the event to assert | ||
| * @return | ||
| */ | ||
| public static EventAssertion assertError(EventRepresentation event) { | ||
| Assertions.assertTrue(event.getType().endsWith("_ERROR"), "Expected error event"); | ||
| return new EventAssertion(event); | ||
| } | ||
|
|
||
| /** | ||
| * Assert an expected LOGIN event | ||
| * | ||
| * @param event the event to assert | ||
| * @return | ||
| */ | ||
| public static EventAssertion expectLoginSuccess(EventRepresentation event) { | ||
| return assertSuccess(event) | ||
| .type(EventType.LOGIN) | ||
| .isCodeId() | ||
| .hasSessionId() | ||
| .hasIpAddress() | ||
| .loginSuccessEventHasAllRequiredDetails(); | ||
| } | ||
|
|
||
| /** | ||
| * Assert an expected LOGIN_ERROR event | ||
| * | ||
| * @param event the event to assert | ||
| * @return | ||
| */ | ||
| public static EventAssertion expectLoginError(EventRepresentation event) { | ||
| return assertError(event) | ||
| .type(EventType.LOGIN_ERROR) | ||
| .isCodeId() | ||
| .hasIpAddress(); | ||
| } | ||
|
|
||
| /** | ||
| * Assert the error message | ||
| * | ||
| * @param error the expected error message | ||
| * @return | ||
| */ | ||
| public EventAssertion error(String error) { | ||
| Assertions.assertEquals(error, event.getError()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the type of the event | ||
| * | ||
| * @param type the expected type of the event | ||
| * @return | ||
| */ | ||
| public EventAssertion type(EventType type) { | ||
| Assertions.assertEquals(type, EventType.valueOf(event.getType())); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event has a sessionId set | ||
| * | ||
| * @return | ||
| */ | ||
| public EventAssertion hasSessionId() { | ||
| MatcherAssert.assertThat(event.getSessionId(), EventMatchers.isSessionId()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event has a userId set | ||
| * | ||
| * @return | ||
| */ | ||
| public EventAssertion hasUserId() { | ||
| MatcherAssert.assertThat(event.getUserId(), EventMatchers.isUUID()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event has the <code>code_id</code> details set | ||
| * @return | ||
| */ | ||
| public EventAssertion isCodeId() { | ||
| MatcherAssert.assertThat(event.getDetails().get(Details.CODE_ID), EventMatchers.isCodeId()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event has an ipAddress set | ||
| * @return | ||
| */ | ||
| public EventAssertion hasIpAddress() { | ||
| Assertions.assertNotNull(event.getIpAddress()); | ||
| Assertions.assertFalse(event.getIpAddress().isEmpty()); | ||
| MatcherAssert.assertThat(event.getIpAddress(), Matchers.anyOf(is(DEFAULT_IP_ADDRESS), is(DEFAULT_IP_ADDRESS_V6), is(DEFAULT_IP_ADDRESS_V6_SHORT))); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the clientId for the event | ||
| * | ||
| * @param clientId the expected clientId | ||
| * @return | ||
| */ | ||
| public EventAssertion clientId(String clientId) { | ||
| Assertions.assertEquals(clientId, event.getClientId()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the sessionId for the event | ||
| * | ||
| * @param sessionId the expected sessionId | ||
| * @return | ||
| */ | ||
| public EventAssertion sessionId(String sessionId) { | ||
| Assertions.assertEquals(sessionId, event.getSessionId()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the userId (sub) of the event | ||
| * | ||
| * @param userId the expected userId | ||
| * @return | ||
| */ | ||
| public EventAssertion userId(String userId) { | ||
| Assertions.assertEquals(userId, event.getUserId()); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event has an entry in the details map with the specified key and value | ||
| * | ||
| * @param key the expected details key | ||
| * @param value the expected details value | ||
| * @return | ||
| */ | ||
| public EventAssertion details(String key, String value) { | ||
| if (value != null) { | ||
| MatcherAssert.assertThat(event.getDetails(), Matchers.hasEntry(key, value)); | ||
| } else { | ||
| withoutDetails(key); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the event details map does not contain the specified keys | ||
| * | ||
| * @param keys the list of keys that are not expected in the details map | ||
| * @return | ||
| */ | ||
| public EventAssertion withoutDetails(String... keys) { | ||
| for (String key : keys) { | ||
| MatcherAssert.assertThat(event.getDetails(), Matchers.not(Matchers.hasKey(key))); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Assert the LOGIN event details with required key set | ||
| * | ||
| * @return | ||
| */ | ||
| private EventAssertion loginSuccessEventHasAllRequiredDetails() { | ||
| Set<String> keyDetails = Set.of("auth_method", "response_type", "redirect_uri", "consent", "code_id", "response_mode"); | ||
| for (String key : keyDetails) { | ||
| MatcherAssert.assertThat(event.getDetails(), Matchers.hasKey(key)); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Return the event associated to the assertion. | ||
| * | ||
| * @return the asserted {@link EventRepresentation} | ||
| */ | ||
| public EventRepresentation getEvent() { | ||
| return event; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rewrite AssertEvents expectLoginEvent to EventAssertion. #48467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.