Skip to content

Commit

Permalink
Merge pull request #189 from Cambalab/161-replace-server-author-servi…
Browse files Browse the repository at this point in the history
…ces-specific-data-generators-with-fake-data-generator

161 replace server author services specific data generators with fake data generator
  • Loading branch information
sgobotta authored Nov 17, 2019
2 parents 6b7a938 + b185004 commit a5831ec
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 201 deletions.
13 changes: 13 additions & 0 deletions utils/server-test/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 utils/server-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.2",
"fake-data-generator": "^0.1.10",
"jsonwebtoken": "^8.5.0"
},
"author": "",
Expand Down
38 changes: 19 additions & 19 deletions utils/server-test/server.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/* eslint-disable import/no-extraneous-dependencies */
const express = require("express");
const bodyParser = require("body-parser");
const express = require('express')
const bodyParser = require('body-parser')

const createMagazinesService = require("./services/magazines");
const createArticlesService = require('./services/articles');
const createAuthorsService = require('./services/authors');
const createAuthService= require('./services/auth');
const createMagazinesService = require('./services/magazines')
const createArticlesService = require('./services/articles')
const createAuthorsService = require('./services/authors')
const createAuthService = require('./services/auth')

/* eslint-enable */

var cors = require("cors");
const app = express();
var cors = require('cors')
const app = express()

app.use(cors());
app.options("*", cors());
app.use(cors())
app.options('*', cors())

app.use(express.static(__dirname));
app.use(express.static(__dirname))

app.use(bodyParser.json());
app.use(bodyParser.json())

createArticlesService(app);
createMagazinesService(app);
createAuthService(app);
createAuthorsService(app);
createArticlesService(app)
createMagazinesService(app)
createAuthService(app)
createAuthorsService(app)

const port = process.env.PORT || 8080;
const port = process.env.PORT || 8080

module.exports = app.listen(port, () => {
/* eslint-disable no-console */
console.log(`Server listening on http://localhost:${port}, Ctrl+C to stop`);
console.log(`Server listening on http://localhost:${port}, Ctrl+C to stop`)
/* eslint-enable no-console */
});
})
97 changes: 48 additions & 49 deletions utils/server-test/services/articles.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,91 @@
const Ipsum = require("bavaria-ipsum");
const Ipsum = require('bavaria-ipsum')

module.exports = function (app) {

const ipsum = new Ipsum();
module.exports = function(app) {
const ipsum = new Ipsum()

const articles = [
{
id: 1,
title: ipsum.generateSentence(),
content: ipsum.generateParagraph()
content: ipsum.generateParagraph(),
},
{
id: 2,
title: ipsum.generateSentence(),
content: ipsum.generateParagraph()
content: ipsum.generateParagraph(),
},
{
id: 3,
title: ipsum.generateSentence(),
content: ipsum.generateParagraph()
}
];
content: ipsum.generateParagraph(),
},
]

app.get("/api/articles", (req, res) => {
res.json(articles);
});
app.get('/api/articles', (req, res) => {
res.json(articles)
})

app.get("/api/articles/:id", (req, res) => {
const article = articles.find(a => a.id.toString() === req.params.id);
const index = articles.indexOf(article);
app.get('/api/articles/:id', (req, res) => {
const article = articles.find(a => a.id.toString() === req.params.id)
const index = articles.indexOf(article)

res.json(articles[index]);
});
res.json(articles[index])
})

app.patch("/api/articles/:id", (req, res) => {
const { body } = req;
const article = articles.find(a => a.id.toString() === req.params.id);
const index = articles.indexOf(article);
app.patch('/api/articles/:id', (req, res) => {
const { body } = req
const article = articles.find(a => a.id.toString() === req.params.id)
const index = articles.indexOf(article)

if (index >= 0) {
article.title = body.title;
article.content = body.content;
articles[index] = article;
article.title = body.title
article.content = body.content
articles[index] = article
}

res.json(article);
});
res.json(article)
})

app.put("/api/articles/:id", (req, res) => {
const { body } = req;
const article = articles.find(a => a.id.toString() === req.params.id);
const index = articles.indexOf(article);
app.put('/api/articles/:id', (req, res) => {
const { body } = req
const article = articles.find(a => a.id.toString() === req.params.id)
const index = articles.indexOf(article)

if (index >= 0) {
article.title = body.title;
article.content = body.content;
articles[index] = article;
article.title = body.title
article.content = body.content
articles[index] = article
}

res.json(article);
});
res.json(article)
})

app.delete("/api/articles/:id", (req, res) => {
const article = articles.find(a => a.id.toString() === req.params.id);
const index = articles.indexOf(article);
app.delete('/api/articles/:id', (req, res) => {
const article = articles.find(a => a.id.toString() === req.params.id)
const index = articles.indexOf(article)

if (index >= 0) articles.splice(index, 1);
if (index >= 0) articles.splice(index, 1)

res.status(202).send();
});
res.status(202).send()
})

app.post("/api/articles", (req, res) => {
app.post('/api/articles', (req, res) => {
let id
if (!articles.length) {
id = 0
} else {
id = articles[articles.length - 1].id + 1;
id = articles[articles.length - 1].id + 1
}
const { body } = req;
const { body } = req

const article = {
id,
title: body.title,
content: body.content
};
content: body.content,
}

articles.push(article);
articles.push(article)

res.status(201).send(article);
});
res.status(201).send(article)
})
}
19 changes: 10 additions & 9 deletions utils/server-test/services/auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken');
const jwt = require('jsonwebtoken')

const secret = 'mySecret'

module.exports = function(app) {

let whitelist = [
{
id: 123456,
Expand All @@ -19,17 +18,19 @@ module.exports = function(app) {
password: '$2b$08$ZRrJWppetUmKe5Zlc1Mtj.w51ZTmmx2M4YKUdS4QOBHXq2gzF/zyS', // bcrypt.hashSync('123456')
permissions: ['admin'],
token: '',
}
},
]

app.post('/api/auth', (req, res) => {
const user = whitelist.find(user => user.email === req.headers.username);
if (!user)
return res.status(401).send('Invalid user or password');
const isPasswordValid = bcrypt.compareSync(req.headers.password, user.password);
const user = whitelist.find(user => user.email === req.headers.username)
if (!user) return res.status(401).send('Invalid user or password')
const isPasswordValid = bcrypt.compareSync(
req.headers.password,
user.password
)
if (!isPasswordValid)
return res.status(401).send('Invalid user or password');
const token = jwt.sign({ id: user.id }, secret, { expiresIn: 3600 });
return res.status(401).send('Invalid user or password')
const token = jwt.sign({ id: user.id }, secret, { expiresIn: 3600 })
// Saves the token in the whitelist user array
whitelist = whitelist.map(_user => {
if (_user.id === user.id) {
Expand Down
Loading

0 comments on commit a5831ec

Please sign in to comment.