mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
chore: improved algorithm for detecting log off word
This commit is contained in:
@@ -1,10 +1,32 @@
|
||||
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)
|
||||
/**
|
||||
* Dictionary should contain only languages in SAS where `logout` text
|
||||
* is represented with more then one word
|
||||
*/
|
||||
const dictionary = ['Log Off']
|
||||
|
||||
if (!username) return 'unknown (error fetching username)'
|
||||
/**
|
||||
* Extracts username assuming the first word after "title" means log off if not found otherwise in the dictionary
|
||||
* @param response SAS response content
|
||||
* @returns username
|
||||
*/
|
||||
export const extractUserNameSas9 = (response: string) => {
|
||||
const regex = /"title":\s?".*"/
|
||||
|
||||
const matched = response?.match(regex)
|
||||
let username = matched?.[0].split(':')[1].trim()
|
||||
let breakIndex = username?.indexOf(' ')
|
||||
|
||||
dictionary.map((logoutWord) => {
|
||||
const index = username?.indexOf(logoutWord) || -1
|
||||
|
||||
if (index > -1) {
|
||||
breakIndex = index + logoutWord.length
|
||||
}
|
||||
})
|
||||
|
||||
username = username?.slice(breakIndex, -1)
|
||||
|
||||
if (!username) return 'unknown'
|
||||
|
||||
return username.trim()
|
||||
}
|
||||
|
||||
@@ -1,41 +1,97 @@
|
||||
import { extractUserNameSas9 } from '../sas9/extractUserNameSas9'
|
||||
|
||||
describe('Extract username SAS9', () => {
|
||||
it('should return username', () => {
|
||||
const response = ` "title": "Log Off SAS User One",`
|
||||
describe('Extract username SAS9 English - two word logout handled language', () => {
|
||||
const logoutWord = 'Log Off'
|
||||
|
||||
it('should return username with space after colon', () => {
|
||||
const response = ` "title": "${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SAS User One')
|
||||
})
|
||||
|
||||
it('should return username with fallback regex', () => {
|
||||
const response = ` "title": "Logout SAS User One",`
|
||||
it('should return username without space after colon', () => {
|
||||
const response = ` "title":"${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SAS User One')
|
||||
})
|
||||
|
||||
it('should return username with one word user name', () => {
|
||||
const response = ` "title": "${logoutWord} SasUserOne",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SasUserOne')
|
||||
})
|
||||
|
||||
it('should return username unknown', () => {
|
||||
const response = ` invalid",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('unknown (error fetching username)')
|
||||
expect(username).toEqual('unknown')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Extract username SAS9 two word logout unhandled language', () => {
|
||||
const logoutWord = 'Log out'
|
||||
|
||||
it('should return username with space after colon', () => {
|
||||
const response = ` "title": "${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('out SAS User One')
|
||||
})
|
||||
|
||||
it('should return username without space after colon', () => {
|
||||
const response = ` "title":"${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('out SAS User One')
|
||||
})
|
||||
|
||||
it('should return username with one word user name', () => {
|
||||
const response = ` "title": "Log Off SasUserOne",`
|
||||
const response = ` "title": "${logoutWord} SasUserOne",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('out SasUserOne')
|
||||
})
|
||||
|
||||
it('should return username unknown', () => {
|
||||
const response = ` invalid",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('unknown')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Extract username SAS9 Spasnish - one word logout languages', () => {
|
||||
const logoutWord = 'Desconexión'
|
||||
|
||||
it('should return username with space after colon', () => {
|
||||
const response = ` "title": "${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SAS User One')
|
||||
})
|
||||
|
||||
it('should return username without space after colon', () => {
|
||||
const response = ` "title":"${logoutWord} SAS User One",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SAS User One')
|
||||
})
|
||||
|
||||
it('should return username with one word user name', () => {
|
||||
const response = ` "title": "${logoutWord} SasUserOne",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
expect(username).toEqual('SasUserOne')
|
||||
})
|
||||
|
||||
it('should return username with unhandled Spanish language', () => {
|
||||
const response = ` "title": "Desconectarse SAS User One",`
|
||||
it('should return username unknown', () => {
|
||||
const response = ` invalid",`
|
||||
const username = extractUserNameSas9(response)
|
||||
|
||||
// Result won't be perfect but it will work Result will be: ctasasuseone
|
||||
// instead of sasuseone
|
||||
|
||||
expect(username).toEqual('ctarse SAS User One')
|
||||
expect(username).toEqual('unknown')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user