NotificationsTests.js 9.2 KB

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