This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: new test for auth directive, move tests to test folder
- Loading branch information
Dara Hayes
committed
Jul 5, 2019
1 parent
c96b112
commit 594e049
Showing
5 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
src/AuthContextProvider.test.ts → test/AuthContextProvider.test.ts
This file contains 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 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,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') | ||
}) |
This file contains 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