Skip to content

Commit

Permalink
restify server
Browse files Browse the repository at this point in the history
  • Loading branch information
sixertoy committed Aug 1, 2015
1 parent dd9d65e commit 717671b
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 217 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"satellizer": "~0.11.2",
"bootstrap": "~3.3.5",
"normalize.css": "~3.0.3",
"lodash": "~3.10.0"
"lodash": "~3.10.0",
"md5": "~0.3.0"
}
}
2 changes: 1 addition & 1 deletion grunt/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
flatten: true,
cwd: './bower_components/',
dest: './src/html/js/lib/',
src: ['ace-builds/src-min-noconflict/ace.js', 'ace-builds/src-min-noconflict//mode-markdown.js', 'angular-sanitize/angular-sanitize.min.js', 'angular/angular.min.js', 'markdown-it/dist/markdown-it.min.js', 'angular-ui-ace/ui-ace.min.js', 'angular-bootstrap/ui-bootstrap-tpls.js', 'lodash/lodash.min.js']
src: ['ace-builds/src-min-noconflict/ace.js', 'ace-builds/src-min-noconflict//mode-markdown.js', 'angular-sanitize/angular-sanitize.min.js', 'angular/angular.min.js', 'markdown-it/dist/markdown-it.min.js', 'angular-ui-ace/ui-ace.min.js', 'angular-bootstrap/ui-bootstrap-tpls.js', 'lodash/lodash.min.js', 'md5/build/md5.min.js']
},
bower_css: {
expand: true,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"description": "Project generated with Yeoman generator-gruntproject v0.1.20",
"main": "index.js",
"scripts": {
"start": "node index",
"clean_modules": "rm -rf node_modules",
"pretest": "sh scripts/pretest",
"test": "istanbul cover ./scripts/jasmine_node -x **/*.spec.js --captureExceptions spec/src"
"test": "istanbul cover ./scripts/jasmine_node -x **/*.spec.js --captureExceptions spec/src",
"prestart": "grunt build",
"start": "node index"
},
"engines": {
"node": ">=0.12.0"
Expand Down
6 changes: 3 additions & 3 deletions spec/fixtures/nedb/project.nedb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"path":"path/to/toto","name":"toto","project_id":"41636dabad50c4de6623a369313f8df5","_id":"DxgJFTp863zLVSYa"}
{"$$indexCreated":{"unique":true,"fieldName":"project_id"}}
{"path":"path/to/toto","name":"toto","project_id":"41636dabad50c4de6623a369313f8df5","_id":"VBlNwaGI1k2d6jLe"}
{"$$deleted":true,"_id":"VBlNwaGI1k2d6jLe"}
{"path":"d:\\npm\\readmepad\\src\\docs","name":"docs","project_id":"0c7b9011b213e91025335a16f70b1a1a","_id":"mDpNii5CMZCLPK60"}
{"$$deleted":true,"_id":"DxgJFTp863zLVSYa"}
{"path":"d:\\npm\\readmepad\\src\\docs","name":"docs","project_id":"0c7b9011b213e91025335a16f70b1a1a","_id":"gwWRBUWbJdclfenY"}
4 changes: 2 additions & 2 deletions spec/fixtures/nedb/store.nedb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"$$indexCreated":{"unique":true,"fieldName":"project_id"}}
{"path":"path/to/toto","name":"toto","project_id":"41636dabad50c4de6623a369313f8df5","_id":"vrFNzG5TOXXSpJAu"}
{"$$deleted":true,"_id":"vrFNzG5TOXXSpJAu"}
{"path":"path/to/toto","name":"toto","project_id":"41636dabad50c4de6623a369313f8df5","_id":"aWdoBrnoyZrRdGlp"}
{"$$deleted":true,"_id":"aWdoBrnoyZrRdGlp"}
140 changes: 46 additions & 94 deletions spec/src/server/controllers/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,91 +39,52 @@
describe('controllers/project', function () {

describe('[GET] /project/open', function () {
it('fail no project_path param', function (done) {
it('fails', function (done) {
request(app)
.get('/project/open')
.post('/project/open')
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.error.status).toEqual(400);
expect(res.text).toEqual('invalid arguments');
expect(res.error.text).toEqual('invalid arguments');
done();
});
});
it('fail empty project_path param', function (done) {
var params = {
project_path: ''
};
request(app)
.get('/project/open')
.send(params)
.expect('Content-Type', /json/)
.expect(400)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.error.status).toEqual(400);
expect(res.text).toEqual('invalid arguments');
expect(res.error.text).toEqual('invalid arguments');
expect(res.status).toEqual(404); // Cannot POST /project/open
done();
});
});
it('fails untrimmed empty project_path param', function (done) {
var params = {
project_path: ' '
};
it('fail no project_id param', function (done) {
request(app)
.get('/project/open')
.send(params)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.error.status).toEqual(400);
expect(res.text).toEqual('invalid arguments');
expect(res.error.text).toEqual('invalid arguments');
expect(res.status).toEqual(404); // Cannot GET /project/open
done();
});
});
it('fail object project_path param', function (done) {
var params = {
project_path: {}
};
it('fail code 404', function (done) {
var project_id = md5(' ');
request(app)
.get('/project/open')
.send(params)
.get('/project/open/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.error.status).toEqual(400);
expect(res.text).toEqual('invalid arguments');
expect(res.error.text).toEqual('invalid arguments');
expect(res.status).toEqual(204);
done();
});
});
it('fail project not exists', function (done) {
var params = {
project_path: 'path/to/non/exists'
};
it('fail code 404', function (done) {
var project_id = md5('path/to/non/exists');
request(app)
.get('/project/open')
.send(params)
.get('/project/open/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(404);
expect(res.error.status).toEqual(404);
expect(res.text).toEqual('unable to find project');
expect(res.error.text).toEqual('unable to find project');
expect(res.status).toEqual(204);
done();
});
});
});

describe('[DELETE] /project/delete', function () {
it('fails', function (done) {
var params = {
project_path: 'path/to/toto'
};
request(app)
.post('/project/delete')
.send()
Expand All @@ -132,40 +93,43 @@
done();
});
});
it('returns status code 400', function (done) {
var params = {
project_path: ''
};
it('fail code 404', function (done) {
request(app)
.delete('/project/delete')
.send(params)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.text).toEqual('invalid arguments');
expect(res.statusCode).toEqual(404);
done();
});
});
it('returns status code 200', function (done) {
var params = {
project_path: 'path/to/toto'
};
it('success code 204', function (done) {
var project_id = md5('project/non/exists');
request(app)
.delete('/project/delete')
.send(params)
.delete('/project/delete/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(204);
done();
});
});
it('success code 200', function (done) {
var project_id = md5('path/to/toto');
request(app)
.delete('/project/delete/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(200);
done();
});
});
it('returns status code 204', function (done) {
var params = {
project_path: 'path/to/toto'
};
it('success code 204', function (done) {
var project_id = md5('path/to/toto');
request(app)
.delete('/project/delete')
.send(params)
.delete('/project/delete/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(204);
Expand Down Expand Up @@ -200,7 +164,6 @@
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.text).toEqual('invalid arguments');
done();
});
});
Expand All @@ -214,7 +177,6 @@
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(400);
expect(res.text).toEqual('invalid arguments');
done();
});
});
Expand All @@ -228,7 +190,6 @@
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(403);
expect(res.text.indexOf('ENOENT') !== -1).toEqual(true);
done();
});
});
Expand All @@ -239,7 +200,7 @@
},
doc = {
name: 'docs',
path: path.join(cwd, 'src', 'docs')
path: fullpath
};
request(app)
.post('/project/create')
Expand All @@ -260,7 +221,7 @@
},
doc = {
name: 'docs',
path: path.join(cwd, 'src', 'docs')
path: fullpath
};
request(app)
.post('/project/create')
Expand Down Expand Up @@ -308,23 +269,14 @@
});

describe('[GET] /project/open', function () {
it('fails', function (done) {
request(app)
.post('/project/delete')
.send()
.end(function (err, res) {
expect(res.statusCode).toEqual(404);
done();
});
});
it('returns a files tree', function (done) {
var params = {
project_path: path.join(cwd, 'src', 'docs')
};
scandir(params.project_path).then(function (data) {
var project_id,
project_path = path.join(cwd, 'src', 'docs');
scandir(project_path).then(function (data) {
project_id = md5(project_path);
request(app)
.get('/project/open')
.send(params)
.get('/project/open/' + project_id)
.send()
.expect('Content-Type', /json/)
.end(function (err, res) {
expect(res.statusCode).toEqual(200);
Expand Down
46 changes: 16 additions & 30 deletions spec/src/server/models/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
});
it('resolve with false project not found', function (done) {
dbname = 'store';
project = 'toto/project';
storeModel.findOneProject(dbname, project).then(function (data) {
var project_id = md5('toto/project');
storeModel.findOneProject(dbname, project_id).then(function (data) {
expect(data).toEqual(false);
done();
}, function (err) {});
Expand All @@ -83,18 +83,19 @@
});
it('reject needs more argument', function (done) {
dbname = 'name';
storeModel.createProject(dbname, 'path/project').then(function () {}, function (err) {
storeModel.createProject(dbname, md5('path/project')).then(function () {}, function (err) {
expect(err.message).toEqual('needs 3 arguments at least');
done();
});
});
it('create a new project', function (done) {
var name = 'toto',
var project_id,
name = 'toto',
fullpath = 'path/to/toto';
dbname = 'store';
storeModel.createProject(dbname, name, fullpath).then(function (doc) {
fullpath = md5(fullpath);
expect(doc.project_id).toEqual(fullpath);
project_id = md5(fullpath);
expect(doc.project_id).toEqual(project_id);
done();
}, function (err) {});
});
Expand All @@ -112,55 +113,40 @@
describe('findOneProject', function () {
it('returns a document', function (done) {
dbname = 'store';
project = 'path/to/toto';
storeModel.findOneProject(dbname, project).then(function (doc) {
expect(md5(project)).toEqual(doc.project_id);
var project_id = md5('path/to/toto');
storeModel.findOneProject(dbname, project_id).then(function (doc) {
expect(project_id).toEqual(doc.project_id);
done();
}, function (err) {});
});
});

describe('deleteProject', function () {
it('reject more argument', function (done) {
dbname = 'store';
storeModel.deleteProject(dbname).then(function () {}, function (err) {
expect(err.message).toEqual('needs 2 argument at least');
done();
});
});
it('reject more argument', function (done) {
dbname = 'store';
storeModel.deleteProject(dbname, {}).then(function () {}, function (err) {
expect(err.message).toEqual('needs 2 argument at least');
done();
});
});
it('reject more argument', function (done) {
dbname = 'store';
storeModel.deleteProject(dbname, '').then(function () {}, function (err) {
storeModel.deleteProject().then(function () {}, function (err) {
expect(err.message).toEqual('needs 2 argument at least');
done();
});
});
it('reject more argument', function (done) {
dbname = 'store';
storeModel.deleteProject(dbname, ' ').then(function () {}, function (err) {
storeModel.deleteProject(dbname).then(function () {}, function (err) {
expect(err.message).toEqual('needs 2 argument at least');
done();
});
});
it('resolve with value 1', function (done) {
dbname = 'store';
project = 'path/to/toto';
storeModel.deleteProject(dbname, project).then(function (num) {
var project_id = md5('path/to/toto');
storeModel.deleteProject(dbname, project_id).then(function (num) {
expect(num).toEqual(1);
done();
}, function () {});
});
it('resolve with value 0 if document not exists', function (done) {
dbname = 'store';
project = 'path/to/toto';
storeModel.deleteProject(dbname, project).then(function (num) {
var project_id = md5('path/to/toto');
storeModel.deleteProject(dbname, project_id).then(function (num) {
expect(num).toEqual(0);
done();
}, function (err) {
Expand Down
Loading

0 comments on commit 717671b

Please sign in to comment.