diff --git a/backend/src/middleware/helpers/email/templateBuilder.spec.ts b/backend/src/middleware/helpers/email/templateBuilder.spec.ts index cb516c0a9..a24bac419 100644 --- a/backend/src/middleware/helpers/email/templateBuilder.spec.ts +++ b/backend/src/middleware/helpers/email/templateBuilder.spec.ts @@ -6,6 +6,7 @@ import { resetPasswordTemplate, wrongAccountTemplate, notificationTemplate, + chatMessageTemplate, } from './templateBuilder' const englishHint = 'English version below!' @@ -34,6 +35,12 @@ const resetPasswordTemplateData = () => ({ name: 'Mr Example', }, }) +const chatMessageTemplateData = { + email: 'test@example.org', + variables: { + name: 'Mr Example', + }, +} const wrongAccountTemplateData = () => ({ email: 'test@example.org', variables: {}, @@ -163,6 +170,31 @@ describe('templateBuilder', () => { }) }) + describe('chatMessageTemplate', () => { + describe('multi language', () => { + it('e-mail is build with all data', () => { + const subject = 'Neue Chatnachricht | New chat message' + const actionUrl = new URL('/chat', CONFIG.CLIENT_URI).toString() + const enContent = 'You have received a new chat message. Click here to read:' + const deContent = 'Du hast eine neue Chatnachricht erhalten. Klicke hier, um sie zu lesen:' + testEmailData(null, chatMessageTemplate, chatMessageTemplateData, [ + ...textsStandard, + { + templPropName: 'subject', + isContaining: false, + text: subject, + }, + englishHint, + actionUrl, + chatMessageTemplateData.variables.name, + enContent, + deContent, + supportUrl, + ]) + }) + }) + }) + describe('wrongAccountTemplate', () => { describe('multi language', () => { it('e-mail is build with all data', () => { diff --git a/backend/src/middleware/helpers/isUserOnline.spec.ts b/backend/src/middleware/helpers/isUserOnline.spec.ts new file mode 100644 index 000000000..bf2cb8d17 --- /dev/null +++ b/backend/src/middleware/helpers/isUserOnline.spec.ts @@ -0,0 +1,46 @@ +import { isUserOnline } from './isUserOnline' + +let user + +describe('isUserOnline', () => { + beforeEach(() => { + user = { + properties: { + lastActiveAt: null, + awaySince: null, + lastOnlineStatus: null, + }, + } + }) + describe('user has lastOnlineStatus `online`', () => { + it('returns true if he was active within the last 90 seconds', () => { + user.properties.lastOnlineStatus = 'online' + user.properties.lastActiveAt = new Date() + expect(isUserOnline(user)).toBe(true) + }) + it('returns false if he was not active within the last 90 seconds', () => { + user.properties.lastOnlineStatus = 'online' + user.properties.lastActiveAt = new Date().getTime() - 90001 + expect(isUserOnline(user)).toBe(false) + }) + }) + + describe('user has lastOnlineStatus `away`', () => { + it('returns true if he went away less then 180 seconds ago', () => { + user.properties.lastOnlineStatus = 'away' + user.properties.awaySince = new Date() + expect(isUserOnline(user)).toBe(true) + }) + it('returns false if he went away more then 180 seconds ago', () => { + user.properties.lastOnlineStatus = 'away' + user.properties.awaySince = new Date().getTime() - 180001 + expect(isUserOnline(user)).toBe(false) + }) + }) + + describe('user is freshly created and has never logged in', () => { + it('returns false', () => { + expect(isUserOnline(user)).toBe(false) + }) + }) +}) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.ts b/backend/src/middleware/notifications/notificationsMiddleware.spec.ts index 6cec5c940..8b3123458 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.ts +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.ts @@ -1,5 +1,5 @@ import gql from 'graphql-tag' -import { cleanDatabase } from '../../db/factories' +import Factory, { cleanDatabase } from '../../db/factories' import { createTestClient } from 'apollo-server-testing' import { getNeode, getDriver } from '../../db/neo4j' import createServer, { pubsub } from '../../server' @@ -10,6 +10,23 @@ import { changeGroupMemberRoleMutation, removeUserFromGroupMutation, } from '../../graphql/groups' +import { createMessageMutation } from '../../graphql/messages' +import { createRoomMutation } from '../../graphql/rooms' + +const sendMailMock = jest.fn() +jest.mock('../helpers/email/sendMail', () => ({ + sendMail: () => sendMailMock(), +})) + +const chatMessageTemplateMock = jest.fn() +jest.mock('../helpers/email/templateBuilder', () => ({ + chatMessageTemplate: () => chatMessageTemplateMock(), +})) + +let isUserOnlineMock = jest.fn() +jest.mock('../helpers/isUserOnline', () => ({ + isUserOnline: () => isUserOnlineMock(), +})) let server, query, mutate, notifiedUser, authenticatedUser let publishSpy @@ -634,6 +651,115 @@ describe('notifications', () => { }) }) + describe('chat email notifications', () => { + let chatSender + let chatReceiver + let roomId + + beforeEach(async () => { + jest.clearAllMocks() + + chatSender = await neode.create( + 'User', + { + id: 'chatSender', + name: 'chatSender', + slug: 'chatSender', + }, + { + email: 'chatSender@example.org', + password: '1234', + }, + ) + + chatReceiver = await Factory.build( + 'user', + { id: 'chatReceiver', name: 'chatReceiver', slug: 'chatReceiver' }, + { email: 'user@example.org' }, + ) + + authenticatedUser = await chatSender.toJson() + + const room = await mutate({ + mutation: createRoomMutation(), + variables: { + userId: 'chatReceiver', + }, + }) + roomId = room.data.CreateRoom.id + }) + + describe('chatReceiver is online', () => { + it('sends no email', async () => { + isUserOnlineMock = jest.fn().mockReturnValue(true) + + await mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'Some nice message to chatReceiver', + }, + }) + + expect(sendMailMock).not.toHaveBeenCalled() + expect(chatMessageTemplateMock).not.toHaveBeenCalled() + }) + }) + + describe('chatReceiver is offline', () => { + it('sends an email', async () => { + isUserOnlineMock = jest.fn().mockReturnValue(false) + + await mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'Some nice message to chatReceiver', + }, + }) + + expect(sendMailMock).toHaveBeenCalledTimes(1) + expect(chatMessageTemplateMock).toHaveBeenCalledTimes(1) + }) + }) + + describe('chatReceiver has blocked chatSender', () => { + it('sends no email', async () => { + isUserOnlineMock = jest.fn().mockReturnValue(false) + await chatReceiver.relateTo(chatSender, 'blocked') + + await mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'Some nice message to chatReceiver', + }, + }) + + expect(sendMailMock).not.toHaveBeenCalled() + expect(chatMessageTemplateMock).not.toHaveBeenCalled() + }) + }) + + describe('chatReceiver has disabled email notifications', () => { + it('sends no email', async () => { + isUserOnlineMock = jest.fn().mockReturnValue(false) + await chatReceiver.update({ sendNotificationEmails: false }) + + await mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'Some nice message to chatReceiver', + }, + }) + + expect(sendMailMock).not.toHaveBeenCalled() + expect(chatMessageTemplateMock).not.toHaveBeenCalled() + }) + }) + }) + describe('group notifications', () => { let groupOwner