NotificationsTests.js 9.2 KB

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