NotificationsTests.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. this.stubbedNotification = {
  62. user_id: ObjectId(user_id),
  63. key: 'notification-key',
  64. messageOpts: 'some info',
  65. templateKey: 'template-key'
  66. }
  67. return (this.stubbedNotificationArray = [this.stubbedNotification])
  68. })
  69. describe('getUserNotifications', () =>
  70. it('should find all notifications and return i', function(done) {
  71. this.findStub.callsArgWith(1, null, this.stubbedNotificationArray)
  72. return this.notifications.getUserNotifications(
  73. user_id,
  74. (err, notifications) => {
  75. notifications.should.equal(this.stubbedNotificationArray)
  76. assert.deepEqual(this.findStub.args[0][0], {
  77. user_id: ObjectId(user_id),
  78. templateKey: { $exists: true }
  79. })
  80. return done()
  81. }
  82. )
  83. }))
  84. describe('addNotification', function() {
  85. beforeEach(function() {
  86. this.stubbedNotification = {
  87. user_id: ObjectId(user_id),
  88. key: 'notification-key',
  89. messageOpts: 'some info',
  90. templateKey: 'template-key'
  91. }
  92. this.expectedDocument = {
  93. user_id: this.stubbedNotification.user_id,
  94. key: 'notification-key',
  95. messageOpts: 'some info',
  96. templateKey: 'template-key'
  97. }
  98. this.expectedQuery = {
  99. user_id: this.stubbedNotification.user_id,
  100. key: 'notification-key'
  101. }
  102. this.updateStub.yields()
  103. return this.countStub.yields(null, 0)
  104. })
  105. it('should insert the notification into the collection', function(done) {
  106. return this.notifications.addNotification(
  107. user_id,
  108. this.stubbedNotification,
  109. err => {
  110. expect(err).not.exists
  111. sinon.assert.calledWith(
  112. this.updateStub,
  113. this.expectedQuery,
  114. this.expectedDocument,
  115. { upsert: true }
  116. )
  117. return done()
  118. }
  119. )
  120. })
  121. describe('when there is an existing notification', function(done) {
  122. beforeEach(function() {
  123. return this.countStub.yields(null, 1)
  124. })
  125. it('should fail to insert', function(done) {
  126. return this.notifications.addNotification(
  127. user_id,
  128. this.stubbedNotification,
  129. err => {
  130. expect(err).not.exists
  131. sinon.assert.notCalled(this.updateStub)
  132. return done()
  133. }
  134. )
  135. })
  136. return it('should update the key if forceCreate is true', function(done) {
  137. this.stubbedNotification.forceCreate = true
  138. return this.notifications.addNotification(
  139. user_id,
  140. this.stubbedNotification,
  141. err => {
  142. expect(err).not.exists
  143. sinon.assert.calledWith(
  144. this.updateStub,
  145. this.expectedQuery,
  146. this.expectedDocument,
  147. { upsert: true }
  148. )
  149. return done()
  150. }
  151. )
  152. })
  153. })
  154. describe('when the notification is set to expire', function() {
  155. beforeEach(function() {
  156. this.stubbedNotification = {
  157. user_id: ObjectId(user_id),
  158. key: 'notification-key',
  159. messageOpts: 'some info',
  160. templateKey: 'template-key',
  161. expires: '2922-02-13T09:32:56.289Z'
  162. }
  163. this.expectedDocument = {
  164. user_id: this.stubbedNotification.user_id,
  165. key: 'notification-key',
  166. messageOpts: 'some info',
  167. templateKey: 'template-key',
  168. expires: new Date(this.stubbedNotification.expires)
  169. }
  170. return (this.expectedQuery = {
  171. user_id: this.stubbedNotification.user_id,
  172. key: 'notification-key'
  173. })
  174. })
  175. return it('should add an `expires` Date field to the document', function(done) {
  176. return this.notifications.addNotification(
  177. user_id,
  178. this.stubbedNotification,
  179. err => {
  180. expect(err).not.exists
  181. sinon.assert.calledWith(
  182. this.updateStub,
  183. this.expectedQuery,
  184. this.expectedDocument,
  185. { upsert: true }
  186. )
  187. return done()
  188. }
  189. )
  190. })
  191. })
  192. return describe('when the notification has a nonsensical expires field', function() {
  193. beforeEach(function() {
  194. this.stubbedNotification = {
  195. user_id: ObjectId(user_id),
  196. key: 'notification-key',
  197. messageOpts: 'some info',
  198. templateKey: 'template-key',
  199. expires: 'WAT'
  200. }
  201. return (this.expectedDocument = {
  202. user_id: this.stubbedNotification.user_id,
  203. key: 'notification-key',
  204. messageOpts: 'some info',
  205. templateKey: 'template-key',
  206. expires: new Date(this.stubbedNotification.expires)
  207. })
  208. })
  209. return it('should produce an error', function(done) {
  210. return this.notifications.addNotification(
  211. user_id,
  212. this.stubbedNotification,
  213. err => {
  214. ;(err instanceof Error).should.equal(true)
  215. sinon.assert.notCalled(this.updateStub)
  216. return done()
  217. }
  218. )
  219. })
  220. })
  221. })
  222. describe('removeNotificationId', () =>
  223. it('should mark the notification id as read', function(done) {
  224. this.updateStub.callsArgWith(2, null)
  225. return this.notifications.removeNotificationId(
  226. user_id,
  227. notification_id,
  228. err => {
  229. const searchOps = {
  230. user_id: ObjectId(user_id),
  231. _id: ObjectId(notification_id)
  232. }
  233. const updateOperation = {
  234. $unset: { templateKey: true, messageOpts: true }
  235. }
  236. assert.deepEqual(this.updateStub.args[0][0], searchOps)
  237. assert.deepEqual(this.updateStub.args[0][1], updateOperation)
  238. return done()
  239. }
  240. )
  241. }))
  242. describe('removeNotificationKey', () =>
  243. it('should mark the notification key as read', function(done) {
  244. this.updateStub.callsArgWith(2, null)
  245. return this.notifications.removeNotificationKey(
  246. user_id,
  247. notification_key,
  248. err => {
  249. const searchOps = {
  250. user_id: ObjectId(user_id),
  251. key: notification_key
  252. }
  253. const updateOperation = {
  254. $unset: { templateKey: true }
  255. }
  256. assert.deepEqual(this.updateStub.args[0][0], searchOps)
  257. assert.deepEqual(this.updateStub.args[0][1], updateOperation)
  258. return done()
  259. }
  260. )
  261. }))
  262. describe('removeNotificationByKeyOnly', () =>
  263. it('should mark the notification key as read', function(done) {
  264. this.updateStub.callsArgWith(2, null)
  265. return this.notifications.removeNotificationByKeyOnly(
  266. notification_key,
  267. err => {
  268. const searchOps = { key: notification_key }
  269. const updateOperation = { $unset: { templateKey: true } }
  270. assert.deepEqual(this.updateStub.args[0][0], searchOps)
  271. assert.deepEqual(this.updateStub.args[0][1], updateOperation)
  272. return done()
  273. }
  274. )
  275. }))
  276. return describe('deleteNotificationByKeyOnly', () =>
  277. it('should completely remove the notification', function(done) {
  278. this.removeStub.callsArgWith(2, null)
  279. return this.notifications.deleteNotificationByKeyOnly(
  280. notification_key,
  281. err => {
  282. const searchOps = { key: notification_key }
  283. const opts = { justOne: true }
  284. assert.deepEqual(this.removeStub.args[0][0], searchOps)
  285. assert.deepEqual(this.removeStub.args[0][1], opts)
  286. return done()
  287. }
  288. )
  289. }))
  290. })