Skip to content

Commit

Permalink
Move tests to resolver layer
Browse files Browse the repository at this point in the history
  • Loading branch information
sbardian committed May 9, 2018
1 parent 7a05e8d commit e14cd0e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/graphql/resolvers/Mutations/login.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import bcrypt from 'bcrypt';
import { login } from './';
import mockUser from '../../../database/models/User';
import { config } from '../../../config';

jest.mock('../../../database/models/User');

describe('Login tests', () => {
it('Returns a token', async () => {
const passHash = await bcrypt.hash('bobspassword', 10);
mockUser.findOne.mockImplementationOnce(() => ({
id: 'someId',
email: '[email protected]',
username: 'bob',
password: passHash,
}));
expect(await login('bob', 'bobspassword', mockUser)).toEqual(
expect.objectContaining({ token: expect.any(String) }),
);
});
it('Returns an error, user not found', async () => {
mockUser.findOne.mockImplementationOnce(() => undefined);
expect(await login('bob', 'bobspassword', mockUser)).toEqual(
expect.any(Error),
);
});
it('Returns an error, bad password', async () => {
mockUser.findOne.mockImplementationOnce(() => ({
id: 'someId',
email: '[email protected]',
username: 'bob',
password: 'wrong',
}));
expect(await login('bob', 'bobspassword', mockUser)).toEqual(
expect.any(Error),
);
});
});
21 changes: 21 additions & 0 deletions src/graphql/resolvers/Mutations/signup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { signup } from './';
import mockUser from '../../../database/models/User';
import { config } from '../../../config';

jest.mock('../../../database/models/User');

describe('Sign up tests', () => {
it('Returns a token', async () => {
mockUser.findOne.mockImplementationOnce(() => false);
mockUser.create.mockImplementationOnce(() => ({ id: 'SomeUserId' }));
expect(
await signup('bob', '[email protected]', 'bobspassword', mockUser),
).toEqual(expect.objectContaining({ token: expect.any(String) }));
});
it('Returns an error', async () => {
mockUser.findOne.mockImplementationOnce(() => true);
expect(
await signup('bob', '[email protected]', 'bobspassword', mockUser),
).toEqual(expect.any(Error));
});
});
21 changes: 21 additions & 0 deletions src/graphql/resolvers/Queries/getUsers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getUsers } from './';
import mockUser from '../../../database/models/User';

jest.mock('../../../database/models/User');

describe('getUsers tests', () => {
it('Returns array of users', async () => {
mockUser.find.mockImplementationOnce(() => [
{ username: 'bob', email: '[email protected]', isAdmin: false, lists: [] },
{ username: 'bob2', email: '[email protected]', isAdmin: true, lists: [] },
]);
expect(await getUsers({ isAdmin: true }, mockUser)).toEqual(
expect.any(Array),
);
});
it('Returns error, user is not Admin', async () => {
expect(await getUsers({ isAdmin: false }, mockUser)).toEqual(
expect.any(Error),
);
});
});
29 changes: 29 additions & 0 deletions src/graphql/resolvers/checkAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const userOfListByItemId = async (user, itemId, User, List) => {
const activeUser = await User.findById(user.id);
const list = await List.find({ 'items.id': itemId });
if (activeUser.lists.includes(list.id)) {
return true;
}
return false;
};

export const userOfListByListId = async (user, listId, User) => {
const activeUser = await User.findById(user.id);
if (activeUser.lists.includes(listId)) {
return true;
}
return false;
};

export const getOnlySelf = (user, userId) => user.id === userId;

export const isAdmin = user => user.isAdmin;

export const ownerOfList = async (user, listId, List) => {
const list = await List.findById(listId);
const { owner } = list;
if (owner === user.id) {
return true;
}
return false;
};

0 comments on commit e14cd0e

Please sign in to comment.