This repository was archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Initial test structure #7
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
249 changes: 249 additions & 0 deletions
249
packages/apollo-voyager-keycloak/src/schemaDirectives/hasRole.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import test from 'ava' | ||
|
||
import { GraphQLSchema } from 'graphql' | ||
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) | ||
}) | ||
}) |
15 changes: 14 additions & 1 deletion
15
packages/apollo-voyager-keycloak/src/schemaDirectives/hasRole.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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".