Skip to content

Commit

Permalink
Merge pull request #51 from JethroF22/feature/#44-fetchTweets
Browse files Browse the repository at this point in the history
Feature/#44 fetch tweets
  • Loading branch information
JethroF22 authored Sep 14, 2018
2 parents 9841a94 + 7608fd3 commit 0775e2a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
27 changes: 27 additions & 0 deletions server/routes/tweet.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
const express = require('express');
const _ = require('lodash');

const User = require('../models/user');
const Tweet = require('../models/tweet');
const authenticate = require('../middleware/authenticate');
const DBErrorParser = require('../utils/errorParser');

const router = express.Router();

// GET routes
router.get('/fetch_tweets/:id', (req, res) => {
const id = req.params.id;

User.findById(id).then(user => {
if (!user) {
return res.status(404).send('User does not exist');
}

const retweets = user.retweets;
Tweet.find({ $or: [{ 'user._id': id }, { _id: { $in: retweets } }] })
.then(tweets => {
if (tweets.length == 0) {
return res.status(404).send('User has not tweeted anything');
}
res.send(tweets);
})
.catch(err => {
const errors = DBErrorParser(err);
res.status(400).send(errors);
});
});
});

// POST routes
router.post('/create', authenticate, (req, res) => {
const user = req.user;
const tweet = _.pick(req.body, ['body', 'imgUrl']);
Expand All @@ -27,6 +53,7 @@ router.post('/create', authenticate, (req, res) => {
});
});

// PATCH routes
router.patch('/retweet/:id', authenticate, (req, res) => {
const user = req.user;
const _id = req.params.id;
Expand Down
9 changes: 9 additions & 0 deletions server/tests/seed/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Tweet = require('../../models/tweet');

const userOneID = new ObjectID();
const userTwoID = new ObjectID();
const userThreeID = new ObjectID();
const tweetOneID = new ObjectID();
const tweetTwoID = new ObjectID();

Expand All @@ -32,6 +33,14 @@ const users = [
_id: tweetOneID
}
]
},
{
_id: userThreeID,
name: 'Alexandra',
email: '[email protected]',
handle: 'QueenAlexa',
password: 'password3',
token: jwt.sign({ id: userThreeID }, process.env.SECRET_KEY)
}
];

Expand Down
53 changes: 52 additions & 1 deletion server/tests/tweet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,62 @@ const { ObjectID } = require('mongodb');

const app = require('../server');
const Tweet = require('../models/tweet');
const { tweets, users, populateTweets } = require('./seed/seed');
const { tweets, users, populateTweets, populateUsers } = require('./seed/seed');

let tweet, id, token, user;

describe('/tweet', () => {
describe('GET', () => {
beforeEach(function(done) {
this.timeout(0);
user = users[1];
populateTweets(done);
});

beforeEach(function(done) {
populateUsers(done);
});

describe('/fetch_tweets/:id', () => {
it('should fetch a users tweets', done => {
id = user._id;

request(app)
.get(`/tweet/fetch_tweets/${id}`)
.expect(200)
.expect(res => {
expect(res.body.length).to.equal(2);
})
.end(done);
});

it('should return an error for invalid ids', done => {
id = new ObjectID();

request(app)
.get(`/tweet/fetch_tweets/${id}`)
.expect(404)
.expect(res => {
expect(res.error.text).to.equal('User does not exist');
})
.end(done);
});

it("should return an error if the user hasn't created tweets", done => {
user = users[2];
id = user._id;

request(app)
.get(`/tweet/fetch_tweets/${id}`)
.expect(404)
.expect(res => {
expect(res.error.text).to.equal('User has not tweeted anything');
})
.end(done);
});
});
});

describe('POST', () => {
beforeEach(() => {
tweet = tweets[1];
Expand Down

0 comments on commit 0775e2a

Please sign in to comment.