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 '../../../app/user'; import { Recipe } from '../../recipe'; import { createContext } from '../../../test/utils/create-mock-context'; import { gqlCall } from '../../../test/utils/gqlCall'; import { RecipeProps } from '../recipe-model'; 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 recipeQuery = ` query Recipe($id: ID) { recipe(id: $id) { id title ingredientList { nodes { ...on IngredientItem { node { name } } ...on IngredientGroup { heading nodes { node { name } } } } } owner { id } } } `; // // Tests // test.only('Should return a recipe when the user is the owner', 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: recipeQuery, contextValue: context, variableValues: { id: recipe.id, }, }); t.falsy(response.errors); // correctly returns recipe data t.is(response.data.recipe.id, recipe.id); // correctly returns owner field t.is(response.data.recipe.owner.id, user.id); }); test.serial( 'Should return an error when the user is not logged in', async t => { const recipe = await Factory.create('recipe'); const context = createContext({ state: { user: null } }); const response = await gqlCall({ source: recipeQuery, contextValue: context, variableValues: { id: recipe.id, }, }); 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 recipe = await Factory.create('recipe'); const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: recipeQuery, contextValue: context, variableValues: { id: recipe.id, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } ); test.serial('Should return an error when the id is malformed', async t => { const recipe = await Factory.create('recipe'); const user = await Factory.create('user'); const context = createContext({ state: { user, abilities: defineUserAbility(user) }, }); const response = await gqlCall({ source: recipeQuery, contextValue: context, variableValues: { id: 'malfored', }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof UserInputError); });