Files
speckle-server/packages/server/modules/core/tests/usersGraphql.spec.ts
T
Alessandro Magionami 12685af29a Alessandro/web 1388 mutation create new email (#2561)
* feat(useremails): userEmails graphql query

* feat(useremails): create user email mutation

* feat(useremails): set primary and delete user email mutations

* chore(useremails): fix update user email type

* chore(useremails): create typed tests for graphql and emails field resolver in user type

* chore(userEmails): group user email mutations in a specific mutation object

* linting fixes

* more lint fixes

* tests fix

* more test fixes

* chore(userEmails): cleanup

* chore(useremails): rely on knex trx for transaction handling

* chore(useremails): fix checkemail not present

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2024-08-05 18:39:52 +02:00

125 lines
3.8 KiB
TypeScript

import { Users } from '@/modules/core/dbSchema'
import { BasicTestUser, createTestUsers } from '@/test/authHelper'
import { getActiveUser, getOtherUser } from '@/test/graphql/users'
import { beforeEachContext, truncateTables } from '@/test/hooks'
import {
buildAuthenticatedApolloServer,
buildUnauthenticatedApolloServer
} from '@/test/serverHelper'
import { ApolloServer } from 'apollo-server-express'
import { expect } from 'chai'
import { createUser } from '@/modules/core/services/users'
import {
createRandomEmail,
createRandomPassword
} from '@/modules/core/helpers/testHelpers'
import { createUserEmailFactory } from '@/modules/core/repositories/userEmails'
import { db } from '@/db/knex'
import { before } from 'mocha'
import { testApolloServer } from '@/test/graphqlHelper'
import { GetActiveUserEmailsDocument } from '@/test/graphql/generated/graphql'
describe('Users (GraphQL)', () => {
const me: BasicTestUser = {
id: '',
email: '',
name: 'its a meeeee',
bio: 'ayyy',
company: 'ayyy inc'
}
const otherGuy: BasicTestUser = {
id: '',
email: '',
name: 'its an other guyyyyy',
bio: 'fffoooo',
company: 'fooooo inc'
}
before(async () => {
await truncateTables([Users.name])
await createTestUsers([me, otherGuy])
})
describe('when unauthenticated', () => {
let apollo: ApolloServer
before(async () => {
apollo = await buildUnauthenticatedApolloServer()
})
it('activeUser returns null', async () => {
const results = await getActiveUser(apollo)
expect(results).to.not.haveGraphQLErrors()
expect(results.data?.activeUser).to.be.null
})
it('otherUser throws an authorization error', async () => {
const results = await getOtherUser(apollo, { id: otherGuy.id })
expect(results.data?.otherUser).to.be.null
expect(results).to.haveGraphQLErrors(
'Your auth token does not have the required scope'
)
})
})
describe('when authenticated', () => {
let apollo: ApolloServer
before(async () => {
apollo = await buildAuthenticatedApolloServer(me.id)
})
it('activeUser returns authenticated user info', async () => {
const results = await getActiveUser(apollo)
expect(results).to.not.haveGraphQLErrors()
expect(results.data?.activeUser?.id).to.eq(me.id)
expect(results.data?.activeUser?.name).to.be.ok
expect(results.data?.activeUser?.bio).to.be.ok
expect(results.data?.activeUser?.company).to.be.ok
})
it('otherUser returns limited user info', async () => {
const results = await getOtherUser(apollo, { id: otherGuy.id })
expect(results).to.not.haveGraphQLErrors()
expect(results.data?.otherUser?.id).to.eq(otherGuy.id)
expect(results.data?.otherUser?.name).to.be.ok
expect(results.data?.otherUser?.bio).to.be.ok
expect(results.data?.otherUser?.company).to.be.ok
})
describe('emails field resolver', () => {
// TODO: this logic should not be here but we need to refactor this test
// We should avoid having the same user used in all tests to avoid tests depending on each other
before(async () => {
await beforeEachContext()
})
it('should return emails for user', async () => {
const userId = await createUser({
name: 'emails user',
email: createRandomEmail(),
password: createRandomPassword()
})
await createUserEmailFactory({ db })({
userEmail: {
email: createRandomEmail(),
userId,
primary: false
}
})
const apollo = await testApolloServer({ authUserId: userId })
const res = await apollo.execute(GetActiveUserEmailsDocument, {})
expect(res).to.not.haveGraphQLErrors()
expect(res?.data?.activeUser?.emails).to.have.length(2)
})
})
})
})