-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |