mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-14 07:30:05 +00:00
feat(auth): added multi-language support to logIn method
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { AuthManager } from '../AuthManager'
|
||||
import * as dotenv from 'dotenv'
|
||||
import { ServerType } from '@sasjs/utils/types'
|
||||
@@ -63,6 +67,12 @@ describe('AuthManager', () => {
|
||||
})
|
||||
|
||||
describe('login - default mechanism', () => {
|
||||
let languageGetter: any
|
||||
|
||||
beforeEach(() => {
|
||||
languageGetter = jest.spyOn(window.navigator, 'language', 'get')
|
||||
})
|
||||
|
||||
it('should call the auth callback and return when already logged in', async () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
@@ -294,6 +304,56 @@ describe('AuthManager', () => {
|
||||
mockLoginAuthoriseRequiredResponse
|
||||
)
|
||||
})
|
||||
|
||||
it('should check login success header based on language preferences of the browser', () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
serverType,
|
||||
requestClient,
|
||||
authCallback
|
||||
)
|
||||
|
||||
// test built in language codes
|
||||
Object.keys(authManager['successHeaders']).forEach((key) => {
|
||||
languageGetter.mockReturnValue(key)
|
||||
|
||||
expect(
|
||||
authManager['isLogInSuccessHeaderPresent'](
|
||||
serverType,
|
||||
authManager['successHeaders'][key]
|
||||
)
|
||||
).toBeTruthy()
|
||||
})
|
||||
|
||||
// test possible longer language codes
|
||||
const possibleLanguageCodes = [
|
||||
{ short: 'en', long: 'en-US' },
|
||||
{ short: 'fr', long: 'fr-FR' },
|
||||
{ short: 'es', long: 'es-ES' }
|
||||
]
|
||||
|
||||
possibleLanguageCodes.forEach((key) => {
|
||||
const { short, long } = key
|
||||
languageGetter.mockReturnValue(long)
|
||||
|
||||
expect(
|
||||
authManager['isLogInSuccessHeaderPresent'](
|
||||
serverType,
|
||||
authManager['successHeaders'][short]
|
||||
)
|
||||
).toBeTruthy()
|
||||
})
|
||||
|
||||
// test falling back to default language code
|
||||
languageGetter.mockReturnValue('WRONG-LANGUAGE')
|
||||
|
||||
expect(
|
||||
authManager['isLogInSuccessHeaderPresent'](
|
||||
serverType,
|
||||
authManager['successHeaders'][authManager['defaultSuccessHeaderKey']]
|
||||
)
|
||||
).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('login - redirect mechanism', () => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { SasAuthResponse } from '@sasjs/utils/types'
|
||||
import { enLoginSuccessHeader } from '../AuthManager'
|
||||
|
||||
export const mockLoginAuthoriseRequiredResponse = `<form id="application_authorization" action="/SASLogon/oauth/authorize" method="POST"><input type="hidden" name="X-Uaa-Csrf" value="2nfuxIn6WaOURWL7tzTXCe"/>`
|
||||
export const mockLoginSuccessResponse = `You have signed in`
|
||||
export const mockLoginSuccessResponse = enLoginSuccessHeader
|
||||
|
||||
export const mockAuthResponse: SasAuthResponse = {
|
||||
access_token: 'acc355',
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
import { verifySas9Login } from '../verifySas9Login'
|
||||
import * as delayModule from '../../utils/delay'
|
||||
import { enLoginSuccessHeader } from '../AuthManager'
|
||||
|
||||
describe('verifySas9Login', () => {
|
||||
const serverUrl = 'http://test-server.com'
|
||||
@@ -18,7 +19,7 @@ describe('verifySas9Login', () => {
|
||||
const popup = {
|
||||
window: {
|
||||
location: { href: serverUrl + `/SASLogon` },
|
||||
document: { body: { innerText: '<h3>You have signed in.</h3>' } }
|
||||
document: { body: { innerText: `<h3>${enLoginSuccessHeader}</h3>` } }
|
||||
}
|
||||
} as unknown as Window
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
import { verifySasViyaLogin } from '../verifySasViyaLogin'
|
||||
import * as delayModule from '../../utils/delay'
|
||||
import { enLoginSuccessHeader } from '../AuthManager'
|
||||
|
||||
describe('verifySasViyaLogin', () => {
|
||||
const serverUrl = 'http://test-server.com'
|
||||
@@ -19,7 +20,7 @@ describe('verifySasViyaLogin', () => {
|
||||
const popup = {
|
||||
window: {
|
||||
location: { href: serverUrl + `/SASLogon` },
|
||||
document: { body: { innerText: '<h3>You have signed in.</h3>' } }
|
||||
document: { body: { innerText: `<h3>${enLoginSuccessHeader}</h3>` } }
|
||||
}
|
||||
} as unknown as Window
|
||||
|
||||
|
||||
Reference in New Issue
Block a user