Search in sources :

Example 6 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testDoubleIndirectClientOneChosen.

@Test
public void testDoubleIndirectClientOneChosen() {
    final IndirectClient indirectClient = new MockIndirectClient(NAME, RedirectAction.redirect(PAC4J_URL), new MockCredentials(), new CommonProfile());
    final IndirectClient indirectClient2 = new MockIndirectClient(VALUE, RedirectAction.redirect(PAC4J_BASE_URL), new MockCredentials(), new CommonProfile());
    config.setClients(new Clients(CALLBACK_URL, indirectClient, indirectClient2));
    clients = NAME + "," + VALUE;
    context.addRequestParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, VALUE);
    call();
    assertEquals(302, context.getResponseStatus());
    assertEquals(PAC4J_BASE_URL, context.getResponseLocation());
}
Also used : MockCredentials(org.pac4j.core.credentials.MockCredentials) CommonProfile(org.pac4j.core.profile.CommonProfile) Test(org.junit.Test)

Example 7 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultCallbackLogicTests method testCallbackNoRenew.

@Test
public void testCallbackNoRenew() {
    final String originalSessionId = request.getSession().getId();
    request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, NAME);
    final CommonProfile profile = new CommonProfile();
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, new MockCredentials(), profile);
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    renewSession = false;
    config.getClients().init();
    call();
    final HttpSession session = request.getSession();
    final String newSessionId = session.getId();
    final LinkedHashMap<String, CommonProfile> profiles = (LinkedHashMap<String, CommonProfile>) session.getAttribute(Pac4jConstants.USER_PROFILES);
    assertTrue(profiles.containsValue(profile));
    assertEquals(1, profiles.size());
    assertEquals(newSessionId, originalSessionId);
    assertEquals(302, response.getStatus());
    assertEquals(Pac4jConstants.DEFAULT_URL_VALUE, response.getRedirectedUrl());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) MockCredentials(org.pac4j.core.credentials.MockCredentials) HttpSession(javax.servlet.http.HttpSession) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) IndirectClient(org.pac4j.core.client.IndirectClient) Clients(org.pac4j.core.client.Clients) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 8 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultCallbackLogicTests method testDirectClient.

@Test
public void testDirectClient() {
    request.addParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, NAME);
    final MockDirectClient directClient = new MockDirectClient(NAME, new MockCredentials(), new CommonProfile());
    config.setClients(new Clients(directClient));
    TestsHelper.expectException(() -> call(), TechnicalException.class, "unable to find one indirect client for the callback: check the callback URL for a client name parameter or" + " suffix path or ensure that your configuration defaults to one indirect client");
}
Also used : MockDirectClient(org.pac4j.core.client.MockDirectClient) MockCredentials(org.pac4j.core.credentials.MockCredentials) CommonProfile(org.pac4j.core.profile.CommonProfile) Clients(org.pac4j.core.client.Clients) Test(org.junit.Test)

Example 9 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testAuthorizerThrowsRequiresHttpAction.

@Test
public void testAuthorizerThrowsRequiresHttpAction() {
    final CommonProfile profile = new CommonProfile();
    final LinkedHashMap<String, CommonProfile> profiles = new LinkedHashMap<>();
    profiles.put(NAME, profile);
    context.getSessionStore().set(context, Pac4jConstants.USER_PROFILES, profiles);
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, new MockCredentials(), new CommonProfile());
    authorizers = NAME;
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    config.addAuthorizer(NAME, (context, prof) -> {
        throw HttpAction.status(400, context);
    });
    call();
    assertEquals(400, context.getResponseStatus());
    assertEquals(0, nbCall);
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 10 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testAlreadyAuthenticatedAndAuthorized.

@Test
public void testAlreadyAuthenticatedAndAuthorized() {
    final CommonProfile profile = new CommonProfile();
    profile.setId(ID);
    final LinkedHashMap<String, CommonProfile> profiles = new LinkedHashMap<>();
    profiles.put(NAME, profile);
    context.getSessionStore().set(context, Pac4jConstants.USER_PROFILES, profiles);
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, new MockCredentials(), new CommonProfile());
    authorizers = NAME;
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    config.addAuthorizer(NAME, (context, prof) -> ID.equals(((CommonProfile) prof.get(0)).getId()));
    call();
    assertEquals(-1, context.getResponseStatus());
    assertEquals(1, nbCall);
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 MockCredentials (org.pac4j.core.credentials.MockCredentials)16 CommonProfile (org.pac4j.core.profile.CommonProfile)16 LinkedHashMap (java.util.LinkedHashMap)9 Clients (org.pac4j.core.client.Clients)4 HttpSession (javax.servlet.http.HttpSession)3 IndirectClient (org.pac4j.core.client.IndirectClient)3 MockIndirectClient (org.pac4j.core.client.MockIndirectClient)3 MockDirectClient (org.pac4j.core.client.MockDirectClient)1