import ava, { TestInterface } from 'ava'; import { Mongoose, Types } from 'mongoose'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { User, UserProps } from '../user-model'; import { Recipe } from '../../recipe'; import { createContext } from '../../../test/utils/create-mock-context'; import { gqlCall } from '../../../test/utils/gqlCall'; import { defineUserAbility } from '../../../server/authorization/user-authorization'; import { UserInputError, ForbiddenError, AuthenticationError, } from 'apollo-server-micro'; // // Setup // const test = ava as TestInterface<{ db: Mongoose }>; test.before(async t => { await testUtils.setupDB(t); }); test.afterEach.always(async t => { await testUtils.cleanupDB(t); }); test.after.always(async t => { await testUtils.tearDownDB(t); }); // // Constants // const deleteUserMutation = ` mutation Delete($id: ID!) { deleteUser(id: $id) { id firstName } } `; // // Tests // // ** delete recipe test.serial('Should delete the user from database', async t => { const user = await Factory.create('user'); let userCount = await User.countDocuments(); t.is(userCount, 1); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: user.id, }, }); const retrievedUser = await User.findById(user.id); t.is(retrievedUser, null); }); test.serial('Should return the deleted user', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: user.id, }, }); t.is(response.data.deleteUser.id, user.id); }); test.serial('Should thow error when no user is not logged in', async t => { const context = createContext({ state: { user: null } }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: '', }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof AuthenticationError); }); test.serial('Should thow error when user does not own the Recipe', async t => { const user = await Factory.create('user'); const otherUser = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: otherUser.id, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); }); test.serial( 'deleteRecipe: Should thow error when Recipe does not exist', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: Types.ObjectId().toHexString(), }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } ); test.serial( 'deleteRecipe: Should thow error when Recipe id is malformed', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: deleteUserMutation, contextValue: context, variableValues: { id: 'malformed', }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof UserInputError); } );