Skip to content

Commit

Permalink
chore: use eslint and create test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 2, 2020
1 parent 2b23eb8 commit 46222a9
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 45 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"plugin:adonis/typescriptPackage"
]
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"pretest": "npm run lint",
"test": "node japaFile.js",
"prepublishOnly": "npm run build",
"lint": "tslint --project tsconfig.json",
"lint": "eslint . --ext=.ts",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
Expand All @@ -30,13 +30,13 @@
"cz-conventional-changelog": "^3.1.0",
"del-cli": "^3.0.0",
"doctoc": "^1.4.0",
"eslint": "^6.8.0",
"eslint-plugin-adonis": "^1.0.8",
"husky": "^4.2.3",
"japa": "^3.0.1",
"mrm": "^2.1.0",
"pkg-ok": "^2.3.1",
"ts-node": "^8.6.2",
"tslint": "^6.0.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "^3.8.3"
},
"dependencies": {
Expand Down
19 changes: 19 additions & 0 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @adonisjs/shield
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { FakeLogger } from '@adonisjs/logger/build/standalone'
import { Profiler } from '@adonisjs/profiler/build/standalone'

const logger = new FakeLogger({ level: 'trace', enabled: false, name: 'adonisjs' })
const profiler = new Profiler(__dirname, logger, {})

export function getCtx () {
return HttpContext.create('/', {}, logger, profiler.create(''), {} as any)
}
22 changes: 11 additions & 11 deletions test/csp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { csp } from '../src/csp'
import { getCtx } from '../test-helpers'

test.group('Csp', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = csp({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('Content-Security-Policy'))
Expand All @@ -24,51 +24,51 @@ test.group('Csp', () => {
const middlewareFn = csp({
enabled: true,
directives: {
defaultSrc: [`'self'`],
defaultSrc: ['\'self\''],
},
})

const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('Content-Security-Policy'), `default-src 'self'`)
assert.equal(ctx.response.getHeader('Content-Security-Policy'), 'default-src \'self\'')
})

test('transform @nonce keyword on scriptSrc', (assert) => {
const middlewareFn = csp({
enabled: true,
directives: {
defaultSrc: [`'self'`],
defaultSrc: ['\'self\''],
scriptSrc: ['@nonce'],
},
})

const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
ctx.response.nonce = '1234'

middlewareFn(ctx)
assert.equal(
ctx.response.getHeader('Content-Security-Policy'),
`default-src 'self'; script-src 'nonce-1234'`,
'default-src \'self\'; script-src \'nonce-1234\'',
)
})

test('transform @nonce keyword on styleSrc', (assert) => {
const middlewareFn = csp({
enabled: true,
directives: {
defaultSrc: [`'self'`],
defaultSrc: ['\'self\''],
styleSrc: ['@nonce'],
},
})

const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
ctx.response.nonce = '1234'

middlewareFn(ctx)
assert.equal(
ctx.response.getHeader('Content-Security-Policy'),
`default-src 'self'; style-src 'nonce-1234'`,
'default-src \'self\'; style-src \'nonce-1234\'',
)
})
})
8 changes: 4 additions & 4 deletions test/dns-prefetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { dnsPrefetch } from '../src/dnsPrefetch'
import { getCtx } from '../test-helpers'

test.group('Dns Prefetch', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = dnsPrefetch({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-DNS-Prefetch-Control'))
})

test('set X-DNS-Prefetch-Control header', (assert) => {
const middlewareFn = dnsPrefetch({ enabled: true, allow: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-DNS-Prefetch-Control'), 'on')
})

test('set X-DNS-Prefetch-Control header to off', (assert) => {
const middlewareFn = dnsPrefetch({ enabled: true, allow: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-DNS-Prefetch-Control'), 'off')
Expand Down
8 changes: 4 additions & 4 deletions test/frame-guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { frameGuard } from '../src/frameGuard'
import { getCtx } from '../test-helpers'

test.group('FrameGuard', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = frameGuard({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-Frame-Options'))
Expand All @@ -27,15 +27,15 @@ test.group('FrameGuard', () => {

test('set X-Frame-Options header', (assert) => {
const middlewareFn = frameGuard({ enabled: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-Frame-Options'), 'SAMEORIGIN')
})

test('set X-Frame-Options header for allow from action', (assert) => {
const middlewareFn = frameGuard({ enabled: true, action: 'ALLOW-FROM', domain: 'foo.com' })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-Frame-Options'), 'ALLOW-FROM foo.com')
Expand Down
12 changes: 6 additions & 6 deletions test/hsts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { hsts } from '../src/hsts'
import { getCtx } from '../test-helpers'

test.group('Hsts', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = hsts({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('Strict-Transport-Security'))
})

test('set Strict-Transport-Security header with defined maxAge', (assert) => {
const middlewareFn = hsts({ enabled: true, maxAge: 100 })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('Strict-Transport-Security'), 'max-age=100')
})

test('handle string based max-age', (assert) => {
const middlewareFn = hsts({ enabled: true, maxAge: '1s' })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('Strict-Transport-Security'), 'max-age=1000')
})

test('entertain includeSubDomains flag', (assert) => {
const middlewareFn = hsts({ enabled: true, maxAge: '1s', includeSubDomains: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('Strict-Transport-Security'), 'max-age=1000; includeSubDomains')
})

test('entertain preload flag', (assert) => {
const middlewareFn = hsts({ enabled: true, maxAge: '1s', includeSubDomains: true, preload: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('Strict-Transport-Security'), 'max-age=1000; includeSubDomains; preload')
Expand Down
6 changes: 3 additions & 3 deletions test/no-open.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { noOpen } from '../src/noOpen'
import { getCtx } from '../test-helpers'

test.group('No Open', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = noOpen({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-Download-Options'))
})

test('set X-Download-Options header', (assert) => {
const middlewareFn = noOpen({ enabled: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-Download-Options'), 'noopen')
Expand Down
6 changes: 3 additions & 3 deletions test/no-sniff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { noSniff } from '../src/noSniff'
import { getCtx } from '../test-helpers'

test.group('No Sniff', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = noSniff({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-Content-Type-Options'))
})

test('set X-Content-Type-Options header', (assert) => {
const middlewareFn = noSniff({ enabled: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-Content-Type-Options'), 'nosniff')
Expand Down
10 changes: 5 additions & 5 deletions test/xss-protections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@
*/

import test from 'japa'
import { HttpContext } from '@adonisjs/http-server/build/standalone'
import { xssProtection } from '../src/xssProtection'
import { getCtx } from '../test-helpers'

test.group('Xss Protection', () => {
test('return noop function when enabled is false', (assert) => {
const middlewareFn = xssProtection({ enabled: false })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.isUndefined(ctx.response.getHeader('X-XSS-Protection'))
})

test('set X-XSS-Protection header', (assert) => {
const middlewareFn = xssProtection({ enabled: true })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1; mode=block')
})

test('disable block mode', (assert) => {
const middlewareFn = xssProtection({ enabled: true, mode: null })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1')
})

test('set report uri', (assert) => {
const middlewareFn = xssProtection({ enabled: true, reportUri: '/' })
const ctx = HttpContext.create('/', {}, {}, {}, {})
const ctx = getCtx()
middlewareFn(ctx)

assert.equal(ctx.response.getHeader('X-XSS-Protection'), '1; mode=block; report=/')
Expand Down
6 changes: 0 additions & 6 deletions tslint.json

This file was deleted.

0 comments on commit 46222a9

Please sign in to comment.