Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add test case for ahrefs bot #1011

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading