1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-14 15:40:06 +00:00

Merge pull request #310 from sasjs/issue-309

Issue 309
This commit is contained in:
Muhammad Saad
2021-03-29 18:07:51 +05:00
committed by GitHub
3 changed files with 83 additions and 17123 deletions

17128
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -82,17 +82,33 @@ export class AuthManager {
* @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`. * @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`.
*/ */
public async checkSession() { public async checkSession() {
const { result: loginResponse } = await this.requestClient.get<string>( //For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution.
this.loginUrl.replace('.do', ''), //For SAS9 we will send request on SASStoredProcess
undefined, const url =
'text/plain' this.serverType === 'SASVIYA'
) ? `${this.serverUrl}/identities`
const responseText = loginResponse : `${this.serverUrl}/SASStoredProcess`
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText)
let loginForm: any = null const { result: loginResponse } = await this.requestClient
.get<string>(url, undefined, 'text/plain')
.catch((err: any) => {
return { result: 'authErr' }
})
const isLoggedIn = loginResponse !== 'authErr'
let loginForm = null
if (!isLoggedIn) { if (!isLoggedIn) {
loginForm = await this.getLoginForm(responseText) //We will logout to make sure cookies are removed and login form is presented
this.logOut()
const { result: formResponse } = await this.requestClient.get<string>(
this.loginUrl.replace('.do', ''),
undefined,
'text/plain'
)
loginForm = await this.getLoginForm(formResponse)
} }
return Promise.resolve({ return Promise.resolve({

View File

@@ -176,41 +176,19 @@ describe('AuthManager', () => {
const response = await authManager.checkSession() const response = await authManager.checkSession()
expect(response.isLoggedIn).toBeTruthy() expect(response.isLoggedIn).toBeTruthy()
expect(mockedAxios.get).toHaveBeenNthCalledWith(1, `/SASLogon/login`, { expect(mockedAxios.get).toHaveBeenNthCalledWith(
withCredentials: true, 1,
responseType: 'text', `http://test-server.com/identities`,
transformResponse: undefined, {
headers: { withCredentials: true,
Accept: '*/*', responseType: 'text',
'Content-Type': 'text/plain' transformResponse: undefined,
headers: {
Accept: '*/*',
'Content-Type': 'text/plain'
}
} }
})
done()
})
it('should check and return session information if logged in', async (done) => {
const authManager = new AuthManager(
serverUrl,
serverType,
requestClient,
authCallback
) )
mockedAxios.get.mockImplementation(() =>
Promise.resolve({ data: '<button onClick="logout">' })
)
const response = await authManager.checkSession()
expect(response.isLoggedIn).toBeTruthy()
expect(mockedAxios.get).toHaveBeenNthCalledWith(1, `/SASLogon/login`, {
withCredentials: true,
responseType: 'text',
transformResponse: undefined,
headers: {
Accept: '*/*',
'Content-Type': 'text/plain'
}
})
done() done()
}) })