KEYCLOAK-2912 Migrate events package to new testsuite

This commit is contained in:
Stan Silvert 2016-05-25 15:21:04 -04:00
parent 5ed09acd94
commit 1042a22cf7
8 changed files with 880 additions and 506 deletions

View file

@ -17,6 +17,10 @@
package org.keycloak.testsuite.rest; package org.keycloak.testsuite.rest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.infinispan.Cache; import org.infinispan.Cache;
import org.keycloak.common.util.Time; import org.keycloak.common.util.Time;
import org.keycloak.connections.infinispan.InfinispanConnectionProvider; import org.keycloak.connections.infinispan.InfinispanConnectionProvider;
@ -47,7 +51,19 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.jboss.resteasy.annotations.cache.NoCache;
import org.jboss.resteasy.spi.BadRequestException;
import org.keycloak.events.EventQuery;
import org.keycloak.events.EventStoreProvider;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.AdminEventQuery;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.events.admin.OperationType;
import org.keycloak.models.utils.RepresentationToModel;
import org.keycloak.representations.idm.AuthDetailsRepresentation;
import org.keycloak.services.resources.admin.RealmAuth;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -191,6 +207,280 @@ public class TestingResourceProvider implements RealmResourceProvider {
return Response.ok().build(); return Response.ok().build();
} }
@GET
@Path("/clear-event-store")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore() {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clear();
return Response.ok().build();
}
@GET
@Path("/clear-event-store-for-realm")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore(@QueryParam("realmId") String realmId) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clear(realmId);
return Response.ok().build();
}
@GET
@Path("/clear-event-store-older-than")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore(@QueryParam("realmId") String realmId, @QueryParam("olderThan") long olderThan) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clear(realmId, olderThan);
return Response.ok().build();
}
/**
* Query events
*
* Returns all events, or filters them based on URL query parameters listed here
*
* @param realmId The realm
* @param types The types of events to return
* @param client App or oauth client name
* @param user User id
* @param dateFrom From date
* @param dateTo To date
* @param ipAddress IP address
* @param firstResult Paging offset
* @param maxResults Paging size
* @return
*/
@Path("query-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<EventRepresentation> queryEvents(@QueryParam("realmId") String realmId, @QueryParam("type") List<String> types, @QueryParam("client") String client,
@QueryParam("user") String user, @QueryParam("dateFrom") Date dateFrom, @QueryParam("dateTo") Date dateTo,
@QueryParam("ipAddress") String ipAddress, @QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
EventQuery query = eventStore.createQuery();
if (realmId != null) {
query.realm(realmId);
}
if (client != null) {
query.client(client);
}
if (types != null & !types.isEmpty()) {
EventType[] t = new EventType[types.size()];
for (int i = 0; i < t.length; i++) {
t[i] = EventType.valueOf(types.get(i));
}
query.type(t);
}
if (user != null) {
query.user(user);
}
if(dateFrom != null) {
query.fromDate(dateFrom);
}
if(dateTo != null) {
query.toDate(dateTo);
}
if (ipAddress != null) {
query.ipAddress(ipAddress);
}
if (firstResult != null) {
query.firstResult(firstResult);
}
if (maxResults != null) {
query.maxResults(maxResults);
}
return toEventListRep(query.getResultList());
}
private List<EventRepresentation> toEventListRep(List<Event> events) {
List<EventRepresentation> reps = new ArrayList<>();
for (Event event : events) {
reps.add(ModelToRepresentation.toRepresentation(event));
}
return reps;
}
@PUT
@Path("/on-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onEvent(final EventRepresentation rep) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.onEvent(repToModel(rep));
}
private Event repToModel(EventRepresentation rep) {
Event event = new Event();
event.setClientId(rep.getClientId());
event.setDetails(rep.getDetails());
event.setError(rep.getError());
event.setIpAddress(rep.getIpAddress());
event.setRealmId(rep.getRealmId());
event.setSessionId(rep.getSessionId());
event.setTime(rep.getTime());
event.setType(EventType.valueOf(rep.getType()));
event.setUserId(rep.getUserId());
return event;
}
@GET
@Path("/clear-admin-event-store")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore() {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clearAdmin();
return Response.ok().build();
}
@GET
@Path("/clear-admin-event-store-for-realm")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore(@QueryParam("realmId") String realmId) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clearAdmin(realmId);
return Response.ok().build();
}
@GET
@Path("/clear-admin-event-store-older-than")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore(@QueryParam("realmId") String realmId, @QueryParam("olderThan") long olderThan) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.clearAdmin(realmId, olderThan);
return Response.ok().build();
}
/**
* Get admin events
*
* Returns all admin events, or filters events based on URL query parameters listed here
*
* @param realmId
* @param operationTypes
* @param authRealm
* @param authClient
* @param authUser user id
* @param authIpAddress
* @param resourcePath
* @param dateFrom
* @param dateTo
* @param firstResult
* @param maxResults
* @return
*/
@Path("query-admin-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<AdminEventRepresentation> getAdminEvents(@QueryParam("realmId") String realmId, @QueryParam("operationTypes") List<String> operationTypes, @QueryParam("authRealm") String authRealm, @QueryParam("authClient") String authClient,
@QueryParam("authUser") String authUser, @QueryParam("authIpAddress") String authIpAddress,
@QueryParam("resourcePath") String resourcePath, @QueryParam("dateFrom") Date dateFrom,
@QueryParam("dateTo") Date dateTo, @QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
AdminEventQuery query = eventStore.createAdminQuery();
if (realmId != null) {
query.realm(realmId);
};
if (authRealm != null) {
query.authRealm(authRealm);
}
if (authClient != null) {
query.authClient(authClient);
}
if (authUser != null) {
query.authUser(authUser);
}
if (authIpAddress != null) {
query.authIpAddress(authIpAddress);
}
if (resourcePath != null) {
query.resourcePath(resourcePath);
}
if (operationTypes != null && !operationTypes.isEmpty()) {
OperationType[] t = new OperationType[operationTypes.size()];
for (int i = 0; i < t.length; i++) {
t[i] = OperationType.valueOf(operationTypes.get(i));
}
query.operation(t);
}
if(dateFrom != null) {
query.fromTime(dateFrom);
}
if(dateTo != null) {
query.toTime(dateTo);
}
if (firstResult != null) {
query.firstResult(firstResult);
}
if (maxResults != null) {
query.maxResults(maxResults);
}
return toAdminEventRep(query.getResultList());
}
private List<AdminEventRepresentation> toAdminEventRep(List<AdminEvent> events) {
List<AdminEventRepresentation> reps = new ArrayList<>();
for (AdminEvent event : events) {
reps.add(ModelToRepresentation.toRepresentation(event));
}
return reps;
}
@POST
@Path("/on-admin-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onAdminEvent(final AdminEventRepresentation rep, @QueryParam("includeRepresentation") boolean includeRepresentation) {
EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
eventStore.onEvent(repToModel(rep), includeRepresentation);
}
private AdminEvent repToModel(AdminEventRepresentation rep) {
AdminEvent event = new AdminEvent();
event.setAuthDetails(repToModel(rep.getAuthDetails()));
event.setError(rep.getError());
event.setOperationType(OperationType.valueOf(rep.getOperationType()));
event.setRealmId(rep.getRealmId());
event.setRepresentation(rep.getRepresentation());
event.setResourcePath(rep.getResourcePath());
event.setTime(rep.getTime());
return event;
}
private AuthDetails repToModel(AuthDetailsRepresentation rep) {
AuthDetails details = new AuthDetails();
details.setClientId(rep.getClientId());
details.setIpAddress(rep.getIpAddress());
details.setRealmId(rep.getRealmId());
details.setUserId(rep.getUserId());
return details;
}
@GET @GET
@Path("/cache/{cache}/{id}") @Path("/cache/{cache}/{id}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

View file

@ -17,6 +17,8 @@
package org.keycloak.testsuite.client.resources; package org.keycloak.testsuite.client.resources;
import java.util.Date;
import java.util.List;
import org.keycloak.representations.idm.AdminEventRepresentation; import org.keycloak.representations.idm.AdminEventRepresentation;
import org.keycloak.representations.idm.EventRepresentation; import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.testsuite.rest.representation.AuthenticatorState; import org.keycloak.testsuite.rest.representation.AuthenticatorState;
@ -32,6 +34,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.Map; import java.util.Map;
import org.jboss.resteasy.annotations.cache.NoCache;
/** /**
* @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a> * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
@ -72,6 +75,99 @@ public interface TestingResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
Response clearAdminEventQueue(); Response clearAdminEventQueue();
@GET
@Path("/clear-event-store")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore();
@GET
@Path("/clear-event-store-for-realm")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore(@QueryParam("realmId") String realmId);
@GET
@Path("/clear-event-store-older-than")
@Produces(MediaType.APPLICATION_JSON)
public Response clearEventStore(@QueryParam("realmId") String realmId, @QueryParam("olderThan") long olderThan);
/**
* Query events
*
* Returns all events, or filters them based on URL query parameters listed here
*
* @param realmId The realm
* @param types The types of events to return
* @param client App or oauth client name
* @param user User id
* @param dateFrom From date
* @param dateTo To date
* @param ipAddress IP address
* @param firstResult Paging offset
* @param maxResults Paging size
* @return
*/
@Path("query-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<EventRepresentation> queryEvents(@QueryParam("realmId") String realmId, @QueryParam("type") List<String> types, @QueryParam("client") String client,
@QueryParam("user") String user, @QueryParam("dateFrom") Date dateFrom, @QueryParam("dateTo") Date dateTo,
@QueryParam("ipAddress") String ipAddress, @QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);
@PUT
@Path("/on-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onEvent(final EventRepresentation rep);
@GET
@Path("/clear-admin-event-store")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore();
@GET
@Path("/clear-admin-event-store-for-realm")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore(@QueryParam("realmId") String realmId);
@GET
@Path("/clear-admin-event-store-older-than")
@Produces(MediaType.APPLICATION_JSON)
public Response clearAdminEventStore(@QueryParam("realmId") String realmId, @QueryParam("olderThan") long olderThan);
/**
* Get admin events
*
* Returns all admin events, or filters events based on URL query parameters listed here
*
* @param realmId
* @param operationTypes
* @param authRealm
* @param authClient
* @param authUser user id
* @param authIpAddress
* @param resourcePath
* @param dateFrom
* @param dateTo
* @param firstResult
* @param maxResults
* @return
*/
@Path("query-admin-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<AdminEventRepresentation> getAdminEvents(@QueryParam("realmId") String realmId, @QueryParam("operationTypes") List<String> operationTypes, @QueryParam("authRealm") String authRealm, @QueryParam("authClient") String authClient,
@QueryParam("authUser") String authUser, @QueryParam("authIpAddress") String authIpAddress,
@QueryParam("resourcePath") String resourcePath, @QueryParam("dateFrom") Date dateFrom,
@QueryParam("dateTo") Date dateTo, @QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults);
@POST
@Path("/on-admin-event")
@Consumes(MediaType.APPLICATION_JSON)
public void onAdminEvent(final AdminEventRepresentation rep, @QueryParam("includeRepresentation") boolean includeRepresentation);
@POST @POST
@Path("/remove-user-session") @Path("/remove-user-session")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

View file

@ -0,0 +1,48 @@
/*
* Copyright 2016 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @author tags. All rights reserved.
*
* 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.events;
import java.util.ArrayList;
import java.util.List;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.client.resources.TestingResource;
/**
*
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public abstract class AbstractEventsTest extends AbstractKeycloakTest {
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
}
protected TestingResource testing() {
return getTestingClient().testing();
}
protected List<String> toList(Enum... enumTypes) {
List<String> enumList = new ArrayList<>();
for (Enum type : enumTypes) {
enumList.add(type.toString());
}
return enumList;
}
}

View file

@ -0,0 +1,220 @@
/*
* 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.events;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.events.admin.OperationType;
import org.keycloak.representations.idm.AdminEventRepresentation;
import org.keycloak.representations.idm.AuthDetailsRepresentation;
/**
* @author <a href="mailto:giriraj.sharma27@gmail.com">Giriraj Sharma</a>
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class AdminEventStoreProviderTest extends AbstractEventsTest {
@After
public void after() {
testing().clearAdminEventStore();
}
@Test
public void save() {
testing().onAdminEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
}
@Test
public void query() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
testing().onAdminEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create("realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(oldest, "realmId", OperationType.CREATE, "realmId", "clientId2", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
Assert.assertEquals(5, testing().getAdminEvents(null, null, null, "clientId", null, null, null, null, null, null, null).size());
Assert.assertEquals(5, testing().getAdminEvents(null, null, "realmId", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, toList(OperationType.CREATE), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, toList(OperationType.CREATE, OperationType.ACTION), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, "userId", null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().getAdminEvents(null, toList(OperationType.ACTION), null, null, "userId", null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, null, 2).size());
Assert.assertEquals(1, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, 5, null).size());
Assert.assertEquals(newest, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, null, 1).get(0).getTime());
Assert.assertEquals(oldest, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, 5, 1).get(0).getTime());
testing().clearAdminEventStore("realmId");
testing().clearAdminEventStore("realmId2");
Assert.assertEquals(0, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, null, null).size());
String d1 = new String("2015-03-04");
String d2 = new String("2015-03-05");
String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try {
date1 = formatter.parse(d1);
date2 = formatter.parse(d2);
date3 = formatter.parse(d3);
date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) {
e.printStackTrace();
}
testing().onAdminEvent(create(date1, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date1, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date2, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date2, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date3, "realmId", OperationType.UPDATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date3, "realmId", OperationType.DELETE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date4, "realmId2", OperationType.CREATE, "realmId2", "clientId2", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(date4, "realmId2", OperationType.CREATE, "realmId2", "clientId2", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, "clientId", null, null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().getAdminEvents(null, null, null, "clientId2", null, null, null, null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, "realmId", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().getAdminEvents(null, null, "realmId2", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, "userId", null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, "userId2", null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().getAdminEvents(null, toList(OperationType.ACTION), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, toList(OperationType.CREATE, OperationType.ACTION), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().getAdminEvents(null, toList(OperationType.UPDATE), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().getAdminEvents(null, toList(OperationType.DELETE), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, toList(OperationType.CREATE), null, null, null, null, null, null, null, null, null).size());
Assert.assertEquals(8, testing().getAdminEvents(null, null, null, null, null, null, null, date1, null, null, null).size());
Assert.assertEquals(8, testing().getAdminEvents(null, null, null, null, null, null, null, null, date4, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, null, null, null, date3, null, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, null, null, null, null, date2, null, null).size());
Assert.assertEquals(0, testing().getAdminEvents(null, null, null, null, null, null, null, date7, null, null, null).size());
Assert.assertEquals(0, testing().getAdminEvents(null, null, null, null, null, null, null, null, date6, null, null).size());
Assert.assertEquals(8, testing().getAdminEvents(null, null, null, null, null, null, null, date1, date4, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, null, date2, date4, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, null, null, null, date1, date2, null, null).size());
Assert.assertEquals(4, testing().getAdminEvents(null, null, null, null, null, null, null, date3, date4, null, null).size());
Assert.assertEquals(0, testing().getAdminEvents(null, null, null, null, null, null, null, date5, date6, null, null).size());
Assert.assertEquals(0, testing().getAdminEvents(null, null, null, null, null, null, null, date7, date8, null, null).size());
}
@Test
public void queryResourcePath() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
testing().onAdminEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create("realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(oldest, "realmId", OperationType.CREATE, "realmId", "clientId2", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/admin", null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/realms", null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/master", null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/admin/realms", null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/realms/master", null, null, null, null).size());
Assert.assertEquals(6, testing().getAdminEvents(null, null, null, null, null, null, "/admin/realms/master", null, null, null, null).size());
}
@Test
public void clear() {
testing().onAdminEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis() - 20000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis() - 30000, "realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().clearAdminEventStore("realmId");
Assert.assertEquals(1, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, null, null).size());
}
@Test
public void clearOld() {
testing().onAdminEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis() - 20000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().onAdminEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
testing().clearAdminEventStore("realmId", System.currentTimeMillis() - 10000);
Assert.assertEquals(2, testing().getAdminEvents(null, null, null, null, null, null, null, null, null, null, null).size());
}
private AdminEventRepresentation create(String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
return create(System.currentTimeMillis(), realmId, operation, authRealmId, authClientId, authUserId, authIpAddress, resourcePath, error);
}
private AdminEventRepresentation create(Date date, String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
return create(date.getTime(), realmId, operation, authRealmId, authClientId, authUserId, authIpAddress, resourcePath, error);
}
private AdminEventRepresentation create(long time, String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
AdminEventRepresentation e = new AdminEventRepresentation();
e.setTime(time);
e.setRealmId(realmId);
e.setOperationType(operation.toString());
AuthDetailsRepresentation authDetails = new AuthDetailsRepresentation();
authDetails.setRealmId(authRealmId);
authDetails.setClientId(authClientId);
authDetails.setUserId(authUserId);
authDetails.setIpAddress(authIpAddress);
e.setAuthDetails(authDetails);
e.setResourcePath(resourcePath);
e.setError(error);
return e;
}
}

View file

@ -0,0 +1,218 @@
/*
* 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.events;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.events.EventType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.keycloak.representations.idm.EventRepresentation;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
* @author Stan Silvert ssilvert@redhat.com (C) 2016 Red Hat Inc.
*/
public class EventStoreProviderTest extends AbstractEventsTest {
@After
public void after() {
testing().clearEventStore();
}
@Test
public void save() {
testing().onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
}
@Test
public void query() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
testing().onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
testing().onEvent(create(EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(oldest, EventType.LOGIN, "realmId", "clientId2", "userId", "127.0.0.1", "error"));
testing().onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
Assert.assertEquals(5, testing().queryEvents(null, null, "clientId", null, null, null, null, null, null).size());
Assert.assertEquals(5, testing().queryEvents("realmId", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, toList(EventType.LOGIN), null, null, null, null, null, null, null).size());
Assert.assertEquals(6, testing().queryEvents(null, toList(EventType.LOGIN, EventType.REGISTER), null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, "userId", null, null, null, null, null).size());
Assert.assertEquals(1, testing().queryEvents(null, toList(EventType.REGISTER), null, "userId", null, null, null, null, null).size());
Assert.assertEquals(2, testing().queryEvents(null, null, null, null, null, null, null, null, 2).size());
Assert.assertEquals(1, testing().queryEvents(null, null, null, null, null, null, null, 5, null).size());
Assert.assertEquals(newest, testing().queryEvents(null, null, null, null, null, null, null, null, 1).get(0).getTime());
Assert.assertEquals(oldest, testing().queryEvents(null, null, null, null, null, null, null, 5, 1).get(0).getTime());
testing().clearEventStore("realmId");
testing().clearEventStore("realmId2");
Assert.assertEquals(0, testing().queryEvents(null, null, null, null, null, null, null, null, null).size());
String d1 = new String("2015-03-04");
String d2 = new String("2015-03-05");
String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try {
date1 = formatter.parse(d1);
date2 = formatter.parse(d2);
date3 = formatter.parse(d3);
date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) {
e.printStackTrace();
}
testing().onEvent(create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(date3, EventType.CODE_TO_TOKEN, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
testing().onEvent(create(date3, EventType.LOGOUT, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
testing().onEvent(create(date4, EventType.UPDATE_PROFILE, "realmId2", "clientId2", "userId2", "127.0.0.1", "error"));
testing().onEvent(create(date4, EventType.UPDATE_EMAIL, "realmId2", "clientId2", "userId2", "127.0.0.1", "error"));
Assert.assertEquals(6, testing().queryEvents(null, null, "clientId", null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().queryEvents(null, null, "clientId2", null, null, null, null, null, null).size());
Assert.assertEquals(6, testing().queryEvents("realmId", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().queryEvents("realmId2", null, null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, "userId", null, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, "userId2", null, null, null, null, null).size());
Assert.assertEquals(2, testing().queryEvents(null, toList(EventType.LOGIN), null, null, null, null, null, null, null).size());
Assert.assertEquals(2, testing().queryEvents(null, toList(EventType.REGISTER), null, null, null, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, toList(EventType.LOGIN, EventType.REGISTER), null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().queryEvents(null, toList(EventType.CODE_TO_TOKEN), null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().queryEvents(null, toList(EventType.LOGOUT), null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().queryEvents(null, toList(EventType.UPDATE_PROFILE), null, null, null, null, null, null, null).size());
Assert.assertEquals(1, testing().queryEvents(null, toList(EventType.UPDATE_EMAIL), null, null, null, null, null, null, null).size());
Assert.assertEquals(8, testing().queryEvents(null, null, null, null, date1, null, null, null, null).size());
Assert.assertEquals(8, testing().queryEvents(null, null, null, null, null, date4, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, null, date3, null, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, null, null, date2, null, null, null).size());
Assert.assertEquals(0, testing().queryEvents(null, null, null, null, date7, null, null, null, null).size());
Assert.assertEquals(0, testing().queryEvents(null, null, null, null, null, date6, null, null, null).size());
Assert.assertEquals(8, testing().queryEvents(null, null, null, null, date1, date4, null, null, null).size());
Assert.assertEquals(6, testing().queryEvents(null, null, null, null, date2, date4, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, null, date1, date2, null, null, null).size());
Assert.assertEquals(4, testing().queryEvents(null, null, null, null, date3, date4, null, null, null).size());
Assert.assertEquals(0, testing().queryEvents(null, null, null, null, date5, date6, null, null, null).size());
Assert.assertEquals(0, testing().queryEvents(null, null, null, null, date7, date8, null, null, null).size());
}
@Test
public void clear() {
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
testing().clearEventStore("realmId");
Assert.assertEquals(1, testing().queryEvents(null, null, null, null, null, null, null, null, null).size());
}
@Test
public void lengthExceedLimit(){
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", StringUtils.repeat("clientId", 100), "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, StringUtils.repeat("realmId", 100), "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", StringUtils.repeat("userId", 100), "127.0.0.1", "error"));
}
@Test
public void maxLengthWithNull(){
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, null, null, null, "127.0.0.1", "error"));
}
@Test
public void clearOld() {
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
testing().onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
testing().clearEventStore("realmId", System.currentTimeMillis() - 10000);
Assert.assertEquals(3, testing().queryEvents(null, null, null, null, null, null, null, null, null).size());
}
private EventRepresentation create(EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
return create(System.currentTimeMillis(), event, realmId, clientId, userId, ipAddress, error);
}
private EventRepresentation create(Date date, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
return create(date.getTime(), event, realmId, clientId, userId, ipAddress, error);
}
private EventRepresentation create(long time, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
EventRepresentation e = new EventRepresentation();
e.setTime(time);
e.setType(event.toString());
e.setRealmId(realmId);
e.setClientId(clientId);
e.setUserId(userId);
e.setIpAddress(ipAddress);
e.setError(error);
Map<String, String> details = new HashMap<String, String>();
details.put("key1", "value1");
details.put("key2", "value2");
e.setDetails(details);
return e;
}
}

View file

@ -17,6 +17,7 @@
package org.keycloak.testsuite.util; package org.keycloak.testsuite.util;
import java.util.Collections;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
@ -72,6 +73,12 @@ public class RealmBuilder {
return this; return this;
} }
public RealmBuilder events() {
rep.setEventsEnabled(true);
rep.setEnabledEventTypes(Collections.<String>emptyList()); // enables all types
return this;
}
public RealmBuilder testMail() { public RealmBuilder testMail() {
Map<String, String> config = new HashMap<>(); Map<String, String> config = new HashMap<>();
config.put("from", MailServerConfiguration.FROM); config.put("from", MailServerConfiguration.FROM);

View file

@ -1,254 +0,0 @@
/*
* 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.events;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.keycloak.events.EventStoreProvider;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.events.admin.OperationType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.testsuite.rule.KeycloakRule;
/**
* @author <a href="mailto:giriraj.sharma27@gmail.com">Giriraj Sharma</a>
*/
public class AdminEventStoreProviderTest {
@ClassRule
public static KeycloakRule kc = new KeycloakRule();
private KeycloakSession session;
private EventStoreProvider eventStore;
@Before
public void before() {
session = kc.startSession();
eventStore = session.getProvider(EventStoreProvider.class);
}
@After
public void after() {
eventStore.clearAdmin();
kc.stopSession(session, true);
}
@Test
public void save() {
eventStore.onEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
}
@Test
public void query() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
eventStore.onEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create("realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(oldest, "realmId", OperationType.CREATE, "realmId", "clientId2", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
resetSession();
Assert.assertEquals(5, eventStore.createAdminQuery().authClient("clientId").getResultList().size());
Assert.assertEquals(5, eventStore.createAdminQuery().authRealm("realmId").getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().operation(OperationType.CREATE).getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().operation(OperationType.CREATE, OperationType.ACTION).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().authUser("userId").getResultList().size());
Assert.assertEquals(1, eventStore.createAdminQuery().authUser("userId").operation(OperationType.ACTION).getResultList().size());
Assert.assertEquals(2, eventStore.createAdminQuery().maxResults(2).getResultList().size());
Assert.assertEquals(1, eventStore.createAdminQuery().firstResult(5).maxResults(5).getResultList().size());
Assert.assertEquals(newest, eventStore.createAdminQuery().maxResults(1).getResultList().get(0).getTime());
Assert.assertEquals(oldest, eventStore.createAdminQuery().firstResult(5).maxResults(1).getResultList().get(0).getTime());
eventStore.clearAdmin("realmId");
eventStore.clearAdmin("realmId2");
Assert.assertEquals(0, eventStore.createAdminQuery().getResultList().size());
String d1 = new String("2015-03-04");
String d2 = new String("2015-03-05");
String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try {
date1 = formatter.parse(d1);
date2 = formatter.parse(d2);
date3 = formatter.parse(d3);
date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) {
e.printStackTrace();
}
eventStore.onEvent(create(date1, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date1, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date2, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date2, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date3, "realmId", OperationType.UPDATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date3, "realmId", OperationType.DELETE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date4, "realmId2", OperationType.CREATE, "realmId2", "clientId2", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(date4, "realmId2", OperationType.CREATE, "realmId2", "clientId2", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
resetSession();
Assert.assertEquals(6, eventStore.createAdminQuery().authClient("clientId").getResultList().size());
Assert.assertEquals(2, eventStore.createAdminQuery().authClient("clientId2").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().authRealm("realmId").getResultList().size());
Assert.assertEquals(2, eventStore.createAdminQuery().authRealm("realmId2").getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().authUser("userId").getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().authUser("userId2").getResultList().size());
Assert.assertEquals(2, eventStore.createAdminQuery().operation(OperationType.ACTION).getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().operation(OperationType.CREATE, OperationType.ACTION).getResultList().size());
Assert.assertEquals(1, eventStore.createAdminQuery().operation(OperationType.UPDATE).getResultList().size());
Assert.assertEquals(1, eventStore.createAdminQuery().operation(OperationType.DELETE).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().operation(OperationType.CREATE).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().fromTime(date1).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().toTime(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date3).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().toTime(date2).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date7).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().toTime(date6).getResultList().size());
Assert.assertEquals(8, eventStore.createAdminQuery().fromTime(date1).toTime(date4).getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().fromTime(date2).toTime(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date1).toTime(date2).getResultList().size());
Assert.assertEquals(4, eventStore.createAdminQuery().fromTime(date3).toTime(date4).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date5).toTime(date6).getResultList().size());
Assert.assertEquals(0, eventStore.createAdminQuery().fromTime(date7).toTime(date8).getResultList().size());
}
@Test
public void queryResourcePath() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
eventStore.onEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(newest, "realmId", OperationType.ACTION, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create("realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(oldest, "realmId", OperationType.CREATE, "realmId", "clientId2", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create("realmId", OperationType.CREATE, "realmId", "clientId", "userId2", "127.0.0.1", "/admin/realms/master", "error"), false);
resetSession();
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/admin").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/realms").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/master").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/admin/realms").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/realms/master").getResultList().size());
Assert.assertEquals(6, eventStore.createAdminQuery().resourcePath("/admin/realms/master").getResultList().size());
}
@Test
public void clear() {
eventStore.onEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis() - 20000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis() - 30000, "realmId2", OperationType.CREATE, "realmId2", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
resetSession();
eventStore.clearAdmin("realmId");
Assert.assertEquals(1, eventStore.createAdminQuery().getResultList().size());
}
@Test
public void clearOld() {
eventStore.onEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis() - 20000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis(), "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
eventStore.onEvent(create(System.currentTimeMillis() - 30000, "realmId", OperationType.CREATE, "realmId", "clientId", "userId", "127.0.0.1", "/admin/realms/master", "error"), false);
resetSession();
eventStore.clearAdmin("realmId", System.currentTimeMillis() - 10000);
Assert.assertEquals(2, eventStore.createAdminQuery().getResultList().size());
}
private AdminEvent create(String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
return create(System.currentTimeMillis(), realmId, operation, authRealmId, authClientId, authUserId, authIpAddress, resourcePath, error);
}
private AdminEvent create(Date date, String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
return create(date.getTime(), realmId, operation, authRealmId, authClientId, authUserId, authIpAddress, resourcePath, error);
}
private AdminEvent create(long time, String realmId, OperationType operation, String authRealmId, String authClientId, String authUserId, String authIpAddress, String resourcePath, String error) {
AdminEvent e = new AdminEvent();
e.setTime(time);
e.setRealmId(realmId);
e.setOperationType(operation);
AuthDetails authDetails = new AuthDetails();
authDetails.setRealmId(authRealmId);
authDetails.setClientId(authClientId);
authDetails.setUserId(authUserId);
authDetails.setIpAddress(authIpAddress);
e.setAuthDetails(authDetails);
e.setResourcePath(resourcePath);
e.setError(error);
return e;
}
private void resetSession() {
kc.stopSession(session, true);
session = kc.startSession();
eventStore = session.getProvider(EventStoreProvider.class);
}
}

View file

@ -1,251 +0,0 @@
/*
* 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.events;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.keycloak.events.Event;
import org.keycloak.events.EventStoreProvider;
import org.keycloak.events.EventType;
import org.keycloak.models.KeycloakSession;
import org.keycloak.testsuite.rule.KeycloakRule;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class EventStoreProviderTest {
@ClassRule
public static KeycloakRule kc = new KeycloakRule();
private KeycloakSession session;
private EventStoreProvider eventStore;
@Before
public void before() {
session = kc.startSession();
eventStore = session.getProvider(EventStoreProvider.class);
}
@After
public void after() {
eventStore.clear();
kc.stopSession(session, true);
}
@Test
public void save() {
eventStore.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
}
@Test
public void query() {
long oldest = System.currentTimeMillis() - 30000;
long newest = System.currentTimeMillis() + 30000;
eventStore.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(newest, EventType.REGISTER, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
eventStore.onEvent(create(EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(oldest, EventType.LOGIN, "realmId", "clientId2", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(EventType.LOGIN, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
resetSession();
Assert.assertEquals(5, eventStore.createQuery().client("clientId").getResultList().size());
Assert.assertEquals(5, eventStore.createQuery().realm("realmId").getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().type(EventType.LOGIN).getResultList().size());
Assert.assertEquals(6, eventStore.createQuery().type(EventType.LOGIN, EventType.REGISTER).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().user("userId").getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().user("userId").type(EventType.REGISTER).getResultList().size());
Assert.assertEquals(2, eventStore.createQuery().maxResults(2).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().firstResult(5).getResultList().size());
Assert.assertEquals(newest, eventStore.createQuery().maxResults(1).getResultList().get(0).getTime());
Assert.assertEquals(oldest, eventStore.createQuery().firstResult(5).maxResults(1).getResultList().get(0).getTime());
eventStore.clear("realmId");
eventStore.clear("realmId2");
Assert.assertEquals(0, eventStore.createQuery().getResultList().size());
String d1 = new String("2015-03-04");
String d2 = new String("2015-03-05");
String d3 = new String("2015-03-06");
String d4 = new String("2015-03-07");
String d5 = new String("2015-03-01");
String d6 = new String("2015-03-03");
String d7 = new String("2015-03-08");
String d8 = new String("2015-03-10");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null, date2 = null, date3 = null, date4 = null;
Date date5 = null, date6 = null, date7 = null, date8 = null;
try {
date1 = formatter.parse(d1);
date2 = formatter.parse(d2);
date3 = formatter.parse(d3);
date4 = formatter.parse(d4);
date5 = formatter.parse(d5);
date6 = formatter.parse(d6);
date7 = formatter.parse(d7);
date8 = formatter.parse(d8);
} catch (ParseException e) {
e.printStackTrace();
}
eventStore.onEvent(create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(date3, EventType.CODE_TO_TOKEN, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
eventStore.onEvent(create(date3, EventType.LOGOUT, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
eventStore.onEvent(create(date4, EventType.UPDATE_PROFILE, "realmId2", "clientId2", "userId2", "127.0.0.1", "error"));
eventStore.onEvent(create(date4, EventType.UPDATE_EMAIL, "realmId2", "clientId2", "userId2", "127.0.0.1", "error"));
resetSession();
Assert.assertEquals(6, eventStore.createQuery().client("clientId").getResultList().size());
Assert.assertEquals(2, eventStore.createQuery().client("clientId2").getResultList().size());
Assert.assertEquals(6, eventStore.createQuery().realm("realmId").getResultList().size());
Assert.assertEquals(2, eventStore.createQuery().realm("realmId2").getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().user("userId").getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().user("userId2").getResultList().size());
Assert.assertEquals(2, eventStore.createQuery().type(EventType.LOGIN).getResultList().size());
Assert.assertEquals(2, eventStore.createQuery().type(EventType.REGISTER).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().type(EventType.LOGIN, EventType.REGISTER).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().type(EventType.CODE_TO_TOKEN).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().type(EventType.LOGOUT).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_PROFILE).getResultList().size());
Assert.assertEquals(1, eventStore.createQuery().type(EventType.UPDATE_EMAIL).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().fromDate(date1).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().toDate(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate(date3).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().toDate(date2).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate(date7).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().toDate(date6).getResultList().size());
Assert.assertEquals(8, eventStore.createQuery().fromDate(date1).toDate(date4).getResultList().size());
Assert.assertEquals(6, eventStore.createQuery().fromDate(date2).toDate(date4).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate(date1).toDate(date2).getResultList().size());
Assert.assertEquals(4, eventStore.createQuery().fromDate(date3).toDate(date4).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate(date5).toDate(date6).getResultList().size());
Assert.assertEquals(0, eventStore.createQuery().fromDate(date7).toDate(date8).getResultList().size());
}
@Test
public void clear() {
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
resetSession();
eventStore.clear("realmId");
Assert.assertEquals(1, eventStore.createQuery().getResultList().size());
}
@Test
public void lengthExceedLimit(){
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", StringUtils.repeat("clientId", 100), "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, StringUtils.repeat("realmId", 100), "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", StringUtils.repeat("userId", 100), "127.0.0.1", "error"));
}
@Test
public void maxLengthWithNull(){
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, null, null, null, "127.0.0.1", "error"));
}
@Test
public void clearOld() {
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 20000, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis(), EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
eventStore.onEvent(create(System.currentTimeMillis() - 30000, EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
resetSession();
eventStore.clear("realmId", System.currentTimeMillis() - 10000);
Assert.assertEquals(3, eventStore.createQuery().getResultList().size());
}
private Event create(EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
return create(System.currentTimeMillis(), event, realmId, clientId, userId, ipAddress, error);
}
private Event create(Date date, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
return create(date.getTime(), event, realmId, clientId, userId, ipAddress, error);
}
private Event create(long time, EventType event, String realmId, String clientId, String userId, String ipAddress, String error) {
Event e = new Event();
e.setTime(time);
e.setType(event);
e.setRealmId(realmId);
e.setClientId(clientId);
e.setUserId(userId);
e.setIpAddress(ipAddress);
e.setError(error);
Map<String, String> details = new HashMap<String, String>();
details.put("key1", "value1");
details.put("key2", "value2");
e.setDetails(details);
return e;
}
private void resetSession() {
kc.stopSession(session, true);
session = kc.startSession();
eventStore = session.getProvider(EventStoreProvider.class);
}
}