Removed Thread.sleep from tests and replaced with Time.offset
This commit is contained in:
parent
517e42f13d
commit
8ebac35706
6 changed files with 35 additions and 29 deletions
|
@ -51,6 +51,7 @@ import org.keycloak.testsuite.rule.WebResource;
|
|||
import org.keycloak.testsuite.rule.WebRule;
|
||||
import org.keycloak.testutils.KeycloakServer;
|
||||
import org.keycloak.util.BasicAuthHelper;
|
||||
import org.keycloak.util.Time;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
|
@ -303,7 +304,7 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
|
||||
// test SSO
|
||||
|
@ -315,6 +316,8 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
realm.setSsoSessionIdleTimeout(originalIdle);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
public void testLoginSSOIdleRemoveExpiredUserSessions() throws Exception {
|
||||
|
@ -336,7 +339,7 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
session = keycloakRule.startSession();
|
||||
realm = session.realms().getRealmByName("demo");
|
||||
|
@ -356,6 +359,8 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
realm.setSsoSessionIdleTimeout(originalIdle);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
public void testLoginSSOMax() throws Exception {
|
||||
|
@ -377,7 +382,7 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
|
||||
// test SSO
|
||||
|
@ -389,6 +394,8 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
realm.setSsoSessionMaxLifespan(original);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -541,7 +548,7 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
driver.navigate().to(logoutUri);
|
||||
|
||||
// Wait until accessToken is expired
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
// Assert that http session was invalidated
|
||||
driver.navigate().to(APP_SERVER_BASE_URL + "/session-portal");
|
||||
|
@ -563,6 +570,8 @@ public class AdapterTestStrategy extends ExternalResource {
|
|||
}
|
||||
|
||||
}, "demo");
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,6 +37,7 @@ import org.keycloak.services.managers.RealmManager;
|
|||
import org.keycloak.testsuite.AssertEvents;
|
||||
import org.keycloak.testsuite.MailUtil;
|
||||
import org.keycloak.testsuite.OAuthClient;
|
||||
import org.keycloak.testsuite.Retry;
|
||||
import org.keycloak.testsuite.pages.AppPage;
|
||||
import org.keycloak.testsuite.pages.AppPage.RequestType;
|
||||
import org.keycloak.testsuite.pages.ErrorPage;
|
||||
|
@ -253,8 +254,6 @@ public class ResetPasswordTest {
|
|||
|
||||
assertEquals("You should receive an email shortly with further instructions.", resetPasswordPage.getSuccessMessage());
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
assertEquals(0, greenMail.getReceivedMessages().length);
|
||||
|
||||
events.expectRequiredAction(EventType.SEND_RESET_PASSWORD).user((String) null).session((String) null).detail(Details.USERNAME, "invalid").removeDetail(Details.EMAIL).removeDetail(Details.CODE_ID).error("user_not_found").assertEvent();
|
||||
|
@ -318,8 +317,6 @@ public class ResetPasswordTest {
|
|||
|
||||
assertEquals("You should receive an email shortly with further instructions.", resetPasswordPage.getSuccessMessage());
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
assertEquals(0, greenMail.getReceivedMessages().length);
|
||||
|
||||
events.expectRequiredAction(EventType.SEND_RESET_PASSWORD).session((String) null).user(userId).detail(Details.USERNAME, "login-test").removeDetail(Details.CODE_ID).error("user_disabled").assertEvent();
|
||||
|
@ -358,8 +355,6 @@ public class ResetPasswordTest {
|
|||
|
||||
assertEquals("You should receive an email shortly with further instructions.", resetPasswordPage.getSuccessMessage());
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
assertEquals(0, greenMail.getReceivedMessages().length);
|
||||
|
||||
events.expectRequiredAction(EventType.SEND_RESET_PASSWORD_ERROR).session((String) null).user(userId).detail(Details.USERNAME, "login-test").removeDetail(Details.CODE_ID).error("invalid_email").assertEvent();
|
||||
|
@ -396,8 +391,6 @@ public class ResetPasswordTest {
|
|||
|
||||
assertEquals("Failed to send email, please try again later.", errorPage.getError());
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
assertEquals(0, greenMail.getReceivedMessages().length);
|
||||
|
||||
events.expectRequiredAction(EventType.SEND_RESET_PASSWORD_ERROR).user(userId).detail(Details.USERNAME, "login-test").removeDetail(Details.CODE_ID).error(Errors.EMAIL_SEND_FAILED).assertEvent();
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.keycloak.representations.idm.CredentialRepresentation;
|
|||
import org.keycloak.services.managers.AuthenticationManager;
|
||||
import org.keycloak.services.managers.AuthenticationManager.AuthenticationStatus;
|
||||
import org.keycloak.services.managers.BruteForceProtector;
|
||||
import org.keycloak.util.Time;
|
||||
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import java.util.UUID;
|
||||
|
@ -238,10 +239,12 @@ public class AuthenticationManagerTest extends AbstractModelTest {
|
|||
String passwordToken = new JWSBuilder().jsonContent(new PasswordToken(realm.getName(), "invalid")).rsa256(realm.getPrivateKey());
|
||||
formData.add(CredentialRepresentation.PASSWORD_TOKEN, passwordToken);
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
AuthenticationStatus status = am.authenticateForm(session, dummyConnection, realm, formData);
|
||||
Assert.assertEquals(AuthenticationStatus.INVALID_CREDENTIALS, status);
|
||||
|
||||
Time.setOffset(0);
|
||||
} finally {
|
||||
realm.setAccessCodeLifespanUserAction(lifespan);
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ import org.keycloak.testsuite.rule.KeycloakRule;
|
|||
import org.keycloak.testsuite.rule.WebResource;
|
||||
import org.keycloak.testsuite.rule.WebRule;
|
||||
import org.keycloak.util.BasicAuthHelper;
|
||||
import org.keycloak.util.Time;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
|
@ -211,10 +212,7 @@ public class AccessTokenTest {
|
|||
|
||||
String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
Time.setOffset(2);
|
||||
|
||||
OAuthClient.AccessTokenResponse response = oauth.doAccessTokenRequest(code, "password");
|
||||
Assert.assertEquals(400, response.getStatusCode());
|
||||
|
@ -231,6 +229,8 @@ public class AccessTokenTest {
|
|||
appRealm.setAccessCodeLifespan(60);
|
||||
}
|
||||
});
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -143,7 +143,7 @@ public class RefreshTokenTest {
|
|||
|
||||
Assert.assertEquals(sessionId, refreshToken.getSessionState());
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
AccessTokenResponse response = oauth.doRefreshTokenRequest(refreshTokenString, "password");
|
||||
AccessToken refreshedToken = oauth.verifyToken(response.getAccessToken());
|
||||
|
@ -157,8 +157,8 @@ public class RefreshTokenTest {
|
|||
Assert.assertThat(response.getExpiresIn(), allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(300)));
|
||||
Assert.assertThat(refreshedToken.getExpiration() - Time.currentTime(), allOf(greaterThanOrEqualTo(250), lessThanOrEqualTo(300)));
|
||||
|
||||
Assert.assertThat(refreshedToken.getExpiration() - token.getExpiration(), allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(5)));
|
||||
Assert.assertThat(refreshedRefreshToken.getExpiration() - refreshToken.getExpiration(), allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(5)));
|
||||
Assert.assertThat(refreshedToken.getExpiration() - token.getExpiration(), allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(10)));
|
||||
Assert.assertThat(refreshedRefreshToken.getExpiration() - refreshToken.getExpiration(), allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(10)));
|
||||
|
||||
Assert.assertNotEquals(token.getId(), refreshedToken.getId());
|
||||
Assert.assertNotEquals(refreshToken.getId(), refreshedRefreshToken.getId());
|
||||
|
@ -177,6 +177,8 @@ public class RefreshTokenTest {
|
|||
Event refreshEvent = events.expectRefresh(tokenEvent.getDetails().get(Details.REFRESH_TOKEN_ID), sessionId).assertEvent();
|
||||
Assert.assertNotEquals(tokenEvent.getDetails().get(Details.TOKEN_ID), refreshEvent.getDetails().get(Details.TOKEN_ID));
|
||||
Assert.assertNotEquals(tokenEvent.getDetails().get(Details.REFRESH_TOKEN_ID), refreshEvent.getDetails().get(Details.UPDATED_REFRESH_TOKEN_ID));
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
PrivateKey privateKey;
|
||||
|
@ -277,7 +279,7 @@ public class RefreshTokenTest {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(2);
|
||||
|
||||
tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "password");
|
||||
|
||||
|
@ -302,7 +304,7 @@ public class RefreshTokenTest {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(4);
|
||||
tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "password");
|
||||
|
||||
session = keycloakRule.startSession();
|
||||
|
@ -323,7 +325,7 @@ public class RefreshTokenTest {
|
|||
session.close();
|
||||
|
||||
events.clear();
|
||||
Thread.sleep(2000);
|
||||
Time.setOffset(6);
|
||||
tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "password");
|
||||
|
||||
// test idle timeout
|
||||
|
@ -341,6 +343,8 @@ public class RefreshTokenTest {
|
|||
session.close();
|
||||
|
||||
events.clear();
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -365,7 +369,7 @@ public class RefreshTokenTest {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Thread.sleep(1000);
|
||||
Time.setOffset(1);
|
||||
|
||||
tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "password");
|
||||
|
||||
|
@ -383,6 +387,8 @@ public class RefreshTokenTest {
|
|||
events.expectRefresh(refreshId, sessionId).error(Errors.INVALID_TOKEN);
|
||||
|
||||
events.clear();
|
||||
|
||||
Time.setOffset(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -126,11 +126,6 @@ public class SamlBindingTest {
|
|||
@WebResource
|
||||
protected LoginPage loginPage;
|
||||
|
||||
//@Test
|
||||
public void runit() throws Exception {
|
||||
Thread.sleep(10000000);
|
||||
}
|
||||
|
||||
protected void checkLoggedOut(String mainUrl) {
|
||||
String pageSource = driver.getPageSource();
|
||||
System.out.println("*** logout pagesouce ***");
|
||||
|
|
Loading…
Reference in a new issue