TwoToc code
[YouAndWeb_TwoToc] / server / api / user / index.spec.js
1 'use strict';
2
3 var proxyquire = require('proxyquire').noPreserveCache();
4
5 var userCtrlStub = {
6   index: 'userCtrl.index',
7   destroy: 'userCtrl.destroy',
8   me: 'userCtrl.me',
9   changePassword: 'userCtrl.changePassword',
10   show: 'userCtrl.show',
11   create: 'userCtrl.create'
12 };
13
14 var authServiceStub = {
15   isAuthenticated: function() {
16     return 'authService.isAuthenticated';
17   },
18   hasRole: function(role) {
19     return 'authService.hasRole.' + role;
20   }
21 };
22
23 var routerStub = {
24   get: sinon.spy(),
25   put: sinon.spy(),
26   post: sinon.spy(),
27   delete: sinon.spy()
28 };
29
30 // require the index with our stubbed out modules
31 var userIndex = proxyquire('./index', {
32   'express': {
33     Router: function() {
34       return routerStub;
35     }
36   },
37   './user.controller': userCtrlStub,
38   '../../auth/auth.service': authServiceStub
39 });
40
41 describe('User API Router:', function() {
42
43   it('should return an express router instance', function() {
44     userIndex.should.equal(routerStub);
45   });
46
47   describe('GET /api/users', function() {
48
49     it('should verify admin role and route to user.controller.index', function() {
50       routerStub.get
51         .withArgs('/', 'authService.hasRole.admin', 'userCtrl.index')
52         .should.have.been.calledOnce;
53     });
54
55   });
56
57   describe('DELETE /api/users/:id', function() {
58
59     it('should verify admin role and route to user.controller.destroy', function() {
60       routerStub.delete
61         .withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy')
62         .should.have.been.calledOnce;
63     });
64
65   });
66
67   describe('GET /api/users/me', function() {
68
69     it('should be authenticated and route to user.controller.me', function() {
70       routerStub.get
71         .withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me')
72         .should.have.been.calledOnce;
73     });
74
75   });
76
77   describe('PUT /api/users/:id/password', function() {
78
79     it('should be authenticated and route to user.controller.changePassword', function() {
80       routerStub.put
81         .withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword')
82         .should.have.been.calledOnce;
83     });
84
85   });
86
87   describe('GET /api/users/:id', function() {
88
89     it('should be authenticated and route to user.controller.show', function() {
90       routerStub.get
91         .withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show')
92         .should.have.been.calledOnce;
93     });
94
95   });
96
97   describe('POST /api/users', function() {
98
99     it('should route to user.controller.create', function() {
100       routerStub.post
101         .withArgs('/', 'userCtrl.create')
102         .should.have.been.calledOnce;
103     });
104
105   });
106
107 });