NotificationsTests.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 { expect } = require('chai')
  17. const modulePath = '../../../app/js/Notifications.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. const assert = require('assert')
  20. const { ObjectId } = require('mongodb')
  21. const user_id = '51dc93e6fb625a261300003b'
  22. const notification_id = 'fb625a26f09d'
  23. const notification_key = 'notification-key'
  24. describe('Notifications Tests', function () {
  25. beforeEach(function () {
  26. this.findToArrayStub = sinon.stub()
  27. this.findStub = sinon.stub().returns({ toArray: this.findToArrayStub })
  28. this.countStub = sinon.stub()
  29. this.updateOneStub = sinon.stub()
  30. this.deleteOneStub = sinon.stub()
  31. this.db = {
  32. notifications: {
  33. find: this.findStub,
  34. count: this.countStub,
  35. updateOne: this.updateOneStub,
  36. deleteOne: this.deleteOneStub,
  37. },
  38. }
  39. this.notifications = SandboxedModule.require(modulePath, {
  40. requires: {
  41. '@overleaf/settings': {},
  42. './mongodb': { db: this.db, ObjectId },
  43. '@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
  44. },
  45. })
  46. this.stubbedNotification = {
  47. user_id: ObjectId(user_id),
  48. key: 'notification-key',
  49. messageOpts: 'some info',
  50. templateKey: 'template-key',
  51. }
  52. return (this.stubbedNotificationArray = [this.stubbedNotification])
  53. })
  54. describe('getUserNotifications', function () {
  55. return it('should find all notifications and return i', function (done) {
  56. this.findToArrayStub.callsArgWith(0, null, this.stubbedNotificationArray)
  57. return this.notifications.getUserNotifications(
  58. user_id,
  59. (err, notifications) => {
  60. notifications.should.equal(this.stubbedNotificationArray)
  61. assert.deepEqual(this.findStub.args[0][0], {
  62. user_id: ObjectId(user_id),
  63. templateKey: { $exists: true },
  64. })
  65. return done()
  66. }
  67. )
  68. })
  69. })
  70. describe('addNotification', function () {
  71. beforeEach(function () {
  72. this.stubbedNotification = {
  73. user_id: ObjectId(user_id),
  74. key: 'notification-key',
  75. messageOpts: 'some info',
  76. templateKey: 'template-key',
  77. }
  78. this.expectedDocument = {
  79. user_id: this.stubbedNotification.user_id,
  80. key: 'notification-key',
  81. messageOpts: 'some info',
  82. templateKey: 'template-key',
  83. }
  84. this.expectedQuery = {
  85. user_id: this.stubbedNotification.user_id,
  86. key: 'notification-key',
  87. }
  88. this.updateOneStub.yields()
  89. return this.countStub.yields(null, 0)
  90. })
  91. it('should insert the notification into the collection', function (done) {
  92. return this.notifications.addNotification(
  93. user_id,
  94. this.stubbedNotification,
  95. err => {
  96. expect(err).not.to.exist
  97. sinon.assert.calledWith(
  98. this.updateOneStub,
  99. this.expectedQuery,
  100. { $set: this.expectedDocument },
  101. { upsert: true }
  102. )
  103. return done()
  104. }
  105. )
  106. })
  107. describe('when there is an existing notification', function (done) {
  108. beforeEach(function () {
  109. return this.countStub.yields(null, 1)
  110. })
  111. it('should fail to insert', function (done) {
  112. return this.notifications.addNotification(
  113. user_id,
  114. this.stubbedNotification,
  115. err => {
  116. expect(err).not.to.exist
  117. sinon.assert.notCalled(this.updateOneStub)
  118. return done()
  119. }
  120. )
  121. })
  122. return it('should update the key if forceCreate is true', function (done) {
  123. this.stubbedNotification.forceCreate = true
  124. return this.notifications.addNotification(
  125. user_id,
  126. this.stubbedNotification,
  127. err => {
  128. expect(err).not.to.exist
  129. sinon.assert.calledWith(
  130. this.updateOneStub,
  131. this.expectedQuery,
  132. { $set: this.expectedDocument },
  133. { upsert: true }
  134. )
  135. return done()
  136. }
  137. )
  138. })
  139. })
  140. describe('when the notification is set to expire', function () {
  141. beforeEach(function () {
  142. this.stubbedNotification = {
  143. user_id: ObjectId(user_id),
  144. key: 'notification-key',
  145. messageOpts: 'some info',
  146. templateKey: 'template-key',
  147. expires: '2922-02-13T09:32:56.289Z',
  148. }
  149. this.expectedDocument = {
  150. user_id: this.stubbedNotification.user_id,
  151. key: 'notification-key',
  152. messageOpts: 'some info',
  153. templateKey: 'template-key',
  154. expires: new Date(this.stubbedNotification.expires),
  155. }
  156. return (this.expectedQuery = {
  157. user_id: this.stubbedNotification.user_id,
  158. key: 'notification-key',
  159. })
  160. })
  161. return it('should add an `expires` Date field to the document', function (done) {
  162. return this.notifications.addNotification(
  163. user_id,
  164. this.stubbedNotification,
  165. err => {
  166. expect(err).not.to.exist
  167. sinon.assert.calledWith(
  168. this.updateOneStub,
  169. this.expectedQuery,
  170. { $set: this.expectedDocument },
  171. { upsert: true }
  172. )
  173. return done()
  174. }
  175. )
  176. })
  177. })
  178. return describe('when the notification has a nonsensical expires field', function () {
  179. beforeEach(function () {
  180. this.stubbedNotification = {
  181. user_id: ObjectId(user_id),
  182. key: 'notification-key',
  183. messageOpts: 'some info',
  184. templateKey: 'template-key',
  185. expires: 'WAT',
  186. }
  187. return (this.expectedDocument = {
  188. user_id: this.stubbedNotification.user_id,
  189. key: 'notification-key',
  190. messageOpts: 'some info',
  191. templateKey: 'template-key',
  192. expires: new Date(this.stubbedNotification.expires),
  193. })
  194. })
  195. return it('should produce an error', function (done) {
  196. return this.notifications.addNotification(
  197. user_id,
  198. this.stubbedNotification,
  199. err => {
  200. ;(err instanceof Error).should.equal(true)
  201. sinon.assert.notCalled(this.updateOneStub)
  202. return done()
  203. }
  204. )
  205. })
  206. })
  207. })
  208. describe('removeNotificationId', function () {
  209. return it('should mark the notification id as read', function (done) {
  210. this.updateOneStub.callsArgWith(2, null)
  211. return this.notifications.removeNotificationId(
  212. user_id,
  213. notification_id,
  214. err => {
  215. const searchOps = {
  216. user_id: ObjectId(user_id),
  217. _id: ObjectId(notification_id),
  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. user_id,
  234. notification_key,
  235. err => {
  236. const searchOps = {
  237. user_id: ObjectId(user_id),
  238. key: notification_key,
  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. notification_key,
  255. err => {
  256. const searchOps = { key: notification_key }
  257. const updateOperation = { $unset: { templateKey: true } }
  258. assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
  259. assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
  260. return done()
  261. }
  262. )
  263. })
  264. })
  265. return describe('deleteNotificationByKeyOnly', function () {
  266. return it('should completely remove the notification', function (done) {
  267. this.deleteOneStub.callsArgWith(1, null)
  268. return this.notifications.deleteNotificationByKeyOnly(
  269. notification_key,
  270. err => {
  271. const searchOps = { key: notification_key }
  272. assert.deepEqual(this.deleteOneStub.args[0][0], searchOps)
  273. return done()
  274. }
  275. )
  276. })
  277. })
  278. })