-
Notifications
You must be signed in to change notification settings - Fork 71
Backend Signup Functionality #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36b76ac
1024e04
b023fd9
3f50fec
c5b1c0f
fee7fbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
jest.mock('bcrypt') | ||
jest.mock('../dbload') | ||
jest.mock('../mattermost') | ||
import bcrypt from 'bcrypt' | ||
import db from '../dbload' | ||
import { login, logout } from './authController' | ||
import { login, logout, signup } from './authController' | ||
import { chatSignUp } from '../mattermost' | ||
|
||
import { ApolloError } from 'apollo-server-micro' | ||
|
||
describe('auth controller', () => { | ||
let userArgs | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
userArgs = { username: 'testuser', password: 'c0d3reallyhard' } | ||
userArgs = { | ||
username: 'testuser', | ||
password: 'c0d3reallyhard', | ||
firstName: 'testuser1', | ||
lastName: 'testuser1', | ||
email: 'testuser@c0d3.com' | ||
} | ||
}) | ||
|
||
test('Login - should throw error when session is null', async () => { | ||
expect( | ||
return expect( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is return needed here now? Just asking for my own knowledge. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're testing an async function and you're not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably explains why I couldn't test something awhile back to see if it was throwing an error! lol |
||
login({}, userArgs, { req: { session: null } }) | ||
).rejects.toThrowError('') | ||
}) | ||
|
@@ -81,4 +91,67 @@ describe('auth controller', () => { | |
success: true | ||
}) | ||
}) | ||
|
||
test('Signup - should reject if user information is incomplete', async () => { | ||
return expect( | ||
signup({}, {}, { req: { session: {} } }) | ||
).rejects.toThrowError('Register form is not completely filled out') | ||
}) | ||
|
||
test('Signup - should reject if user already exists', async () => { | ||
db.User.findOne = jest.fn().mockReturnValue({ username: 'c0d3user' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this not return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess since you mock it the return value in itself doesn't matter as long as something is returned? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't matter, it just have to return some user. A nitpick would be to return the same username though, it'll make prevent questions like this in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return expect( | ||
signup({}, userArgs, { req: { session: {} } }) | ||
).rejects.toThrowError('User already exists') | ||
}) | ||
|
||
test('Signup - should reject on second findOne. The first request checks for username, second request checks for email', async () => { | ||
db.User.findOne = jest | ||
hwong0305 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.fn() | ||
.mockReturnValueOnce(null) | ||
.mockReturnValue({ username: 'c0d3user' }) // Second call for User.findOne checks for email | ||
return expect( | ||
signup({}, userArgs, { req: { session: {} } }) | ||
).rejects.toThrowError('Email already exists') | ||
}) | ||
|
||
test('Signup - should not create user if chat signup responds with 401 or 403', async () => { | ||
db.User.findOne = jest.fn().mockReturnValue(null) | ||
db.User.create = jest.fn() | ||
|
||
chatSignUp.mockRejectedValue( | ||
'Invalid or missing parameter in mattermost request' | ||
) | ||
|
||
await expect( | ||
signup({}, userArgs, { req: { session: {} } }) | ||
).rejects.toThrowError('Invalid or missing parameter in mattermost request') | ||
|
||
expect(db.User.create).not.toBeCalled() | ||
}) | ||
|
||
test('Signup - should not create user if chat signup throws an error', async () => { | ||
db.User.findOne = jest.fn().mockReturnValue(null) | ||
db.User.create = jest.fn() | ||
|
||
chatSignUp.mockRejectedValueOnce('Mattermost Error') | ||
|
||
await expect( | ||
signup({}, userArgs, { req: { session: {} } }) | ||
).rejects.toThrow('Mattermost Error') | ||
|
||
expect(db.User.create).not.toBeCalled() | ||
}) | ||
|
||
test('Signup - should resolve with success true if signup successful ', async () => { | ||
db.User.findOne = jest.fn().mockReturnValue(null) | ||
db.User.create = jest.fn().mockReturnValue({ username: 'user' }) | ||
chatSignUp.mockResolvedValueOnce({ | ||
success: true | ||
}) | ||
const result = await signup({}, userArgs, { req: { session: {} } }) | ||
expect(result).toEqual({ | ||
username: 'user' | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
jest.mock('node-fetch') | ||
import fetch from 'node-fetch' | ||
import { chatSignUp } from './mattermost' | ||
|
||
describe('Chat Signup', () => { | ||
let userArgs | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
userArgs = { | ||
username: 'testuser', | ||
password: 'c0d3reallyhard', | ||
email: 'testuser@c0d3.com' | ||
} | ||
}) | ||
|
||
test('ChatSignUp - should resolve if response is 201', async () => { | ||
fetch.mockResolvedValue({ status: 201 }) | ||
return expect( | ||
chatSignUp(userArgs.username, userArgs.password, userArgs.email) | ||
).resolves.toEqual({ success: true }) | ||
}) | ||
|
||
test('ChatSignUp - should reject with invalid parameter if response is 401', async () => { | ||
fetch.mockResolvedValue({ status: 401 }) | ||
return expect( | ||
chatSignUp(userArgs.username, userArgs.password, userArgs.email) | ||
).rejects.toThrowError('Invalid or missing parameter in mattermost request') | ||
}) | ||
|
||
test('ChatSignUp - should reject with invalid permission if response is 403', async () => { | ||
fetch.mockResolvedValue({ status: 403 }) | ||
return expect( | ||
chatSignUp(userArgs.username, userArgs.password, userArgs.email) | ||
).rejects.toThrowError('Invalid permission') | ||
}) | ||
|
||
test('ChatSignUp - should reject if response status is invalid', async () => { | ||
fetch.mockResolvedValue({ status: 418 }) // MatterMost only returns 201, 401 and 403 LOL teapot | ||
return expect( | ||
chatSignUp(userArgs.username, userArgs.password, userArgs.email) | ||
).rejects.toThrowError('Unexpected Response') | ||
}) | ||
|
||
test('ChatSignUp - should reject if internal server error', async () => { | ||
fetch.mockRejectedValue('') | ||
return expect( | ||
chatSignUp(userArgs.username, userArgs.password, userArgs.email) | ||
).rejects.toThrowError('Internal Server Error') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import fetch from 'node-fetch' | ||
const accessToken = process.env.MATTERMOST_ACCESS_TOKEN | ||
const chatServiceUrl = process.env.CHAT_URL | ||
|
||
const headers = { | ||
Authorization: `Bearer ${accessToken}` | ||
} | ||
|
||
export const chatSignUp = async ( | ||
username: string, | ||
password: string, | ||
email: string | ||
) => { | ||
try { | ||
const response = await fetch(`${chatServiceUrl}/users`, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
username, | ||
email, | ||
password | ||
}) | ||
}) | ||
|
||
if (response.status === 201) { | ||
return { success: true } | ||
} | ||
if (response.status === 401) { | ||
throw new Error('Invalid or missing parameter in mattermost request') | ||
} | ||
if (response.status === 403) { | ||
throw new Error('Invalid permission') | ||
} | ||
|
||
throw new Error('Unexpected Response') // Mattermost will only respond with 201, 401 and 403 | ||
} catch (err) { | ||
throw new Error(err || 'Internal Server Error') | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.