Skip to content

Commit

Permalink
fix: handle starting slash on opa path (#6)
Browse files Browse the repository at this point in the history
* fix: handle starting slash on opa path

* feat: reverse slash adding to removing

* chore: bump version

* chore: bump version
  • Loading branch information
DavideArena authored Jul 23, 2024
1 parent 8c5bac6 commit 45addb3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@treedom/mercurius-auth-opa",
"version": "2.0.0",
"version": "2.0.1",
"main": "lib/index.js",
"scripts": {
"test": "borp",
Expand Down
6 changes: 5 additions & 1 deletion src/OpenPolicyAgentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export class OpenPolicyAgentClient<TCache extends Cache> {
resource: string,
input?: unknown
): Promise<TResponse> {
const resourcePath = resource.replace(/\./gi, '/')
let resourcePath = resource.replace(/\./gi, '/')

if (resourcePath.startsWith('/')) {
resourcePath = resourcePath.substring(1, resourcePath.length)
}

const cacheKey = getCacheKey(resourcePath, input)
const cached = this.cache?.get(cacheKey) as TResponse | undefined
Expand Down
61 changes: 61 additions & 0 deletions test/opaAuthPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,64 @@ query { ping(message: "pong") }
],
})
})

test('authenticated query should succeed when opa path starts with slash', async () => {
const app = fastify({ logger: testLogger })

const schema = `#graphql
${fs.readFileSync(path.join(__dirname, '../../../src/opaAuthDirective.gql'), 'utf-8')}
type Query {
ping(message: String!): String! @opa(path: "/query/ping", options: { bar: "foo", baz: 123, qux: true, bing: { bong: "doo" }, fl: 1.34, n: null, arr: [{a: "b"}, {c: "d"}] })
}`

app.register(mercurius, {
schema,
resolvers: {
Query: {
ping: (source, args) => args.message,
},
},
})

app.register(opaAuthPlugin, {
opaOptions: {
url: 'http://opa.test:3000',
},
})

const opaPolicyMock = sinon
.stub<never, ReturnType<MockInterceptor.MockReplyOptionsCallback>>()
.returns({
statusCode: 200,
data: { result: true },
responseOptions: { headers: { 'Content-Type': 'application/json' } },
})

mockAgent
.get('http://opa.test:3000')
.intercept({
path: '/v1/data/query/ping',
method: 'POST',
})
.reply(opaPolicyMock)

const testClient = createMercuriusTestClient(app)
const response = await testClient.query(`#graphql
query { ping(message: "pong") }
`)

deepStrictEqual(response, { data: { ping: 'pong' } })

const body = JSON.parse(opaPolicyMock.firstCall?.firstArg?.body)

deepStrictEqual(body?.input?.args, { message: 'pong' })
deepStrictEqual(body?.input?.options, {
bar: 'foo',
baz: 123,
qux: true,
bing: { bong: 'doo' },
fl: 1.34,
n: null,
arr: [{ a: 'b' }, { c: 'd' }],
})
})

0 comments on commit 45addb3

Please sign in to comment.