Search in sources :

Example 51 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class ParameterClientTests method testAuthentication.

@Test
public void testAuthentication() {
    final ParameterClient client = new ParameterClient(PARAMETER_NAME, new SimpleTestTokenAuthenticator());
    client.setSupportGetRequest(SUPPORT_GET);
    client.setSupportPostRequest(SUPPORT_POST);
    final MockWebContext context = MockWebContext.create();
    context.addRequestParameter(PARAMETER_NAME, VALUE);
    context.setRequestMethod(HttpConstants.HTTP_METHOD.GET.name());
    final TokenCredentials credentials = client.getCredentials(context);
    final CommonProfile profile = client.getUserProfile(credentials, context);
    assertEquals(VALUE, profile.getId());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) CommonProfile(org.pac4j.core.profile.CommonProfile) SimpleTestTokenAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 52 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class FormClientTests method testGetCredentialsMissingUsername.

@Test
public void testGetCredentialsMissingUsername() {
    final FormClient formClient = getFormClient();
    final MockWebContext context = MockWebContext.create();
    TestsHelper.expectException(() -> formClient.getCredentials(context.addRequestParameter(formClient.getUsernameParameter(), USERNAME)), HttpAction.class, "Perfoming a 302 HTTP action");
    assertEquals(302, context.getResponseStatus());
    assertEquals(LOGIN_URL + "?" + formClient.getUsernameParameter() + "=" + USERNAME + "&" + FormClient.ERROR_PARAMETER + "=" + FormClient.MISSING_FIELD_ERROR, context.getResponseHeaders().get(HttpConstants.LOCATION_HEADER));
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Example 53 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class FormClientTests method testGetUserProfile.

@Test
public void testGetUserProfile() {
    final FormClient formClient = getFormClient();
    formClient.setProfileCreator((credentials, context) -> {
        String username = credentials.getUsername();
        final CommonProfile profile = new CommonProfile();
        profile.setId(username);
        profile.addAttribute(Pac4jConstants.USERNAME, username);
        return profile;
    });
    final MockWebContext context = MockWebContext.create();
    final CommonProfile profile = formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, USERNAME), context);
    assertEquals(USERNAME, profile.getId());
    assertEquals(CommonProfile.class.getName() + CommonProfile.SEPARATOR + USERNAME, profile.getTypedId());
    assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CommonProfile.class));
    assertEquals(USERNAME, profile.getUsername());
    assertEquals(1, profile.getAttributes().size());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) CommonProfile(org.pac4j.core.profile.CommonProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 54 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class FormClientTests method testGetCredentials.

@Test
public void testGetCredentials() {
    final FormClient formClient = getFormClient();
    final MockWebContext context = MockWebContext.create();
    TestsHelper.expectException(() -> formClient.getCredentials(context.addRequestParameter(formClient.getUsernameParameter(), USERNAME).addRequestParameter(formClient.getPasswordParameter(), PASSWORD)), HttpAction.class, "Perfoming a 302 HTTP action");
    assertEquals(302, context.getResponseStatus());
    assertEquals(LOGIN_URL + "?" + formClient.getUsernameParameter() + "=" + USERNAME + "&" + FormClient.ERROR_PARAMETER + "=" + CredentialsException.class.getSimpleName(), context.getResponseHeaders().get(HttpConstants.LOCATION_HEADER));
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Example 55 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class IndirectBasicAuthClientTests method testGetCredentialsMissingHeader.

@Test
public void testGetCredentialsMissingHeader() {
    final IndirectBasicAuthClient basicAuthClient = getBasicAuthClient();
    final MockWebContext context = MockWebContext.create();
    verifyGetCredentialsFailsWithAuthenticationRequired(basicAuthClient, context);
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Aggregations

MockWebContext (org.pac4j.core.context.MockWebContext)96 Test (org.junit.Test)91 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)20 CommonProfile (org.pac4j.core.profile.CommonProfile)18 CasConfiguration (org.pac4j.cas.config.CasConfiguration)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)5 HttpAction (org.pac4j.core.exception.HttpAction)5 CasProfile (org.pac4j.cas.profile.CasProfile)4 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)4 DigestCredentials (org.pac4j.http.credentials.DigestCredentials)3 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)2 CasRestProfile (org.pac4j.cas.profile.CasRestProfile)2 Credentials (org.pac4j.core.credentials.Credentials)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 KerberosCredentials (org.pac4j.kerberos.credentials.KerberosCredentials)2 URL (java.net.URL)1 IndirectClient (org.pac4j.core.client.IndirectClient)1 Cookie (org.pac4j.core.context.Cookie)1 PathParameterCallbackUrlResolver (org.pac4j.core.http.callback.PathParameterCallbackUrlResolver)1 JavaSerializationHelper (org.pac4j.core.util.JavaSerializationHelper)1