| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import { vi, assert } from 'vitest'
- import sinon from 'sinon'
- import path from 'node:path'
- const modulePath = path.join(
- import.meta.dirname,
- '../../../../app/src/Features/Notifications/NotificationsHandler.mjs'
- )
- describe('NotificationsHandler', function () {
- const userId = '123nd3ijdks'
- const notificationId = '123njdskj9jlk'
- const notificationUrl = 'notification.overleaf.testing'
- beforeEach(async function (ctx) {
- ctx.request = sinon.stub().callsArgWith(1)
- vi.doMock('@overleaf/settings', () => ({
- default: {
- apis: { notifications: { url: notificationUrl } },
- },
- }))
- vi.doMock('request', () => ({
- default: ctx.request,
- }))
- ctx.handler = (await import(modulePath)).default
- })
- describe('getUserNotifications', function () {
- it('should get unread notifications', async function (ctx) {
- const stubbedNotifications = [{ _id: notificationId, user_id: userId }]
- ctx.request.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- stubbedNotifications
- )
- const unreadNotifications =
- await ctx.handler.promises.getUserNotifications(userId)
- stubbedNotifications.should.deep.equal(unreadNotifications)
- const getOpts = {
- uri: `${notificationUrl}/user/${userId}`,
- json: true,
- timeout: 1000,
- method: 'GET',
- }
- ctx.request.calledWith(getOpts).should.equal(true)
- })
- it('should return empty arrays if there are no notifications', async function (ctx) {
- ctx.request.callsArgWith(1, null, { statusCode: 200 }, null)
- const unreadNotifications =
- await ctx.handler.promises.getUserNotifications(userId)
- unreadNotifications.length.should.equal(0)
- })
- })
- describe('markAsRead', function () {
- beforeEach(function (ctx) {
- ctx.key = 'some key here'
- })
- it('should send a delete request when a delete has been received to mark a notification', async function (ctx) {
- await ctx.handler.promises.markAsReadWithKey(userId, ctx.key)
- const opts = {
- uri: `${notificationUrl}/user/${userId}`,
- json: {
- key: ctx.key,
- },
- timeout: 1000,
- method: 'DELETE',
- }
- ctx.request.calledWith(opts).should.equal(true)
- })
- })
- describe('createNotification', function () {
- beforeEach(function (ctx) {
- ctx.key = 'some key here'
- ctx.messageOpts = { value: 12344 }
- ctx.templateKey = 'renderThisHtml'
- ctx.expiry = null
- })
- it('should post the message over', async function (ctx) {
- await ctx.handler.promises.createNotification(
- userId,
- ctx.key,
- ctx.templateKey,
- ctx.messageOpts,
- ctx.expiry
- )
- const args = ctx.request.args[0][0]
- args.uri.should.equal(`${notificationUrl}/user/${userId}`)
- args.timeout.should.equal(1000)
- const expectedJson = {
- key: ctx.key,
- templateKey: ctx.templateKey,
- messageOpts: ctx.messageOpts,
- forceCreate: true,
- }
- assert.deepEqual(args.json, expectedJson)
- })
- describe('when expiry date is supplied', function () {
- beforeEach(function (ctx) {
- ctx.key = 'some key here'
- ctx.messageOpts = { value: 12344 }
- ctx.templateKey = 'renderThisHtml'
- ctx.expiry = new Date()
- })
- it('should post the message over with expiry field', async function (ctx) {
- await ctx.handler.promises.createNotification(
- userId,
- ctx.key,
- ctx.templateKey,
- ctx.messageOpts,
- ctx.expiry
- )
- const args = ctx.request.args[0][0]
- args.uri.should.equal(`${notificationUrl}/user/${userId}`)
- args.timeout.should.equal(1000)
- const expectedJson = {
- key: ctx.key,
- templateKey: ctx.templateKey,
- messageOpts: ctx.messageOpts,
- expires: ctx.expiry,
- forceCreate: true,
- }
- assert.deepEqual(args.json, expectedJson)
- })
- })
- })
- describe('markAsReadByKeyOnly', function () {
- beforeEach(function (ctx) {
- ctx.key = 'some key here'
- })
- it('should send a delete request when a delete has been received to mark a notification', async function (ctx) {
- await ctx.handler.promises.markAsReadByKeyOnly(ctx.key)
- const opts = {
- uri: `${notificationUrl}/key/${ctx.key}`,
- timeout: 1000,
- method: 'DELETE',
- }
- ctx.request.calledWith(opts).should.equal(true)
- })
- })
- })
|