Skip to content

Commit aac7621

Browse files
authored
Merge pull request #516 from GSA/1784-filter-logic-update
1784: Filter Logic Update
2 parents 612741b + 757279d commit aac7621

File tree

5 files changed

+236
-42
lines changed

5 files changed

+236
-42
lines changed

entities/core-result.entity.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,14 @@ export class CoreResult {
657657
'www_status_code',
658658
'www_title',
659659
];
660+
661+
static filteredMediaTypes = [
662+
'application/javascript',
663+
'application/json',
664+
'application/xml',
665+
'application/xhtml+xml',
666+
'image/jpeg',
667+
'text/javascript',
668+
'text/xml',
669+
];
660670
}

libs/database/src/core-results/core-result.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
22
import { TypeOrmModule } from '@nestjs/typeorm';
33
import { CoreResult } from 'entities/core-result.entity';
44
import { CoreResultService } from './core-result.service';
5+
import { WebsiteModule } from '../websites/website.module';
56

67
@Module({
7-
imports: [TypeOrmModule.forFeature([CoreResult])],
8+
imports: [TypeOrmModule.forFeature([CoreResult]), WebsiteModule],
89
providers: [CoreResultService],
910
exports: [TypeOrmModule, CoreResultService],
1011
})

libs/database/src/core-results/core-result.service.spec.ts

Lines changed: 204 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@ import { Website } from 'entities/website.entity';
55
import { mock, mockReset } from 'jest-mock-extended';
66
import { Repository } from 'typeorm';
77
import { CoreResultService } from './core-result.service';
8+
import { WebsiteService } from '@app/database/websites/websites.service';
89
import { Logger } from '@nestjs/common';
910
import { ScanStatus } from 'entities/scan-status';
1011

1112
describe('CoreResultService', () => {
1213
let service: CoreResultService;
1314
let mockRepository: any;
15+
let mockWebsiteService: any;
1416

1517
beforeEach(async () => {
1618
mockRepository = mock<Repository<CoreResult>>();
19+
mockWebsiteService = mock<WebsiteService>();
1720
const module: TestingModule = await Test.createTestingModule({
1821
providers: [
1922
CoreResultService,
2023
{
2124
provide: getRepositoryToken(CoreResult),
2225
useValue: mockRepository,
2326
},
27+
{
28+
provide: WebsiteService,
29+
useValue: mockWebsiteService,
30+
},
2431
],
2532
}).compile();
2633

@@ -29,6 +36,7 @@ describe('CoreResultService', () => {
2936

3037
afterEach(async () => {
3138
mockReset(mockRepository);
39+
mockReset(mockWebsiteService);
3240
});
3341

3442
it('should be defined', () => {
@@ -59,17 +67,6 @@ describe('CoreResultService', () => {
5967
expect(result).toStrictEqual(coreResult);
6068
});
6169

62-
it('should create a CoreResult', async () => {
63-
const coreResult = new CoreResult();
64-
const website = new Website();
65-
website.id = 1;
66-
coreResult.id = 1;
67-
coreResult.website = website;
68-
69-
await service.create(coreResult);
70-
expect(mockRepository.insert).toHaveBeenCalledWith(coreResult);
71-
});
72-
7370
it('should create a CoreResult from CoreResultPages', async () => {
7471
const websiteId = 1;
7572
const scanStatus: ScanStatus = ScanStatus['Completed'];
@@ -285,5 +282,201 @@ describe('CoreResultService', () => {
285282
websiteUrl,
286283
);
287284
expect(mockRepository.insert).toHaveBeenCalled();
285+
expect(mockWebsiteService.setFilter).not.toHaveBeenCalled();
286+
expect(mockRepository.insert).toHaveBeenCalledWith(
287+
expect.objectContaining({ filter: false }),
288+
);
289+
});
290+
291+
it('should call setFilter when finalUrlMIMEType is a filtered type', async () => {
292+
const websiteId = 1;
293+
const scanStatus: ScanStatus = ScanStatus['Completed'];
294+
const websiteUrl = 'https://example.gov';
295+
296+
const pages = {
297+
base: {
298+
targetUrlBaseDomain: 'example.gov',
299+
},
300+
notFound: {
301+
status: scanStatus,
302+
result: {
303+
notFoundScan: { targetUrl404Test: false },
304+
},
305+
},
306+
primary: {
307+
status: scanStatus,
308+
result: {
309+
urlScan: {
310+
targetUrlRedirects: null,
311+
finalUrl: null,
312+
finalUrlIsLive: null,
313+
finalUrlBaseDomain: null,
314+
finalUrlWebsite: null,
315+
finalUrlTopLevelDomain: null,
316+
finalUrlMIMEType: 'application/json',
317+
finalUrlSameDomain: null,
318+
finalUrlStatusCode: null,
319+
finalUrlSameWebsite: null,
320+
finalUrlPageHash: null,
321+
},
322+
dapScan: {
323+
dapDetected: null,
324+
dapParameters: null,
325+
dapVersion: '',
326+
gaTagIds: '',
327+
},
328+
seoScan: {
329+
ogTitleFinalUrl: null,
330+
ogDescriptionFinalUrl: null,
331+
ogArticlePublishedFinalUrl: null,
332+
ogArticleModifiedFinalUrl: null,
333+
mainElementFinalUrl: null,
334+
canonicalLink: null,
335+
pageTitle: null,
336+
metaDescriptionContent: null,
337+
metaKeywordsContent: null,
338+
ogImageContent: null,
339+
ogTypeContent: null,
340+
ogUrlContent: null,
341+
htmlLangContent: null,
342+
hrefLangContent: null,
343+
dcDateContent: null,
344+
dcDateCreatedContent: null,
345+
dctermsCreatedContent: null,
346+
revisedContent: null,
347+
lastModifiedContent: null,
348+
dateContent: null,
349+
},
350+
thirdPartyScan: {
351+
thirdPartyServiceDomains: null,
352+
thirdPartyServiceCount: null,
353+
cookieDomains: null,
354+
thirdPartyServiceUrls: null,
355+
},
356+
cookieScan: { domains: '' },
357+
uswdsScan: {
358+
usaClasses: null,
359+
usaClassesUsed: null,
360+
uswdsString: null,
361+
uswdsInlineCss: null,
362+
uswdsUsFlag: null,
363+
uswdsStringInCss: null,
364+
uswdsUsFlagInCss: null,
365+
uswdsPublicSansFont: null,
366+
uswdsSemanticVersion: null,
367+
uswdsVersion: null,
368+
uswdsCount: null,
369+
heresHowYouKnowBanner: null,
370+
},
371+
loginScan: { loginDetected: null, loginProvider: null },
372+
cmsScan: { cms: null },
373+
requiredLinksScan: {
374+
requiredLinksUrl: null,
375+
requiredLinksText: null,
376+
},
377+
searchScan: { searchDetected: null, searchgov: null },
378+
mobileScan: { viewportMetaTag: false },
379+
toolingScan: { tooling: null },
380+
},
381+
},
382+
robotsTxt: {
383+
status: scanStatus,
384+
result: {
385+
robotsTxtScan: {
386+
robotsTxtFinalUrl: null,
387+
robotsTxtStatusCode: null,
388+
robotsTxtFinalUrlLive: null,
389+
robotsTxtDetected: null,
390+
robotsTxtFinalUrlMimeType: null,
391+
robotsTxtTargetUrlRedirects: null,
392+
robotsTxtFinalUrlSize: null,
393+
robotsTxtCrawlDelay: null,
394+
robotsTxtSitemapLocations: null,
395+
},
396+
},
397+
},
398+
sitemapXml: {
399+
status: scanStatus,
400+
result: {
401+
sitemapXmlScan: {
402+
sitemapXmlDetected: null,
403+
sitemapXmlStatusCode: null,
404+
sitemapXmlFinalUrl: null,
405+
sitemapXmlFinalUrlLive: null,
406+
sitemapTargetUrlRedirects: null,
407+
sitemapXmlFinalUrlFilesize: null,
408+
sitemapXmlFinalUrlMimeType: null,
409+
sitemapXmlLastMod: null,
410+
sitemapXmlPageHash: null,
411+
sitemapXmlCount: null,
412+
sitemapXmlPdfCount: null,
413+
},
414+
},
415+
},
416+
dns: {
417+
status: scanStatus,
418+
result: { dnsScan: { ipv6: true, dnsHostname: null } },
419+
},
420+
accessibility: {
421+
status: scanStatus,
422+
result: {
423+
accessibilityScan: {
424+
accessibilityResults: '',
425+
accessibilityResultsList: '',
426+
},
427+
},
428+
},
429+
performance: {
430+
status: scanStatus,
431+
result: {
432+
performanceScan: {
433+
largestContentfulPaint: null,
434+
cumulativeLayoutShift: null,
435+
},
436+
},
437+
},
438+
security: {
439+
status: scanStatus,
440+
result: { securityScan: { httpsEnforced: null, hsts: null } },
441+
},
442+
clientRedirect: {
443+
status: scanStatus,
444+
result: {
445+
clientRedirectScan: {
446+
hasClientRedirect: null,
447+
usesJsRedirect: null,
448+
usesMetaRefresh: null,
449+
},
450+
},
451+
},
452+
www: {
453+
status: scanStatus,
454+
result: {
455+
wwwScan: {
456+
wwwFinalUrl: null,
457+
wwwStatusCode: null,
458+
wwwTitle: null,
459+
wwwSame: null,
460+
},
461+
},
462+
},
463+
};
464+
const logger = mock<Logger>();
465+
466+
await service.createFromCoreResultPages(
467+
websiteId,
468+
pages,
469+
logger,
470+
false,
471+
1,
472+
1,
473+
websiteUrl,
474+
);
475+
476+
expect(mockRepository.insert).toHaveBeenCalled();
477+
expect(mockWebsiteService.setFilter).toHaveBeenCalledWith(websiteId, true);
478+
expect(mockRepository.insert).toHaveBeenCalledWith(
479+
expect.objectContaining({ filter: true }),
480+
);
288481
});
289482
});

libs/database/src/core-results/core-result.service.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import { CoreResult } from 'entities/core-result.entity';
55
import { Website } from 'entities/website.entity';
66
import { ScanStatus } from 'entities/scan-status';
77
import { CoreResultPages } from 'entities/core-result.entity';
8-
import e from 'express';
8+
import { WebsiteService } from '@app/database/websites/websites.service';
99

1010
@Injectable()
1111
export class CoreResultService {
1212
constructor(
1313
@InjectRepository(CoreResult)
1414
private coreResultRepository: Repository<CoreResult>,
15+
private websiteService: WebsiteService,
1516
) {}
1617

1718
async findAll(): Promise<CoreResult[]> {
1819
const results = await this.coreResultRepository.find();
1920
return results;
2021
}
2122

22-
createFromCoreResultPages(
23+
async createFromCoreResultPages(
2324
websiteId: number,
2425
pages: CoreResultPages,
2526
logger: Logger,
@@ -48,10 +49,6 @@ export class CoreResultService {
4849
this.updateSecurityScanResults(coreResult, pages, logger);
4950
this.updateWwwScanResults(coreResult, pages, logger);
5051

51-
return this.create(coreResult);
52-
}
53-
54-
async create(coreResult: CoreResult) {
5552
const exists = await this.coreResultRepository.findOne({
5653
where: {
5754
website: {
@@ -60,6 +57,14 @@ export class CoreResultService {
6057
},
6158
});
6259

60+
if (
61+
coreResult.finalUrlMIMEType &&
62+
CoreResult.filteredMediaTypes.includes(coreResult.finalUrlMIMEType)
63+
) {
64+
coreResult.filter = true;
65+
await this.websiteService.setFilter(websiteId, true);
66+
}
67+
6368
if (exists) {
6469
await this.coreResultRepository.update(exists.id, coreResult);
6570
} else {

0 commit comments

Comments
 (0)