-
Notifications
You must be signed in to change notification settings - Fork 23
Initial test structure #7
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,8 @@ reports/ | |
junit.xml | ||
|
||
# Node modules | ||
node_modules/ | ||
node_modules/ | ||
|
||
# Intellij files | ||
.idea | ||
*.iml |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,23 @@ | |
"scripts": { | ||
"compile": "tsc --build tsconfig.json", | ||
"watch": "tsc --build tsconfig.json --watch", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"compile:clean": "tsc --build tsconfig.json --clean" | ||
"compile:clean": "tsc --build tsconfig.json --clean", | ||
"test": "ava '*.test.ts' '**/*.test.ts'" | ||
}, | ||
"devDependencies": { | ||
"ava": "1.0.0-rc.2", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.1.6" | ||
}, | ||
"author": "AeroGear Team<[email protected]>", | ||
"license": "Apache-2.0" | ||
"license": "Apache-2.0", | ||
"ava": { | ||
"compileEnhancements": false, | ||
"extensions": [ | ||
"ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import test from 'ava' | ||
|
||
test('DELETE ME DUMMY', t => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 |
||
t.is(true, true) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import test from 'ava' | ||
|
||
import {GraphQLSchema} from 'graphql' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really minor thing but just check spacing around imports like this. I'm wondering is this something your IDE does? I wonder can we lint for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my IDE does format like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @darahayes I tried tslint's --fix now and it doesn't fix the spaces for that case. UPDATE: I found this open issue on tslint: palantir/tslint#1044 Anyway, I will fix this manually. |
||
import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor' | ||
import { HasRoleDirective } from './hasRole' | ||
|
||
import {KeycloakAuthContextProvider} from '../AuthContextProvider' | ||
|
||
const createHasRoleDirective = (directiveArgs:any) => { | ||
return new HasRoleDirective({ | ||
name: 'testHasRoleDirective', | ||
args: directiveArgs, | ||
visitedType: ({} as VisitableSchemaType), | ||
schema: ({} as GraphQLSchema), | ||
context: [] | ||
}) | ||
} | ||
|
||
test('context.auth.hasRole() is called', async (t) => { | ||
t.plan(3) | ||
const directiveArgs = { | ||
role: 'admin' | ||
} | ||
|
||
const directive = createHasRoleDirective(directiveArgs) | ||
|
||
const field = { | ||
resolve: (root: any, args: any, context: any, info: any) => { | ||
t.pass() | ||
}, | ||
name: 'testField' | ||
} | ||
|
||
directive.visitFieldDefinition(field) | ||
|
||
const root = {} | ||
const args = {} | ||
const req = { | ||
kauth: { | ||
grant: { | ||
access_token: { | ||
hasRole: (role: string) => { | ||
t.pass() | ||
t.deepEqual(role, directiveArgs.role) | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const context = { | ||
request: req, | ||
auth: new KeycloakAuthContextProvider(req) | ||
} | ||
|
||
const info = { | ||
parentType: { | ||
name: 'testParent' | ||
} | ||
} | ||
|
||
await field.resolve(root, args, context, info) | ||
}) | ||
|
||
test('visitFieldDefinition accepts an array of roles', async (t) => { | ||
t.plan(4) | ||
const directiveArgs = { | ||
role: ['foo', 'bar', 'baz'] | ||
} | ||
|
||
const directive = createHasRoleDirective(directiveArgs) | ||
|
||
const field = { | ||
resolve: (root: any, args: any, context: any, info: any) => { | ||
t.pass() | ||
}, | ||
name: 'testField' | ||
} | ||
|
||
directive.visitFieldDefinition(field) | ||
|
||
const root = {} | ||
const args = {} | ||
const req = { | ||
kauth: { | ||
grant: { | ||
access_token: { | ||
hasRole: (role: string) => { | ||
t.log(`checking has role ${role}`) | ||
t.pass() | ||
return (role === 'baz') // this makes sure it doesn't return true instantly | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const context = { | ||
request: req, | ||
auth: new KeycloakAuthContextProvider(req) | ||
} | ||
|
||
const info = { | ||
parentType: { | ||
name: 'testParent' | ||
} | ||
} | ||
|
||
await field.resolve(root, args, context, info) | ||
}) | ||
|
||
test('if there is no authentication, then an error is returned and the original resolver will not execute', async (t) => { | ||
const directiveArgs = { | ||
role: 'admin' | ||
} | ||
|
||
const directive = createHasRoleDirective(directiveArgs) | ||
|
||
const field = { | ||
resolve: (root: any, args: any, context: any, info: any) => { | ||
return new Promise((resolve, reject) => { | ||
t.fail('the original resolver should never be called when an auth error is thrown') | ||
return reject(new Error('the original resolver should never be called when an auth error is thrown')) | ||
}) | ||
}, | ||
name: 'testField' | ||
} | ||
|
||
directive.visitFieldDefinition(field) | ||
|
||
const root = {} | ||
const args = {} | ||
const req = {} | ||
const context = { | ||
request: req, | ||
auth: new KeycloakAuthContextProvider(req) | ||
} | ||
|
||
const info = { | ||
parentType: { | ||
name: 'testParent' | ||
} | ||
} | ||
|
||
await t.throwsAsync(async () => { | ||
await field.resolve(root, args, context, info) | ||
}, `Unable to find authentication. Authorization is required for field ${field.name} on parent ${info.parentType.name}. Must have one of the following roles: [${directiveArgs.role}]`) | ||
}) | ||
|
||
test('if token does not have the required role, then an error is returned and the original resolver will not execute', async (t) => { | ||
const directiveArgs = { | ||
role: 'admin' | ||
} | ||
|
||
const directive = createHasRoleDirective(directiveArgs) | ||
|
||
const field = { | ||
resolve: (root: any, args: any, context: any, info: any) => { | ||
return new Promise((resolve, reject) => { | ||
t.fail('the original resolver should never be called when an auth error is thrown') | ||
return reject(new Error('the original resolver should never be called when an auth error is thrown')) | ||
}) | ||
}, | ||
name: 'testField' | ||
} | ||
|
||
directive.visitFieldDefinition(field) | ||
|
||
const root = {} | ||
const args = {} | ||
const req = { | ||
kauth: { | ||
grant: { | ||
access_token: { | ||
hasRole: (role: string) => { | ||
t.deepEqual(role, directiveArgs.role) | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const context = { | ||
request: req, | ||
auth: new KeycloakAuthContextProvider(req) | ||
} | ||
|
||
const info = { | ||
parentType: { | ||
name: 'testParent' | ||
} | ||
} | ||
|
||
await t.throwsAsync(async () => { | ||
await field.resolve(root, args, context, info) | ||
}, `user is not authorized for field ${field.name} on parent ${info.parentType.name}. Must have one of the following roles: [${directiveArgs.role}]`) | ||
}) | ||
|
||
test('if hasRole arguments are invalid, visitSchemaDirective does not throw, but field.resolve will return a generic error to the user and original resolver will not be called', async (t) => { | ||
const directiveArgs = { | ||
role: 'admin', | ||
some: 'unknown arg' | ||
} | ||
|
||
const directive = createHasRoleDirective(directiveArgs) | ||
|
||
const field = { | ||
resolve: (root: any, args: any, context: any, info: any) => { | ||
return new Promise((resolve, reject) => { | ||
t.fail('the original resolver should never be called when an auth error is thrown') | ||
return reject(new Error('the original resolver should never be called when an auth error is thrown')) | ||
}) | ||
}, | ||
name: 'testField' | ||
} | ||
|
||
t.notThrows(() => { | ||
directive.visitFieldDefinition(field) | ||
}) | ||
|
||
const root = {} | ||
const args = {} | ||
const req = { | ||
id: '123', | ||
kauth: { | ||
grant: { | ||
access_token: { | ||
hasRole: (role: string) => { | ||
t.deepEqual(role, directiveArgs.role) | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const context = { | ||
request: req, | ||
auth: new KeycloakAuthContextProvider(req) | ||
} | ||
|
||
const info = { | ||
parentType: { | ||
name: 'testParent' | ||
} | ||
} | ||
|
||
await t.throwsAsync(async () => { | ||
await field.resolve(root, args, context, info) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering what's the
> /dev/null
thing for here? Is this going to suppress error output?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer yes:
Long answer
2 scripts:
lint
: Complains with "ERROR: bla bla" messages, doesn't fix, exits with non-zero in case of error, only used in non-test codeformat
: It runs on test and non-test code. Don't want to see "ERROR: bla bla" in the output because the tests might have too many of them, so that's why I put "> dev/null". I don't want it to return non-zero, so I put "--force".