-
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.
Merge pull request #6 from jkarenzi/ft-auth-#2
Feat: Implement authentication and Authorization #2
- Loading branch information
Showing
22 changed files
with
998 additions
and
1,493 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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
{ | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"extends": ["plugin:@typescript-eslint/recommended"], | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
"es2021": true | ||
}, | ||
"rules": { | ||
"no-console": "warn", | ||
"quotes": ["error", "single"] | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": ["dist/**/*", "__tests__/*"] | ||
} | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"quotes": ["error", "single"], | ||
"no-console": ["warn", { "allow": ["warn", "error"] }], | ||
"@typescript-eslint/no-var-requires": "off" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,16 +1,11 @@ | ||
**what does this PR do?** | ||
|
||
|
||
**Description of the task to be completed** | ||
|
||
|
||
**How can this be manually tested?** | ||
|
||
|
||
**Swagger documentation screenshot** | ||
|
||
|
||
**Test screenshot** | ||
|
||
|
||
**What are the relevant pivotal trackers/story id?** | ||
**What are the relevant pivotal trackers/story id?** |
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
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,134 @@ | ||
export {}; | ||
const request = require('supertest'); | ||
const app = require('../src/app'); | ||
const bcrypt = require('bcrypt'); | ||
const { | ||
signUpSchema, | ||
loginSchema, | ||
} = require('../src/middleware/validators/authSchema'); | ||
const jwt = require('jsonwebtoken') | ||
const User = require('../src/models/User'); | ||
|
||
jest.mock('../src/models/User'); | ||
jest.mock('../src/middleware/validators/authSchema'); | ||
jest.mock('bcrypt'); | ||
jest.mock('jsonwebtoken') | ||
|
||
describe('Auth Controller Tests', () => { | ||
const signUpFormData = { | ||
fullName: 'test tester', | ||
email: '[email protected]', | ||
password: 'test123456', | ||
}; | ||
|
||
const loginFormData = { | ||
email: '[email protected]', | ||
password: 'test123456', | ||
}; | ||
|
||
const returnedUser = { | ||
_id:'some id', | ||
fullName:'mock user', | ||
email:'[email protected]', | ||
password:'password1234', | ||
createdAt: 'some date', | ||
updatedAt: 'some date' | ||
} | ||
|
||
it('should return a 201 if signup is successful', async () => { | ||
signUpSchema.validate.mockReturnValueOnce({ error: null }); | ||
|
||
User.findOne.mockImplementationOnce(() => Promise.resolve(null)); | ||
|
||
bcrypt.hash.mockResolvedValueOnce('hashed password'); | ||
|
||
User.prototype.save.mockResolvedValueOnce(returnedUser); | ||
|
||
const response = await request(app).post('/api/auth/signup').send(signUpFormData); | ||
expect(response.status).toBe(201); | ||
expect(User.prototype.save).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return a 400 if validation fails on signup', async () => { | ||
signUpSchema.validate.mockReturnValueOnce({ error: { details: [{ message: 'Validation failed' }] } }); | ||
|
||
const response = await request(app).post('/api/auth/signup').send(signUpFormData); | ||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBeDefined(); | ||
}) | ||
|
||
it('should return a 409 if email already exists', async () => { | ||
signUpSchema.validate.mockReturnValueOnce({ error: null }); | ||
|
||
User.findOne.mockImplementationOnce(() => Promise.resolve({ | ||
_id:'some id', | ||
fullName:'mock user', | ||
email:'[email protected]', | ||
password:'password1234', | ||
createdAt: 'some date', | ||
updatedAt: 'some date' | ||
})); | ||
|
||
const response = await request(app).post('/api/auth/signup').send(signUpFormData); | ||
expect(response.status).toBe(409); | ||
expect(response.body.message).toBe('Email already in use'); | ||
}) | ||
|
||
it('should return a 500 if an error occurs during signup', async () => { | ||
signUpSchema.validate.mockReturnValueOnce({ error: null }); | ||
User.findOne.mockImplementationOnce(() => {throw new Error('DB error')}) | ||
|
||
const response = await request(app).post('/api/auth/signup').send(signUpFormData); | ||
expect(response.status).toBe(500); | ||
expect(response.body.message).toBe('Internal Server Error'); | ||
}) | ||
|
||
it('should return a 200 if login is successful', async () => { | ||
loginSchema.validate.mockReturnValueOnce({ error: null }); | ||
User.findOne.mockImplementationOnce(() => Promise.resolve(returnedUser)) | ||
|
||
bcrypt.compare.mockResolvedValueOnce(true) | ||
|
||
jwt.sign.mockResolvedValueOnce('fake token') | ||
|
||
const response = await request(app).post('/api/auth/login').send(loginFormData); | ||
expect(response.status).toBe(200); | ||
expect(response.body.message).toBe('Login successful'); | ||
}) | ||
|
||
it('should return a 400 if validation fails on login', async () => { | ||
loginSchema.validate.mockReturnValueOnce({ error: { details: [{ message: 'Validation failed' }] } }); | ||
|
||
const response = await request(app).post('/api/auth/login').send(loginFormData); | ||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBeDefined() | ||
}) | ||
|
||
it('should return a 404 if account if not found during login', async () => { | ||
loginSchema.validate.mockReturnValueOnce({ error: null }); | ||
User.findOne.mockImplementationOnce(() => Promise.resolve(null)) | ||
const response = await request(app).post('/api/auth/login').send(loginFormData); | ||
expect(response.status).toBe(404); | ||
expect(response.body.message).toBe('Account not found'); | ||
}) | ||
|
||
it('should return a 401 if password provided is incorrect, in login', async () => { | ||
loginSchema.validate.mockReturnValueOnce({ error: null }); | ||
User.findOne.mockImplementationOnce(() => Promise.resolve(returnedUser)) | ||
|
||
bcrypt.compare.mockResolvedValueOnce(false) | ||
|
||
const response = await request(app).post('/api/auth/login').send(loginFormData); | ||
expect(response.status).toBe(401); | ||
expect(response.body.message).toBe('Incorrect password'); | ||
}) | ||
|
||
it('should return a 500 if an error occurs during login', async () => { | ||
loginSchema.validate.mockReturnValueOnce({ error: null }); | ||
User.findOne.mockImplementationOnce(() => {throw new Error('DB error')}) | ||
|
||
const response = await request(app).post('/api/auth/login').send(loginFormData); | ||
expect(response.status).toBe(500); | ||
expect(response.body.message).toBe('Internal Server 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 |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import { test } from '../src/controllers/testController'; | ||
export {} | ||
const request = require('supertest'); | ||
const app = require('../src/app'); | ||
|
||
const res: any = {}; | ||
|
||
(res.json = jest.fn((x: Object) => x)), | ||
(res.status = jest.fn((x: number) => res)); | ||
|
||
const req: any = { | ||
body: { | ||
name: 'test', | ||
}, | ||
}; | ||
|
||
describe('Test', () => { | ||
it('should return 200 successful upon testing route', async () => { | ||
await test(req, res); | ||
|
||
expect(res.status).toHaveBeenCalledWith(200); | ||
const response = await request(app).get('/api/test') | ||
expect(response.status).toBe(200); | ||
}); | ||
}); |
Oops, something went wrong.