-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* includes tests * user must be owner to access information * server side for issue #60
- Loading branch information
Showing
10 changed files
with
189 additions
and
66 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
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,15 +1,22 @@ | ||
const authHelpers = require('../../helpers/auth') | ||
|
||
exports.seed = (knex, Promise) => { | ||
exports.seed = knex => { | ||
return knex('asks').del() | ||
.then(() => knex('users').del()) | ||
.then(() => { | ||
const pwHash = authHelpers.hashPassword('testuser123') | ||
return knex('users').insert({ | ||
firstName: 'Test', | ||
lastName: 'User', | ||
email: '[email protected]', | ||
password: pwHash | ||
}) | ||
return knex('users').insert([ | ||
{ | ||
firstName: 'Test', | ||
lastName: 'User', | ||
email: '[email protected]', | ||
password: authHelpers.hashPassword('testuser123') | ||
}, | ||
{ | ||
firstName: 'Another', | ||
lastName: 'User', | ||
email: '[email protected]', | ||
password: authHelpers.hashPassword('anotheruser123') | ||
} | ||
]) | ||
}) | ||
} |
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
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,30 @@ | ||
const express = require('express') | ||
const _ = require('lodash') | ||
const User = require('../db/models/User') | ||
const authHelpers = require('../helpers/auth') | ||
|
||
const router = express.Router() | ||
|
||
router.get('/:id', authHelpers.requireLogin, (req, res, next) => { | ||
const requestedUserID = parseInt(req.params.id, 10) | ||
const authToken = req.headers.authorization.split(' ')[1] | ||
const currentUserID = authHelpers.decodeTokenSync(authToken).sub | ||
User.getUserByID(requestedUserID) | ||
.then((user) => { | ||
if (!user) { | ||
const error = new Error('User not found') | ||
error.status = 404 | ||
return next(error) | ||
} | ||
if (requestedUserID !== currentUserID) { | ||
const error = new Error('You are not authorized to access this page') | ||
error.status = 403 | ||
return next(error) | ||
} | ||
user = _.pick(user, ['firstName', 'lastName', 'email']) | ||
return res.json(user) | ||
}) | ||
.catch(err => next(err)) | ||
}) | ||
|
||
module.exports = router |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ const should = chai.should() | |
|
||
chai.use(chaiHttp) | ||
|
||
describe('API Routes', function () { | ||
describe('Auth Routes', function () { | ||
beforeEach(() => { | ||
return knex.migrate.rollback() | ||
.then(() => knex.migrate.latest()) | ||
|
@@ -94,40 +94,4 @@ describe('API Routes', function () { | |
}) | ||
}) | ||
}) | ||
|
||
describe('GET api/v1/auth/user', () => { | ||
it('should return with status: success', (done) => { | ||
chai.request(server) | ||
.post('/api/v1/auth/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: 'testuser123' | ||
}) | ||
.end((error, response) => { | ||
should.not.exist(error) | ||
const token = response.body.token | ||
chai.request(server) | ||
.get('/api/v1/auth/user') | ||
.set('authorization', `Bearer ${token}`) | ||
.end((err, res) => { | ||
should.not.exist(err) | ||
res.status.should.eql(200) | ||
res.should.be.json | ||
res.body.status.should.eql('success') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should throw an error if a user is not logged in', (done) => { | ||
chai.request(server) | ||
.get('/api/v1/auth/user') | ||
.end((err, res) => { | ||
should.exist(err) | ||
res.status.should.eql(500) | ||
res.should.be.json | ||
res.body.status.should.eql('error') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
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,18 @@ | ||
const chai = require('chai') | ||
const chaiHttp = require('chai-http') | ||
const server = require('../app') | ||
|
||
chai.use(chaiHttp) | ||
|
||
function loginUser () { | ||
return chai.request(server) | ||
.post('/api/v1/auth/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: 'testuser123' | ||
}) | ||
} | ||
|
||
module.exports = { | ||
loginUser | ||
} |
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,102 @@ | ||
/* eslint-disable no-undef, no-unused-expressions, handle-callback-err */ | ||
process.env.NODE_ENV = 'test' | ||
|
||
const chai = require('chai') | ||
const chaiHttp = require('chai-http') | ||
const server = require('../app') | ||
const knex = require('../db/knex') | ||
const User = require('../db/models/User') | ||
const should = chai.should() | ||
const authHelpers = require('../helpers/auth') | ||
const specHelpers = require('./spec_helpers') | ||
chai.use(chaiHttp) | ||
|
||
describe('User Routes', function () { | ||
beforeEach(() => { | ||
return knex.migrate.rollback() | ||
.then(() => knex.migrate.latest()) | ||
.then(() => knex.seed.run()) | ||
}) | ||
|
||
afterEach(() => { | ||
return knex.migrate.rollback() | ||
}) | ||
|
||
describe('GET /api/v1/user/:id', () => { | ||
it('provides user data to authenticated user', (done) => { | ||
specHelpers.loginUser() | ||
.end((err, res) => { | ||
const authToken = res.body.token | ||
const userID = authHelpers.decodeTokenSync(authToken).sub | ||
chai.request(server) | ||
.get(`/api/v1/users/${userID}`) | ||
.set('authorization', `Bearer ${authToken}`) | ||
.end((err, res) => { | ||
should.not.exist(err) | ||
res.should.be.json | ||
res.status.should.eql(200) | ||
res.body.should.include.keys('firstName', 'lastName', 'email') | ||
res.body.should.not.include.keys('password') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('returns 404 error if the user does not exist', (done) => { | ||
specHelpers.loginUser() | ||
.end((err, res) => { | ||
const authToken = res.body.token | ||
const userID = authHelpers.decodeTokenSync(authToken).sub | ||
chai.request(server) | ||
.get('/api/v1/users/0') | ||
.set('authorization', `Bearer ${authToken}`) | ||
.end((err, res) => { | ||
should.exist(err) | ||
res.should.be.json | ||
res.status.should.equal(404) | ||
res.body.status.should.equal('error') | ||
res.body.error.should.equal('User not found') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('returns 401 error if the user provides no authentication', (done) => { | ||
specHelpers.loginUser() | ||
.end((err, res) => { | ||
const authToken = res.body.token | ||
const userID = authHelpers.decodeTokenSync(authToken).sub | ||
chai.request(server) | ||
.get(`/api/v1/users/${userID}`) | ||
// NOT setting authentication header | ||
.end((err, res) => { | ||
should.exist(err) | ||
res.should.be.json | ||
res.status.should.equal(401) | ||
res.body.status.should.equal('error') | ||
res.body.error.should.equal('Please log in') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('returns 403 error if the user does not own the profile', (done) => { | ||
specHelpers.loginUser() | ||
.end((err, res) => { | ||
const authToken = res.body.token | ||
User.getUserByEmail('[email protected]') | ||
.then((anotherUser) => { | ||
chai.request(server) | ||
.get(`/api/v1/users/${anotherUser.id}`) | ||
.set('authorization', `Bearer ${authToken}`) | ||
.end((err, res) => { | ||
should.exist(err) | ||
res.should.be.json | ||
res.status.should.equal(403) | ||
res.body.status.should.equal('error') | ||
res.body.error.should.equal('You are not authorized to access this page') | ||
done() | ||
}) | ||
}) | ||
.catch(err => console.log(err)) | ||
}) | ||
}) | ||
}) | ||
}) |