Skip to content

Commit

Permalink
test: increase tests coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Feb 18, 2024
1 parent f5cdea6 commit 801ec26
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { generateSRIHashes } from './core.mjs'
*/
export const sriCSP =
sriCspOptions => /** @satisfies {import('astro').AstroIntegration} */ ({
name: 'scp-sri-postbuild',
name: 'astro-sri-csp',
hooks: {
'astro:build:done': async ({ dir, logger }) =>
await generateSRIHashes(logger, {
Expand Down
85 changes: 84 additions & 1 deletion tests/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
updateSriHashes,
} from '../core.mjs'
import { AstroIntegrationLogger } from 'astro'
import exp from 'node:constants'

const testsDir = new URL('.', import.meta.url).pathname
const rootDir = resolve(testsDir, '..')
Expand Down Expand Up @@ -147,6 +146,45 @@ describe('updateSriHashes', () => {
expect(h.extStyleHashes.size).toBe(0)
})

it('preserves sri hash in inline script', async () => {
const content = `<html>
<head>
<title>My Test Page</title>
</head>
<body>
<script integrity="sha256-TWupyvVdPa1DyFqLnQMqRpuUWdS3nKPnz70IcS/1o3Q=">console.log("Hello World!")</script>
</body>
</html>`

const expected = `<html>
<head>
<title>My Test Page</title>
</head>
<body>
<script integrity="sha256-TWupyvVdPa1DyFqLnQMqRpuUWdS3nKPnz70IcS/1o3Q=">console.log("Hello World!")</script>
</body>
</html>`

const h = getEmptyHashes()
const updated = await updateSriHashes(
console as unknown as AstroIntegrationLogger,
testsDir,
content,
h,
)

expect(updated).toEqual(expected)
expect(h.inlineScriptHashes.size).toBe(1)
expect(
h.inlineScriptHashes.has(
'sha256-TWupyvVdPa1DyFqLnQMqRpuUWdS3nKPnz70IcS/1o3Q=',
),
).toBe(true)
expect(h.inlineStyleHashes.size).toBe(0)
expect(h.extScriptHashes.size).toBe(0)
expect(h.extStyleHashes.size).toBe(0)
})

it('adds sri hash to inline style', async () => {
const content = `<html>
<head>
Expand Down Expand Up @@ -270,5 +308,50 @@ describe('updateSriHashes', () => {
expect(h.extStyleHashes.size).toBe(0)
})

it('adds sri hash to external style (same origin)', async () => {
const content = `<html>
<head>
<title>My Test Page</title>
<link rel="canonical" href="https://example.com" />
<link rel="stylesheet" href="/fake.css">
</head>
<body>
<h1>My Test Page</h1>
<p>Some text</p>
</body>
</html>`

const expected = `<html>
<head>
<title>My Test Page</title>
<link rel="canonical" href="https://example.com" />
<link rel="stylesheet" href="/fake.css" integrity="sha256-gl5rCtPAw9BpVpGpdLhrf4LFwVUQ0FgQ5D231KxY2/w="/>
</head>
<body>
<h1>My Test Page</h1>
<p>Some text</p>
</body>
</html>`

const h = getEmptyHashes()
const updated = await updateSriHashes(
console as unknown as AstroIntegrationLogger,
testsDir,
content,
h,
)

expect(updated).toEqual(expected)
expect(h.extStyleHashes.size).toBe(1)
expect(
h.extStyleHashes.has(
'sha256-gl5rCtPAw9BpVpGpdLhrf4LFwVUQ0FgQ5D231KxY2/w=',
),
).toBe(true)
expect(h.inlineScriptHashes.size).toBe(0)
expect(h.extScriptHashes.size).toBe(0)
expect(h.inlineStyleHashes.size).toBe(0)
})

// TODO: Add tests for external styles
})
1 change: 1 addition & 0 deletions tests/fake.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 { color: red; }
20 changes: 20 additions & 0 deletions tests/main.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest'

import defaultIntegrationExport, { sriCSP } from '../main.mjs'

describe('sriCSP', () => {
it('is exported as default', () => {
expect(defaultIntegrationExport).toBe(sriCSP)
expect(sriCSP).toBeInstanceOf(Function)
})

it('returns a valid AstroIntegration object', () => {
const integration = sriCSP({})

expect(Object.keys(integration).sort()).toEqual(['hooks', 'name'])
expect(integration.name).toBe('astro-sri-csp')

expect(Object.keys(integration.hooks).sort()).toEqual(['astro:build:done'])
expect(integration.hooks['astro:build:done']).toBeInstanceOf(Function)
})
})
8 changes: 4 additions & 4 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default defineConfig({
include: ['*.mjs'],
exclude: ['tests/**/*'],
thresholds: {
branches: 60.0,
lines: 50.0,
functions: 50.0,
statements: 60.0,
statements: 70.0,
branches: 75.0,
functions: 60.0,
lines: 70.0,
},
},
},
Expand Down

0 comments on commit 801ec26

Please sign in to comment.