Skip to content

Commit

Permalink
Update cookie scan and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
akuny committed Nov 15, 2023
1 parent 27d1309 commit e5d33cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion libs/core-scanner/src/pages/primary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('primary scanner', () => {

it('should return the correct response', async () => {
await newTestPage(async ({ page }) => {
// We make an actual network request to 18f.gov in this test case,
// so this is more so an integration test than a unit test per se.
const input: CoreInputDto = {
websiteId: 1,
url: '18f.gov',
Expand Down Expand Up @@ -59,7 +61,7 @@ describe('primary scanner', () => {
uswdsUsFlagInCss: 0,
uswdsStringInCss: 20,
uswdsPublicSansFont: 0,
uswdsSemanticVersion: 'v3.6.1',
uswdsSemanticVersion: 'v3.7.0',
uswdsVersion: 100,
uswdsCount: 197,
},
Expand Down
22 changes: 18 additions & 4 deletions libs/core-scanner/src/scans/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@ import { buildCookieResult } from './cookies';
import { browserInstance, newTestPage } from '../test-helper';

describe('cookie scan', () => {
it('non-navigation different domains treated as third-parties', async () => {
it('detects if cookies are present', async () => {
await newTestPage(async ({ page }) => {
expect(await buildCookieResult(page)).toEqual({
// I'm not sure the best way to test this; MHT files don't include
// simulated cookie requests, so we just end up with empty results.
// We make an actual network request to 18f.gsa.gov in this test case,
// so this is more so an integration test than a unit test per se.
await page.goto('https://18f.gsa.gov/');

const result = await buildCookieResult(page);

expect(result).toEqual({
domains: '.18f.gsa.gov,.gsa.gov',
});
});
});

it('detects if no cookies are present', async () => {
await newTestPage(async ({ page }) => {
const result = await buildCookieResult(page);

expect(result).toEqual({
domains: '',
});
});
Expand Down
12 changes: 7 additions & 5 deletions libs/core-scanner/src/scans/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export const buildCookieResult = async (page: Page): Promise<CookieScan> => {
};
};

const cookieDomains = async (page: Page) => {
const client = (page as any)._client;
const result = await client.send('Network.getAllCookies');
const domains = uniq(result.cookies.map((cookie) => cookie.domain)).sort();
return domains.join(',');
const cookieDomains = async (page: Page): Promise<string> => {
const cookies = await page.cookies();
const cookieDomains = uniq(
cookies.map((cookieObj) => cookieObj.domain),
).sort();

return cookieDomains.join(',');
};

0 comments on commit e5d33cc

Please sign in to comment.