Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Odstraneni mocku pro testovani stranek

  • Loading branch information...
commit 46185cb56a2860fed4481aec0c1157041ca6d4e5 1 parent 3465087
Jakub Mrozek authored
4 .travis.yml
View
@@ -1,3 +1,5 @@
language: node_js
node_js:
- - 0.8
+ - 0.8
+services:
+ - mongodb
7 app.js
View
@@ -1,6 +1,5 @@
var express = require('express')
, resource = require('express-resource')
- , mongoose = require('mongoose')
, config = require('./config')
, app = express();
@@ -14,13 +13,7 @@ var PageController = require('./app/controllers/PageController');
//modely
var Page = require('./app/models/Page');
-//middleware
-var error = require('./app/middleware/error');
-
//API stranky
app.resource('pages', PageController, {base: '/api/', load: Page.findOneByUrl});
-
-//zpracovani chyby
-app.use(error());
module.exports = app;
32 app/controllers/PageController.js
View
@@ -4,21 +4,11 @@ var Page = require('../models/Page');
* GET /pages
*/
exports.index = function(req, res, next){
- var fields = {};
-
- //povolit jen specifikovana pole
- if (req.query.fields) {
- var queryFields = req.query.fields.split(',');
- queryFields.forEach(function(field) {
- if (field == 'title' || field == 'url' || field == 'content') {
- fields[field] = 1;
- } else {
- return next(400);
- }
- })
+ if (!Page.inSchema(req.zdrojak.fields)) {
+ return next(400);
}
- Page.find({}, fields, function(err, docs) {
+ Page.find({}, req.zdrojak.fields, function(err, docs) {
if (err) return next(err);
res.json(docs);
});
@@ -37,13 +27,6 @@ exports.show = function(req, res, next){
* @todo
*/
exports.create = function(req, res, next){
- req.page.title = req.body.title || '';
- req.page.content = req.body.content || '';
-
- if (!req.page.title || !req.page.content) {
- return next(400);
- }
-
var page = new Page();
page.title = req.body.title;
page.url = req.body.title;
@@ -59,13 +42,8 @@ exports.create = function(req, res, next){
*
*/
exports.update = function(req, res, next){
- req.page.title = req.body.title || '';
- req.page.content = req.body.content || '';
-
- if (!req.page.title || !req.page.content) {
- return next(400);
- }
-
+ req.page.title = req.body.title;
+ req.page.content = req.body.content;
req.page.save(function(err, doc) {
if (err) return next(err);
res.json(doc);
12 app/middleware/error.js
View
@@ -1,9 +1,13 @@
module.exports = function() {
return function(err, req, res, next){
- if (typeof(err) == 'number') {
- res.send(err);
- } else {
- next(err);
+ if (typeof(err) === 'number') {
+ return res.send(err);
}
+
+ if (err.name && err.name == 'ValidationError') {
+ return res.send(400);
+ }
+
+ next(err);
}
};
12 app/middleware/fields.js
View
@@ -0,0 +1,12 @@
+module.exports = function() {
+ return function(req, res, next){
+ if (!req.zdrojak) req.zdrojak = {};
+ req.zdrojak.fields = {};
+ if (req.query.fields) {
+ req.query.fields.split(',').forEach(function(field){
+ req.zdrojak.fields[field] = 1;
+ });
+ }
+ next();
+ }
+};
22 app/models/Page.js
View
@@ -1,15 +1,25 @@
var mongoose = require('mongoose');
-//schema
-var Schema = new mongoose.Schema({
- title: String
- , url: String
- , content: String
-});
+var fields = {
+ title: {type: String, required: true}
+ , url: {type: String, required: true}
+ , content: {type: String, required: true}
+};
+
+var Schema = new mongoose.Schema(fields);
Schema.statics.findOneByUrl = function(url, cb) {
Model.findOne({url: url}, cb);
};
+Schema.statics.inSchema = function(obj) {
+ for (var field in obj) {
+ if (typeof fields[field] === 'undefined') {
+ return false;
+ }
+ }
+ return true;
+};
+
var Model = module.exports = mongoose.model('Page', Schema);
13 config.js
View
@@ -6,7 +6,9 @@ exports.configure = function(app) {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(process.cwd() + '/public'));
+ app.use(require('./app/middleware/fields')());
app.use(app.router);
+ app.use(require('./app/middleware/error')());
});
app.configure('development', function(){
app.set('db uri', 'mongodb://localhost/zdrojak');
@@ -15,15 +17,12 @@ exports.configure = function(app) {
app.set('db uri', 'mongodb://user:pass@host:port/dbname');
});
app.configure('test', function(){
- //app.set('db uri', 'mongodb://localhost/zdrojaktest');
+ app.set('db uri', 'mongodb://localhost/zdrojaktest');
});
}
exports.connect = function(app) {
- var uri = app.get('db uri');
- if (uri) {
- mongoose.connect(uri, function(err) {
- if(err) console.log(err);
- });
- }
+ mongoose.connect(app.get('db uri'), function(err) {
+ if(err) console.log(err);
+ });
}
3  package.json
View
@@ -15,7 +15,8 @@
"mocha": "1.6.x",
"supertest": "0.4.x",
"should": "1.2.x",
- "horaa": "0.1.x"
+ "horaa": "0.1.x",
+ "mongodb": "1.1.1"
},
"repository": {
"type": "git",
143 test/api/pages.js
View
@@ -1,80 +1,70 @@
var request = require('supertest')
- , horaa = require('horaa')
- , cwd = process.cwd()
- , app = require(cwd + '/app')
- , Page = horaa(cwd + '/app/models/Page');
+ , app = require(process.cwd() + '/app')
+ , Page = require(process.cwd() + '/app/models/Page');
-//vzorova testovaci data
-var testData = {
- all: [
- {'_id': 'abc', 'title': 'titulek', 'url': 'titulek', 'content': 'lorem ipsum'},
- {'_id': 'abd', 'title': 'titule2', 'url': 'titule2', 'content': 'lorem ipsum'}
- ],
- columns: [
- {'_id': 'abc', 'url': 'titulek'},
- ]
+var data = {
+ title: 'Stranka 1',
+ url: 'stranka-1',
+ content: 'lorem ipsum'
};
-
describe('API pages', function(){
+ beforeEach(function(done) {
+ Page.remove({}, function(err){
+ if (err) throw err;
+ var page = new Page();
+ for (var field in data) {
+ page[field] = data[field];
+ }
+ page.save(function(err) {
+ if (err) throw err;
+ done();
+ });
+ });
+ });
+
describe('GET /api/pages', function(){
-
it('vrati seznam vsech polozek v databazi', function(done){
- Page.hijack('find', function(cond, cols, cb){
- cb(null, testData.all);
- });
- request(app).get('/api/pages')
+ request(app)
+ .get('/api/pages')
.end(function(err, res) {
- res.body.should.eql(testData.all);
+ res.body.length.should.eql(1);
+ res.body[0].should.include(data);
done();
});
});
-
it('vrati jen urcene sloupce', function(done){
- Page.hijack('find', function(cond, cols, cb){
- cols.should.eql({url:1});
- cb(null, testData.columns);
- });
- request(app).get('/api/pages?fields=url')
+ request(app)
+ .get('/api/pages?fields=url')
.end(function(err, res) {
- res.body.should.eql(testData.columns);
+ res.body[0].should.not.include({
+ title: 'Stranka 1',
+ content: 'lorem ipsum'
+ });
done();
});
});
-
it('vrati kod 400 pri zadani pole, ktere neexistuje v databazi', function(done){
- Page.hijack('find', function(cond, cols, cb){
- cb(null, []);
- });
- request(app).get('/api/pages?fields=abc').expect(400, done);
+ request(app)
+ .get('/api/pages?fields=abc')
+ .expect(400, done);
})
})
-
describe('GET /api/pages/page', function(){
-
- afterEach(function(){
- Page.restore('findOne');
- });
-
it('vrati detail jedne stranky', function(done){
- Page.hijack('findOne', function(cond, cb){
- cond.should.eql({url: 'abc'});
- cb(null, testData.all[0]);
- });
- request(app).get('/api/pages/abc')
+ request(app)
+ .get('/api/pages/stranka-1')
.end(function(err, res) {
- res.body.should.eql(testData.all[0]);
+ res.body.should.include(data);
done();
});
});
-
it('vrati 404, pokud stranka neexistuje', function(done){
- Page.hijack('findOne', function(cond, cb){
- cb();
- });
- request(app).get('/api/pages/neexistuje').expect(404, done);
+ request(app)
+ .get('/api/pages/neexistuje')
+ .expect(404, done);
});
});
@@ -83,65 +73,34 @@ describe('API pages', function(){
});
describe('PUT /api/pages/page', function(){
- afterEach(function(){
- Page.restore('findOne');
- });
-
it('upravi obsah stranky', function(done){
- Page.hijack('findOne', function(cond, cb){
- var page = testData.all[0];
- page.save = function(cb) {
- page.title.should.eql('titulek ABC');
- page.content.should.eql('lorem ipsum set dolorem');
- cb(null, testData.all[0]);
- }
- cb(null, page);
- });
- request(app).put('/api/pages/abc')
+ request(app)
+ .put('/api/pages/stranka-1')
.send({title: 'titulek ABC', content: 'lorem ipsum set dolorem'})
.expect(200, done);
});
-
it('vrati 400, pokud chybi titulek nebo obsah', function(done){
- Page.hijack('findOne', function(cond, cb){
- cb(null, testData.all[0]);
- });
- request(app).put('/api/pages/abc')
+ request(app).put('/api/pages/stranka-1')
.expect(400, done);
});
-
it('vrati 404, pokud stranka neexistuje', function(done){
- Page.hijack('findOne', function(cond, cb){
- cb();
- });
- request(app).put('/api/pages/neexistuje').expect(404, done);
+ request(app)
+ .put('/api/pages/neexistuje')
+ .expect(404, done);
});
});
describe('DELETE /api/pages/page', function(){
-
- afterEach(function(){
- Page.restore('findOne');
- });
-
it('smaze stranku z databaze', function(done){
- Page.hijack('findOne', function(cond, cb){
- var page = testData.all[0];
- page.remove = function(cb) {
- cb(null, testData.all[0]);
- }
- cb(null, page);
- });
- request(app).del('/api/pages/abc').expect(200, done);
+ request(app)
+ .del('/api/pages/stranka-1')
+ .expect(200, done);
});
-
it('vrati 404, pokud stranka neexistuje', function(done){
- Page.hijack('findOne', function(cond, cb){
- cb();
- });
- request(app).del('/api/pages/neexistuje').expect(404, done);
+ request(app)
+ .del('/api/pages/neexistuje')
+ .expect(404, done);
});
-
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.