Files
speckle-server/packages/server/test/plugins/graphql.ts
T
Kristaps Fabians Geikins ede566eed9 feat(server): serverInvites refactor + workspace invites CRUD & GQL API (#2530)
* prep for new resources algo

* typescriptifying stuff

* minor types fix

* migrate to resources col

* repo & creation updated, WIP processing/retrieval

* WIP invite processing

* finished finalization refactor

* project invite management

* transformed all invites services

* fixed up projects & core serverinvites resolvers

* test fixes

* WIP workspace create GQL & test

* basic invite creation test works

* a buncha working tests

* more tests

* cancelation tests

* minor invite use refactor

* invite retrieval tasks

* invite use() works as expected

* filtering out broken invites

* enabled invite retrieval by token irregardless of who is it for

* minor adjustments

* tests fix

* test config improvements

* test env adjustment

* extra test case

* making resource access limits harder to ignore

* linter fixes

* eventBus type cleanup

* better generic names

* refactored serverinvites resource migration

* fix(server): better error message in project invite edge case
2024-07-29 14:37:54 +03:00

82 lines
2.4 KiB
TypeScript

import { Optional } from '@/modules/shared/helpers/typeHelper'
import { GraphQLResponse } from 'apollo-server-core'
import { AssertionError } from 'chai'
type ChaiPluginThis<O = Record<string, unknown>> = {
__flags: {
message: Optional<string>
negate: Optional<boolean>
object: O
}
}
/**
* Adds various useful assertions for GraphQL API integration tests
*/
const graphqlChaiPlugin: Chai.ChaiPlugin = (_chai, utils) => {
const { Assertion } = _chai
utils.addMethod(
Assertion.prototype,
'haveGraphQLErrors',
function (this: ChaiPluginThis<GraphQLResponse>, matchMessage?: string) {
const { negate, object } = this.__flags
const { errors } = object
const shouldNotHaveErrors = negate
const shouldHaveErrors = !negate
const errorsArr = errors || []
try {
if (shouldNotHaveErrors) {
new Assertion(errorsArr).to.have.lengthOf(0)
} else {
new Assertion(errorsArr).to.have.lengthOf.greaterThanOrEqual(1)
}
if (matchMessage) {
if (shouldNotHaveErrors) {
new Assertion(
errorsArr.map((e) => e.message.toLowerCase()).join('\n')
).to.not.contain(matchMessage.toLowerCase())
} else {
new Assertion(
errorsArr.map((e) => e.message.toLowerCase()).join('\n')
).to.contain(matchMessage.toLowerCase())
}
}
} catch (e) {
if (!(e instanceof AssertionError)) {
throw e
}
const getPrettyErrors = () => `\nErrors: ${JSON.stringify(errorsArr, null, 2)}`
let msg = ''
if (shouldHaveErrors) {
if (matchMessage) {
msg = `Expected GraphQL response to have errors containing "${matchMessage}", but`
msg += errorsArr.length
? ' only found others' + getPrettyErrors()
: ' found none'
} else {
msg = 'Expected GraphQL response to have errors, but found none'
}
} else {
if (matchMessage) {
msg = `Expected GraphQL response to have no errors containing "${matchMessage}", but found some`
} else {
msg = 'Expected GraphQL response to have no errors, but found some'
}
msg += getPrettyErrors()
}
throw new AssertionError(msg)
}
}
)
}
export default graphqlChaiPlugin