Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix: new test for auth directive, move tests to test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Dara Hayes committed Jul 5, 2019
1 parent c96b112 commit 594e049
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
"@types/keycloak-connect": "4.5.1",
"@types/node": "10.14.10",
"@types/pino": "5.20.0",
"@types/sinon": "^7.0.13",
"ava": "2.1.0",
"coveralls": "3.0.4",
"express": "^4.17.1",
"graphql": "14.4.1",
"nyc": "14.1.1",
"sinon": "^7.3.2",
"ts-node": "8.3.0",
"tslint": "5.18.0",
"typescript": "3.5.2"
Expand Down
1 change: 0 additions & 1 deletion src/schemaDirectives/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class AuthDirective extends SchemaDirectiveVisitor {

constructor (config: {
name: string
args: { [name: string]: any }
visitedType: VisitableSchemaType
schema: GraphQLSchema
context: { [key: string]: any }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { KeycloakAuthContextProvider } from './AuthContextProvider'
import { KeycloakAuthContextProvider } from '../src/AuthContextProvider'

test('AuthContextProvider accessToken is the access_token in req.kauth', (t) => {

Expand Down
138 changes: 138 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import test from 'ava'
import sinon from 'sinon'

import { GraphQLSchema } from 'graphql'
import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'
import { AuthDirective } from '../src/schemaDirectives/auth'

import { KeycloakAuthContextProvider } from '../src/AuthContextProvider'
import { spawnSync } from 'child_process';

const createHasRoleDirective = () => {
return new AuthDirective({
name: 'testAuthDirective',
visitedType: ({} as VisitableSchemaType),
schema: ({} as GraphQLSchema),
context: []
})
}

test('happy path: context.auth.isAuthenticated() is called, then original resolver is called', async (t) => {
const directive = createHasRoleDirective()

const field = {
resolve: (root: any, args: any, context: any, info: any) => {
t.pass()
},
name: 'testField'
}

const resolverSpy = sinon.spy(field, 'resolve')

directive.visitFieldDefinition(field)

const root = {}
const args = {}
const req = {
kauth: {
grant: {
access_token: {
isExpired: () => {
return false
}
}
}
}
}
const context = {
request: req,
auth: new KeycloakAuthContextProvider({ req })
}

const isAuthenticatedSpy = sinon.spy(context.auth, 'isAuthenticated')

const info = {
parentType: {
name: 'testParent'
}
}

await field.resolve(root, args, context, info)

t.truthy(isAuthenticatedSpy.called)
t.truthy(resolverSpy.called)
})

test('resolver will throw if context.auth is not present', async (t) => {
const directive = createHasRoleDirective()

const field = {
resolve: (root: any, args: any, context: any, info: any) => {
t.fail()
},
name: 'testField'
}

directive.visitFieldDefinition(field)

const root = {}
const args = {}
const req = {
kauth: {
grant: {
access_token: {
isExpired: () => {
return false
}
}
}
}
}
const context = {
request: req
}

const info = {
parentType: {
name: 'testParent'
}
}

await t.throwsAsync(async () => {
await field.resolve(root, args, context, info)
}, 'User not Authenticated')
})

test('resolver will throw if context.auth present but context.auth.isAuthenticated returns false', async (t) => {
const directive = createHasRoleDirective()

const field = {
resolve: (root: any, args: any, context: any, info: any) => {
t.fail()
},
name: 'testField'
}

directive.visitFieldDefinition(field)

const root = {}
const args = {}
const req = {}

const context = {
request: req,
auth: {
isAuthenticated: () => false
}
}

const info = {
parentType: {
name: 'testParent'
}
}

await t.throwsAsync(async () => {
await field.resolve(root, args, context, info)
}, 'User not Authenticated')
})
4 changes: 2 additions & 2 deletions src/schemaDirectives/hasRole.test.ts → test/hasRole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import test from 'ava'

import { GraphQLSchema } from 'graphql'
import { VisitableSchemaType } from 'graphql-tools/dist/schemaVisitor'
import { HasRoleDirective } from './hasRole'
import { HasRoleDirective } from '../src/schemaDirectives/hasRole'

import { KeycloakAuthContextProvider } from '../AuthContextProvider'
import { KeycloakAuthContextProvider } from '../src/AuthContextProvider'

const createHasRoleDirective = (directiveArgs: any) => {
return new HasRoleDirective({
Expand Down

0 comments on commit 594e049

Please sign in to comment.