1
0
Эх сурвалжийг харах

Feature: add resource to list all accounts

febbweiss 10 жил өмнө
parent
commit
a28ed5d302

+ 12 - 0
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;

+ 1 - 1
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');
     });
 };

+ 51 - 0
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

+ 77 - 0
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": "<p>application/json</p> "
+          },
+          {
+            "group": "Header",
+            "type": "String",
+            "optional": false,
+            "field": "Authorization",
+            "description": "<p>The valid JWT token provided by the {post} /users/login resource</p> "
+          }
+        ]
+      },
+      "examples": [
+        {
+          "title": "Authorization header example:",
+          "content": "\"Authorization\": \"JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ\"",
+          "type": "string"
+        }
+      ]
+    },
+    "error": {
+      "fields": {
+        "401": [
+          {
+            "group": "401",
+            "type": "<p>json</p> ",
+            "optional": false,
+            "field": "AuthenticationFailed",
+            "description": "<p>The user can't be found.</p> "
+          }
+        ]
+      },
+      "examples": [
+        {
+          "title": "AuthenticationFailed:",
+          "content": "HTTP/1.1 401 Not Found\n{\n  \"message\": \"Authentication failed\"\n}",
+          "type": "json"
+        }
+      ]
+    },
+    "success": {
+      "fields": {
+        "200": [
+          {
+            "group": "200",
+            "type": "<p>json</p> ",
+            "optional": false,
+            "field": "accounts",
+            "description": "<p>List of all accounts and their (sub)categories.</p> "
+          }
+        ]
+      },
+      "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",

+ 77 - 0
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": "<p>application/json</p> "
+          },
+          {
+            "group": "Header",
+            "type": "String",
+            "optional": false,
+            "field": "Authorization",
+            "description": "<p>The valid JWT token provided by the {post} /users/login resource</p> "
+          }
+        ]
+      },
+      "examples": [
+        {
+          "title": "Authorization header example:",
+          "content": "\"Authorization\": \"JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTVlNmU0ZTAwNTIzMGY0OTI3MWM3MDc4IiwiaWF0IjoxNDQxMTk1MjMyfQ.eWh9nuXVVSVDKKCmTMDoc9FBU55-KgkiOJH1hrdQRTQ\"",
+          "type": "string"
+        }
+      ]
+    },
+    "error": {
+      "fields": {
+        "401": [
+          {
+            "group": "401",
+            "type": "<p>json</p> ",
+            "optional": false,
+            "field": "AuthenticationFailed",
+            "description": "<p>The user can't be found.</p> "
+          }
+        ]
+      },
+      "examples": [
+        {
+          "title": "AuthenticationFailed:",
+          "content": "HTTP/1.1 401 Not Found\n{\n  \"message\": \"Authentication failed\"\n}",
+          "type": "json"
+        }
+      ]
+    },
+    "success": {
+      "fields": {
+        "200": [
+          {
+            "group": "200",
+            "type": "<p>json</p> ",
+            "optional": false,
+            "field": "accounts",
+            "description": "<p>List of all accounts and their (sub)categories.</p> "
+          }
+        ]
+      },
+      "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",

+ 1 - 1
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"
   }

+ 1 - 1
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"
   }

+ 35 - 0
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) {