stub new user sign up

This commit is contained in:
Sorrel Bri 2020-01-14 12:06:25 -08:00
parent 86bd4e93db
commit ff24d4a288
4 changed files with 51 additions and 36 deletions

View file

@ -1,11 +1,17 @@
const knex = require('../data/db')
const signToken = require('../services/signToken');
const signUp = async (req, res, next) => {
const user = req.body;
try {
signToken(res, user);
res.send('ok').status(200);
knex('user')
.insert(user)
.then(newUser => {
signToken(res, newUser[0]);
res.send('ok').status(200);
}
)
}
catch (err) {
res.status(500).json(err)

View file

@ -0,0 +1,24 @@
const dotenv = require('dotenv');
const jwt = require('jsonwebtoken');
dotenv.config();
const verifyToken = async (req, res, next) => {
const token = req.cookies.token || '';
const secret = process.env.NODE_ENV === 'test' ? process.env.TEST_SECRET : process.env.JWT_SECRET;
try {
if (!token) {
return res.status(401).json('You need to Login')
}
const decrypt = await jwt.verify(token, secret);
req.user = {
id: decrypt.id,
username: username,
};
next();
} catch (err) {
return res.status(500).json(err.toString());
}
};
module.exports = verifyToken;

View file

@ -1,4 +1,4 @@
const authSpec = (chai, server) => {
const authSpec = (chai, knex, server) => {
const newUserFormData = {
'username':'newUser',
'password':'password',
@ -27,6 +27,21 @@ const authSpec = (chai, server) => {
res.should.cookie('token');
done();
});
});
});
it('post to sign up should add user to db', done => {
chai.request(server)
.post('/auth/signup')
.type('form')
.send(newUserFormData)
.end((err, res) => {
if (err) done(err);
knex('user').where({'username': newUserFormData.username}).then(results => {
const newUser = results[0];
if (newUser.username === newUserFormData.username) done();
})
});
})
}
module.exports = authSpec;

View file

@ -8,7 +8,7 @@ const server = require('../server');
const should = chai.should();
// const authSpec = require('./auth.spec');
const authSpec = require('./auth.spec');
chai.use(chaiHttp);
// ! to run tests from other testing modules
@ -25,37 +25,7 @@ const setupDb = () => {
describe('Auth Routes', function() {
setupDb();
// authSpec(chai, server)
const newUserFormData = {
'username':'newUser',
'password':'password',
'email':'user@example.com'
}
it('post to sign up should return 200 status', function(done) {
chai.request(server)
.post('/auth/signup')
.type('form')
.send(newUserFormData)
.end((err, res) => {
if (err) done(err);
res.should.status(200);
done();
});
});
it('post to sign up should return token', done => {
chai.request(server)
.post('/auth/signup')
.type('form')
.send(newUserFormData)
.end((err, res) => {
if (err) done(err);
res.should.cookie('token');
done();
});
});
authSpec(chai, knex, server)
})