Skip to content

Commit

Permalink
chore: add test case for ahrefs bot (#1011)
Browse files Browse the repository at this point in the history
* chore: add bot test case

* fix nesting

* parameterise tests
  • Loading branch information
pauldambra authored Feb 9, 2024
1 parent a9f25e0 commit 30979cd
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,51 +195,45 @@ describe('utils', () => {
new_script.onerror!('uh-oh')
expect(callback).toHaveBeenCalledWith('uh-oh')
})
})

describe('user agent blocking', () => {
it.each(DEFAULT_BLOCKED_UA_STRS.concat('testington'))(
'blocks a bot based on the user agent %s',
(botString) => {
const randomisedUserAgent = userAgentFor(botString)

expect(_isBlockedUA(randomisedUserAgent, ['testington'])).toBe(true)
}
)

it('should block googlebot desktop', () => {
expect(
_isBlockedUA(
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36',
[]
)
).toBe(true)
})
describe('user agent blocking', () => {
it.each(DEFAULT_BLOCKED_UA_STRS.concat('testington'))(
'blocks a bot based on the user agent %s',
(botString) => {
const randomisedUserAgent = userAgentFor(botString)

it('should block openai bot', () => {
expect(
_isBlockedUA(
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.0; +https://openai.com/gptbot)',
[]
)
).toBe(true)
})
expect(_isBlockedUA(randomisedUserAgent, ['testington'])).toBe(true)
}
)

it.each([
[
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36',
],
['AhrefsSiteAudit (Desktop) - Mozilla/5.0 (compatible; AhrefsSiteAudit/6.1; +http://ahrefs.com/robot/)'],
['Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.0; +https://openai.com/gptbot)'],
])('blocks based on user agent', (botString) => {
expect(_isBlockedUA(botString, [])).toBe(true)
expect(_isBlockedUA(botString.toLowerCase(), [])).toBe(true)
expect(_isBlockedUA(botString.toUpperCase(), [])).toBe(true)
})
})

describe('check for cross domain cookies', () => {
it.each([
[false, 'https://test.herokuapp.com'],
[false, 'test.herokuapp.com'],
[false, 'herokuapp.com'],
[false, undefined],
// ensure it isn't matching herokuapp anywhere in the domain
[true, 'https://test.herokuapp.com.impersonator.io'],
[true, 'mysite-herokuapp.com'],
[true, 'https://bbc.co.uk'],
[true, 'bbc.co.uk'],
[true, 'www.bbc.co.uk'],
])('should return %s when hostname is %s', (expectedResult, hostname) => {
expect(isCrossDomainCookie({ hostname } as unknown as Location)).toEqual(expectedResult)
})
describe('check for cross domain cookies', () => {
it.each([
[false, 'https://test.herokuapp.com'],
[false, 'test.herokuapp.com'],
[false, 'herokuapp.com'],
[false, undefined],
// ensure it isn't matching herokuapp anywhere in the domain
[true, 'https://test.herokuapp.com.impersonator.io'],
[true, 'mysite-herokuapp.com'],
[true, 'https://bbc.co.uk'],
[true, 'bbc.co.uk'],
[true, 'www.bbc.co.uk'],
])('should return %s when hostname is %s', (expectedResult, hostname) => {
expect(isCrossDomainCookie({ hostname } as unknown as Location)).toEqual(expectedResult)
})
})

Expand Down

0 comments on commit 30979cd

Please sign in to comment.