|
| 1 | +import { ApolloServerTestClient } from "apollo-server-testing"; |
| 2 | +import gql from "graphql-tag"; |
| 3 | +import jwt from "jsonwebtoken"; |
| 4 | + |
| 5 | +import { createAuthDirectives } from "../src"; |
| 6 | +import { createClient } from "./utils/createClient"; |
| 7 | + |
| 8 | +describe("Testing isAuthenticated directive without handler overriding", () => { |
| 9 | + let client: ApolloServerTestClient; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + process.env.JWT_SECRET = "jest"; |
| 13 | + const token = jwt.sign({}, process.env.JWT_SECRET); |
| 14 | + client = createClient( |
| 15 | + { |
| 16 | + typeDefs: gql` |
| 17 | + input TestInput { |
| 18 | + input: String! |
| 19 | + protectedInput: String @isAuthenticated |
| 20 | + } |
| 21 | + type Query { |
| 22 | + protectedQuery(data: TestInput!): String! |
| 23 | + } |
| 24 | + type Mutation { |
| 25 | + protectedMutation(data: TestInput!): String! @isAuthenticated |
| 26 | + } |
| 27 | + `, |
| 28 | + }, |
| 29 | + { |
| 30 | + req: { headers: { authorization: `Bearer ${token}` } }, |
| 31 | + }, |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + afterAll(() => { |
| 36 | + delete process.env.JWT_SECRET; |
| 37 | + }); |
| 38 | + |
| 39 | + it("should allow accessing a query's input authenticated", async () => { |
| 40 | + const res = await client.query({ |
| 41 | + query: gql` |
| 42 | + query protectedQuery($data: TestInput!) { |
| 43 | + protectedQuery(data: $data) |
| 44 | + } |
| 45 | + `, |
| 46 | + variables: { |
| 47 | + data: { |
| 48 | + input: "foo", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(res.data).toMatchSnapshot(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should allow accessing a mutation authenticated", async () => { |
| 57 | + const res = await client.mutate({ |
| 58 | + mutation: gql` |
| 59 | + mutation protectedMutation($data: TestInput!) { |
| 60 | + protectedMutation(data: $data) |
| 61 | + } |
| 62 | + `, |
| 63 | + variables: { |
| 64 | + data: { |
| 65 | + input: "foo", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + expect(res.data).toMatchSnapshot(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should allow accessing a mutation's input authenticated", async () => { |
| 74 | + const res = await client.mutate({ |
| 75 | + mutation: gql` |
| 76 | + mutation protectedMutation($data: TestInput!) { |
| 77 | + protectedMutation(data: $data) |
| 78 | + } |
| 79 | + `, |
| 80 | + variables: { |
| 81 | + data: { |
| 82 | + input: "foo", |
| 83 | + protectedInput: "bar", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(res.data).toMatchSnapshot(); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe("Testing isAuthenticated directive with handler overriding", () => { |
| 93 | + let client: ApolloServerTestClient; |
| 94 | + |
| 95 | + beforeAll(() => { |
| 96 | + const authDirectives = createAuthDirectives({ |
| 97 | + isAuthenticatedHandler: ctx => { |
| 98 | + throw new Error(`Authorization header: ${ctx.req.headers.authorization}`); |
| 99 | + }, |
| 100 | + }); |
| 101 | + client = createClient( |
| 102 | + { |
| 103 | + typeDefs: gql` |
| 104 | + type Query { |
| 105 | + protectedQuery: String! @isAuthenticated |
| 106 | + } |
| 107 | + `, |
| 108 | + resolvers: { |
| 109 | + Query: { |
| 110 | + protectedQuery: () => "Protected Query", |
| 111 | + }, |
| 112 | + }, |
| 113 | + schemaDirectives: { |
| 114 | + ...authDirectives, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + req: { headers: { authorization: `Bearer TOKEN` } }, |
| 119 | + }, |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it("should override the isAuthenticated default handler correctly and throw an error", async () => { |
| 124 | + const res = await client.query({ |
| 125 | + query: gql` |
| 126 | + { |
| 127 | + protectedQuery |
| 128 | + } |
| 129 | + `, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(res.errors).toMatchSnapshot(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments