diff --git a/src/org/labkey/test/tests/SiteWideTermsOfUseTest.java b/src/org/labkey/test/tests/SiteWideTermsOfUseTest.java index cdf24982f6..47f865c896 100644 --- a/src/org/labkey/test/tests/SiteWideTermsOfUseTest.java +++ b/src/org/labkey/test/tests/SiteWideTermsOfUseTest.java @@ -18,16 +18,24 @@ import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.labkey.remoteapi.CommandException; +import org.labkey.remoteapi.Connection; +import org.labkey.remoteapi.SimplePostCommand; import org.labkey.test.BaseWebDriverTest; import org.labkey.test.Locator; import org.labkey.test.Locators; import org.labkey.test.TestTimeoutException; +import org.labkey.test.WebTestHelper; import org.labkey.test.categories.Daily; import org.labkey.test.util.PasswordUtil; import org.openqa.selenium.WebElement; +import java.io.IOException; +import java.util.Map; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; @Category({Daily.class}) @BaseWebDriverTest.ClassTimeout(minutes = 7) @@ -89,6 +97,7 @@ public void editTermsAdminConsoleLinkTest() protected void assureSiteWideTermsOfUsePage() { createTermsOfUsePage(null, SITE_WIDE_TERMS_TEXT); + setFrequency(0); } // Test that the site-wide terms appear when you log out, even if you've accepted the terms when logged in @@ -232,6 +241,71 @@ public void testFailedLoginBadPassword() waitForText(SITE_WIDE_TERMS_TEXT); // should show } + private final static int FREQUENCY_SECONDS = 10; + + @Test + public void testRememberMeTerms() + { + log("Testing \"Require terms-of-use acceptance\" set to " + FREQUENCY_SECONDS + " seconds"); + + // Remember terms acceptance for FREQUENCY_SECONDS + setFrequency(FREQUENCY_SECONDS); + boolean firstSignIn = true; + boolean secondAcceptance = false; + long firstAcceptance = 0; + long diff; + + // Sign out and back in again for up to FREQUENCY_SECONDS * 1.5 seconds + double max_millis = FREQUENCY_SECONDS * 1.5 * 1000; + do + { + simpleSignOut(); + // Terms dialog doesn't show a sign-in link, so navigate directly to the login page + beginAt(WebTestHelper.buildURL("login", "login")); + assertTextNotPresent(SITE_WIDE_TERMS_TEXT); + doAndWaitForPageToLoad(() -> fillSignInFormAndSubmit("Sign In")); + if (isTextPresent(SITE_WIDE_TERMS_TEXT)) + { + acceptTermsOfUse(SITE_WIDE_TERMS_TEXT, true); + if (firstSignIn) + { + firstAcceptance = System.currentTimeMillis(); + firstSignIn = false; + } + else + { + secondAcceptance = true; + } + } + diff = System.currentTimeMillis() - firstAcceptance; + } + while (!secondAcceptance && !firstSignIn && diff < max_millis); + + assertFalse("First sign in didn't result in TOU!", firstSignIn); + assertTrue("First acceptance time was 0!", firstAcceptance > 0); + assertTrue("Second acceptance didn't occur within 15 seconds!", diff < max_millis); + assertFalse("Second acceptance occurred too early (" + diff + "ms)", diff < FREQUENCY_SECONDS * 1000); + + // Back to every sign-in + setFrequency(0); + } + + private void setFrequency(int frequency) + { + // Use direct API call to set a value that may not appear in the drop-down list + Connection conn = createDefaultConnection(); + SimplePostCommand frequencyCommand = new SimplePostCommand("admin", "setTermsOfUseFrequency"); + frequencyCommand.setParameters(Map.of("seconds", frequency)); + try + { + frequencyCommand.execute(conn, "/"); + } + catch (IOException | CommandException e) + { + throw new RuntimeException(e); + } + } + protected void signOutWithSiteWideTerms(String termsText, boolean acceptTerms) { log("Signing out");