|
2 | 2 |
|
3 | 3 | var proxyquire = require('proxyquire').noPreserveCache(); |
4 | 4 |
|
5 | | - /* user.controller stub */ |
6 | | -var userCtrl = { |
7 | | - index: 'userCtrl.index', |
8 | | - destroy: 'userCtrl.destroy', |
9 | | - me: 'userCtrl.me', |
10 | | - changePassword: 'userCtrl.changePassword', |
11 | | - show: 'userCtrl.show', |
12 | | - create: 'userCtrl.create' |
13 | | - }, |
14 | | - /* auth.service stub */ |
15 | | - authService = { |
16 | | - isAuthenticated: function() { |
17 | | - return 'authService.isAuthenticated'; |
18 | | - }, |
19 | | - hasRole: function(role) { |
20 | | - return 'authService.hasRole.' + role; |
21 | | - } |
22 | | - }, |
23 | | - /* express.Router().router stub */ |
24 | | - router = { |
25 | | - get: sinon.spy(), |
26 | | - put: sinon.spy(), |
27 | | - post: sinon.spy(), |
28 | | - delete: sinon.spy() |
29 | | - }, |
30 | | - /* stubbed user router */ |
31 | | - index = proxyquire('./index', { |
32 | | - 'express': { |
33 | | - Router: function() { |
34 | | - return router; |
35 | | - } |
36 | | - }, |
37 | | - './user.controller': userCtrl, |
38 | | - '../../auth/auth.service': authService |
39 | | - }); |
| 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 | 40 |
|
41 | 41 | describe('User API Router:', function() { |
42 | 42 |
|
43 | 43 | it('should return an express router instance', function() { |
44 | | - index.should.equal(router); |
| 44 | + userIndex.should.equal(routerStub); |
45 | 45 | }); |
46 | 46 |
|
47 | 47 | describe('GET /api/users', function() { |
48 | 48 |
|
49 | 49 | it('should verify admin role and route to user.controller.index', function() { |
50 | | - router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce; |
| 50 | + routerStub.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce; |
51 | 51 | }); |
52 | 52 |
|
53 | 53 | }); |
54 | 54 |
|
55 | 55 | describe('DELETE /api/users/:id', function() { |
56 | 56 |
|
57 | 57 | it('should verify admin role and route to user.controller.destroy', function() { |
58 | | - router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce; |
| 58 | + routerStub.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce; |
59 | 59 | }); |
60 | 60 |
|
61 | 61 | }); |
62 | 62 |
|
63 | 63 | describe('GET /api/users/me', function() { |
64 | 64 |
|
65 | 65 | it('should be authenticated and route to user.controller.me', function() { |
66 | | - router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce; |
| 66 | + routerStub.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce; |
67 | 67 | }); |
68 | 68 |
|
69 | 69 | }); |
70 | 70 |
|
71 | 71 | describe('PUT /api/users/:id/password', function() { |
72 | 72 |
|
73 | 73 | it('should be authenticated and route to user.controller.changePassword', function() { |
74 | | - router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce; |
| 74 | + routerStub.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce; |
75 | 75 | }); |
76 | 76 |
|
77 | 77 | }); |
78 | 78 |
|
79 | 79 | describe('GET /api/users/:id', function() { |
80 | 80 |
|
81 | 81 | it('should be authenticated and route to user.controller.show', function() { |
82 | | - router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce; |
| 82 | + routerStub.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce; |
83 | 83 | }); |
84 | 84 |
|
85 | 85 | }); |
86 | 86 |
|
87 | 87 | describe('POST /api/users', function() { |
88 | 88 |
|
89 | 89 | it('should route to user.controller.create', function() { |
90 | | - router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce; |
| 90 | + routerStub.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce; |
91 | 91 | }); |
92 | 92 |
|
93 | 93 | }); |
|
0 commit comments