Skip to content

Commit

Permalink
cherry pick change from 6.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjcool authored and mmoayyed committed May 26, 2020
1 parent 46f1ce9 commit f909bc6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static String mapExceptionToMessage(final AuthenticationException authnh
final Throwable ex) {
val authnMsg = StringUtils.defaultIfBlank(ex.getMessage(), "Authentication Failure: " + authnhandlerErrors.getMessage());
val authnBundleMsg = getTranslatedMessageForExceptionClass(ex.getClass().getSimpleName(), request, applicationContext);
return String.format("%s:%s:%s", ex.getClass().getSimpleName(), authnMsg, authnBundleMsg);
return String.format("%s:%s", authnMsg, authnBundleMsg);
}

private String getTranslatedMessageForExceptionClass(final String className,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.apereo.cas;

import org.apereo.cas.config.CasRestConfigurationTests;
import org.apereo.cas.support.rest.RestResourceUtilsTests;
import org.apereo.cas.support.rest.ServiceTicketResourceTests;
import org.apereo.cas.support.rest.TicketGrantingTicketResourceTests;
import org.apereo.cas.support.rest.TicketStatusResourceTests;
Expand All @@ -17,6 +18,7 @@
* @since 5.2.0
*/
@SelectClasses({
RestResourceUtilsTests.class,
CasRestConfigurationTests.class,
TicketGrantingTicketResourceTests.class,
ServiceTicketResourceTests.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.apereo.cas.support.rest;

import org.apereo.cas.authentication.AuthenticationException;
import org.apereo.cas.authentication.adaptive.UnauthorizedAuthenticationException;
import org.apereo.cas.support.rest.resources.RestResourceUtils;
import org.apereo.cas.util.CollectionUtils;

import lombok.val;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.*;

/**
* This is {@link RestResourceUtilsTests}.
*
* @author john.j.cool
* @since 6.1.6
*/
@Tag("RestfulApi")
public class RestResourceUtilsTests {
@Test
public void verifyCreateResponseEntityForAuthnFailure() {
val request = new MockHttpServletRequest();
val map = CollectionUtils.<String, Throwable>wrap(
UnauthorizedAuthenticationException.class.getSimpleName(),
new UnauthorizedAuthenticationException("test")
);
val applicationContext = new StaticApplicationContext();
applicationContext.refresh();
val response = RestResourceUtils.createResponseEntityForAuthnFailure(
new AuthenticationException("test", map, new HashMap<>(0)),
request, applicationContext);

assertTrue(response.getStatusCode().isError());
assertNotNull(response.getBody());
assertFalse(response.getBody().contains(UnauthorizedAuthenticationException.class.getSimpleName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void creationOfSTWithAuthenticationException() throws Exception {
.param(PASSWORD, TEST_VALUE))
.andExpect(status().isUnauthorized())
.andReturn().getResponse().getContentAsString();
assertTrue(content.contains("LoginException"));
assertTrue(content.contains("Login failed"));
}

private void configureCasMockSTCreationToThrow(final Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void creationOfTGTWithAuthenticationException() throws Exception {
.param(PASSWORD, TEST_VALUE))
.andExpect(status().isUnauthorized())
.andReturn().getResponse().getContentAsString();
assertTrue(content.contains("LoginException"));
assertTrue(content.contains("Login failed"));
}

@Test
Expand Down

0 comments on commit f909bc6

Please sign in to comment.