-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #428 from AikidoSec/graphql-tools
Add support for @graphql-tools/executor
- Loading branch information
Showing
4 changed files
with
198 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,93 @@ | ||
import * as t from "tap"; | ||
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting"; | ||
import { getContext, runWithContext } from "../agent/Context"; | ||
import { GraphQL } from "./GraphQL"; | ||
import { Token } from "../agent/api/Token"; | ||
import { createTestAgent } from "../helpers/createTestAgent"; | ||
import type { ExecutionArgs } from "graphql"; | ||
|
||
t.test("it works", async () => { | ||
const agent = createTestAgent({ | ||
api: new ReportingAPIForTesting({ | ||
success: true, | ||
endpoints: [ | ||
{ | ||
method: "POST", | ||
route: "/graphql", | ||
forceProtectionOff: false, | ||
rateLimiting: { | ||
enabled: true, | ||
maxRequests: 3, | ||
windowSizeInMS: 60 * 1000, | ||
}, | ||
graphql: { | ||
name: "getFile", | ||
type: "query", | ||
}, | ||
}, | ||
], | ||
allowedIPAddresses: [], | ||
configUpdatedAt: 0, | ||
heartbeatIntervalInMS: 10 * 60 * 1000, | ||
blockedUserIds: [], | ||
}), | ||
token: new Token("123"), | ||
}); | ||
|
||
agent.start([new GraphQL()]); | ||
|
||
const { parse, buildSchema } = require("graphql"); | ||
const { execute } = require("@graphql-tools/executor"); | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
getFile(path: String): String | ||
} | ||
`); | ||
|
||
const root = { | ||
getFile: ({ path }: { path: string }) => { | ||
return "file content"; | ||
}, | ||
}; | ||
|
||
const query = async (path: string) => { | ||
const executionArgs: ExecutionArgs = { | ||
schema, | ||
document: parse(`{ getFile(path: "${path}") }`), | ||
rootValue: root, | ||
}; | ||
|
||
return execute(executionArgs); | ||
}; | ||
|
||
const context = { | ||
remoteAddress: "::1", | ||
method: "POST", | ||
url: "http://localhost:4000/graphql", | ||
query: {}, | ||
headers: {}, | ||
body: { query: '{ getFile(path: "/etc/bashrc") }' }, | ||
cookies: {}, | ||
routeParams: {}, | ||
source: "express", | ||
route: "/graphql", | ||
}; | ||
|
||
await query("/etc/bashrc"); | ||
|
||
await runWithContext(context, async () => { | ||
await query("/etc/bashrc"); | ||
t.same(getContext()?.graphql, ["/etc/bashrc"]); | ||
}); | ||
|
||
// Rate limiting works | ||
await runWithContext(context, async () => { | ||
const success = await query("/etc/bashrc"); | ||
t.same(success.data.getFile, "file content"); | ||
await query("/etc/bashrc"); | ||
await query("/etc/bashrc"); | ||
const result = await query("/etc/bashrc"); | ||
t.same(result.errors[0].message, "You are rate limited by Zen."); | ||
}); | ||
}); |
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