Skip to content

Commit dfa67fd

Browse files
committed
Polish Tests
- Use test objects - Ensure assertThat is checked Issue gh-11725
1 parent e6dfb63 commit dfa67fd

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/authentication/OpenSaml4AuthenticationProviderTests.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.opensaml.saml.saml2.core.EncryptedAssertion;
5252
import org.opensaml.saml.saml2.core.EncryptedAttribute;
5353
import org.opensaml.saml.saml2.core.EncryptedID;
54-
import org.opensaml.saml.saml2.core.Issuer;
5554
import org.opensaml.saml.saml2.core.NameID;
5655
import org.opensaml.saml.saml2.core.OneTimeUse;
5756
import org.opensaml.saml.saml2.core.ProxyRestriction;
@@ -737,16 +736,7 @@ public void authenticateWhenCustomResponseValidatorThenUses() {
737736

738737
@Test
739738
public void authenticateWhenResponseStatusIsNotSuccessThenOnlyReturnParentStatusCodes() {
740-
ResponseToken mockResponseToken = mock(ResponseToken.class);
741-
Saml2AuthenticationToken mockSamlToken = mock(Saml2AuthenticationToken.class);
742-
given(mockResponseToken.getToken()).willReturn(mockSamlToken);
743-
744-
RelyingPartyRegistration mockRelyingPartyRegistration = mock(RelyingPartyRegistration.class);
745-
given(mockSamlToken.getRelyingPartyRegistration()).willReturn(mockRelyingPartyRegistration);
746-
747-
RelyingPartyRegistration.AssertingPartyDetails mockAssertingPartyDetails = mock(
748-
RelyingPartyRegistration.AssertingPartyDetails.class);
749-
given(mockRelyingPartyRegistration.getAssertingPartyDetails()).willReturn(mockAssertingPartyDetails);
739+
Saml2AuthenticationToken token = TestSaml2AuthenticationTokens.token();
750740

751741
Status parentStatus = new StatusBuilder().buildObject();
752742
StatusCode parentStatusCode = new StatusCodeBuilder().buildObject();
@@ -756,40 +746,27 @@ public void authenticateWhenResponseStatusIsNotSuccessThenOnlyReturnParentStatus
756746
parentStatusCode.setStatusCode(childStatusCode);
757747
parentStatus.setStatusCode(parentStatusCode);
758748

759-
Response mockResponse = mock(Response.class);
760-
given(mockResponse.getStatus()).willReturn(parentStatus);
761-
Issuer mockIssuer = mock(Issuer.class);
762-
given(mockIssuer.getValue()).willReturn("mockedIssuer");
763-
given(mockResponse.getIssuer()).willReturn(mockIssuer);
764-
765-
given(mockResponseToken.getResponse()).willReturn(mockResponse);
749+
Response response = TestOpenSamlObjects.response();
750+
response.setStatus(parentStatus);
751+
response.setIssuer(TestOpenSamlObjects.issuer("mockedIssuer"));
766752

767753
Converter<ResponseToken, Saml2ResponseValidatorResult> validator = OpenSaml4AuthenticationProvider
768754
.createDefaultResponseValidator();
769-
Saml2ResponseValidatorResult result = validator.convert(mockResponseToken);
755+
Saml2ResponseValidatorResult result = validator.convert(new ResponseToken(response, token));
770756

771757
String expectedErrorMessage = String.format("Invalid status [%s] for SAML response",
772758
parentStatusCode.getValue());
773759
assertThat(
774-
result.getErrors().stream().anyMatch((error) -> error.getDescription().contains(expectedErrorMessage)));
760+
result.getErrors().stream().anyMatch((error) -> error.getDescription().contains(expectedErrorMessage)))
761+
.isTrue();
775762
assertThat(result.getErrors()
776763
.stream()
777-
.noneMatch((error) -> error.getDescription().contains(childStatusCode.getValue())));
764+
.noneMatch((error) -> error.getDescription().contains(childStatusCode.getValue()))).isTrue();
778765
}
779766

780767
@Test
781768
public void authenticateWhenResponseStatusIsNotSuccessThenReturnParentAndChildStatusCode() {
782-
ResponseToken mockResponseToken = mock(ResponseToken.class);
783-
Saml2AuthenticationToken mockSamlToken = mock(Saml2AuthenticationToken.class);
784-
given(mockResponseToken.getToken()).willReturn(mockSamlToken);
785-
786-
RelyingPartyRegistration mockRelyingPartyRegistration = mock(RelyingPartyRegistration.class);
787-
given(mockSamlToken.getRelyingPartyRegistration()).willReturn(mockRelyingPartyRegistration);
788-
789-
RelyingPartyRegistration.AssertingPartyDetails mockAssertingPartyDetails = mock(
790-
RelyingPartyRegistration.AssertingPartyDetails.class);
791-
given(mockRelyingPartyRegistration.getAssertingPartyDetails()).willReturn(mockAssertingPartyDetails);
792-
769+
Saml2AuthenticationToken token = TestSaml2AuthenticationTokens.token();
793770
Status parentStatus = new StatusBuilder().buildObject();
794771
StatusCode parentStatusCode = new StatusCodeBuilder().buildObject();
795772
parentStatusCode.setValue(StatusCode.REQUESTER);
@@ -798,28 +775,24 @@ public void authenticateWhenResponseStatusIsNotSuccessThenReturnParentAndChildSt
798775
parentStatusCode.setStatusCode(childStatusCode);
799776
parentStatus.setStatusCode(parentStatusCode);
800777

801-
Response mockResponse = mock(Response.class);
802-
given(mockResponse.getStatus()).willReturn(parentStatus);
803-
Issuer mockIssuer = mock(Issuer.class);
804-
given(mockIssuer.getValue()).willReturn("mockedIssuer");
805-
given(mockResponse.getIssuer()).willReturn(mockIssuer);
806-
807-
given(mockResponseToken.getResponse()).willReturn(mockResponse);
778+
Response response = TestOpenSamlObjects.response();
779+
response.setStatus(parentStatus);
780+
response.setIssuer(TestOpenSamlObjects.issuer("mockedIssuer"));
808781

809782
Converter<ResponseToken, Saml2ResponseValidatorResult> validator = OpenSaml4AuthenticationProvider
810783
.createDefaultResponseValidator();
811-
Saml2ResponseValidatorResult result = validator.convert(mockResponseToken);
784+
Saml2ResponseValidatorResult result = validator.convert(new ResponseToken(response, token));
812785

813786
String expectedParentErrorMessage = String.format("Invalid status [%s] for SAML response",
814787
parentStatusCode.getValue());
815788
String expectedChildErrorMessage = String.format("Invalid status [%s] for SAML response",
816789
childStatusCode.getValue());
817790
assertThat(result.getErrors()
818791
.stream()
819-
.anyMatch((error) -> error.getDescription().contains(expectedParentErrorMessage)));
792+
.anyMatch((error) -> error.getDescription().contains(expectedParentErrorMessage))).isTrue();
820793
assertThat(result.getErrors()
821794
.stream()
822-
.anyMatch((error) -> error.getDescription().contains(expectedChildErrorMessage)));
795+
.anyMatch((error) -> error.getDescription().contains(expectedChildErrorMessage))).isTrue();
823796
}
824797

825798
@Test

0 commit comments

Comments
 (0)