a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington/*
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * The contents of this file are subject to the terms of the Common Development and
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * Distribution License (the License). You may not use this file except in compliance with the
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * License.
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington *
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * specific language governing permission and limitations under the License.
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington *
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * When distributing Covered Software, include this CDDL Header Notice in each file and include
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * Header, with the fields enclosed by brackets [] replaced by your own identifying
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington * information: "Portions copyright [year] [name of copyright owner]".
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington *
412e1e94939e06eaec3523eafe3e1130349f9486Tom Rumsey * Copyright 2015-2016 ForgeRock AS.
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington */
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonpackage org.forgerock.openam.rest;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static com.google.inject.multibindings.MapBinder.newMapBinder;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.assertj.core.api.Assertions.assertThat;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.forgerock.http.routing.RoutingMode.EQUALS;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.forgerock.http.routing.RoutingMode.STARTS_WITH;
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Majorimport static org.forgerock.json.JsonValue.field;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.json.JsonValue.json;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.json.JsonValue.object;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.json.resource.Responses.newResourceResponse;
dfa51161ad226f5998270e3becb25817774aa168Tony Bamfordimport static org.forgerock.openam.audit.AuditConstants.EventName;
7483f02ef17d2c2051a288b7bcbe4f27f2af9fbbJaco Joosteimport static org.forgerock.openam.audit.AuditConstants.ACCESS_TOPIC;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.openam.audit.AuditConstants.Component.AUTHENTICATION;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.openam.audit.AuditConstants.Component.CONFIG;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.openam.audit.AuditConstants.Component.USERS;
7e863fbb4994becee1defd0839fdd67b33b19342Jaco Joosteimport static org.forgerock.openam.audit.AuditConstants.NO_REALM;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.forgerock.openam.rest.Routers.ssoToken;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.forgerock.util.promise.Promises.newResultPromise;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.mockito.BDDMockito.given;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport static org.mockito.Matchers.any;
412e1e94939e06eaec3523eafe3e1130349f9486Tom Rumseyimport static org.mockito.Matchers.anyString;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.doThrow;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.eq;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.mock;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.spy;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.verify;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport static org.mockito.Mockito.verifyZeroInteractions;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.google.inject.Binder;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.google.inject.Key;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.google.inject.Module;
5af486b23de789a6978e855f20235758122bd2abAndrew Forrestimport com.google.inject.TypeLiteral;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.google.inject.multibindings.MapBinder;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.google.inject.name.Names;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport com.iplanet.sso.SSOException;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.iplanet.sso.SSOToken;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.iplanet.sso.SSOTokenManager;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport com.sun.identity.idm.IdRepoException;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport com.sun.identity.shared.Constants;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport com.sun.identity.shared.debug.Debug;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.guice.core.GuiceModuleLoader;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.guice.core.GuiceModules;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.guice.core.GuiceTestCase;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.guice.core.InjectorConfiguration;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.guice.core.InjectorHolder;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.Handler;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.HttpApplication;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.protocol.Request;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.protocol.Response;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.protocol.Status;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.http.routing.ResourceApiVersionBehaviourManager;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.http.session.Session;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.http.session.SessionContext;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.json.resource.CollectionResourceProvider;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.json.resource.ReadRequest;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.json.resource.Requests;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.json.resource.ResourceException;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.json.resource.ResourceResponse;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.json.resource.Router;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.json.resource.SingletonResourceProvider;
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Majorimport org.forgerock.json.resource.http.HttpContext;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.audit.AbstractHttpAccessAuditFilter;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.audit.AuditConstants;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.audit.AuditEventFactory;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.audit.AuditEventPublisher;
890c120a8dac4ce3f54f804a6776769f5ba3980cJaco Joosteimport org.forgerock.openam.audit.AuditServiceProvider;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.authentication.service.AuthUtilsWrapper;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.core.CoreWrapper;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.http.HttpGuiceModule;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.http.annotations.Get;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport org.forgerock.openam.rest.resource.SSOTokenContext;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.forgerock.openam.rest.router.RestRealmValidator;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport org.forgerock.openam.session.SessionCache;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport org.forgerock.openam.session.SessionConstants;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.services.context.AttributesContext;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.services.context.Context;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.services.context.RequestAuditContext;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.services.context.RootContext;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.services.context.SecurityContext;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.util.promise.NeverThrowsException;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport org.forgerock.util.promise.Promise;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.mockito.ArgumentCaptor;
5af486b23de789a6978e855f20235758122bd2abAndrew Forrestimport org.mockito.Mock;
5af486b23de789a6978e855f20235758122bd2abAndrew Forrestimport org.mockito.MockitoAnnotations;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.testng.annotations.BeforeMethod;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonimport org.testng.annotations.Test;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport javax.inject.Inject;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport javax.inject.Named;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpottsimport javax.security.auth.Subject;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport javax.servlet.http.HttpServletRequest;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport java.lang.annotation.Annotation;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport java.net.URI;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport java.net.URISyntaxException;
5af486b23de789a6978e855f20235758122bd2abAndrew Forrestimport java.security.PrivilegedAction;
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Majorimport java.util.Arrays;
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Majorimport java.util.Collections;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport java.util.HashSet;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Joosteimport java.util.Set;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington@GuiceModules({HttpGuiceModule.class, RestGuiceModule.class})
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunningtonpublic class RestRouterIT extends GuiceTestCase {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private Handler handler;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private SingletonResourceProvider configResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private CollectionResourceProvider usersResource;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste private CollectionResourceProvider internalResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private DashboardResource dashboardResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private AuthenticateResource authenticateResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private AbstractHttpAccessAuditFilter httpAccessAuditFilter;
7483f02ef17d2c2051a288b7bcbe4f27f2af9fbbJaco Jooste private AuditEventPublisher auditEventPublisher;
890c120a8dac4ce3f54f804a6776769f5ba3980cJaco Jooste private AuditServiceProvider auditServiceProvider;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private ResourceApiVersionBehaviourManager versionBehaviourManager;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private SSOTokenManager ssoTokenManager;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private AuthUtilsWrapper authUtilsWrapper;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private CoreWrapper coreWrapper;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private RestRealmValidator realmValidator;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest @Mock
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest private PrivilegedAction<SSOToken> ssoTokenAction;
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @BeforeMethod
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void setupMocks() {
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest MockitoAnnotations.initMocks(this);
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington configResource = mock(SingletonResourceProvider.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington usersResource = mock(CollectionResourceProvider.class);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste internalResource = mock(CollectionResourceProvider.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington dashboardResource = spy(new DashboardResource());
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington authenticateResource = spy(new AuthenticateResource());
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
7483f02ef17d2c2051a288b7bcbe4f27f2af9fbbJaco Jooste httpAccessAuditFilter = spy(new AbstractHttpAccessAuditFilter(AUTHENTICATION, mock(AuditEventPublisher.class)
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste , mock(AuditEventFactory.class)) {
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste @Override
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste protected String getRealm(Context context) {
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste return null;
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste }
e0e60ffbf58765cdd4437bcd5c631ad370e46c8fJaco Jooste });
7483f02ef17d2c2051a288b7bcbe4f27f2af9fbbJaco Jooste auditEventPublisher = mock(AuditEventPublisher.class);
890c120a8dac4ce3f54f804a6776769f5ba3980cJaco Jooste auditServiceProvider = mock(AuditServiceProvider.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington versionBehaviourManager = mock(ResourceApiVersionBehaviourManager.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington ssoTokenManager = mock(SSOTokenManager.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington authUtilsWrapper = mock(AuthUtilsWrapper.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington coreWrapper = mock(CoreWrapper.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington SSOToken adminToken = mock(SSOToken.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(coreWrapper.getAdminToken()).willReturn(adminToken);
412e1e94939e06eaec3523eafe3e1130349f9486Tom Rumsey given(coreWrapper.isValidFQDN(anyString())).willReturn(true);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington realmValidator = mock(RestRealmValidator.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @BeforeMethod(dependsOnMethods = "setupMocks")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void setupGuiceModules() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington InjectorConfiguration.setGuiceModuleLoader(new GuiceModuleLoader() {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public Set<Class<? extends Module>> getGuiceModules(Class<? extends Annotation> clazz) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington return new HashSet<>();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington });
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington super.setupGuiceModules();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void configure(Binder binder) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington MapBinder<AuditConstants.Component, AbstractHttpAccessAuditFilter> httpAccessAuditFilterMapBinder
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington = newMapBinder(binder, AuditConstants.Component.class, AbstractHttpAccessAuditFilter.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington httpAccessAuditFilterMapBinder.addBinding(AUTHENTICATION).toInstance(httpAccessAuditFilter);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
7483f02ef17d2c2051a288b7bcbe4f27f2af9fbbJaco Jooste binder.bind(AuditEventPublisher.class).toInstance(auditEventPublisher);
890c120a8dac4ce3f54f804a6776769f5ba3980cJaco Jooste binder.bind(AuditServiceProvider.class).toInstance(auditServiceProvider);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(Key.get(SingletonResourceProvider.class, Names.named("ConfigResource"))).toInstance(configResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(Key.get(CollectionResourceProvider.class, Names.named("UsersResource"))).toInstance(usersResource);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste binder.bind(Key.get(CollectionResourceProvider.class, Names.named("InternalResource"))).toInstance(internalResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(Key.get(Object.class, Names.named("DashboardResource"))).toInstance(dashboardResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(Key.get(Object.class, Names.named("AuthenticateResource"))).toInstance(authenticateResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(SSOTokenManager.class).toInstance(ssoTokenManager);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(AuthUtilsWrapper.class).toInstance(authUtilsWrapper);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(CoreWrapper.class).toInstance(coreWrapper);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(RestRealmValidator.class).toInstance(realmValidator);
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest
5af486b23de789a6978e855f20235758122bd2abAndrew Forrest binder.bind(new TypeLiteral<PrivilegedAction<SSOToken>>() {}).toInstance(ssoTokenAction);
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts binder.bind(SessionCache.class).toInstance(mock(SessionCache.class));
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts binder.bind(Debug.class).annotatedWith(Names.named(SessionConstants.SESSION_DEBUG))
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts .toInstance(mock(Debug.class));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void configureOverrideBindings(Binder binder) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington binder.bind(ResourceApiVersionBehaviourManager.class).toInstance(versionBehaviourManager);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @BeforeMethod(dependsOnMethods = "setupGuiceModules")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void setup() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler = InjectorHolder.getInstance(HttpApplication.class).start();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington mockDnsAlias("HOSTNAME", "/");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington doThrow(IdRepoException.class).when(coreWrapper).getOrganization(any(SSOToken.class), eq("users"));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington doThrow(IdRepoException.class).when(coreWrapper).getOrganization(any(SSOToken.class), eq("authenticate"));
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste doThrow(IdRepoException.class).when(coreWrapper).getOrganization(any(SSOToken.class), eq("internal"));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadCrestEndpointOnRootHandler() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts Context context = mockRequiredContexts();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/config");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington auditingOff();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(configResource).readInstance(any(Context.class), any(ReadRequest.class));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadChfEndpointOnRootHandler() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Context context = mockContext();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/dashboard");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(dashboardResource).get();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadCrestEndpointOnRealmHandlerWithRootRealm() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts Context context = mockRequiredContexts();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/users/demo");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington auditingOff();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(usersResource).readInstance(contextCaptor.capture(), eq("demo"), any(ReadRequest.class));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington assertThat(contextCaptor.getValue().asContext(RealmContext.class).getResolvedRealm()).isEqualTo("/");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadCrestEndpointOnRealmHandlerWithSubRealm() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts Context context = mockRequiredContexts();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/subrealm/users/demo");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington auditingOff();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington mockRealm("/subrealm");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington ArgumentCaptor<Context> contextCaptor = ArgumentCaptor.forClass(Context.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(usersResource).readInstance(contextCaptor.capture(), eq("demo"), any(ReadRequest.class));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington assertThat(contextCaptor.getValue().asContext(RealmContext.class).getResolvedRealm()).isEqualTo("/subrealm");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadChfEndpointOnRealmHandlerWithRootRealm() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts Context context = mockRequiredContexts();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/authenticate");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(authenticateResource).get();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Test
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void shouldReadChfEndpointOnRealmHandlerWithSubRealm() throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Given
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Context context = mockContext();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = newRequest("GET", "/json/subrealm/authenticate");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington mockRealm("/subrealm");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //When
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington handler.handle(context, request);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington //Then
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington verify(authenticateResource).get();
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste @Test
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste public void shouldNotBePossibleToReachInternalResourceViaChf() throws Exception {
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // Given
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Context context = mockContext();
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Request request = newRequest("GET", "/json/internal");
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // When
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Promise<Response, NeverThrowsException> promise = handler.handle(context, request);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // Then
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Response response = promise.get();
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste assertThat(response.getStatus()).isEqualTo(Status.NOT_FOUND);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste verifyZeroInteractions(internalResource);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste }
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste @Test
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste public void shouldBeAbleToReachInternalViaInternalRouter() throws Exception {
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // Given
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Promise<ResourceResponse, ResourceException> promise =
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste newResultPromise(newResourceResponse("1", "1", json(object())));
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste given(internalResource.readInstance(any(Context.class), eq("123"), any(ReadRequest.class))).willReturn(promise);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste Router internalRouter = InjectorHolder.getInstance(Key.get(Router.class, Names.named("InternalCrestRouter")));
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major Context context = mockRequiredContexts();
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste ReadRequest request = Requests.newReadRequest("internal/123");
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // When
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste internalRouter.handleRead(context, request);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste // Then
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste verify(internalResource).readInstance(any(Context.class), eq("123"), any(ReadRequest.class));
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste }
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private Context mockContext() {
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major return mockContext(null);
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major }
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major private Context mockContext(Context parent) {
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major if (parent == null) {
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major parent = new RootContext();
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major }
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major AttributesContext httpRequestContext = new AttributesContext(new SessionContext(parent, mock(Session.class)));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington httpRequestContext.getAttributes().put(HttpServletRequest.class.getName(), httpServletRequest);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington return new RequestAuditContext(httpRequestContext);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major private Context mockRequiredContexts() {
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major final HttpContext httpContext = new HttpContext(json(object(
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major field(HttpContext.ATTR_HEADERS, Collections.singletonMap("Accept-Language", Arrays.asList("en"))),
9edb7d0c3f707c96c3ee9a9bbe327ea9ffeafcefPeter Major field(HttpContext.ATTR_PARAMETERS, Collections.emptyMap()))), null);
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts SecurityContext securityContext = new SecurityContext(mockContext(httpContext), null, null);
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts return new SSOTokenContext(mock(Debug.class), null, securityContext) {
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts @Override
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts public Subject getCallerSubject() {
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts return new Subject();
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts }
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts @Override
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts public SSOToken getCallerSSOToken() {
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts SSOToken token = mock(SSOToken.class);
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts try {
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts given(token.getProperty(Constants.AM_CTX_ID)).willReturn("TRACKING_ID");
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts given(token.getProperty(Constants.UNIVERSAL_IDENTIFIER)).willReturn("USER_ID");
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts } catch (SSOException e) {
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts // won't happen - it's a mock
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts }
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts return token;
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts }
ae38b3b0c7668321cd32405f4ad510601d6f1276James Phillpotts };
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste }
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private Request newRequest(String method, String uri) throws URISyntaxException {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington Request request = new Request()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .setMethod(method)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .setUri(URI.create(uri));
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington request.getUri().setHost("HOSTNAME");
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington return request;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private void auditingOff() {
dfa51161ad226f5998270e3becb25817774aa168Tony Bamford given(auditEventPublisher.isAuditing(eq(NO_REALM), eq(ACCESS_TOPIC), any(EventName.class))).willReturn(false);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private void mockDnsAlias(String alias, String realm) throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington mockRealmAlias(alias, realm);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private void mockRealm(String realm) throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(coreWrapper.getOrganization(any(SSOToken.class), eq(realm))).willReturn(realm);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(coreWrapper.convertOrgNameToRealmName(realm)).willReturn(realm);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(realmValidator.isRealm(realm)).willReturn(true);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private void mockRealmAlias(String alias, String realm) throws Exception {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(coreWrapper.getOrganization(any(SSOToken.class), eq(alias))).willReturn(realm);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(coreWrapper.convertOrgNameToRealmName(realm)).willReturn(realm);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington given(realmValidator.isRealm(realm)).willReturn(true);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public static final class RestRouteTestRestRouteProvider extends AbstractRestRouteProvider {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private SingletonResourceProvider configResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private CollectionResourceProvider usersResource;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste private CollectionResourceProvider internalResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private Object dashboardResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington private Object authenticateResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void addResourceRoutes(ResourceRouter rootRouter, ResourceRouter realmRouter) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington rootRouter.route("config")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authenticateWith(ssoToken().exceptRead())
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .auditAs(CONFIG)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(1, 1)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(2)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .toSingleton(configResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington realmRouter.route("users")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authenticateWith(ssoToken().exceptRead())
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .auditAs(USERS)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(1, 1)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(2)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .authorizeWith()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .toCollection(usersResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Override
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public void addServiceRoutes(ServiceRouter rootRouter, ServiceRouter realmRouter) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington rootRouter.route("dashboard")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(1, 1)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(2)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .toService(STARTS_WITH, dashboardResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington realmRouter.route("authenticate")
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .auditAs(AUTHENTICATION)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(1, 1)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .forVersion(2)
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .through()
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington .toService(EQUALS, authenticateResource);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste @Override
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste public void addInternalRoutes(ResourceRouter internalRouter) {
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste internalRouter.route("internal")
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste .authenticateWith(ssoToken().exceptRead())
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste .toCollection(internalResource);
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste }
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Inject
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington void setConfigResource(@Named("ConfigResource") SingletonResourceProvider configResource) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington this.configResource = configResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Inject
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington void setUsersResource(@Named("UsersResource") CollectionResourceProvider usersResource) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington this.usersResource = usersResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste @Inject
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste void setInternalResource(@Named("InternalResource") CollectionResourceProvider internalResource) {
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste this.internalResource = internalResource;
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste }
ea968a2519a7cf9282263e27b81ad7eb6706ef3aJaco Jooste
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Inject
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington void setDashboardResource(@Named("DashboardResource") Object dashboardResource) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington this.dashboardResource = dashboardResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Inject
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington void setAuthenticateResource(@Named("AuthenticateResource") Object authenticateResource) {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington this.authenticateResource = authenticateResource;
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public static class DashboardResource {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Get
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public Response get() {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington return new Response(Status.OK);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public static class AuthenticateResource {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington @Get
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington public Response get() {
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington return new Response(Status.OK);
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington }
a6fc9ae6bd096a653649fdf8e4189f102dd4cdf2Phill Cunnington}