-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7067777
commit 79a80b5
Showing
18 changed files
with
1,045 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: npm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"netid": "qz25", | ||
"frontend": "https://hw7qz25fronted.surge.sh", | ||
"backend": "https://hw7qz25.herokuapp.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dummy_username": "sep1test", | ||
"dummy_password": "native-web-tester", | ||
"dummy_url": "https://webdev-dummy.herokuapp.com", | ||
"site_username": "zhao", | ||
"site_password": "1234", | ||
"site_url": "http://localhost:3000" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
const cookieParser = require('cookie-parser') | ||
if (process.env.NODE_ENV !== "production") { | ||
require('dotenv').load() | ||
} | ||
|
||
|
||
const corsMiddleware = (req, res, next) => { | ||
|
||
res.header('Access-Control-Allow-Origin',req.headers.origin) | ||
res.header('Access-Control-Allow-Credentials',true) | ||
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS') | ||
res.header('Access-Control-Allow-Headers','Authorization, Content-Type') | ||
if (req.method == 'OPTIONS') | ||
{ | ||
res.sendStatus(200) | ||
} | ||
else{ | ||
next() | ||
} | ||
} | ||
|
||
const app = express() | ||
app.use(cookieParser()); | ||
app.use(bodyParser.json()) | ||
app.use(bodyParser.urlencoded({extended:false})) | ||
app.use(corsMiddleware) | ||
require('./src/auth')(app) | ||
require('./src/profile')(app) | ||
require('./src/articles')(app) | ||
require('./src/following')(app) | ||
//require('./uploadCloudinary.js').setup(app) | ||
|
||
// Get the port from the environment, i.e., Heroku sets it | ||
const port = process.env.PORT || 3000 | ||
const server = app.listen(port, () => { | ||
const addr = server.address() | ||
console.log(`Server listening at http://${addr.address}:${addr.port}`) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
// This script logs into the dummy server and logs into your server | ||
// it pulls data from the dummy server and pushes it into your server | ||
|
||
var fs = require('fs') | ||
var request = require('request').defaults({jar: true}) | ||
|
||
var cred = {} | ||
fs.readFile('./cred.json', function(err, data) { | ||
var d = JSON.parse(data) | ||
Object.keys(d).forEach(function(key) { | ||
cred[key] = d[key] | ||
}) | ||
login() | ||
}) | ||
|
||
function login() { | ||
request({ url: cred.dummy_url + '/login', method: 'POST', | ||
json: { "username": cred.dummy_username, "password": cred.dummy_password } | ||
}, function (err, res, body) { | ||
if (err || body.result !== "success") { | ||
console.error("There was an error logging into the dummy server with credentials: " + cred, err) | ||
process.exit(1) | ||
} | ||
getArticles() | ||
}) | ||
} | ||
|
||
var articlesToPost; | ||
function getArticles(cookie) { | ||
request({ url: cred.dummy_url + '/articles', method: 'GET', json:true }, function(err, res, body) { | ||
if (err) { | ||
console.error("There was an error grabbing articles from the dummy server", err) | ||
process.exit(1) | ||
} | ||
articlesToPost = body.articles | ||
console.log("Read " + articlesToPost.length + " articles from dummy server") | ||
loginToSite() | ||
}) | ||
} | ||
|
||
function loginToSite() { | ||
request({ url: cred.site_url + '/login', method: 'POST', | ||
json: { "username": cred.site_username, "password": cred.site_password } | ||
}, function(err, res, body) { | ||
if (err) { | ||
console.error("There was an error logging into YOUR server with credentials: " + cred, err) | ||
process.exit(1) | ||
} | ||
getArticleCount(sendArticles) | ||
}) | ||
} | ||
|
||
function sendArticles() { | ||
var article = articlesToPost.pop() | ||
//console.log("articles: " + article) | ||
if (article) { | ||
request({ url: cred.site_url + '/article', method: 'POST', json: article }, function(err, res, body) { | ||
if (err) { | ||
console.error("There was an error POSTing an article to YOUR server. article=" + article, err) | ||
process.exit(1) | ||
} | ||
sendArticles() | ||
}) | ||
} else { | ||
getArticleCount(function() { | ||
console.log('You now have some data in your database!') | ||
}) | ||
} | ||
} | ||
|
||
function getArticleCount(next) { | ||
request({ url: cred.site_url + '/articles', method: 'GET', json:true }, function(err, res, body) { | ||
if (err) { | ||
console.error("There was an error grabbing articles from YOUR server", err) | ||
process.exit(1) | ||
} | ||
console.log("Read " + body.articles.length + " articles from YOUR server") | ||
if (next) { | ||
next() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--compilers js:babel-core/register | ||
--require jsdom-global/register | ||
--recursive | ||
--colors | ||
--timeout 10000 | ||
--bail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "stub", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js", | ||
"watch": "nodemon index.js", | ||
"test": "mocha --opts mocha.opts ./src/*.spec.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.17.1", | ||
"cloudinary": "^1.8.0", | ||
"cookie-parser": "^1.4.3", | ||
"express": "^4.13.4", | ||
"express-session": "^1.15.2", | ||
"md5": "^2.2.1", | ||
"mongoose": "^4.9.5", | ||
"multer": "^1.3.0", | ||
"passport": "^0.3.2", | ||
"passport-facebook": "^2.1.1", | ||
"redis": "^2.7.1", | ||
"request": "^2.81.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"dotenv": "^4.0.0", | ||
"isomorphic-fetch": "^2.2.1", | ||
"mocha": "^3.1.2", | ||
"nodemon": "^1.11.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
const Article = require('./model.js').Article | ||
const Comment = require('./model.js').Comments | ||
const md5 = require('md5') | ||
|
||
//add article part | ||
const addArticle = (req, res) => { | ||
if(!req.body.text) { | ||
res.status(400).send('Add articles error') | ||
return | ||
} | ||
const articleObj = new Article({author: req.username, img: null, date: new Date(), text: req.body.text, comments: []}) | ||
new Article(articleObj).save(function(err, article){ | ||
if(err) throw err | ||
else { | ||
res.status(200).send({articles: [article]}) | ||
} | ||
}) | ||
} | ||
//get a article | ||
const getArticles = (req, res) => { | ||
if(req.params.id){ | ||
Article.find({_id:req.params.id}).exec(function(err, article){ | ||
if(article === null || article.length === 0) { | ||
res.status(401).send('Get articles error') | ||
return | ||
} | ||
const articleObj = article[0] | ||
res.status(200).send({articles: articleObj}) | ||
}) | ||
} else { | ||
Article.find({}).exec(function(err, articles){ | ||
res.status(200).send( { articles: articles}) | ||
}) | ||
} | ||
} | ||
//put an article | ||
const putArticle = (req, res) => { | ||
if(!req.params.id) { | ||
res.status(400).send('Put articles error: no id') | ||
return | ||
} | ||
Article.find({_id:req.params.id}).exec(function(err, article){ | ||
if(article === null || article.length === 0) { | ||
res.status(401).send('Put articles error: no specified article') | ||
return | ||
} | ||
if(req.body.commentId == "-1") { | ||
console.log("-11-1") | ||
const commentId = md5(req.username + new Date().getTime()) | ||
const commentObj = new Comment({commentId: commentId, author: req.username, date: new Date(), text: req.body.text}) | ||
new Comment(commentObj).save(function(err, comment){ | ||
if(err) throw err | ||
}) | ||
Article.findByIdAndUpdate(req.params.id, {$addToSet: {comments:commentObj}}, {upsert:true, new:true}, function(err, article){}) | ||
Article.find({_id:req.params.id}).exec(function(err, article){ | ||
res.status(200).send({articles: article}) | ||
}) | ||
} else if(req.body.commentId) { | ||
Comment.find({commentId: req.body.commentId}).exec(function(err, comments){ | ||
if(comments === null || comments.length === 0) { | ||
res.status(401).send('Put articles error: no specified comment') | ||
return | ||
} | ||
|
||
if(comments[0].author === req.username) { | ||
Comment.update({commentId: req.body.commentId}, { $set: {text:req.body.text}}, {new:true}, function(err, comments){}) | ||
Article.update({_id:req.params.id, 'comments.commentId':req.body.commentId}, {$set:{'comments.$.text':req.body.text}}, {new: true}, function(err, articles){}) | ||
Article.find({_id:req.params.id}).exec(function(err, article){ | ||
res.status(200).send({articles: article}) | ||
}) | ||
} | ||
}) | ||
} else { | ||
if(article[0].author === req.username) { | ||
Article.findByIdAndUpdate(req.params.id, {$set: {text:req.body.text}}, {new: true}, function(err, article){ | ||
res.status(200).send({articles: article}) | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
module.exports = (app) => { | ||
app.get('/articles/:id*?', getArticles) | ||
app.post('/article', addArticle) | ||
app.put('/articles/:id', putArticle) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const expect = require('chai').expect | ||
const fetch = require('isomorphic-fetch') | ||
|
||
const resource = (method, endpoint, payload) => { | ||
const url = `http://localhost:3000/${endpoint}` | ||
const options = { method, headers: { 'Content-Type': 'application/json' }} | ||
if (payload) options.body = JSON.stringify(payload) | ||
return fetch(url, options).then(r => { | ||
if (r.status == 200) { | ||
return r.json() | ||
} else { | ||
const msg = `ERROR ${method} ${endpoint} returned ${r.status}` | ||
console.error(msg) | ||
throw new Error(msg) | ||
} | ||
}) | ||
} | ||
|
||
|
||
describe('Validate POST /article', ()=>{ | ||
|
||
let nextid = 0; | ||
it('should GET articles', done=>{ | ||
resource('GET','articles') | ||
.then(body=>{ | ||
expect(body.nextid).to.be.at.least(4) | ||
nextid = body.nextid | ||
}) | ||
.then(done).catch(done) | ||
}) | ||
|
||
it('should POST a new articles', done=>{ | ||
const text = 'just for test' | ||
resource('POST','article', {text}) | ||
.then(body=>{ | ||
expect(body.articles[0].text).to.eql(text) | ||
}) | ||
.then(done).catch(done) | ||
}) | ||
|
||
it('should GET articles with length + 1', done=>{ | ||
resource('GET', 'articles') | ||
.then(body=>{ | ||
expect(body.nextid).to.eql(nextid+1) | ||
}) | ||
.then(done).catch(done) | ||
}) | ||
}) |
Oops, something went wrong.