mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 21:30: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,107 +60,188 @@ describe('AuthManager', () => {
|
|||||||
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
|
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call the auth callback and return when already logged in', async () => {
|
describe('login - default mechanism', () => {
|
||||||
const authManager = new AuthManager(
|
it('should call the auth callback and return when already logged in', async () => {
|
||||||
serverUrl,
|
const authManager = new AuthManager(
|
||||||
serverType,
|
serverUrl,
|
||||||
requestClient,
|
serverType,
|
||||||
authCallback
|
requestClient,
|
||||||
)
|
authCallback
|
||||||
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
)
|
||||||
Promise.resolve({
|
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
||||||
isLoggedIn: true,
|
Promise.resolve({
|
||||||
userName,
|
isLoggedIn: true,
|
||||||
loginForm: 'test'
|
userName,
|
||||||
})
|
loginForm: 'test'
|
||||||
)
|
})
|
||||||
|
)
|
||||||
|
|
||||||
const loginResponse = await authManager.logIn(userName, password)
|
const loginResponse = await authManager.logIn(userName, password)
|
||||||
|
|
||||||
expect(loginResponse.isLoggedIn).toBeTruthy()
|
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||||
expect(loginResponse.userName).toEqual(userName)
|
expect(loginResponse.userName).toEqual(userName)
|
||||||
expect(authCallback).toHaveBeenCalledTimes(1)
|
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||||
})
|
|
||||||
|
|
||||||
it('should post a login request to the server if not logged in', async () => {
|
|
||||||
const authManager = new AuthManager(
|
|
||||||
serverUrl,
|
|
||||||
serverType,
|
|
||||||
requestClient,
|
|
||||||
authCallback
|
|
||||||
)
|
|
||||||
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
|
||||||
Promise.resolve({
|
|
||||||
isLoggedIn: false,
|
|
||||||
userName: '',
|
|
||||||
loginForm: { name: 'test' }
|
|
||||||
})
|
|
||||||
)
|
|
||||||
mockedAxios.post.mockImplementation(() =>
|
|
||||||
Promise.resolve({ data: mockLoginSuccessResponse })
|
|
||||||
)
|
|
||||||
|
|
||||||
const loginResponse = await authManager.logIn(userName, password)
|
|
||||||
|
|
||||||
expect(loginResponse.isLoggedIn).toBeTruthy()
|
|
||||||
expect(loginResponse.userName).toEqual(userName)
|
|
||||||
|
|
||||||
const loginParams = serialize({
|
|
||||||
_service: 'default',
|
|
||||||
username: userName,
|
|
||||||
password,
|
|
||||||
name: 'test'
|
|
||||||
})
|
})
|
||||||
expect(mockedAxios.post).toHaveBeenCalledWith(
|
|
||||||
`/SASLogon/login`,
|
it('should post a login request to the server if not logged in', async () => {
|
||||||
loginParams,
|
const authManager = new AuthManager(
|
||||||
{
|
serverUrl,
|
||||||
withCredentials: true,
|
serverType,
|
||||||
headers: {
|
requestClient,
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
authCallback
|
||||||
Accept: '*/*'
|
)
|
||||||
|
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: false,
|
||||||
|
userName: '',
|
||||||
|
loginForm: { name: 'test' }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
mockedAxios.post.mockImplementation(() =>
|
||||||
|
Promise.resolve({ data: mockLoginSuccessResponse })
|
||||||
|
)
|
||||||
|
|
||||||
|
const loginResponse = await authManager.logIn(userName, password)
|
||||||
|
|
||||||
|
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||||
|
expect(loginResponse.userName).toEqual(userName)
|
||||||
|
|
||||||
|
const loginParams = serialize({
|
||||||
|
_service: 'default',
|
||||||
|
username: userName,
|
||||||
|
password,
|
||||||
|
name: 'test'
|
||||||
|
})
|
||||||
|
expect(mockedAxios.post).toHaveBeenCalledWith(
|
||||||
|
`/SASLogon/login`,
|
||||||
|
loginParams,
|
||||||
|
{
|
||||||
|
withCredentials: true,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
Accept: '*/*'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
)
|
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||||
expect(authCallback).toHaveBeenCalledTimes(1)
|
})
|
||||||
|
|
||||||
|
it('should parse and submit the authorisation form when necessary', async () => {
|
||||||
|
const authManager = new AuthManager(
|
||||||
|
serverUrl,
|
||||||
|
serverType,
|
||||||
|
requestClient,
|
||||||
|
authCallback
|
||||||
|
)
|
||||||
|
jest
|
||||||
|
.spyOn(requestClient, 'authorize')
|
||||||
|
.mockImplementation(() => Promise.resolve())
|
||||||
|
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: false,
|
||||||
|
userName: 'test',
|
||||||
|
loginForm: { name: 'test' }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
mockedAxios.post.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
data: mockLoginAuthoriseRequiredResponse,
|
||||||
|
config: { url: 'https://test.com/SASLogon/login' },
|
||||||
|
request: { responseURL: 'https://test.com/OAuth/authorize' }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
mockedAxios.get.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
data: mockLoginAuthoriseRequiredResponse
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
await authManager.logIn(userName, password)
|
||||||
|
|
||||||
|
expect(requestClient.authorize).toHaveBeenCalledWith(
|
||||||
|
mockLoginAuthoriseRequiredResponse
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should parse and submit the authorisation form when necessary', async () => {
|
describe('login - redirect mechanism', () => {
|
||||||
const authManager = new AuthManager(
|
beforeAll(() => {
|
||||||
serverUrl,
|
jest.mock('../openWebPage')
|
||||||
serverType,
|
jest
|
||||||
requestClient,
|
.spyOn(openWebPageModule, 'openWebPage')
|
||||||
authCallback
|
.mockImplementation(() =>
|
||||||
)
|
Promise.resolve({ close: jest.fn() } as unknown as Window)
|
||||||
jest
|
)
|
||||||
.spyOn(requestClient, 'authorize')
|
jest.mock('../verifySasViyaLogin')
|
||||||
.mockImplementation(() => Promise.resolve())
|
jest
|
||||||
jest.spyOn(authManager, 'checkSession').mockImplementation(() =>
|
.spyOn(verifySasViyaLoginModule, 'verifySasViyaLogin')
|
||||||
Promise.resolve({
|
.mockImplementation(() => Promise.resolve({ isLoggedIn: true }))
|
||||||
isLoggedIn: false,
|
})
|
||||||
userName: 'test',
|
|
||||||
loginForm: { name: 'test' }
|
|
||||||
})
|
|
||||||
)
|
|
||||||
mockedAxios.post.mockImplementationOnce(() =>
|
|
||||||
Promise.resolve({
|
|
||||||
data: mockLoginAuthoriseRequiredResponse,
|
|
||||||
config: { url: 'https://test.com/SASLogon/login' },
|
|
||||||
request: { responseURL: 'https://test.com/OAuth/authorize' }
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
mockedAxios.get.mockImplementationOnce(() =>
|
it('should call the auth callback and return when already logged in', async () => {
|
||||||
Promise.resolve({
|
const authManager = new AuthManager(
|
||||||
data: mockLoginAuthoriseRequiredResponse
|
serverUrl,
|
||||||
})
|
serverType,
|
||||||
)
|
requestClient,
|
||||||
|
authCallback
|
||||||
|
)
|
||||||
|
jest
|
||||||
|
.spyOn<any, any>(authManager, 'fetchUserName')
|
||||||
|
.mockImplementation(() =>
|
||||||
|
Promise.resolve({
|
||||||
|
isLoggedIn: true,
|
||||||
|
userName
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
await authManager.logIn(userName, password)
|
const loginResponse = await authManager.redirectedLogIn({})
|
||||||
|
|
||||||
expect(requestClient.authorize).toHaveBeenCalledWith(
|
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||||
mockLoginAuthoriseRequiredResponse
|
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 () => {
|
||||||
|
|||||||
@@ -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