Last active
December 8, 2021 14:46
-
-
Save Tsugami/9862be88b32cfed43acc60658b2b5548 to your computer and use it in GitHub Desktop.
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
import { clearDatabase, closeDatabase, connectDatabase } from '../../../../../tests/mongo'; | |
import { mutate } from '../../../../../tests/app'; | |
import { createUserFixture } from '../../../../../tests/user.factory'; | |
import ERRORS from '../../../../errors'; | |
beforeAll(async () => await connectDatabase()); | |
beforeEach(async () => { | |
await clearDatabase(); | |
}); | |
afterAll(async () => await closeDatabase()); | |
const LOGIN_MUTATION = ` | |
mutation Login($input: LoginWithEmailAndPasswordInput!) { | |
LoginWithEmailAndPassword(input: $input) { | |
token | |
error | |
} | |
} | |
`; | |
it('should return a valid token if valid attrs is provided', async () => { | |
const input = { email: '[email protected]', password: 'valid_password' }; | |
await createUserFixture(input, true); | |
const response = await mutate(LOGIN_MUTATION, { input }); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.LoginWithEmailAndPassword.token).toBeTruthy(); | |
expect(response.data.LoginWithEmailAndPassword.error).toBeNull(); | |
}); | |
it('should throw an error if invalid password is provided', async () => { | |
const invalidPasswordInput = { email: '[email protected]', password: 'invalid_password' }; | |
await createUserFixture({ email: '[email protected]', password: 'valid_password' }); | |
const response = await mutate(LOGIN_MUTATION, { input: invalidPasswordInput }); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.LoginWithEmailAndPassword.token).toBeNull(); | |
expect(response.data.LoginWithEmailAndPassword.error).toBe(ERRORS.INVALID_EMAIL_OR_PASSWORD); | |
}); | |
it('should throw an error if invalid email is provided', async () => { | |
const invalidEmailInput = { email: '[email protected]', password: 'valid_password' }; | |
await createUserFixture({ email: '[email protected]', password: 'valid_password' }); | |
const response = await mutate(LOGIN_MUTATION, { input: invalidEmailInput }); | |
expect(response.errors).toBeUndefined(); | |
expect(response.data.LoginWithEmailAndPassword.token).toBeNull(); | |
expect(response.data.LoginWithEmailAndPassword.error).toBe(ERRORS.INVALID_EMAIL_OR_PASSWORD); | |
}); |
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
import bcrypt from 'bcryptjs'; | |
import { generateAuthToken } from '../src/auth'; | |
import { UserGenderEnum } from '../src/modules/user/schemas/gender.enum'; | |
import UserModal, { IUserBase } from '../src/modules/user/user.model'; | |
import { makeID } from './app'; | |
export const VALID_USER_ATTRS = { | |
name: 'Valid Name', | |
username: 'valid.username', | |
email: '[email protected]', | |
bio: 'Valid Bio', | |
birthDate: '2000-02-20', | |
gender: UserGenderEnum.MALE, | |
password: 'valid_password', | |
website: 'www.valid-website.com', | |
}; | |
type UserFixtureInput = Partial<IUserBase & { password?: string }>; | |
export const createUserFixture = async ( | |
user: UserFixtureInput = {}, | |
shouldEncodePassowrd = false, | |
) => { | |
const passwordHash = shouldEncodePassowrd | |
? await bcrypt.hash(user?.password ?? VALID_USER_ATTRS.password, 12) | |
: 'valid_password'; | |
const data = { ...VALID_USER_ATTRS, ...user, passwordHash }; | |
return UserModal.create(data); | |
}; | |
export const createUserFixtureAndToken = async (attrs: UserFixtureInput = {}) => { | |
const user = await createUserFixture(attrs); | |
const accessToken = generateAuthToken(user._id); | |
const userID = makeID('User', user._id); | |
return { user, accessToken, userID }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment