mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-17 17:10:05 +00:00
test(AuthManager): specs added for redirected login
This commit is contained in:
@@ -31,6 +31,18 @@ export class AuthManager {
|
|||||||
public async redirectedLogIn({
|
public async redirectedLogIn({
|
||||||
onLoggedOut
|
onLoggedOut
|
||||||
}: LoginOptions): Promise<LoginResult> {
|
}: LoginOptions): Promise<LoginResult> {
|
||||||
|
const { isLoggedIn: isLoggedInAlready, userName: currentSessionUsername } =
|
||||||
|
await this.fetchUserName()
|
||||||
|
|
||||||
|
if (isLoggedInAlready) {
|
||||||
|
await this.loginCallback()
|
||||||
|
|
||||||
|
return {
|
||||||
|
isLoggedIn: true,
|
||||||
|
userName: currentSessionUsername
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const loginPopup = await openWebPage(
|
const loginPopup = await openWebPage(
|
||||||
this.loginPreventRedirectUrl,
|
this.loginPreventRedirectUrl,
|
||||||
'SASLogon',
|
'SASLogon',
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import {
|
|||||||
mockLoginSuccessResponse
|
mockLoginSuccessResponse
|
||||||
} from './mockResponses'
|
} from './mockResponses'
|
||||||
import { serialize } from '../../utils'
|
import { serialize } from '../../utils'
|
||||||
|
import * as openWebPageModule from '../openWebPage'
|
||||||
|
import * as verifySasViyaLoginModule from '../verifySasViyaLogin'
|
||||||
import { RequestClient } from '../../request/RequestClient'
|
import { RequestClient } from '../../request/RequestClient'
|
||||||
jest.mock('axios')
|
jest.mock('axios')
|
||||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||||
@@ -58,6 +60,7 @@ describe('AuthManager', () => {
|
|||||||
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
|
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('login - default mechanism', () => {
|
||||||
it('should call the auth callback and return when already logged in', async () => {
|
it('should call the auth callback and return when already logged in', async () => {
|
||||||
const authManager = new AuthManager(
|
const authManager = new AuthManager(
|
||||||
serverUrl,
|
serverUrl,
|
||||||
@@ -160,6 +163,86 @@ describe('AuthManager', () => {
|
|||||||
mockLoginAuthoriseRequiredResponse
|
mockLoginAuthoriseRequiredResponse
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('login - redirect mechanism', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
jest.mock('../openWebPage')
|
||||||
|
jest
|
||||||
|
.spyOn(openWebPageModule, 'openWebPage')
|
||||||
|
.mockImplementation(() =>
|
||||||
|
Promise.resolve({ close: jest.fn() } as unknown as Window)
|
||||||
|
)
|
||||||
|
jest.mock('../verifySasViyaLogin')
|
||||||
|
jest
|
||||||
|
.spyOn(verifySasViyaLoginModule, 'verifySasViyaLogin')
|
||||||
|
.mockImplementation(() => Promise.resolve({ isLoggedIn: true }))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should call the auth callback and return when already logged in', async () => {
|
||||||
|
const authManager = new AuthManager(
|
||||||
|
serverUrl,
|
||||||
|
serverType,
|
||||||
|
requestClient,
|
||||||
|
authCallback
|
||||||
|
)
|
||||||
|
jest
|
||||||
|
.spyOn<any, any>(authManager, 'fetchUserName')
|
||||||
|
.mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: true,
|
||||||
|
userName
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const loginResponse = await authManager.redirectedLogIn({})
|
||||||
|
|
||||||
|
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||||
|
expect(loginResponse.userName).toEqual(userName)
|
||||||
|
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should open pop up if not logged in', async () => {
|
||||||
|
const authManager = new AuthManager(
|
||||||
|
serverUrl,
|
||||||
|
serverType,
|
||||||
|
requestClient,
|
||||||
|
authCallback
|
||||||
|
)
|
||||||
|
jest
|
||||||
|
.spyOn<any, any>(authManager, 'fetchUserName')
|
||||||
|
.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: false,
|
||||||
|
userName: ''
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: true,
|
||||||
|
userName
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const loginResponse = await authManager.redirectedLogIn({})
|
||||||
|
|
||||||
|
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||||
|
expect(loginResponse.userName).toEqual(userName)
|
||||||
|
|
||||||
|
expect(openWebPageModule.openWebPage).toHaveBeenCalledWith(
|
||||||
|
`/SASLogon/home`,
|
||||||
|
'SASLogon',
|
||||||
|
{
|
||||||
|
width: 500,
|
||||||
|
height: 600
|
||||||
|
},
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
expect(authManager['fetchUserName']).toHaveBeenCalledTimes(2)
|
||||||
|
|
||||||
|
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should check and return session information if logged in', async () => {
|
it('should check and return session information if logged in', async () => {
|
||||||
const authManager = new AuthManager(
|
const authManager = new AuthManager(
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { delay } from '../utils'
|
import { delay } from '../utils'
|
||||||
|
|
||||||
export async function verifySas9Login(loginPopup: Window) {
|
export async function verifySas9Login(loginPopup: Window): Promise<{
|
||||||
|
isLoggedIn: boolean
|
||||||
|
}> {
|
||||||
let isLoggedIn = false
|
let isLoggedIn = false
|
||||||
let startTime = new Date()
|
let startTime = new Date()
|
||||||
let elapsedSeconds = 0
|
let elapsedSeconds = 0
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { delay } from '../utils'
|
import { delay } from '../utils'
|
||||||
|
|
||||||
export async function verifySasViyaLogin(loginPopup: Window) {
|
export async function verifySasViyaLogin(loginPopup: Window): Promise<{
|
||||||
|
isLoggedIn: boolean
|
||||||
|
}> {
|
||||||
let isLoggedIn = false
|
let isLoggedIn = false
|
||||||
let startTime = new Date()
|
let startTime = new Date()
|
||||||
let elapsedSeconds = 0
|
let elapsedSeconds = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user