Skip to content

Commit

Permalink
fix Seneca-CDOT#707 adding post route.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grommers00 committed Feb 16, 2020
1 parent 8b5bc0e commit f649b5c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/backend/web/routes/feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const Feed = require('../../data/feed');
const { getFeeds } = require('../../utils/storage');
const { logger } = require('../../utils/logger');
// const { protect } = require('../authentication');

const feeds = express.Router();

Expand All @@ -10,8 +11,8 @@ feeds.get('/', async (req, res) => {

try {
ids = await getFeeds();
} catch (err) {
logger.error({ err }, 'Unable to get feeds from Redis');
} catch (error) {
logger.error({ error }, 'Unable to get feeds from Redis');
res.status(503).json({
message: 'Unable to connect to database',
});
Expand Down Expand Up @@ -43,12 +44,36 @@ feeds.get('/:id', async (req, res) => {
} else {
res.json(feed);
}
} catch (err) {
logger.error({ err }, 'Unable to get feeds from Redis');
} catch (error) {
logger.error({ error }, 'Unable to get feeds from Redis');
res.status(503).json({
message: 'Unable to connect to database',
});
}
});

// add protect here for development.

feeds.post('/', async (req, res) => {
const feedData = req.body;

try {
if (!(feedData.url && feedData.author)) {
return res.status(400).json({ message: `URL and Author must be submitted` });
}
if (await Feed.byUrl(feedData.url)) {
return res.status(409).json({ message: `Feed for url ${feedData.url} already exists.` });
}
const feedId = await Feed.create(feedData);
return res
.status(201)
.json({ message: `Feed was successfully added.`, id: feedId, url: `/feeds/${feedId}` });
} catch (error) {
logger.error({ error }, 'Unable to add feed to Redis');
return res.status(503).json({
message: 'Unable to add to Feed',
});
}
});

module.exports = feeds;
53 changes: 52 additions & 1 deletion test/feeds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const app = require('../src/backend/web/app');
const Feed = require('../src/backend/data/feed');
const hash = require('../src/backend/data/hash');

describe('test /feeds endpoint', () => {
describe('test GET /feeds endpoint', () => {
const createdItems = 150;

// Array of feeds
Expand Down Expand Up @@ -62,3 +62,54 @@ describe('test /feeds/:id responses', () => {
expect(res.body instanceof Array).toBe(false);
});
});

describe('test POST /feeds endpoint', () => {
const createdItems = 5;
const feedData = {
author: 'foo',
url: 'http://telescope200.cdot.systems',
};

// Array of feeds
const feeds = [...Array(createdItems).keys()].map(item => {
return new Feed('foo', `http://telescope${item}.cdot.systems`);
});

beforeAll(() => Promise.all(feeds.map(feed => feed.save())));

it('responds with json', async () => {
const res = await request(app)
.post('/feeds')
.send(feedData)
.set('Accept', 'application/json');
expect(res.status).toEqual(201);
});

it('no author being sent', async () => {
feedData.author = null;
const res = await request(app)
.post('/feeds')
.send(feedData)
.set('Accept', 'application/json');
expect(res.status).toEqual(400);
});

it('no url being sent', async () => {
feedData.author = 'foo';
feedData.url = null;
const res = await request(app)
.post('/feeds')
.send(feedData)
.set('Accept', 'application/json');
expect(res.status).toEqual(400);
});

it('url already in the feed list', async () => {
feedData.url = 'http://telescope3.cdot.systems';
const res = await request(app)
.post('/feeds')
.send(feedData)
.set('Accept', 'application/json');
expect(res.status).toEqual(409);
});
});

0 comments on commit f649b5c

Please sign in to comment.