diff --git a/src/test/java/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest.java b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest.java new file mode 100644 index 00000000..411e0c34 --- /dev/null +++ b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest.java @@ -0,0 +1,159 @@ +package hudson.plugins.active_directory; + +import org.htmlunit.FailingHttpStatusCodeException; +import org.htmlunit.html.HtmlButton; +import org.htmlunit.html.HtmlElement; +import org.htmlunit.html.HtmlForm; +import org.htmlunit.html.HtmlInput; +import org.htmlunit.html.HtmlPage; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.jvnet.hudson.test.FlagRule; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.recipes.LocalData; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +public class ActiveDirectoryDomainIntegrationTest { + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @ClassRule + public static FlagRule fipsSystemPropertyRule = + FlagRule.systemProperty("jenkins.security.FIPS140.COMPLIANCE", "true"); + + + /** + * Tests the behavior of the "Test Domain" button when a short password is configured. + * + *

For the preconfigured value, the password is "small" in the local data. + * When the "Test Domain" button is clicked, the page should display an error message + * indicating that the password is too short, along with an "angry Jenkins" error message.

+ * + */ + @LocalData + @Test + public void testActiveDirectoryDomainTestDomainButtonClickWithShortPassword() throws Exception { + JenkinsRule.WebClient webClient = jenkins.createWebClient(); + // Navigate to the configuration page + HtmlPage configPage = webClient.goTo("configureSecurity"); + HtmlForm form = configPage.getFormByName("config"); + + //Check that the password is too short message is present + assertTrue(form.asNormalizedText().contains(Messages.passwordTooShortFIPS())); + + // Click the "Test Domain" button + HtmlPage resultPage = getButtonByText(form, "Test Domain").click(); + + webClient.waitForBackgroundJavaScript(2000); // Wait for up to 5 seconds + + String responseContent = resultPage.asNormalizedText(); + // Assert that the error message is present in the page content + assertTrue(responseContent.contains("A problem occurred while processing the request")); + + //Check that the password is too short message is present + assertTrue(responseContent.contains(Messages.passwordTooShortFIPS())); + } + + /** + * Tests the behavior of the "Save" button when a short password is configured. + * + *

For the preconfigured value, the password is "small" in the local data. + * When the "Save" button is clicked, an exception is expected because the password + * does not meet the minimum length requirement.

+ * + */ + @LocalData + @Test + public void testActiveDirectoryDomainSaveButtonClick() throws Exception { + JenkinsRule.WebClient webClient = jenkins.createWebClient(); + // Navigate to the configuration page + HtmlPage configPage = webClient.goTo("configureSecurity"); + HtmlForm form = configPage.getFormByName("config"); + + //Check that the password is too short message is present + assertTrue(form.asNormalizedText().contains(Messages.passwordTooShortFIPS())); + + // Expect FailingHttpStatusCodeException + thrown.expect(FailingHttpStatusCodeException.class); + + // Find the "Submit" button and click it + getButtonByText(form, "Save").click(); + } + + /** + * Tests the behavior of the "Save" button when a short password is configured. + * + *

For the preconfigured value, the password is "small" in the local data. + * When the "Apply" button is clicked, an exception is expected because the password + * does not meet the minimum length requirement.

+ * + */ + @LocalData + @Test + public void testActiveDirectoryDomainApplyButtonClick() throws Exception { + JenkinsRule.WebClient webClient = jenkins.createWebClient(); + // Navigate to the configuration page + HtmlPage configPage = webClient.goTo("configureSecurity"); + HtmlForm form = configPage.getFormByName("config"); + + //Check that the password is too short message is present + assertTrue(form.asNormalizedText().contains(Messages.passwordTooShortFIPS())); + + // Expect FailingHttpStatusCodeException + thrown.expect(FailingHttpStatusCodeException.class); + + // Find the "Apply" button and click it + getButtonByText(form, "Apply").click(); + } + + /** + * Tests the behavior of the "Apply" button when a valid password is initially configured. then updated to a + * short password + * + *

For the preconfigured value, the password is "samell" in the local data. + * When the "Apply" button is clicked, an exception is expected because the password + * does not meet the minimum length requirement.

+ * + */ + @LocalData + @Test + public void testActiveDirectoryDomainSettingShortPassword() throws Exception { + JenkinsRule.WebClient webClient = jenkins.createWebClient(); + // Navigate to the configuration page + HtmlPage configPage = webClient.goTo("configureSecurity"); + HtmlForm form = configPage.getFormByName("config"); + + //Since password is valid is should not contain password too short message + assertFalse(form.asNormalizedText().contains(Messages.passwordTooShortFIPS())); + //Since password is valid, it should not throw exception oon clicking apply + assertEquals(200, getButtonByText(form, "Apply").click().getWebResponse().getStatusCode()); + + // Find the binf password filed and set an invalid password + HtmlInput bindPasswordField = form.getInputByName("_.bindPassword"); + bindPasswordField.setValueAttribute("small"); // Replace with your password value + + // Expect FailingHttpStatusCodeException + thrown.expect(FailingHttpStatusCodeException.class); + + // Find the "Submit" button and click it + getButtonByText(form, "Apply").click(); + } + + private HtmlButton getButtonByText(HtmlForm form, String text) throws Exception { + for (HtmlElement e : form.getElementsByTagName("button")) { + if (e.getTextContent().contains(text)) { + return ((HtmlButton) e); + } + } + throw new AssertionError(String.format("Button [%s] not found", text)); + } + +} diff --git a/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeShortPasswordTest.java b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeShortPasswordTest.java index b4fa11f0..7b0f9ae3 100644 --- a/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeShortPasswordTest.java +++ b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeShortPasswordTest.java @@ -30,6 +30,7 @@ public class ActiveDirectoryJCasCCompatibilityFIPSModeShortPasswordTest { @Test public void checkOfIncorrectConfigurationsWithShortPasswordInFIPSMode() throws IOException { thrown.expect(IllegalStateException.class); + thrown.expectMessage(Messages.passwordTooShortFIPS()); String resourcePath = "configuration-as-code-fips-short-password.yaml"; String resourceContent = this.getResourceContent(resourcePath); diff --git a/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeValidPasswordTest.java b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeValidPasswordTest.java index 2d874aed..4e28cacd 100644 --- a/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeValidPasswordTest.java +++ b/src/test/java/hudson/plugins/active_directory/ActiveDirectoryJCasCCompatibilityFIPSModeValidPasswordTest.java @@ -28,7 +28,7 @@ protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenk assertEquals("admin", domain.bindName); assertEquals("ad1.acme.com:123,ad2.acme.com:456", domain.servers); assertEquals("site", domain.getSite()); - assertEquals("S3cur3P@ssw0rd!", domain.getBindPassword().getPlainText()); // check for valid password + assertEquals("veryLargePassword", domain.getBindPassword().getPlainText()); // check for valid password assertEquals(TlsConfiguration.JDK_TRUSTSTORE, domain.getTlsConfiguration()); assertEquals(2, realm.getEnvironmentProperties().size()); diff --git a/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainApplyButtonClick/config.xml b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainApplyButtonClick/config.xml new file mode 100644 index 00000000..56403a15 --- /dev/null +++ b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainApplyButtonClick/config.xml @@ -0,0 +1,50 @@ + + + + 1.0 + 2 + NORMAL + true + + + + + samdom.example.com + CN=Administrator,CN=Users,DC=samdom,DC=example,DC=com + small + TRUST_ALL_CERTIFICATES + admin + + + true + RECURSIVE + false + + admin + + + false + + ${ITEM_ROOTDIR}/workspace + ${ITEM_ROOTDIR}/builds + + + + + + 0 + + + + All + false + false + + + + All + -1 + + + + \ No newline at end of file diff --git a/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSaveButtonClick/config.xml b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSaveButtonClick/config.xml new file mode 100644 index 00000000..56403a15 --- /dev/null +++ b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSaveButtonClick/config.xml @@ -0,0 +1,50 @@ + + + + 1.0 + 2 + NORMAL + true + + + + + samdom.example.com + CN=Administrator,CN=Users,DC=samdom,DC=example,DC=com + small + TRUST_ALL_CERTIFICATES + admin + + + true + RECURSIVE + false + + admin + + + false + + ${ITEM_ROOTDIR}/workspace + ${ITEM_ROOTDIR}/builds + + + + + + 0 + + + + All + false + false + + + + All + -1 + + + + \ No newline at end of file diff --git a/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSettingShortPassword/config.xml b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSettingShortPassword/config.xml new file mode 100644 index 00000000..0263de3a --- /dev/null +++ b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainSettingShortPassword/config.xml @@ -0,0 +1,50 @@ + + + + 1.0 + 2 + NORMAL + true + + + + + samdom.example.com + CN=Administrator,CN=Users,DC=samdom,DC=example,DC=com + verylargepassword12345 + TRUST_ALL_CERTIFICATES + admin + + + true + RECURSIVE + false + + admin + + + false + + ${ITEM_ROOTDIR}/workspace + ${ITEM_ROOTDIR}/builds + + + + + + 0 + + + + All + false + false + + + + All + -1 + + + + \ No newline at end of file diff --git a/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainTestDomainButtonClickWithShortPassword/config.xml b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainTestDomainButtonClickWithShortPassword/config.xml new file mode 100644 index 00000000..56403a15 --- /dev/null +++ b/src/test/resources/hudson/plugins/active_directory/ActiveDirectoryDomainIntegrationTest/testActiveDirectoryDomainTestDomainButtonClickWithShortPassword/config.xml @@ -0,0 +1,50 @@ + + + + 1.0 + 2 + NORMAL + true + + + + + samdom.example.com + CN=Administrator,CN=Users,DC=samdom,DC=example,DC=com + small + TRUST_ALL_CERTIFICATES + admin + + + true + RECURSIVE + false + + admin + + + false + + ${ITEM_ROOTDIR}/workspace + ${ITEM_ROOTDIR}/builds + + + + + + 0 + + + + All + false + false + + + + All + -1 + + + + \ No newline at end of file diff --git a/src/test/resources/hudson/plugins/active_directory/configuration-as-code-fips-valid-password.yaml b/src/test/resources/hudson/plugins/active_directory/configuration-as-code-fips-valid-password.yaml index 8f214a22..a4eb8a92 100644 --- a/src/test/resources/hudson/plugins/active_directory/configuration-as-code-fips-valid-password.yaml +++ b/src/test/resources/hudson/plugins/active_directory/configuration-as-code-fips-valid-password.yaml @@ -7,7 +7,7 @@ jenkins: customDomain: true domains: - bindName: "admin" - bindPassword: "S3cur3P@ssw0rd!" # Updated to a stronger password + bindPassword: "veryLargePassword" name: "acme" servers: "ad1.acme.com:123,ad2.acme.com:456" site: "site"