1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-15 18:54:36 +00:00

fix: sas9 extractUserName improvement

This commit is contained in:
2022-09-22 01:05:53 +02:00
parent 16185eba4d
commit a01eb948b5
40 changed files with 228 additions and 165 deletions

View File

@@ -2,6 +2,7 @@ import { ServerType } from '@sasjs/utils/types'
import { RequestClient } from '../request/RequestClient'
import { LoginOptions, LoginResult } from '../types/Login'
import { serialize } from '../utils'
import { extractUserNameSas9 } from '../utils/sas9/extractUserNameSas9'
import { openWebPage } from './openWebPage'
import { verifySas9Login } from './verifySas9Login'
import { verifySasViyaLogin } from './verifySasViyaLogin'
@@ -280,15 +281,7 @@ export class AuthManager {
return response?.id
case ServerType.Sas9:
const matched = response?.match(/"title":"Log Off [0-1a-zA-Z ]*"/)
const username = matched?.[0].slice(17, -1)
if (!username.includes(' ')) return username
return username
.split(' ')
.map((name: string) => name.slice(0, 3).toLowerCase())
.join('')
return extractUserNameSas9(response)
case ServerType.Sasjs:
return response?.username

View File

@@ -0,0 +1,14 @@
export const extractUserNameSas9 = (response: string) => {
const regex = /"title":\s?"Log Off [0-1a-zA-Z ]*"/
const fallbackRegex = /"title":\s?"[0-1a-zA-Z ]*"/
const matched = response?.match(regex) || response?.match(fallbackRegex)
const username = matched?.[0].slice(17, -1)
if (!username) return 'unknown (error fetching username)'
if (!username.trim().includes(' ')) return username.trim()
return username
.split(' ')
.map((name: string) => name.slice(0, 3).toLowerCase())
.join('')
}

View File

@@ -0,0 +1,48 @@
import { extractUserNameSas9 } from '../sas9/extractUserNameSas9'
describe('Extract username SAS9', () => {
it('should return username', () => {
const response = ` "title": "Log Off SAS User One",`
const username = extractUserNameSas9(response)
expect(username).toEqual('sasuseone')
})
it('should return username with fallback regex', () => {
const response = ` "title": "Logout SAS User One",`
const username = extractUserNameSas9(response)
expect(username).toEqual('sasuseone')
})
it('should return username unknown', () => {
const response = ` invalid",`
const username = extractUserNameSas9(response)
expect(username).toEqual('unknown (error fetching username)')
})
it('should return username without shortening (one word user name)', () => {
const response = ` "title": "Log Off SasUserOne",`
const username = extractUserNameSas9(response)
expect(username).toEqual('SasUserOne')
})
it('should return username with falback regex without shortening (one word user name)', () => {
const response = ` "title": "Logout SasUserOne",`
const username = extractUserNameSas9(response)
expect(username).toEqual('SasUserOne')
})
it('should return username with unhandled Spanish language', () => {
const response = ` "title": "Desconectarse SAS User One",`
const username = extractUserNameSas9(response)
// Result won't be perfect but it will work Result will be: ctasasuseone
// instead of sasuseone
expect(username).toEqual('ctasasuseone')
})
})