import ava, { TestInterface } from 'ava'; import { Mongoose, Types } from 'mongoose'; import { UserInputError, ForbiddenError, AuthenticationError, } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import { User } from '../../../app/user'; 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'; // // 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 updateRecipeMutation = ` mutation Update($recipe: UpdateRecipeInput!) { updateRecipe(input: $recipe) { id title } } `; // // Tests // // ** update recipe test.serial('Should save updates to DB', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe', { ownerId: user.id, title: 'Old Title', }); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: recipe.id, title: 'A New Title', }, }, }); const updatedRecipe = await Recipe.findById(recipe.id); t.is(updatedRecipe.title, 'A New Title'); }); test.serial('Should return the updated recipe', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe', { ownerId: user.id, title: 'Old Title', }); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: recipe.id, title: 'A New Title', }, }, }); t.is(response.data.updateRecipe.id, recipe.id); t.is(response.data.updateRecipe.title, 'A New Title'); }); test.serial( 'Should return an error when the user is not logged in', async t => { const context = createContext({ state: { user: null } }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: Types.ObjectId().toHexString(), title: 'A New Title', }, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof AuthenticationError); } ); test.serial( 'Should return an error when the user does not own the recipe', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: recipe.id, title: 'A New Title', }, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } ); test.serial('Should return an error when recipe doesnt exist', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const fakeId = Types.ObjectId.createFromTime(Date.now()).toHexString(); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: fakeId, title: 'A New Title', }, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); }); test.serial('Should return an error when params are invalid', async t => { const user = await Factory.create('user'); const recipe = await Factory.create('recipe', { ownerId: user.id, }); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: recipe.id, title: true, }, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof TypeError); }); test.serial('Should return an error when id is malformed', async t => { const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: updateRecipeMutation, contextValue: context, variableValues: { recipe: { id: 'malformed', title: 'A New Title', }, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof UserInputError); });