NotificationsTests.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-dupe-keys,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon');
  16. const chai = require('chai');
  17. const expect = chai.should;
  18. const should = chai.should();
  19. const modulePath = "../../../app/js/Notifications.js";
  20. const SandboxedModule = require('sandboxed-module');
  21. const assert = require('assert');
  22. const { ObjectId } = require("mongojs");
  23. const user_id = "51dc93e6fb625a261300003b";
  24. const notification_id = "fb625a26f09d";
  25. const notification_key = "notification-key";
  26. describe('Notifications Tests', function() {
  27. beforeEach(function() {
  28. const self = this;
  29. this.findStub = sinon.stub();
  30. this.insertStub = sinon.stub();
  31. this.countStub = sinon.stub();
  32. this.updateStub = sinon.stub();
  33. this.removeStub = sinon.stub();
  34. this.mongojs = () => {
  35. return {
  36. notifications: {
  37. update: self.mongojsUpdate,
  38. find: this.findStub,
  39. insert: this.insertStub,
  40. count: this.countStub,
  41. update: this.updateStub,
  42. remove: this.removeStub
  43. }
  44. };
  45. };
  46. this.mongojs.ObjectId = ObjectId;
  47. this.notifications = SandboxedModule.require(modulePath, {
  48. requires: {
  49. 'logger-sharelatex': {
  50. log(){},
  51. error(){}
  52. },
  53. 'settings-sharelatex': {},
  54. 'mongojs':this.mongojs,
  55. 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
  56. },
  57. globals: {
  58. console
  59. }
  60. }
  61. );
  62. this.stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"};
  63. return this.stubbedNotificationArray = [this.stubbedNotification];});
  64. describe('getUserNotifications', () =>
  65. it("should find all notifications and return i", function(done){
  66. this.findStub.callsArgWith(1, null, this.stubbedNotificationArray);
  67. return this.notifications.getUserNotifications(user_id, (err, notifications)=> {
  68. notifications.should.equal(this.stubbedNotificationArray);
  69. assert.deepEqual(this.findStub.args[0][0], {"user_id" :ObjectId(user_id), "templateKey": {"$exists":true}});
  70. return done();
  71. });
  72. })
  73. );
  74. describe('addNotification', function() {
  75. beforeEach(function() {
  76. this.stubbedNotification = {
  77. user_id: ObjectId(user_id),
  78. key:"notification-key",
  79. messageOpts:"some info",
  80. templateKey:"template-key"
  81. };
  82. this.expectedDocument = {
  83. user_id: this.stubbedNotification.user_id,
  84. key:"notification-key",
  85. messageOpts:"some info",
  86. templateKey:"template-key"
  87. };
  88. this.expectedQuery = {
  89. user_id: this.stubbedNotification.user_id,
  90. key:"notification-key",
  91. };
  92. this.updateStub.yields();
  93. return this.countStub.yields(null, 0);
  94. });
  95. it('should insert the notification into the collection', function(done){
  96. return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
  97. expect(err).not.exists;
  98. sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
  99. return done();
  100. });
  101. });
  102. describe('when there is an existing notification', function(done) {
  103. beforeEach(function() {
  104. return this.countStub.yields(null, 1);
  105. });
  106. it('should fail to insert', function(done){
  107. return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
  108. expect(err).not.exists;
  109. sinon.assert.notCalled(this.updateStub);
  110. return done();
  111. });
  112. });
  113. return it("should update the key if forceCreate is true", function(done) {
  114. this.stubbedNotification.forceCreate = true;
  115. return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
  116. expect(err).not.exists;
  117. sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
  118. return done();
  119. });
  120. });
  121. });
  122. describe('when the notification is set to expire', function() {
  123. beforeEach(function() {
  124. this.stubbedNotification = {
  125. user_id: ObjectId(user_id),
  126. key:"notification-key",
  127. messageOpts:"some info",
  128. templateKey:"template-key",
  129. expires: '2922-02-13T09:32:56.289Z'
  130. };
  131. this.expectedDocument = {
  132. user_id: this.stubbedNotification.user_id,
  133. key:"notification-key",
  134. messageOpts:"some info",
  135. templateKey:"template-key",
  136. expires: new Date(this.stubbedNotification.expires),
  137. };
  138. return this.expectedQuery = {
  139. user_id: this.stubbedNotification.user_id,
  140. key:"notification-key",
  141. };});
  142. return it('should add an `expires` Date field to the document', function(done){
  143. return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
  144. expect(err).not.exists;
  145. sinon.assert.calledWith(this.updateStub, this.expectedQuery, this.expectedDocument, { upsert: true });
  146. return done();
  147. });
  148. });
  149. });
  150. return describe('when the notification has a nonsensical expires field', function() {
  151. beforeEach(function() {
  152. this.stubbedNotification = {
  153. user_id: ObjectId(user_id),
  154. key:"notification-key",
  155. messageOpts:"some info",
  156. templateKey:"template-key",
  157. expires: 'WAT'
  158. };
  159. return this.expectedDocument = {
  160. user_id: this.stubbedNotification.user_id,
  161. key:"notification-key",
  162. messageOpts:"some info",
  163. templateKey:"template-key",
  164. expires: new Date(this.stubbedNotification.expires),
  165. };});
  166. return it('should produce an error', function(done){
  167. return this.notifications.addNotification(user_id, this.stubbedNotification, err=> {
  168. (err instanceof Error).should.equal(true);
  169. sinon.assert.notCalled(this.updateStub);
  170. return done();
  171. });
  172. });
  173. });
  174. });
  175. describe('removeNotificationId', () =>
  176. it('should mark the notification id as read', function(done){
  177. this.updateStub.callsArgWith(2, null);
  178. return this.notifications.removeNotificationId(user_id, notification_id, err=> {
  179. const searchOps = {
  180. user_id:ObjectId(user_id),
  181. _id:ObjectId(notification_id)
  182. };
  183. const updateOperation =
  184. {"$unset": {templateKey:true, messageOpts:true}};
  185. assert.deepEqual(this.updateStub.args[0][0], searchOps);
  186. assert.deepEqual(this.updateStub.args[0][1], updateOperation);
  187. return done();
  188. });
  189. })
  190. );
  191. describe('removeNotificationKey', () =>
  192. it('should mark the notification key as read', function(done){
  193. this.updateStub.callsArgWith(2, null);
  194. return this.notifications.removeNotificationKey(user_id, notification_key, err=> {
  195. const searchOps = {
  196. user_id:ObjectId(user_id),
  197. key: notification_key
  198. };
  199. const updateOperation = {
  200. "$unset": {templateKey:true}
  201. };
  202. assert.deepEqual(this.updateStub.args[0][0], searchOps);
  203. assert.deepEqual(this.updateStub.args[0][1], updateOperation);
  204. return done();
  205. });
  206. })
  207. );
  208. describe('removeNotificationByKeyOnly', () =>
  209. it('should mark the notification key as read', function(done){
  210. this.updateStub.callsArgWith(2, null);
  211. return this.notifications.removeNotificationByKeyOnly(notification_key, err=> {
  212. const searchOps =
  213. {key: notification_key};
  214. const updateOperation =
  215. {"$unset": {templateKey:true}};
  216. assert.deepEqual(this.updateStub.args[0][0], searchOps);
  217. assert.deepEqual(this.updateStub.args[0][1], updateOperation);
  218. return done();
  219. });
  220. })
  221. );
  222. return describe('deleteNotificationByKeyOnly', () =>
  223. it('should completely remove the notification', function(done){
  224. this.removeStub.callsArgWith(2, null);
  225. return this.notifications.deleteNotificationByKeyOnly(notification_key, err=> {
  226. const searchOps =
  227. {key: notification_key};
  228. const opts =
  229. {justOne: true};
  230. assert.deepEqual(this.removeStub.args[0][0], searchOps);
  231. assert.deepEqual(this.removeStub.args[0][1], opts);
  232. return done();
  233. });
  234. })
  235. );
  236. });