-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(fix): fix the issues of test cases from file alert.js
- Loading branch information
1 parent
0f46df7
commit b59e563
Showing
2 changed files
with
148 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const { getStorageConnection } = require('../../lib/main/server/storageConnection'); | ||
const axios = require('axios'); | ||
const nodemailer = require('nodemailer'); | ||
const { EmailService, SlackService, customLoggerAlert, handleUncaughtExceptions, testSlackAlert, testEmailAlert, clearEmailTransport } = require('../../lib/main/server/utils/alerts'); // Adjust the path as needed | ||
const { EmailService, SlackService, testSlackAlert, testEmailAlert, clearEmailTransport, customLoggerAlert, handleUncaughtExceptions } = require('../../lib/main/server/utils/alerts'); // Adjust the path as needed | ||
/* globals expect, jest, beforeEach, describe, beforeAll, afterAll, it, afterEach */ | ||
|
||
jest.mock('axios'); | ||
|
@@ -86,6 +86,7 @@ describe('EmailService', () => { | |
})); | ||
expect(EmailService.transporter).toBe(mockTransporter); | ||
}); | ||
|
||
it('should successfully send an email alert', async () => { | ||
const mockConfig = { | ||
item: { | ||
|
@@ -113,7 +114,7 @@ describe('EmailService', () => { | |
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Errsole: Test type (TestApp app, TestEnv environment)', | ||
text: 'App Name: TestApp\nEnvironment Name: TestEnv\n\nTest message' | ||
html: '<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>' | ||
})); | ||
expect(result).toBe(true); | ||
}); | ||
|
@@ -193,12 +194,13 @@ describe('EmailService', () => { | |
nodemailer.createTransport.mockReturnValue(mockTransporter); | ||
|
||
const result = await EmailService.sendAlert('Test message', 'Test type', { appName: 'TestApp', environmentName: 'TestEnv' }); | ||
|
||
expect(result).toBe(true); | ||
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Errsole: Test type (TestApp app, TestEnv environment)', | ||
text: 'App Name: TestApp\nEnvironment Name: TestEnv\n\nTest message' | ||
html: '<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>' | ||
})); | ||
}); | ||
|
||
|
@@ -224,12 +226,13 @@ describe('EmailService', () => { | |
nodemailer.createTransport.mockReturnValue(mockTransporter); | ||
|
||
const result = await EmailService.sendAlert('Test message', 'Test type', { appName: 'TestApp' }); | ||
|
||
expect(result).toBe(true); | ||
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Errsole: Test type (TestApp app)', | ||
text: 'App Name: TestApp\n\nTest message' | ||
html: '<p><b>App Name: TestApp</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>' | ||
})); | ||
}); | ||
|
||
|
@@ -255,12 +258,13 @@ describe('EmailService', () => { | |
nodemailer.createTransport.mockReturnValue(mockTransporter); | ||
|
||
const result = await EmailService.sendAlert('Test message', 'Test type', { environmentName: 'TestEnv' }); | ||
|
||
expect(result).toBe(true); | ||
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Errsole: Test type (TestEnv environment)', | ||
text: 'Environment Name: TestEnv\n\nTest message' | ||
html: '<p><b>Environment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>' | ||
})); | ||
}); | ||
|
||
|
@@ -286,12 +290,13 @@ describe('EmailService', () => { | |
nodemailer.createTransport.mockReturnValue(mockTransporter); | ||
|
||
const result = await EmailService.sendAlert('Test message', 'Test type', {}); | ||
|
||
expect(result).toBe(true); | ||
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({ | ||
from: '[email protected]', | ||
to: '[email protected]', | ||
subject: 'Errsole: Test type', | ||
text: 'Test message' | ||
html: 'Test message' | ||
})); | ||
}); | ||
}); | ||
|
@@ -404,154 +409,6 @@ describe('SlackService', () => { | |
}); | ||
}); | ||
|
||
describe('customLoggerAlert', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
SlackService.sendAlert = jest.fn(); | ||
EmailService.sendAlert = jest.fn(); | ||
}); | ||
|
||
it('should send alerts via Slack and Email successfully', async () => { | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
axios.post.mockResolvedValue({}); | ||
nodemailer.createTransport.mockReturnValue({ | ||
sendMail: jest.fn().mockResolvedValue({}) | ||
}); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }); | ||
expect(result).toBe(true); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
}); | ||
|
||
it('should handle Slack error and not call Email alert', async () => { | ||
SlackService.sendAlert.mockRejectedValue(new Error('Slack failed')); | ||
EmailService.sendAlert.mockResolvedValue(true); // This should not be called | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
}); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
|
||
it('should handle Email error after successful Slack alert', async () => { | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockRejectedValue(new Error('Email failed')); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false if only Slack alert fails', async () => { | ||
SlackService.sendAlert.mockRejectedValue(new Error('Slack failed')); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
}); | ||
|
||
describe('handleUncaughtExceptions', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
SlackService.sendAlert = jest.fn(); | ||
EmailService.sendAlert = jest.fn(); | ||
}); | ||
|
||
it('should handle exceptions via Slack and Email successfully', async () => { | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
axios.post.mockResolvedValue({}); | ||
nodemailer.createTransport.mockReturnValue({ | ||
sendMail: jest.fn().mockResolvedValue({}) | ||
}); | ||
|
||
const result = await handleUncaughtExceptions('Exception message', { appName: 'TestApp' }); | ||
expect(result).toBe(true); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
}); | ||
|
||
it('should return false if Slack alert fails', async () => { | ||
SlackService.sendAlert.mockRejectedValue(new Error('Slack failed')); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
}); | ||
|
||
const result = await handleUncaughtExceptions('Exception message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false if Email alert fails', async () => { | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockRejectedValue(new Error('Email failed')); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
|
||
const result = await handleUncaughtExceptions('Exception message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false if both Slack and Email alerts fail', async () => { | ||
SlackService.sendAlert.mockRejectedValue(new Error('Slack failed')); | ||
EmailService.sendAlert.mockRejectedValue(new Error('Email failed')); | ||
getStorageConnection.mockReturnValue({ | ||
getConfig: jest.fn() | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, url: 'http://slack-webhook-url', username: 'Errsole' }) }) | ||
.mockResolvedValueOnce({ item: JSON.stringify({ status: true, host: 'smtp.test.com', port: '587', username: 'testuser', password: 'testpass', sender: '[email protected]', receivers: ['[email protected]'] }) }) | ||
}); | ||
|
||
const result = await handleUncaughtExceptions('Exception message', { appName: 'TestApp' }); | ||
expect(result).toBe(false); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Exception message', 'Uncaught Exception', { appName: 'TestApp' }); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
}); | ||
|
||
describe('testSlackAlert', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
@@ -666,3 +523,109 @@ describe('EmailService.clearEmailTransport', () => { | |
expect(EmailService.transporter).toBe(null); | ||
}); | ||
}); | ||
|
||
describe('customLoggerAlert', () => { | ||
beforeEach(() => { | ||
// Ensure that SlackService.sendAlert and EmailService.sendAlert are mocked as Jest functions | ||
SlackService.sendAlert = jest.fn(); | ||
EmailService.sendAlert = jest.fn(); | ||
}); | ||
|
||
it('should send both Slack and email alerts successfully and return true', async () => { | ||
// Mock successful responses for Slack and email alerts | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }, 'logId123'); | ||
|
||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when email alert fails but Slack alert succeeds', async () => { | ||
// Simulate Email alert failure | ||
SlackService.sendAlert.mockResolvedValue(true); // Slack succeeds | ||
EmailService.sendAlert.mockRejectedValue(new Error('Email failed')); | ||
|
||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }, 'logId123'); | ||
|
||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false when Slack alert fails but email alert succeeds', async () => { | ||
SlackService.sendAlert.mockRejectedValueOnce(new Error('Slack failed')); | ||
EmailService.sendAlert.mockResolvedValueOnce(true); | ||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }, 'logId123'); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false when both Slack and email alerts fail', async () => { | ||
SlackService.sendAlert.mockRejectedValueOnce(new Error('Slack failed')); | ||
EmailService.sendAlert.mockRejectedValueOnce(new Error('Email failed')); | ||
const result = await customLoggerAlert('Test message', { appName: 'TestApp' }, 'logId123'); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Alert', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in customLoggerAlert:', expect.any(Error)); | ||
}); | ||
}); | ||
|
||
describe('handleUncaughtExceptions', () => { | ||
beforeEach(() => { | ||
// Ensure that SlackService.sendAlert and EmailService.sendAlert are mocked as Jest functions | ||
SlackService.sendAlert = jest.fn(); | ||
EmailService.sendAlert = jest.fn(); | ||
}); | ||
|
||
it('should send both Slack and email alerts successfully and return true', async () => { | ||
// Mock successful responses for Slack and email alerts | ||
SlackService.sendAlert.mockResolvedValue(true); | ||
EmailService.sendAlert.mockResolvedValue(true); | ||
|
||
const result = await handleUncaughtExceptions('Test message', { appName: 'TestApp' }, 'logId123'); | ||
|
||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when email alert fails but Slack alert succeeds', async () => { | ||
// Simulate Email alert failure | ||
SlackService.sendAlert.mockResolvedValue(true); // Slack succeeds | ||
EmailService.sendAlert.mockRejectedValue(new Error('Email failed')); | ||
|
||
const result = await handleUncaughtExceptions('Test message', { appName: 'TestApp' }, 'logId123'); | ||
|
||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false when Slack alert fails but email alert succeeds', async () => { | ||
SlackService.sendAlert.mockRejectedValueOnce(new Error('Slack failed')); | ||
EmailService.sendAlert.mockResolvedValueOnce(true); | ||
const result = await handleUncaughtExceptions('Test message', { appName: 'TestApp' }, 'logId123'); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
|
||
it('should return false when both Slack and email alerts fail', async () => { | ||
SlackService.sendAlert.mockRejectedValueOnce(new Error('Slack failed')); | ||
EmailService.sendAlert.mockRejectedValueOnce(new Error('Email failed')); | ||
const result = await handleUncaughtExceptions('Test message', { appName: 'TestApp' }, 'logId123'); | ||
expect(SlackService.sendAlert).toHaveBeenCalledWith('Test message', 'Uncaught Exception', { appName: 'TestApp' }, 'logId123'); | ||
expect(EmailService.sendAlert).not.toHaveBeenCalled(); | ||
expect(result).toBe(false); | ||
expect(console.error).toHaveBeenCalledWith('Error in handleUncaughtExceptions:', expect.any(Error)); | ||
}); | ||
}); |
Oops, something went wrong.