import ava, { TestInterface } from 'ava'; import { UserInputError, ForbiddenError, AuthenticationError, } from 'apollo-server-micro'; import { Factory } from '../../../database/factory'; import * as testUtils from '../../../test/utils'; import type { DBTestContext } from '../../../test/utils'; import { User } 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'; // // Setup // const test = ava as TestInterface; 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 userQuery = ` query User($id: ID) { user(id: $id) { id recipes { id } } } `; // // Tests // test.only('db gets set up correctly', async t => { console.log('test'); t.pass(); }) // ** FetchRecipe test.serial( 'Should return the logged in user when no ID is provided (and user is logged in)', 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: userQuery, contextValue: context, variableValues: {}, }); t.falsy(response.errors); // return user data t.is(response.data.user.id, user.id); // returns user recipes t.is(response.data.user.recipes[0].id, recipe.id); } ); 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: userQuery, contextValue: context, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof AuthenticationError); } ); test.serial( 'Should return an error when the logged in user is requesting a different user', 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: userQuery, contextValue: context, variableValues: { id: otherUser.id, }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof ForbiddenError); } ); test.todo('Should return user with given id when request is from an admin'); 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: userQuery, contextValue: context, variableValues: { id: 'malfored', }, }); t.truthy(response.errors.length > 0); t.true(response.errors[0].originalError instanceof UserInputError); });