Skip to content

Commit a987d14

Browse files
[TNZ-27070]: refactor tests after review
1 parent 8a87453 commit a987d14

File tree

2 files changed

+86
-137
lines changed

2 files changed

+86
-137
lines changed

server/src/test/java/org/cloudfoundry/identity/uaa/provider/saml/OpenSaml4AuthenticationProviderUnitTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,50 @@ void authenticateWhenNonStandardPortMismatchThenThrowsException() {
228228
.satisfies(errorOf(Saml2ErrorCodes.INVALID_DESTINATION));
229229
}
230230

231+
@Test
232+
void authenticateWhenDestinationIsEmpty_thenSkipsValidationWithoutNPE() {
233+
// Test that when destination is empty/null, validation is skipped without throwing NPE
234+
String validLocation = DESTINATION;
235+
236+
// Create response with null/empty destination
237+
Response response = response(null, ASSERTING_PARTY_ENTITY_ID);
238+
Assertion assertion = assertion();
239+
assertion.getSubject().getSubjectConfirmations().forEach(sc ->
240+
sc.getSubjectConfirmationData().setRecipient(validLocation));
241+
response.getAssertions().add(assertion);
242+
243+
RelyingPartyRegistration.Builder registrationBuilder = verifying(registration())
244+
.assertionConsumerServiceLocation(validLocation);
245+
246+
Saml2AuthenticationToken token = token(signed(response), registrationBuilder);
247+
248+
// This should not throw a NullPointerException when destination is null
249+
// The validation should be skipped since StringUtils.hasText(destination) returns false
250+
assertThatNoException().isThrownBy(() -> this.provider.authenticate(token));
251+
}
252+
253+
@Test
254+
void authenticateWhenMalformedUrlsButIdentical_thenSucceeds() {
255+
// Test that malformed URLs that can't be normalized still work if they're identical
256+
// This tests the robustness of the comparison when normalization might fail
257+
String malformedUrl = "http://[malformed:url:with:brackets]/saml/SSO/alias/integration-saml-entity-id";
258+
259+
Response response = response(malformedUrl, ASSERTING_PARTY_ENTITY_ID);
260+
Assertion assertion = assertion();
261+
assertion.getSubject().getSubjectConfirmations().forEach(sc ->
262+
sc.getSubjectConfirmationData().setRecipient(malformedUrl));
263+
response.getAssertions().add(assertion);
264+
265+
RelyingPartyRegistration.Builder registrationBuilder = verifying(registration())
266+
.assertionConsumerServiceLocation(malformedUrl);
267+
268+
Saml2AuthenticationToken token = token(signed(response), registrationBuilder);
269+
270+
// Even with malformed URLs that normalization can't handle,
271+
// authentication should succeed if both URLs are identical
272+
assertThatNoException().isThrownBy(() -> this.provider.authenticate(token));
273+
}
274+
231275
@Test
232276
void authenticateWhenNoAssertionsPresentThenThrowAuthenticationException() {
233277
Saml2AuthenticationToken token = token();

server/src/test/java/org/cloudfoundry/identity/uaa/util/UaaUrlUtilsTest.java

Lines changed: 42 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -713,148 +713,53 @@ private static List<String> convertToHttps(List<String> urls) {
713713
}
714714

715715
// Tests for normalizeUrlForPortComparison method
716-
@Test
717-
void normalizeUrlForPortComparison_withHttpStandardPort_removesPort() {
718-
String urlWithPort = "http://example.com:80/path";
719-
String expected = "http://example.com/path";
720-
721-
String result = normalizeUrlForPortComparison(urlWithPort);
722-
723-
assertThat(result).isEqualTo(expected);
724-
}
725-
726-
@Test
727-
void normalizeUrlForPortComparison_withHttpsStandardPort_removesPort() {
728-
String urlWithPort = "https://example.com:443/path";
729-
String expected = "https://example.com/path";
730-
731-
String result = normalizeUrlForPortComparison(urlWithPort);
732-
733-
assertThat(result).isEqualTo(expected);
734-
}
735-
736-
@Test
737-
void normalizeUrlForPortComparison_withHttpNonStandardPort_keepsPort() {
738-
String urlWithPort = "http://example.com:8080/path";
739-
String expected = "http://example.com:8080/path";
740-
741-
String result = normalizeUrlForPortComparison(urlWithPort);
742-
743-
assertThat(result).isEqualTo(expected);
744-
}
745-
746-
@Test
747-
void normalizeUrlForPortComparison_withHttpsNonStandardPort_keepsPort() {
748-
String urlWithPort = "https://example.com:8443/path";
749-
String expected = "https://example.com:8443/path";
750-
751-
String result = normalizeUrlForPortComparison(urlWithPort);
752-
753-
assertThat(result).isEqualTo(expected);
754-
}
755-
756-
@Test
757-
void normalizeUrlForPortComparison_withNoPort_remainsUnchanged() {
758-
String urlWithoutPort = "http://example.com/path";
759-
String expected = "http://example.com/path";
760-
761-
String result = normalizeUrlForPortComparison(urlWithoutPort);
762-
763-
assertThat(result).isEqualTo(expected);
764-
}
765-
766-
@Test
767-
void normalizeUrlForPortComparison_withHttpsNoPort_remainsUnchanged() {
768-
String urlWithoutPort = "https://example.com/path";
769-
String expected = "https://example.com/path";
770-
771-
String result = normalizeUrlForPortComparison(urlWithoutPort);
772-
773-
assertThat(result).isEqualTo(expected);
774-
}
775-
776-
@Test
777-
void normalizeUrlForPortComparison_withQueryParams_preservesQueryParams() {
778-
String urlWithPort = "http://example.com:80/path?param1=value1&param2=value2";
779-
String expected = "http://example.com/path?param1=value1&param2=value2";
780-
781-
String result = normalizeUrlForPortComparison(urlWithPort);
782-
783-
assertThat(result).isEqualTo(expected);
784-
}
785-
786-
@Test
787-
void normalizeUrlForPortComparison_withFragment_preservesFragment() {
788-
String urlWithPort = "https://example.com:443/path#section1";
789-
String expected = "https://example.com/path#section1";
790-
791-
String result = normalizeUrlForPortComparison(urlWithPort);
792-
793-
assertThat(result).isEqualTo(expected);
794-
}
795-
796-
@Test
797-
void normalizeUrlForPortComparison_withMalformedUrl_returnsOriginal() {
798-
String malformedUrl = "not-a-valid-url";
799-
800-
String result = normalizeUrlForPortComparison(malformedUrl);
801-
802-
assertThat(result).isEqualTo(malformedUrl);
716+
@ParameterizedTest(name = "normalizeUrlForPortComparison: \"{0}\" should become \"{1}\"")
717+
@CsvSource({
718+
// Standard port removal
719+
"'http://example.com:80/path', 'http://example.com/path'",
720+
"'https://example.com:443/path', 'https://example.com/path'",
721+
722+
// Non-standard ports preserved
723+
"'http://example.com:8080/path', 'http://example.com:8080/path'",
724+
"'https://example.com:8443/path', 'https://example.com:8443/path'",
725+
726+
// URLs without explicit ports remain unchanged
727+
"'http://example.com/path', 'http://example.com/path'",
728+
"'https://example.com/path', 'https://example.com/path'",
729+
730+
// Query parameters and fragments preserved
731+
"'http://example.com:80/path?param1=value1&param2=value2', 'http://example.com/path?param1=value1&param2=value2'",
732+
"'https://example.com:443/path#section1', 'https://example.com/path#section1'",
733+
734+
// Complex URLs with user info
735+
"'http://user:[email protected]:80/path/to/resource?query=value#fragment', 'http://user:[email protected]/path/to/resource?query=value#fragment'",
736+
737+
// Subdomains preserved
738+
"'https://subdomain.example.com:443/path', 'https://subdomain.example.com/path'",
739+
740+
// Different schemes keep non-standard ports
741+
"'ftp://example.com:80/path', 'ftp://example.com:80/path'",
742+
743+
// SAML-specific URL
744+
"'https://uaa.example.com:443/saml/SSO/alias/provider-name?RelayState=https://app.example.com&param=value#section', 'https://uaa.example.com/saml/SSO/alias/provider-name?RelayState=https://app.example.com&param=value#section'",
745+
746+
// Malformed URLs return unchanged
747+
"'not-a-valid-url', 'not-a-valid-url'",
748+
"'http://[invalid:url:with:colons:everywhere', 'http://[invalid:url:with:colons:everywhere'",
749+
"'http://example.com:80/path with spaces and special chars!@#$%^&*()', 'http://example.com:80/path with spaces and special chars!@#$%^&*()'",
750+
751+
// Empty string
752+
"'', ''"
753+
})
754+
void normalizeUrlForPortComparison_parameterizedTests(String inputUrl, String expectedUrl) {
755+
String result = normalizeUrlForPortComparison(inputUrl);
756+
assertThat(result).isEqualTo(expectedUrl);
757+
assertThat(result).isNotNull(); // Ensure we never return null for non-null input
803758
}
804759

805760
@Test
806761
void normalizeUrlForPortComparison_withNullUrl_returnsNull() {
807762
String result = normalizeUrlForPortComparison(null);
808-
809763
assertThat(result).isNull();
810764
}
811-
812-
@Test
813-
void normalizeUrlForPortComparison_withEmptyUrl_returnsEmpty() {
814-
String emptyUrl = "";
815-
816-
String result = normalizeUrlForPortComparison(emptyUrl);
817-
818-
assertThat(result).isEqualTo(emptyUrl);
819-
}
820-
821-
@Test
822-
void normalizeUrlForPortComparison_withComplexUrl_handlesCorrectly() {
823-
String complexUrl = "http://user:[email protected]:80/path/to/resource?query=value#fragment";
824-
String expected = "http://user:[email protected]/path/to/resource?query=value#fragment";
825-
826-
String result = normalizeUrlForPortComparison(complexUrl);
827-
828-
assertThat(result).isEqualTo(expected);
829-
}
830-
831-
@Test
832-
void normalizeUrlForPortComparison_withSubdomain_preservesSubdomain() {
833-
String urlWithSubdomain = "https://subdomain.example.com:443/path";
834-
String expected = "https://subdomain.example.com/path";
835-
836-
String result = normalizeUrlForPortComparison(urlWithSubdomain);
837-
838-
assertThat(result).isEqualTo(expected);
839-
}
840-
841-
@Test
842-
void normalizeUrlForPortComparison_withDifferentScheme_keepsPort() {
843-
String urlWithPort = "ftp://example.com:80/path";
844-
String expected = "ftp://example.com:80/path";
845-
846-
String result = normalizeUrlForPortComparison(urlWithPort);
847-
848-
assertThat(result).isEqualTo(expected);
849-
}
850-
851-
@Test
852-
void normalizeUrlForPortComparison_withSamlSpecificUrl_handlesCorrectly() {
853-
String samlUrl = "https://uaa.example.com:443/saml/SSO/alias/provider-name?RelayState=https://app.example.com&param=value#section";
854-
String expected = "https://uaa.example.com/saml/SSO/alias/provider-name?RelayState=https://app.example.com&param=value#section";
855-
856-
String result = normalizeUrlForPortComparison(samlUrl);
857-
858-
assertThat(result).isEqualTo(expected);
859-
}
860765
}

0 commit comments

Comments
 (0)