|
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + fakeAsync, |
| 4 | + TestBed, |
| 5 | + tick |
| 6 | +} from '@angular/core/testing'; |
| 7 | +import { CpsTooltipDirective } from './cps-tooltip.directive'; |
| 8 | +import { Component } from '@angular/core'; |
| 9 | +import { By } from '@angular/platform-browser'; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + template: `<div cpsTooltip="<style onload='alert(420);'></style>"></div>`, |
| 13 | + imports: [CpsTooltipDirective] |
| 14 | +}) |
| 15 | +class MaliciousTooltipComponent {} |
| 16 | + |
| 17 | +@Component({ |
| 18 | + template: `<div cpsTooltip="<h1>Legit tooltip</h1>"></div>`, |
| 19 | + imports: [CpsTooltipDirective] |
| 20 | +}) |
| 21 | +class LegitTooltipComponent {} |
| 22 | + |
| 23 | +describe('CpsTooltipDirective', () => { |
| 24 | + let legitComponent: LegitTooltipComponent; |
| 25 | + let legitComponentFixture: ComponentFixture<LegitTooltipComponent>; |
| 26 | + |
| 27 | + let maliciousComponent: MaliciousTooltipComponent; |
| 28 | + let maliciousComponentFixture: ComponentFixture<MaliciousTooltipComponent>; |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + await TestBed.configureTestingModule({ |
| 32 | + imports: [] |
| 33 | + }).compileComponents(); |
| 34 | + }); |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + legitComponentFixture = TestBed.createComponent(LegitTooltipComponent); |
| 38 | + legitComponent = legitComponentFixture.componentInstance; |
| 39 | + legitComponentFixture.detectChanges(); |
| 40 | + |
| 41 | + maliciousComponentFixture = TestBed.createComponent( |
| 42 | + MaliciousTooltipComponent |
| 43 | + ); |
| 44 | + maliciousComponent = maliciousComponentFixture.componentInstance; |
| 45 | + maliciousComponentFixture.detectChanges(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should create the component', () => { |
| 49 | + expect(maliciousComponent).toBeTruthy(); |
| 50 | + expect(legitComponent).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should sanitize the malicious tooltip content', fakeAsync(() => { |
| 54 | + const consoleWarnSpy = jest |
| 55 | + .spyOn(console, 'warn') |
| 56 | + .mockImplementation(() => {}); |
| 57 | + |
| 58 | + const divElement = maliciousComponentFixture.debugElement.query( |
| 59 | + By.css('div') |
| 60 | + ); |
| 61 | + divElement.triggerEventHandler('mouseenter', null); |
| 62 | + maliciousComponentFixture.detectChanges(); |
| 63 | + |
| 64 | + tick(300); |
| 65 | + |
| 66 | + const tooltipElement: HTMLElement | null = |
| 67 | + document.body.querySelector('.cps-tooltip'); |
| 68 | + |
| 69 | + expect(tooltipElement).toBeTruthy(); |
| 70 | + expect(tooltipElement?.innerHTML).toBe( |
| 71 | + '<div class="cps-tooltip-content">Add your text to this tooltip</div>' |
| 72 | + ); |
| 73 | + // Angular informs about stripping some content during sanitization |
| 74 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 75 | + expect.stringContaining('sanitizing HTML stripped some content') |
| 76 | + ); |
| 77 | + |
| 78 | + divElement.triggerEventHandler('mouseleave', null); |
| 79 | + maliciousComponentFixture.detectChanges(); |
| 80 | + |
| 81 | + tick(500); |
| 82 | + |
| 83 | + expect(document.body.querySelector('.cps-tooltip')).toBeFalsy(); |
| 84 | + })); |
| 85 | + |
| 86 | + it('should properly show legit tooltip', fakeAsync(() => { |
| 87 | + const divElement = legitComponentFixture.debugElement.query(By.css('div')); |
| 88 | + divElement.triggerEventHandler('mouseenter', null); |
| 89 | + legitComponentFixture.detectChanges(); |
| 90 | + |
| 91 | + tick(300); |
| 92 | + |
| 93 | + const tooltipElement: HTMLElement | null = |
| 94 | + document.body.querySelector('.cps-tooltip'); |
| 95 | + |
| 96 | + expect(tooltipElement).toBeTruthy(); |
| 97 | + expect(tooltipElement?.innerHTML).toBe( |
| 98 | + '<div class="cps-tooltip-content"><h1>Legit tooltip</h1></div>' |
| 99 | + ); |
| 100 | + |
| 101 | + divElement.triggerEventHandler('mouseleave', null); |
| 102 | + legitComponentFixture.detectChanges(); |
| 103 | + |
| 104 | + tick(500); |
| 105 | + |
| 106 | + expect(document.body.querySelector('.cps-tooltip')).toBeFalsy(); |
| 107 | + })); |
| 108 | +}); |
0 commit comments