From a28ed5d30272ddef252a93a6e51ddf39b32fb4bb Mon Sep 17 00:00:00 2001 From: febbweiss Date: Tue, 29 Sep 2015 13:16:14 +0000 Subject: [PATCH] Feature: add resource to list all accounts --- app/controllers/accounts.js | 12 ++++++ app/routes.js | 2 +- app/routes/accounts.js | 51 ++++++++++++++++++++++++ public/api_data.js | 77 +++++++++++++++++++++++++++++++++++++ public/api_data.json | 77 +++++++++++++++++++++++++++++++++++++ public/api_project.js | 2 +- public/api_project.json | 2 +- test/accounts.js | 35 +++++++++++++++++ 8 files changed, 255 insertions(+), 3 deletions(-) diff --git a/app/controllers/accounts.js b/app/controllers/accounts.js index e26dd3e..e9af1c5 100644 --- a/app/controllers/accounts.js +++ b/app/controllers/accounts.js @@ -117,6 +117,18 @@ module.exports = { }); }, + retrieve_accounts : function(request, response) { + Account + .find({user_id: request.user.id}) + .sort({name: 1}) + .exec(function(errors, accounts) { + if( errors ) { + Handler.errorHandler(errors, 400, response); + } + return response.json(accounts); + }); + }, + modify : function(request, response) { return check_account(request, response, function(error, account) { account.name = request.body.name; diff --git a/app/routes.js b/app/routes.js index 53272c1..4e62736 100644 --- a/app/routes.js +++ b/app/routes.js @@ -10,7 +10,7 @@ module.exports = function(app) { } }) - app.get('*', function(req, res) { + app.get('*', function(req, res, next) { res.sendfile('./public/index.html'); }); }; \ No newline at end of file diff --git a/app/routes/accounts.js b/app/routes/accounts.js index 6c8bc16..ad8ddf4 100644 --- a/app/routes/accounts.js +++ b/app/routes/accounts.js @@ -2,6 +2,57 @@ var passport = require('../security/passport'), AccountController = require('../controllers/accounts'); module.exports = function(app) { + /** + * @api {get} /accounts List accounts + * @apiVersion 1.0.0 + * @apiName Retrieve accounts + * @apiGroup Accounts + * + * @apiHeader {String} Content-Type application/json + * + * @apiHeader {String} Authorization The valid JWT token provided by the {post} /users/login resource + * @apiHeaderExample {string} Authorization header example: + * "Authorization": "JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ" + * @apiError (401) {json} AuthenticationFailed The user can't be found. + * @apiErrorExample AuthenticationFailed: + * HTTP/1.1 401 Not Found + * { + * "message": "Authentication failed" + * } + * + * @apiSuccess (200) {json} accounts List of all accounts and their (sub)categories. + * @apiSuccessExample Success-Response: + * HTTP/1.1 200 OK + * [{ + * "name": "Home", + * "reference": "1234567890", + * "user_id": "55e6e4e005230f49271c7078", + * "_id": "55e8218912c65a1730c34858", + * "created_at": "2015-09-03T10:31:37.889Z", + * "categories": [ + * { + * "key": "alimony_payments", + * "label": "Alimony Payments", + * "_id": "55e8218912c65a1730c34859", + * "sub_categories": [] + * }, + * { + * "key": "automobile_expenses", + * "label": "Automobile Expenses", + * "_id": "55e8218912c65a1730c3485a", + * "sub_categories": [ + * { + * "label": "Car Payment", + * "key": "car_payment", + * "_id": "55e8218912c65a1730c3485d" + * } + * ] + * } + * ] + * }] + */ + app.get('/api/accounts', passport.jwt, AccountController.retrieve_accounts); + /** * @api {post} /accounts Create account * @apiVersion 1.0.0 diff --git a/public/api_data.js b/public/api_data.js index 102bf8a..9dc433d 100644 --- a/public/api_data.js +++ b/public/api_data.js @@ -463,6 +463,83 @@ define({ "api": [ "filename": "app/routes/accounts.js", "groupTitle": "Accounts" }, + { + "type": "get", + "url": "/accounts", + "title": "List accounts", + "version": "1.0.0", + "name": "Retrieve_accounts", + "group": "Accounts", + "header": { + "fields": { + "Header": [ + { + "group": "Header", + "type": "String", + "optional": false, + "field": "Content-Type", + "description": "

application/json

" + }, + { + "group": "Header", + "type": "String", + "optional": false, + "field": "Authorization", + "description": "

The valid JWT token provided by the {post} /users/login resource

" + } + ] + }, + "examples": [ + { + "title": "Authorization header example:", + "content": "\"Authorization\": \"JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ\"", + "type": "string" + } + ] + }, + "error": { + "fields": { + "401": [ + { + "group": "401", + "type": "

json

", + "optional": false, + "field": "AuthenticationFailed", + "description": "

The user can't be found.

" + } + ] + }, + "examples": [ + { + "title": "AuthenticationFailed:", + "content": "HTTP/1.1 401 Not Found\n{\n \"message\": \"Authentication failed\"\n}", + "type": "json" + } + ] + }, + "success": { + "fields": { + "200": [ + { + "group": "200", + "type": "

json

", + "optional": false, + "field": "accounts", + "description": "

List of all accounts and their (sub)categories.

" + } + ] + }, + "examples": [ + { + "title": "Success-Response:", + "content": "HTTP/1.1 200 OK\n [{\n \"name\": \"Home\",\n \"reference\": \"1234567890\",\n \"user_id\": \"55e6e4e005230f49271c7078\",\n \"_id\": \"55e8218912c65a1730c34858\",\n \"created_at\": \"2015-09-03T10:31:37.889Z\",\n \"categories\": [\n {\n \"key\": \"alimony_payments\",\n \"label\": \"Alimony Payments\",\n \"_id\": \"55e8218912c65a1730c34859\",\n \"sub_categories\": []\n },\n {\n \"key\": \"automobile_expenses\",\n \"label\": \"Automobile Expenses\",\n \"_id\": \"55e8218912c65a1730c3485a\",\n \"sub_categories\": [\n {\n \"label\": \"Car Payment\",\n \"key\": \"car_payment\",\n \"_id\": \"55e8218912c65a1730c3485d\"\n }\n ]\n }\n ]\n }]", + "type": "json" + } + ] + }, + "filename": "app/routes/accounts.js", + "groupTitle": "Accounts" + }, { "type": "post", "url": "/accounts/:account_id/entries", diff --git a/public/api_data.json b/public/api_data.json index b563e2f..8e0227b 100644 --- a/public/api_data.json +++ b/public/api_data.json @@ -463,6 +463,83 @@ "filename": "app/routes/accounts.js", "groupTitle": "Accounts" }, + { + "type": "get", + "url": "/accounts", + "title": "List accounts", + "version": "1.0.0", + "name": "Retrieve_accounts", + "group": "Accounts", + "header": { + "fields": { + "Header": [ + { + "group": "Header", + "type": "String", + "optional": false, + "field": "Content-Type", + "description": "

application/json

" + }, + { + "group": "Header", + "type": "String", + "optional": false, + "field": "Authorization", + "description": "

The valid JWT token provided by the {post} /users/login resource

" + } + ] + }, + "examples": [ + { + "title": "Authorization header example:", + "content": "\"Authorization\": \"JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ\"", + "type": "string" + } + ] + }, + "error": { + "fields": { + "401": [ + { + "group": "401", + "type": "

json

", + "optional": false, + "field": "AuthenticationFailed", + "description": "

The user can't be found.

" + } + ] + }, + "examples": [ + { + "title": "AuthenticationFailed:", + "content": "HTTP/1.1 401 Not Found\n{\n \"message\": \"Authentication failed\"\n}", + "type": "json" + } + ] + }, + "success": { + "fields": { + "200": [ + { + "group": "200", + "type": "

json

", + "optional": false, + "field": "accounts", + "description": "

List of all accounts and their (sub)categories.

" + } + ] + }, + "examples": [ + { + "title": "Success-Response:", + "content": "HTTP/1.1 200 OK\n [{\n \"name\": \"Home\",\n \"reference\": \"1234567890\",\n \"user_id\": \"55e6e4e005230f49271c7078\",\n \"_id\": \"55e8218912c65a1730c34858\",\n \"created_at\": \"2015-09-03T10:31:37.889Z\",\n \"categories\": [\n {\n \"key\": \"alimony_payments\",\n \"label\": \"Alimony Payments\",\n \"_id\": \"55e8218912c65a1730c34859\",\n \"sub_categories\": []\n },\n {\n \"key\": \"automobile_expenses\",\n \"label\": \"Automobile Expenses\",\n \"_id\": \"55e8218912c65a1730c3485a\",\n \"sub_categories\": [\n {\n \"label\": \"Car Payment\",\n \"key\": \"car_payment\",\n \"_id\": \"55e8218912c65a1730c3485d\"\n }\n ]\n }\n ]\n }]", + "type": "json" + } + ] + }, + "filename": "app/routes/accounts.js", + "groupTitle": "Accounts" + }, { "type": "post", "url": "/accounts/:account_id/entries", diff --git a/public/api_project.js b/public/api_project.js index 6a319c3..aa23684 100644 --- a/public/api_project.js +++ b/public/api_project.js @@ -8,7 +8,7 @@ define({ "apidoc": "0.2.0", "generator": { "name": "apidoc", - "time": "2015-09-03T11:08:47.201Z", + "time": "2015-09-29T13:08:53.151Z", "url": "http://apidocjs.com", "version": "0.13.1" } diff --git a/public/api_project.json b/public/api_project.json index f6dcca1..3fe206e 100644 --- a/public/api_project.json +++ b/public/api_project.json @@ -8,7 +8,7 @@ "apidoc": "0.2.0", "generator": { "name": "apidoc", - "time": "2015-09-03T11:08:47.201Z", + "time": "2015-09-29T13:08:53.151Z", "url": "http://apidocjs.com", "version": "0.13.1" } diff --git a/test/accounts.js b/test/accounts.js index b3f2d57..169bd19 100644 --- a/test/accounts.js +++ b/test/accounts.js @@ -17,6 +17,41 @@ describe('API /accounts', function() { after( function() { globalServer.close(); }); + + describe('* List', function() { + it('should return the list of accounts', function(done) { + request(globalServer) + .get('/api/accounts') + .set('Authorization', 'JWT ' + token) + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', /json/) + .end( function(error, result) { + should.not.exist(error); + var accounts = result.body; + should.exist(accounts); + accounts.should.be.instanceof(Array).and.have.lengthOf(1); + var account = accounts[0]; + account._id.should.be.equal(account_id); + account.name.should.be.equal('Default'); + account.reference.should.be.equal('1234567890'); + done(); + }); + }); + + it('should fail to list accounts without valid token', function(done) { + request(globalServer) + .get('/api/accounts') + .set('Authorization', 'JWT fake') + .expect(401, done); + }); + + it('should fail to list accounts without token', function(done) { + request(globalServer) + .get('/api/accounts') + .expect(401, done); + }); + }); describe('* Creation', function() { it('should create an account', function(done) {