KEYCLOAK-12249 added test to test that time is localized

This commit is contained in:
Erik Jan de Wit 2020-06-30 10:49:22 +02:00 committed by Stan Silvert
parent 582046bbfe
commit ace64c1f0c
6 changed files with 79 additions and 13 deletions

View file

@ -51,7 +51,8 @@ public class LocaleDropdown {
else {
Actions actions = new Actions(driver);
log.info("Moving mouse cursor to the localization menu");
actions.moveToElement(currentLocaleLink).perform();
actions.moveToElement(currentLocaleLink, -10, 0)
.moveToElement(currentLocaleLink).perform();
}
// click desired locale

View file

@ -1,2 +1,2 @@
parent=${theme-default-name}-preview
locales=en,lang01,lang02,lang03,lang04,lang05,test,lang06,lang07,lang08,lang09,lang10
locales=en,de,lang01,lang02,lang03,lang04,lang05,test,lang06,lang07,lang08,lang09,lang10

View file

@ -1,2 +1,2 @@
parent=${theme-default-name}
locales=en,lang01,lang02,lang03,lang04,lang05,test,lang06,lang07,lang08,lang09,lang10
locales=en,de,lang01,lang02,lang03,lang04,lang05,test,lang06,lang07,lang08,lang09,lang10

View file

@ -26,6 +26,7 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.testsuite.ui.account2.page.AbstractLoggedInPage;
import org.keycloak.testsuite.ui.account2.page.DeviceActivityPage;
import org.keycloak.testsuite.util.ClientBuilder;
@ -33,7 +34,9 @@ import org.keycloak.testsuite.util.OAuthClient;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -50,8 +53,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.keycloak.representations.idm.CredentialRepresentation.PASSWORD;
import static org.keycloak.testsuite.auth.page.AuthRealm.TEST;
import static org.keycloak.testsuite.util.UIUtils.refreshPageAndWaitForLoad;
/**
* @author Vaclav Muzikar <vmuzikar@redhat.com>
@ -101,6 +106,7 @@ public class DeviceActivityTest extends BaseAccountPageTest {
));
realm.setAccountTheme(LOCALIZED_THEME_PREVIEW); // using localized custom theme for the client localized name
configureInternationalizationForRealm(testRealms.get(0));
}
@Before
@ -229,6 +235,27 @@ public class DeviceActivityTest extends BaseAccountPageTest {
assertEquals(startedAt.plusSeconds(ssoLifespan), expiresAt);
}
@Test
public void timeLocaleTest() {
String sessionId = createSession(Browsers.CHROME);
UserRepresentation user = testUserResource().toRepresentation();
final Locale locale = Locale.GERMAN;
user.setAttributes(new HashMap<String, List<String>>() {{
put("locale", Collections.singletonList(locale.toLanguageTag()));
}});
testUserResource().update(user);
refreshPageAndWaitForLoad();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d. MMMM yyyy, H:mm", locale);
DeviceActivityPage.Session session = deviceActivityPage.getSession(sessionId);
try {
LocalDateTime.parse(session.getLastAccess(), formatter);
} catch (DateTimeParseException e) {
fail("Time was not formatted with the locale");
}
}
@Test
public void ipTest() {
final String ip = "146.58.69.12";
@ -255,8 +282,7 @@ public class DeviceActivityTest extends BaseAccountPageTest {
// using direct grant not to use current browser
res = oauth.doGrantAccessTokenRequest(
TEST, testUser.getUsername(), PASSWORD, null, TEST_CLIENT_ID, TEST_CLIENT_SECRET, browser.userAgent);
}
catch (Exception e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
return res.getSessionState(); // session id
@ -267,8 +293,7 @@ public class DeviceActivityTest extends BaseAccountPageTest {
assertTrue("Session should be present", session.isPresent());
if (browser.sessionBrowser != null) {
assertEquals(browser.sessionBrowser, session.getBrowser());
}
else {
} else {
assertFalse("Browser identification shouldn't be present", session.isBrowserDisplayed());
}
assertEquals(browser.iconName, session.getBrowserIconName());

View file

@ -21,17 +21,24 @@ import org.jboss.arquillian.graphene.page.Page;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.models.UserModel;
import org.keycloak.models.credential.PasswordCredentialModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.ui.account2.page.PersonalInfoPage;
import org.keycloak.testsuite.ui.account2.page.SigningInPage;
import org.keycloak.testsuite.ui.account2.page.WelcomeScreen;
import org.keycloak.testsuite.util.WaitUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Vaclav Muzikar <vmuzikar@redhat.com>
@ -43,6 +50,10 @@ public class InternationalizationTest extends AbstractAccountTest {
@Page
private PersonalInfoPage personalInfoPage;
@Page
private SigningInPage signingInPage;
private SigningInPage.CredentialType passwordCredentialType;
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
super.addTestRealms(testRealms);
@ -52,6 +63,7 @@ public class InternationalizationTest extends AbstractAccountTest {
@Before
public void beforeI18nTest() {
assertTestUserLocale(null);
passwordCredentialType = signingInPage.getCredentialType(PasswordCredentialModel.TYPE);
}
@Test
@ -93,6 +105,34 @@ public class InternationalizationTest extends AbstractAccountTest {
assertCustomLocalePersonalInfo();
}
@Test
public void shouldDisplayTimeUsingSelectedLocale() {
signingInPage.navigateTo();
loginToAccount();
SigningInPage.UserCredential passwordCred =
passwordCredentialType.getUserCredential(testUserResource().credentials().get(0).getId());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy, h:mm a", Locale.ENGLISH);
try {
LocalDateTime.parse(passwordCred.getCreatedAtStr(), formatter);
} catch (DateTimeParseException e) {
fail("Time was not formatted with the locale");
}
signingInPage.header().clickLogoutBtn();
signingInPage.navigateTo();
loginPage.localeDropdown().selectAndAssert("Deutsch");
loginPage.form().login(testUser);
DateTimeFormatter formatterDe = DateTimeFormatter.ofPattern("d. MMMM yyyy, H:mm", Locale.GERMAN);
try {
LocalDateTime.parse(passwordCred.getCreatedAtStr(), formatterDe);
} catch (DateTimeParseException e) {
fail("Time was not formatted with the locale");
}
}
private void assertCustomLocaleWelcomeScreen() {
assertEquals("Vítejte v Keycloaku", welcomeScreen.getWelcomeMessage());
}

View file

@ -41,21 +41,21 @@ public class WelcomeScreen extends AbstractAccountPage {
@FindBy(xpath = "//*[@id='" + ROOT_ELEMENT_ID + "']//header")
private WelcomeScreenHeader header;
@FindBy(xpath = "//*[@id='landing-personal-info']/a")
@FindBy(xpath = "//a[@id='landing-personal-info']")
private WebElement personalInfoLink;
@FindBy(xpath = "//*[@id='landingChangePasswordLink']/a")
private WebElement changePasswordLink;
@FindBy(xpath = "//*[@id='landing-authenticator']/a")
@FindBy(xpath = "//a[@id='landing-authenticator']")
private WebElement authenticatorLink;
@FindBy(xpath = "//*[@id='landing-device-activity']/a")
private WebElement deviceActivityLink;
@FindBy(xpath = "//*[@id='landing-linked-accounts']/a")
private WebElement linkedAccountsLink;
@FindBy(xpath = "//*[@id='landing-applications']/a")
@FindBy(xpath = "//a[@id='landing-applications']")
private WebElement applicationsLink;
@FindBy(id = "landing-resources")
private WebElement myResourcesCard;
@FindBy(xpath = "//*[@id='landing-resources']/a")
@FindBy(xpath = "//a[@id='landing-resources']")
private WebElement myResourcesLink;
@FindBy(id = "landingLogo")
private WebElement logoLink;