Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up tests #2

Merged
merged 4 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/two-donuts-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"zemble-plugin-auth": patch
"zemble-plugin-bull": patch
"zemble-plugin-cms-users": patch
"zemble-plugin-cms": patch
"@zemble/graphql": patch
---

Update readme
10 changes: 9 additions & 1 deletion packages/auth/graphql/Query/advancedWithOr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ describe('advancedWithOr', () => {
it('Should fail authentication', async () => {
const app = await plugin.testApp()

const response = await app.gqlRequest(AdvancedWithOrQuery, { organisationId: '123' })
const response = await app.gqlRequest(
AdvancedWithOrQuery,
{ organisationId: '123' },
{ silenceErrors: true },
)

expect(response.errors?.[0].message).toEqual(`Accessing 'Query.advancedWithOr' requires authentication.`)
})

Expand All @@ -27,6 +32,7 @@ describe('advancedWithOr', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})

expect(response.errors?.[0].message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
Expand All @@ -41,6 +47,7 @@ describe('advancedWithOr', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})

expect(response.errors?.[0].message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
Expand Down Expand Up @@ -111,6 +118,7 @@ describe('advancedWithOr', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})

expect(response.errors?.[0].message).toEqual(`Accessing 'Query.advancedWithOr' requires token including arrays matching one of [{"includes":{"roles":{"role":"admin","organisationId":"123"}}},{"includes":{"roles":{"role":"superadmin","organisationId":"123"}}}].`)
Expand Down
8 changes: 7 additions & 1 deletion packages/auth/graphql/Query/includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('Includes', () => {
it('Should fail authentication', async () => {
const app = await plugin.testApp()

const response = await app.gqlRequest(IncludesQuery, { id: '1' })
const response = await app.gqlRequest(
IncludesQuery,
{ id: '1' },
{ silenceErrors: true },
)
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.includes' requires authentication.`)
})

Expand All @@ -27,6 +31,7 @@ describe('Includes', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.includes' requires token including arrays matching {"roles":{"role":"admin","organisationId":"1"}}.`)
})
Expand All @@ -40,6 +45,7 @@ describe('Includes', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.includes' requires token including arrays matching {"roles":{"role":"admin","organisationId":"1"}}.`)
})
Expand Down
6 changes: 1 addition & 5 deletions packages/auth/graphql/Query/privateShit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ const PrivateShitQuery = graphql(`

describe('privateShit', () => {
it('Should fail authentication', async () => {
console.log('test')
const app = await plugin.testApp()
console.log('test2')

const response = await app.gqlRequest(PrivateShitQuery, {})
const response = await app.gqlRequest(PrivateShitQuery, {}, { silenceErrors: true })
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.privateShit' requires authentication.`)
})

it('Should succeed authentication', async () => {
console.log('test')
const app = await plugin.testApp()
console.log('test2')

const token = await signJwt({ data: { } })

Expand Down
7 changes: 6 additions & 1 deletion packages/auth/graphql/Query/privateShitWithRole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('PrivateShitWithRole', () => {
it('Should fail authentication', async () => {
const app = await plugin.testApp()

const response = await app.gqlRequest(PrivateShitWithRoleQuery, {})
const response = await app.gqlRequest(
PrivateShitWithRoleQuery,
{},
{ silenceErrors: true },
)
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.privateShitWithRole' requires authentication.`)
})

Expand All @@ -27,6 +31,7 @@ describe('PrivateShitWithRole', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.privateShitWithRole' requires token matching {"role":"admin"}.`)
})
Expand Down
8 changes: 7 additions & 1 deletion packages/auth/graphql/Query/variableReference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('variableReference', () => {
it('Should fail authentication', async () => {
const app = await plugin.testApp()

const response = await app.gqlRequest(VariableReferenceQuery, { id: '1' })
const response = await app.gqlRequest(
VariableReferenceQuery,
{ id: '1' },
{ silenceErrors: true },
)
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.variableReference' requires authentication.`)
})

Expand All @@ -27,6 +31,7 @@ describe('variableReference', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.variableReference' requires token matching {"role":"admin","organisationId":"1"}.`)
})
Expand All @@ -40,6 +45,7 @@ describe('variableReference', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})
expect(response.errors?.[0].message).toEqual(`Accessing 'Query.variableReference' requires token matching {"role":"admin","organisationId":"1"}.`)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ describe('Mutation.updatePermissions', () => {
it('Should fail without permission', async () => {
const app = await plugin.testApp()

const { errors } = await app.gqlRequest(UpdatePermissionsMutation, { userId: 'abc', permissions: [{ type: PermissionType.MODIFY_ENTITY, scope: '*' }] })
const { errors } = await app.gqlRequest(
UpdatePermissionsMutation,
{ userId: 'abc', permissions: [{ type: PermissionType.MODIFY_ENTITY, scope: '*' }] },
{ silenceErrors: true },
)

expect(errors?.[0].message).toEqual(`Accessing 'Mutation.updatePermissions' requires authentication.`)
})
Expand Down Expand Up @@ -84,6 +88,7 @@ describe('Mutation.updatePermissions', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})

expect(errors?.[0].message).toEqual('User not found')
Expand All @@ -108,6 +113,7 @@ describe('Mutation.updatePermissions', () => {
headers: {
Authorization: `Bearer ${token}`,
},
silenceErrors: true,
})

expect(errors?.[0].message).toEqual('You cannot remove your own user-admin permission')
Expand Down
2 changes: 1 addition & 1 deletion packages/cms-users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "plugin.ts",
"private": true,
"scripts": {
"test": "MONGOMS_DEBUG=1 bun test",
"test": "bun test",
"dev": "MONGO_URL=mongodb://localhost:27017/test-db REDIS_URL=redis://localhost:6379 PLUGIN_DEV=true bun --hot plugin.ts",
"lint": "eslint .",
"lint-quiet": "eslint . --quiet",
Expand Down
8 changes: 4 additions & 4 deletions packages/graphql-yoga/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function gqlRequestUntyped<TRes, TVars>(
app: Zemble.Server,
query: string,
variables: TVars,
options?: {readonly headers?: Record<string, string>},
options?: {readonly headers?: Record<string, string>, readonly silenceErrors?: boolean},
) {
const res = await app.fetch(new Request('http://localhost/graphql', {
method: 'POST',
Expand All @@ -59,7 +59,7 @@ async function gqlRequestUntyped<TRes, TVars>(
readonly errors: readonly GraphQLFormattedError[]
}

if (resJson.errors) {
if (resJson.errors && !options?.silenceErrors) {
console.error(resJson.errors)
}

Expand All @@ -70,7 +70,7 @@ async function gqlRequest<TQuery, TVars>(
app: Zemble.Server,
query: TypedDocumentNode<TQuery, TVars>,
variables: TVars,
options: {readonly headers?: Record<string, string>} = {},
options: {readonly headers?: Record<string, string>, readonly silenceErrors?: boolean} = {},
) {
const req = new Request('http://localhost/graphql', {
method: 'POST',
Expand All @@ -91,7 +91,7 @@ async function gqlRequest<TQuery, TVars>(
readonly errors: readonly GraphQLFormattedError[]
}

if (resJson.errors) {
if (resJson.errors && !options?.silenceErrors) {
console.error(resJson.errors)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-yoga/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare global {
readonly gqlRequest: <TQuery, TVars>(
query: TypedDocumentNode<TQuery, TVars>,
vars?: TVars,
opts?: {readonly headers?: Record<string, string>}
opts?: {readonly headers?: Record<string, string>, readonly silenceErrors?: boolean}
) => Promise<{
readonly data?: ResultOf<TypedDocumentNode<TQuery, TVars>>,
readonly errors?: readonly GraphQLFormattedError[]
Expand All @@ -27,7 +27,7 @@ declare global {
readonly gqlRequestUntyped: <TRes, TVars = unknown>(
query: string,
vars?: TVars,
opts?: {readonly headers?: Record<string, string>}
opts?: {readonly headers?: Record<string, string>, readonly silenceErrors?: boolean}
) => Promise<{
readonly data?: TRes,
readonly errors?: readonly GraphQLFormattedError[]
Expand Down