forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-13205 Apply locale resolution strategy to admin console.
- Loading branch information
Showing
4 changed files
with
222 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...n/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleWhoAmILocaleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package org.keycloak.testsuite.admin; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.broker.provider.util.SimpleHttp; | ||
import org.keycloak.representations.AccessTokenResponse; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
import org.keycloak.testsuite.AbstractKeycloakTest; | ||
import org.keycloak.testsuite.util.RealmBuilder; | ||
import org.keycloak.testsuite.util.UserBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.keycloak.models.AdminRoles.REALM_ADMIN; | ||
import static org.keycloak.models.Constants.ADMIN_CLI_CLIENT_ID; | ||
import static org.keycloak.models.Constants.REALM_MANAGEMENT_CLIENT_ID; | ||
import static org.keycloak.testsuite.util.AdminClientUtil.createAdminClient; | ||
|
||
public class AdminConsoleWhoAmILocaleTest extends AbstractKeycloakTest { | ||
|
||
private static final String REALM_I18N_OFF = "realm-i18n-off"; | ||
private static final String REALM_I18N_ON = "realm-i18n-on"; | ||
private static final String USER_WITHOUT_LOCALE = "user-without-locale"; | ||
private static final String USER_WITH_LOCALE = "user-with-locale"; | ||
private static final String PASSWORD = "password"; | ||
private static final String DEFAULT_LOCALE = "en"; | ||
private static final String REALM_LOCALE = "no"; | ||
private static final String USER_LOCALE = "de"; | ||
private static final String EXTRA_LOCALE = "ru"; | ||
|
||
private CloseableHttpClient client; | ||
|
||
@Before | ||
public void createHttpClient() throws Exception { | ||
client = HttpClientBuilder.create().build(); | ||
} | ||
|
||
@After | ||
public void closeHttpClient() { | ||
try { | ||
client.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void addTestRealms(List<RealmRepresentation> testRealms) { | ||
RealmBuilder realm = RealmBuilder.create() | ||
.name(REALM_I18N_OFF) | ||
.internationalizationEnabled(false); | ||
realm.user(UserBuilder.create() | ||
.username(USER_WITHOUT_LOCALE) | ||
.password(PASSWORD) | ||
.role(REALM_MANAGEMENT_CLIENT_ID, REALM_ADMIN)); | ||
realm.user(UserBuilder.create() | ||
.username(USER_WITH_LOCALE) | ||
.password(PASSWORD) | ||
.addAttribute("locale", USER_LOCALE) | ||
.role(REALM_MANAGEMENT_CLIENT_ID, REALM_ADMIN)); | ||
testRealms.add(realm.build()); | ||
|
||
realm = RealmBuilder.create() | ||
.name(REALM_I18N_ON) | ||
.internationalizationEnabled(true) | ||
.supportedLocales(new HashSet<>(asList(REALM_LOCALE, USER_LOCALE, EXTRA_LOCALE))) | ||
.defaultLocale(REALM_LOCALE); | ||
realm.user(UserBuilder.create() | ||
.username(USER_WITHOUT_LOCALE) | ||
.password(PASSWORD) | ||
.role(REALM_MANAGEMENT_CLIENT_ID, REALM_ADMIN)); | ||
realm.user(UserBuilder.create() | ||
.username(USER_WITH_LOCALE) | ||
.password(PASSWORD) | ||
.addAttribute("locale", USER_LOCALE) | ||
.role(REALM_MANAGEMENT_CLIENT_ID, REALM_ADMIN)); | ||
testRealms.add(realm.build()); | ||
} | ||
|
||
private String accessToken(String realmName, String username) throws Exception { | ||
try (Keycloak adminClient = createAdminClient(true, realmName, username, PASSWORD, ADMIN_CLI_CLIENT_ID, null)) { | ||
AccessTokenResponse accessToken = adminClient.tokenManager().getAccessToken(); | ||
assertNotNull(accessToken); | ||
return accessToken.getToken(); | ||
} | ||
} | ||
|
||
private String whoAmiUrl(String realmName) { | ||
return suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth/admin/" + realmName + "/console/whoami"; | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nDisabledUserWithoutLocale() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_OFF), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_OFF, USER_WITHOUT_LOCALE)) | ||
.asJson(); | ||
assertEquals(REALM_I18N_OFF, whoAmI.get("realm").asText()); | ||
assertEquals(DEFAULT_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nDisabledUserWithLocale() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_OFF), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_OFF, USER_WITH_LOCALE)) | ||
.asJson(); | ||
assertEquals(REALM_I18N_OFF, whoAmI.get("realm").asText()); | ||
assertEquals(DEFAULT_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nEnabledUserWithoutLocale() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_ON), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_ON, USER_WITHOUT_LOCALE)) | ||
.asJson(); | ||
assertEquals(REALM_I18N_ON, whoAmI.get("realm").asText()); | ||
assertEquals(REALM_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nEnabledUserWithLocale() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_ON), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_ON, USER_WITH_LOCALE)) | ||
.asJson(); | ||
assertEquals(REALM_I18N_ON, whoAmI.get("realm").asText()); | ||
assertEquals(USER_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nEnabledAcceptLanguageHeader() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_ON), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_ON, USER_WITHOUT_LOCALE)) | ||
.header("Accept-Language", EXTRA_LOCALE) | ||
.asJson(); | ||
assertEquals(REALM_I18N_ON, whoAmI.get("realm").asText()); | ||
assertEquals(EXTRA_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
|
||
@Test | ||
public void testLocaleRealmI18nEnabledKeycloakLocaleCookie() throws Exception { | ||
JsonNode whoAmI = SimpleHttp | ||
.doGet(whoAmiUrl(REALM_I18N_ON), client) | ||
.header("Accept", "application/json") | ||
.auth(accessToken(REALM_I18N_ON, USER_WITHOUT_LOCALE)) | ||
.header("Cookie", "KEYCLOAK_LOCALE=" + EXTRA_LOCALE) | ||
.asJson(); | ||
assertEquals(REALM_I18N_ON, whoAmI.get("realm").asText()); | ||
assertEquals(EXTRA_LOCALE, whoAmI.get("locale").asText()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters