Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
blackdevelopa committed Apr 25, 2019
1 parent 3227ae3 commit 5bb3d99
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 16 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"express-session": "1.15.6",
"joi": "14.3.1",
"jsonwebtoken": "8.5.0",
"jwt-decode": "2.2.0",
"morgan": "1.9.1",
"passport": "0.4.0",
"passport-facebook": "3.0.0",
Expand Down
5 changes: 1 addition & 4 deletions server/controllers/social-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ const authUser = (req, res) => {
socialProvider: userInfo.provider
}
}).then(([user]) => {
const {
dataValues: { id }
} = user;
user.username = userInfo.displayName;
user.image = userInfo.photos[0].value;
user.email = userInfo.emails[0].value;
user.socialProvider = userInfo.provider;
const verificationToken = generateToken({ id }, '1d');
const verificationToken = generateToken({ user }, '1d');
user.token = verificationToken;
return res.redirect(
`${process.env.UI_CLIENT_HOST}/auth?token=${user.token}`
Expand Down
1 change: 0 additions & 1 deletion server/routes/social-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ router.get(
passport.authenticate('google', { scope: ['profile', 'email'] })
);
router.get('/google/callback', passport.authenticate('google'), authUser);

router.get(
'/login/twitter',
passport.authenticate('twitter', {
Expand Down
8 changes: 8 additions & 0 deletions server/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jwt from 'jsonwebtoken';
import bcrypt from 'bcrypt';
import Joi from 'joi';
import jwtDecode from 'jwt-decode';
import env from '../config/env-config';

const { SECRET, UI_CLIENT_HOST, HEROKU_APP_NAME } = env;
Expand All @@ -26,6 +27,13 @@ export const generateToken = (payload, expiresIn = '7d') =>
*/
export const verifyToken = token => jwt.verify(token, SECRET);

/**
* Synchronously decode given JWT token
* @param {string} token JWT token
* @returns {string | object} decoded JWT payload
*/
export const decodeToken = token => jwtDecode(token);

/**
* Sends a success response to the client
* @param {Response} res Response object
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ export const randomSocialUser = {
emails: [{ value: faker.internet.email() }],
photos: [{ value: faker.image.image() }]
};

export const userWithNoEmail = {
id: faker.random.number(),
displayName: faker.random.alphaNumeric(10),
photos: [{ value: faker.image.image() }]
};
13 changes: 8 additions & 5 deletions test/mockStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class MockStrategy extends passport.Strategy {
/**
* @param {*} name
* @param {*} callback
* @param {*} user
*/
constructor(name, callback) {
constructor(name, callback, user) {
super(name, callback);
this.name = name;
this._cb = callback;
this._user = { ...randomSocialUser, provider: name };
this._user = { ...user, provider: name };
}

/**
Expand All @@ -29,8 +30,10 @@ class MockStrategy extends passport.Strategy {
}
}

passport.use(new MockStrategy('google', socialAuthCallback));
passport.use(new MockStrategy('facebook', socialAuthCallback));
passport.use(new MockStrategy('twitter', socialAuthCallback));
passport.use(new MockStrategy('google', socialAuthCallback, randomSocialUser));
passport.use(
new MockStrategy('facebook', socialAuthCallback, randomSocialUser)
);
passport.use(new MockStrategy('twitter', socialAuthCallback, randomSocialUser));

export { MockStrategy, randomSocialUser };
33 changes: 27 additions & 6 deletions test/socialAuth.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import passport from 'passport';
import server from '../server';
import { randomSocialUser } from './mockStrategy';
import { MockStrategy, randomSocialUser } from './mockStrategy';
import { userWithNoEmail } from './fixtures';
import { decodeToken, socialAuthCallback } from '../server/utils/helpers';

chai.use(chaiHttp);

Expand All @@ -12,8 +15,13 @@ describe('GET Social Authentication', () => {
.request(server)
.get('/api/auth/twitter/callback')
.end((err, res) => {
const { user } = decodeToken(res.redirects[0].split('token=')[1]);
expect(user.username).to.equal(randomSocialUser.displayName);
expect(user.email).to.equal(randomSocialUser.emails[0].value);
expect(user.image).to.equal(randomSocialUser.photos[0].value);
expect(user.socialProvider).to.equal('twitter');
expect(res.redirects.length).to.equal(1);
expect(res.redirects[0].includes('token')).to.equal(true);
expect(res.redirects[0].includes('/auth?token=')).to.equal(true);
done(err);
});
});
Expand All @@ -23,8 +31,13 @@ describe('GET Social Authentication', () => {
.request(server)
.get('/api/auth/facebook/callback', randomSocialUser)
.end((err, res) => {
const { user } = decodeToken(res.redirects[0].split('token=')[1]);
expect(user.username).to.equal(randomSocialUser.displayName);
expect(user.email).to.equal(randomSocialUser.emails[0].value);
expect(user.image).to.equal(randomSocialUser.photos[0].value);
expect(user.socialProvider).to.equal('facebook');
expect(res.redirects.length).to.equal(1);
expect(res.redirects[0].includes('token')).to.equal(true);
expect(res.redirects[0].includes('/auth?token=')).to.equal(true);
done(err);
});
});
Expand All @@ -34,18 +47,26 @@ describe('GET Social Authentication', () => {
.request(server)
.get('/api/auth/google/callback')
.end((err, res) => {
const { user } = decodeToken(res.redirects[0].split('token=')[1]);
expect(user.username).to.equal(randomSocialUser.displayName);
expect(user.email).to.equal(randomSocialUser.emails[0].value);
expect(user.image).to.equal(randomSocialUser.photos[0].value);
expect(user.socialProvider).to.equal('google');
expect(res.redirects.length).to.equal(1);
expect(res.redirects[0].includes('token')).to.equal(true);
expect(res.redirects[0].includes('/auth?token=')).to.equal(true);
done(err);
});
});

it('should return an error if no email is present', done => {
passport.use(
new MockStrategy('facebook', socialAuthCallback, userWithNoEmail)
);
chai
.request(server)
.get('/api/auth/google/callback')
.get('/api/auth/facebook/callback')
.end((err, res) => {
expect(res.error.status).to.equal(404);
expect(res.body.errors[0]).to.equal('No email found. Add email');
done(err);
});
});
Expand Down

0 comments on commit 5bb3d99

Please sign in to comment.