2022-11-25 21:11:15 +00:00
import LoginPage from "../support/pages/LoginPage" ;
import SidebarPage from "../support/pages/admin-ui/SidebarPage" ;
import adminClient from "../support/util/AdminClient" ;
import { keycloakBefore } from "../support/util/keycloak_hooks" ;
import ProviderPage from "../support/pages/admin-ui/manage/providers/ProviderPage" ;
import RealmRepresentation from "libs/keycloak-admin-client/lib/defs/realmRepresentation" ;
const loginPage = new LoginPage ( ) ;
const sidebarPage = new SidebarPage ( ) ;
const providersPage = new ProviderPage ( ) ;
const usernameI18nTest = "user_i18n_test" ;
let usernameI18nId : string ;
let originalMasterRealm : RealmRepresentation ;
describe ( "i18n tests" , ( ) = > {
before ( ( ) = > {
cy . wrap ( null ) . then ( async ( ) = > {
const realm = ( await adminClient . getRealm ( "master" ) ) ! ;
originalMasterRealm = realm ;
realm . supportedLocales = [ "en" , "de" , "de-CH" , "fo" ] ;
realm . internationalizationEnabled = true ;
await adminClient . updateRealm ( "master" , realm ) ;
const { id : userId } = await adminClient . createUser ( {
username : usernameI18nTest ,
enabled : true ,
credentials : [
{ type : "password" , temporary : false , value : usernameI18nTest } ,
] ,
} ) ;
usernameI18nId = userId ! ;
await adminClient . addRealmRoleToUser ( usernameI18nId , "admin" ) ;
} ) ;
} ) ;
after ( async ( ) = > {
await adminClient . deleteUser ( usernameI18nTest ) ;
if ( originalMasterRealm ) {
await adminClient . updateRealm ( "master" , originalMasterRealm ) ;
}
} ) ;
afterEach ( async ( ) = > {
await adminClient . removeAllLocalizationTexts ( ) ;
} ) ;
const realmLocalizationEn = "realmSettings en" ;
const themeLocalizationEn = "Realm settings" ;
const realmLocalizationDe = "realmSettings de" ;
const themeLocalizationDe = "Realm-Einstellungen" ;
const realmLocalizationDeCh = "realmSettings de-CH" ;
it ( "should use THEME localization for fallback (en) when language without theme localization is requested and no realm localization exists" , ( ) = > {
updateUserLocale ( "fo" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( themeLocalizationEn ) ;
} ) ;
it ( "should use THEME localization for language when language with theme localization is requested and no realm localization exists" , ( ) = > {
updateUserLocale ( "de" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( themeLocalizationDe ) ;
} ) ;
it ( "should use REALM localization for fallback (en) when language without theme localization is requested and realm localization exists for fallback (en)" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "en" , realmLocalizationEn ) ;
updateUserLocale ( "fo" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( realmLocalizationEn ) ;
} ) ;
it ( "should use THEME localization for language when language with theme localization is requested and realm localization exists for fallback (en) only" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "en" , realmLocalizationEn ) ;
updateUserLocale ( "de" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( themeLocalizationDe ) ;
} ) ;
it ( "should use REALM localization for language when language is requested and realm localization exists for language" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "de" , realmLocalizationDe ) ;
updateUserLocale ( "de" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( realmLocalizationDe ) ;
} ) ;
// TODO: currently skipped due to https://github.com/keycloak/keycloak/issues/20412
it . skip ( "should use REALM localization for region when region is requested and realm localization exists for region" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "de-CH" , realmLocalizationDeCh ) ;
updateUserLocale ( "de-CH" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( realmLocalizationDeCh ) ;
} ) ;
it ( "should use REALM localization for language when language is requested and realm localization exists for fallback (en), language, region" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "en" , realmLocalizationEn ) ;
addCommonRealmSettingsLocalizationText ( "de" , realmLocalizationDe ) ;
addCommonRealmSettingsLocalizationText ( "de-CH" , realmLocalizationDeCh ) ;
updateUserLocale ( "de" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( realmLocalizationDe ) ;
} ) ;
it ( "should use REALM localization for language when region is requested and realm localization exists for fallback (en), language" , ( ) = > {
addCommonRealmSettingsLocalizationText ( "en" , realmLocalizationEn ) ;
addCommonRealmSettingsLocalizationText ( "de" , realmLocalizationDe ) ;
updateUserLocale ( "de-CH" ) ;
goToUserFederationPage ( ) ;
sidebarPage . checkRealmSettingsLinkContainsText ( realmLocalizationDe ) ;
} ) ;
it ( "should apply plurals and interpolation for THEME localization" , ( ) = > {
updateUserLocale ( "en" ) ;
goToUserFederationPage ( ) ;
// check key "user-federation:addProvider_other"
providersPage . assertCardContainsText ( "ldap" , "Add Ldap providers" ) ;
} ) ;
it ( "should apply plurals and interpolation for REALM localization" , ( ) = > {
addLocalization (
"en" ,
2023-09-08 13:17:17 +00:00
"addProvider_other" ,
2023-07-11 14:03:21 +00:00
"addProvider_other en: {{provider}}" ,
2022-11-25 21:11:15 +00:00
) ;
updateUserLocale ( "en" ) ;
goToUserFederationPage ( ) ;
providersPage . assertCardContainsText ( "ldap" , "addProvider_other en: Ldap" ) ;
} ) ;
function goToUserFederationPage() {
loginPage . logIn ( usernameI18nTest , usernameI18nTest ) ;
keycloakBefore ( ) ;
sidebarPage . goToUserFederation ( ) ;
}
function updateUserLocale ( locale : string ) {
cy . wrap ( null ) . then ( ( ) = >
2023-07-11 14:03:21 +00:00
adminClient . updateUser ( usernameI18nId , {
attributes : { locale : locale } ,
} ) ,
2022-11-25 21:11:15 +00:00
) ;
}
function addCommonRealmSettingsLocalizationText (
locale : string ,
2023-07-11 14:03:21 +00:00
value : string ,
2022-11-25 21:11:15 +00:00
) {
2023-09-08 13:17:17 +00:00
addLocalization ( locale , "realmSettings" , value ) ;
2022-11-25 21:11:15 +00:00
}
function addLocalization ( locale : string , key : string , value : string ) {
cy . wrap ( null ) . then ( ( ) = >
2023-07-11 14:03:21 +00:00
adminClient . addLocalizationText ( locale , key , value ) ,
2022-11-25 21:11:15 +00:00
) ;
}
} ) ;