users.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var should = require('should'),
  2. request = require('supertest'),
  3. app = require('../server.js'),
  4. mongoose = require('mongoose'),
  5. User = mongoose.model('User'),
  6. Db = require('./db.js'),
  7. sinon = require('sinon'),
  8. EventEmitter = require('../app/events/listeners'),
  9. globalServer, token, hacker_token, account_id, user_id;
  10. describe('API /users', function() {
  11. before(function(done) {
  12. globalServer = app.listen();
  13. token = Db.get_user_token();
  14. hacker_token = Db.get_hacker_token();
  15. account_id = Db.ACCOUNT_ID;
  16. user_id = Db.USER_ID;
  17. Db.init(done);
  18. });
  19. after( function() {
  20. globalServer.close();
  21. });
  22. describe('* Login', function() {
  23. it('should log successfully', function(done) {
  24. request(globalServer)
  25. .post('/api/users/login')
  26. .send({
  27. username: 'test',
  28. password: 's3cr3t'
  29. })
  30. .set('Accept', 'application/json')
  31. .expect(200)
  32. .expect('Content-Type', /json/)
  33. .end( function(error, result) {
  34. should.not.exist(error);
  35. var user = result.body;
  36. should.exist(user);
  37. user.username.should.be.equal('test');
  38. should.exist(user.token);
  39. done();
  40. });
  41. });
  42. it('should fail login', function(done) {
  43. request(globalServer)
  44. .post('/api/users/login')
  45. .send({
  46. username: 'test',
  47. password: 'secret'
  48. })
  49. .set('Accept', 'application/json')
  50. .expect(401, done);
  51. });
  52. it('should logout', function(done) {
  53. request(globalServer)
  54. .delete('/api/users/login')
  55. .expect(200, done);
  56. });
  57. });
  58. describe('* Registration', function() {
  59. it('should fail without any params', function(done) {
  60. request(globalServer)
  61. .post('/api/users')
  62. .set('Accept', 'application/json')
  63. .expect(400)
  64. .end(function(err, result) {
  65. var errors = result.body;
  66. should.exist(errors);
  67. errors.should.be.instanceof(Array).and.have.lengthOf(2);
  68. done();
  69. });
  70. });
  71. it('should fail without a password', function(done) {
  72. request(globalServer)
  73. .post('/api/users')
  74. .send( { username: 'registration'})
  75. .expect(400, done);
  76. });
  77. it('should fail without an username', function(done) {
  78. request(globalServer)
  79. .post('/api/users')
  80. .send({password: 'secret'})
  81. .set('Accept', 'application/json')
  82. .expect(400, done);
  83. });
  84. it('should fail on duplicate account', function(done) {
  85. request(globalServer)
  86. .post('/api/users')
  87. .send({
  88. username: 'test',
  89. password: 'secret'
  90. })
  91. .set('Accept', 'application/json')
  92. .expect(409, done);
  93. });
  94. it('should register successfully', function(done) {
  95. request(globalServer)
  96. .post('/api/users')
  97. .send({
  98. username: 'registration',
  99. password: 'secret'
  100. })
  101. .set('Accept', 'application/json')
  102. .expect(201)
  103. .end(function(error, result) {
  104. should.not.exist(error);
  105. var user = result.body;
  106. should.exist(user);
  107. user.username.should.be.equal('registration');
  108. should.exist(user.token);
  109. User.getAuthenticated('registration', 'secret', function(error, user) {
  110. should.not.exist(error);
  111. should.exist(user);
  112. done();
  113. });
  114. });
  115. });
  116. });
  117. describe('* Deregistration', function() {
  118. it('should fail to delete user account without security token', function(done) {
  119. request(globalServer)
  120. .delete('/api/users')
  121. .expect(401, done);
  122. });
  123. it('should fail to delete user account with fake security token', function(done) {
  124. request(globalServer)
  125. .delete('/api/users')
  126. .set('Authorization', 'JWT fake_token')
  127. .expect(401, done);
  128. });
  129. it('should delete user with accounts and entries', function(done) {
  130. var eventEmitter= EventEmitter.eventEmitter,
  131. spy_accounts = sinon.spy(),
  132. spy_entries = sinon.spy();
  133. eventEmitter.on(EventEmitter.events.ACCOUNTS_DELETE_BY_USER_ID_EVT, spy_accounts);
  134. eventEmitter.on(EventEmitter.events.ENTRIES_DELETE_BY_ACCOUNT_EVT, spy_entries)
  135. request(globalServer)
  136. .delete('/api/users')
  137. .set('Authorization', 'JWT ' + token)
  138. .expect(204)
  139. .end(function(error, result) {
  140. User.findOne({username: 'test'}, function(error, user) {
  141. should.not.exist(error);
  142. should.not.exist(user);
  143. sinon.assert.calledWith(spy_accounts, user_id);
  144. spy_entries.called.should.equal.true;
  145. spy_entries.args[0][0].id.should.be.equal(account_id);
  146. done();
  147. });
  148. });
  149. });
  150. it('should delete user without account', function(done) {
  151. var eventEmitter= EventEmitter.eventEmitter,
  152. spy_accounts = sinon.spy(),
  153. spy_entries = sinon.spy();
  154. eventEmitter.on(EventEmitter.events.ACCOUNTS_DELETE_BY_USER_ID_EVT, spy_accounts);
  155. eventEmitter.on(EventEmitter.events.ENTRIES_DELETE_BY_ACCOUNT_EVT, spy_entries)
  156. request(globalServer)
  157. .delete('/api/users')
  158. .set('Authorization', 'JWT ' + hacker_token)
  159. .expect(204)
  160. .end(function(error, result) {
  161. User.findOne({username: 'hacker'}, function(error, user) {
  162. should.not.exist(error);
  163. should.not.exist(user);
  164. spy_accounts.called.should.equal.true;
  165. spy_entries.called.should.equal.false;
  166. done();
  167. });
  168. });
  169. });
  170. });
  171. });