Skip to content

Commit

Permalink
Merge 2e1e0c6 into 978f95b
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanusi committed Apr 15, 2019
2 parents 978f95b + 2e1e0c6 commit db2ec6e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/controllers/authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { User } from '../models';

const getAuthors = async (req, res) => {
try {
const authors = User.findAll({
where: { role: 'author' },
});

if (authors) {
res.status(200).json({ status: 'success', data: authors });
}

if (authors.length === 0) {
res.status(200).json({ status: 'success', message: 'The is no Author on the database' });
}
} catch (error) {
console.log(error);
}
};
export default getAuthors;
19 changes: 13 additions & 6 deletions src/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { login, createUser, linkedinUser, linkedinCallback } from '../controller
import checkBody from '../middlewares/signUpValidator';
import likeArticle from '../controllers/like';
import Authenticator from '../middlewares/authenticator';

import getAuthors from '../controllers/authors';

const router = Router();

Expand Down Expand Up @@ -52,7 +52,11 @@ router.get(
// Route for google Authentication
router.get('/auth/google', passport.authenticate('google', { scope: ['email profile'] }));

router.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/api/v1/auth/login' }), socialRedirect);
router.get(
'/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/api/v1/auth/login' }),
socialRedirect,
);

router.get('/auth/linkedin/callback', linkedinCallback);
router.get('/auth/linkedin', linkedinUser);
Expand All @@ -67,15 +71,18 @@ router.get('/auth/linkedin', linkedinUser);
* @returns Response Object
*/

router
.post('/signup', checkBody, createUser);
router.post('/signup', checkBody, createUser);

// route for twitter authentication
router.get('/auth/twitter', passport.authenticate('twitter'));

router.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/api/v1/auth/login' }), socialRedirect);

router.get(
'/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/api/v1/auth/login' }),
socialRedirect,
);

router.post('/login', checkFields, passportAuth, login);
router.get('/authors', Authenticator.verifyToken, getAuthors);

export default router;
38 changes: 38 additions & 0 deletions tests/integration/authorList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import { startServer } from '../../src/server';
import LOGIN from '../mock/login';

const { expect } = chai;
let app = null;
let agent = null;
let userToken;

chai.use(chaiHttp);

describe('LIST AUTHORS', () => {
beforeEach(async () => {
app = await startServer(6600);
agent = chai.request(app);
});
it('login User', (done) => {
agent
.post('/api/v1/login')
.send(LOGIN)
.end((err, res) => {
const { token } = res.body.data;
userToken = token;
done();
});
});
it('should list authors', (done) => {
const jwt = `Bearer ${userToken}`;
agent
.get('/api/v1/authors')
.set('Authorization', jwt)
.end((err, res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
});

0 comments on commit db2ec6e

Please sign in to comment.