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());
}
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));
}
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());
}
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));
}
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);
}
Aggregations