Skip to content

Commit

Permalink
Adds notification tests and updates server middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSJaay committed Oct 14, 2024
1 parent 0e3497e commit cc1d964
Show file tree
Hide file tree
Showing 21 changed files with 958 additions and 223 deletions.
1 change: 1 addition & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default defineConfig({
videosFolder: 'test/e2e/setup/videos',
downloadsFolder: 'test/e2e/setup/downloads',
supportFile: 'test/e2e/setup/support/e2e.js',
experimentalRunAllSpecs: true,
},
});
10 changes: 2 additions & 8 deletions shared/validators/notifications/discord.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// messageType: type of message for discord webhook (basic, pretty, nerdy)
// friendlyName: friendly name for discord webhook
// textMessage: text message for discord webhook (optional)
// token: url for discord webhook
// username: username for discord webhook (optional)

import { NotificationValidatorError } from '../../utils/errors.js';

const friendlyNameRegex = /^[a-zA-Z0-9_ ]+$/;
const friendlyNameRegex = /^[a-zA-Z0-9_-]+$/;
const messageTypes = ['basic', 'pretty', 'nerdy'];
const textMessageRegex = /^[a-zA-Z0-9_ ]+$/;
const tokenRegex =
/^https:\/\/discord.com\/api\/webhooks\/[0-9]+\/[0-9a-zA-Z_.-]+$/;
const usernameRegex = /^[a-zA-Z0-9_]{1,32}$/;
Expand All @@ -23,18 +21,14 @@ const Discord = ({
if (friendlyNameRegex && !friendlyNameRegex.test(friendlyName)) {
throw new NotificationValidatorError(
'friendlyName',
'Invalid Friendly Name. Must be alphanumeric, Spaces, and underscores only.'
'Invalid Friendly Name. Must be alphanumeric, dashes, and underscores only.'
);
}

if (!messageTypes.includes(messageType)) {
throw new NotificationValidatorError('messageType', 'Invalid Message Type');
}

if (textMessage && !textMessageRegex.test(textMessage)) {
throw new NotificationValidatorError('textMessage', 'Invalid Text Message');
}

if (!tokenRegex.test(token)) {
throw new NotificationValidatorError(
'token',
Expand Down
11 changes: 3 additions & 8 deletions shared/validators/notifications/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { NotificationValidatorError } from '../../utils/errors.js';

const channelRegex = /^#[a-zA-Z0-9_]{1,32}$/;
const friendlyNameRegex = /^[a-zA-Z0-9_ ]+$/;
const channelRegex = /^[a-z0-9][a-z0-9_-]{0,79}$/;
const friendlyNameRegex = /^[a-zA-Z0-9_-]+$/;
const messageTypes = ['basic', 'pretty', 'nerdy'];
const textMessageRegex = /^[a-zA-Z0-9_ ]+$/;
const tokenRegex =
/^https:\/\/hooks.slack.com\/services\/[0-9a-zA-Z]+\/[0-9a-zA-Z]+\/[0-9a-zA-Z]+$/;
const usernameRegex = /^[a-zA-Z0-9_]{1,32}$/;
Expand All @@ -26,7 +25,7 @@ const Slack = ({
if (friendlyNameRegex && !friendlyNameRegex.test(friendlyName)) {
throw new NotificationValidatorError(
'friendlyName',
'Invalid Friendly Name. Must be alphanumeric, Spaces, and underscores only.'
'Invalid Friendly Name. Must be alphanumeric, dashes, and underscores only.'
);
}

Expand All @@ -38,10 +37,6 @@ const Slack = ({
throw new NotificationValidatorError('messageType', 'Invalid Message Type');
}

if (textMessage && !textMessageRegex.test(textMessage)) {
throw new NotificationValidatorError('textMessage', 'Invalid Text Message');
}

if (!tokenRegex.test(token)) {
throw new NotificationValidatorError('token', 'Invalid Slack Webhook URL');
}
Expand Down
6 changes: 3 additions & 3 deletions shared/validators/notifications/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { NotificationValidatorError } from '../../utils/errors.js';

const chatIdRegex = /^[0-9]+$/;
const friendlyNameRegex = /^[a-zA-Z0-9_ ]+$/;
const friendlyNameRegex = /^[a-zA-Z0-9_-]+$/;
const messageTypes = ['basic', 'pretty', 'nerdy'];
const tokenRegex = /^[a-zA-Z0-9_]{1,32}$/;

Expand All @@ -23,7 +23,7 @@ const Telegram = ({
if (friendlyNameRegex && !friendlyNameRegex.test(friendlyName)) {
throw new NotificationValidatorError(
'friendlyName',
'Invalid Friendly Name. Must be alphanumeric, Spaces, and underscores only.'
'Invalid Friendly Name. Must be alphanumeric, dashes, and underscores only.'
);
}

Expand Down Expand Up @@ -52,7 +52,7 @@ const Telegram = ({
if (!tokenRegex.test(token)) {
throw new NotificationValidatorError(
'token',
'Invalid Telegram Webhook URL'
'Invalid Telegram Bot Token'
);
}

Expand Down
8 changes: 4 additions & 4 deletions shared/validators/notifications/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { NotificationValidatorError } from '../../utils/errors.js';

const friendlyNameRegex = /^[a-zA-Z0-9_ ]+$/;
const friendlyNameRegex = /^[a-zA-Z0-9_-]+$/;
const messageTypes = ['basic', 'pretty', 'nerdy'];
const requestTypes = ['application/json', 'form-data'];
const tokenRegex =
Expand All @@ -33,22 +33,22 @@ const Webhook = ({
if (friendlyNameRegex && !friendlyNameRegex.test(friendlyName)) {
throw new NotificationValidatorError(
'friendlyName',
'Invalid Friendly Name. Must be alphanumeric, Spaces, and underscores only.'
'Invalid Friendly Name. Must be alphanumeric, dashes, and underscores only.'
);
}

if (showAdditionalHeaders && !isJson(additionalHeaders)) {
throw new NotificationValidatorError(
'additionalHeaders',
'Invalid Additional Headers'
'Invalid Additional Headers Format'
);
}

if (!messageTypes.includes(messageType)) {
throw new NotificationValidatorError('messageType', 'Invalid Message Type');
}

if (!tokenRegex.test(token)) {
if (!tokenRegex.test(token)) {
throw new NotificationValidatorError('token', 'Invalid Webhook URL');
}

Expand Down
115 changes: 0 additions & 115 deletions test/e2e/monitor.test.js

This file was deleted.

65 changes: 65 additions & 0 deletions test/e2e/monitor/http.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import loginDetails from '../setup/fixtures/login.json';
import monitorDetails from '../setup/fixtures/monitor.json';

describe('Monitor HTTP - Advance', () => {
context('create a monitor with basic information', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('should show errors for invalid values and create a monitor with valid values', () => {
cy.createMonitor(monitorDetails.http);
});
});

context('edit a monitor', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('Edit monitor name', () => {
cy.get(`[id="monitor-${monitorDetails.http.name.value}"]`).click();

cy.get('[id="monitor-edit-button"]').click();

cy.typeText(monitorDetails.http.name.id, '-Edited');

cy.get('[id="monitor-create-button"]').click();

cy.equals(
'[id="monitor-view-menu-name"]',
`${monitorDetails.http.name.value}-Edited`
);
});
});

context('delete a monitor', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('Delete monitor', () => {
cy.get(`[id="monitor-${monitorDetails.http.name.value}-Edited"]`).click();

cy.get('[id="monitor-delete-button"]').click();

cy.get('[id="monitor-delete-confirm-button"]').click();

cy.get(`[id="monitor-${monitorDetails.http.name.value}-Edited"]`).should(
'not.exist'
);
});
});
});
65 changes: 65 additions & 0 deletions test/e2e/monitor/tcp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import loginDetails from '../setup/fixtures/login.json';
import monitorDetails from '../setup/fixtures/monitor.json';

describe('Monitor TCP - Advance', () => {
context('create a monitor with basic information', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('should show errors for invalid values and create a monitor with valid values', () => {
cy.createMonitor(monitorDetails.tcp);
});
});

context('edit a monitor', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('Edit monitor name', () => {
cy.get(`[id="monitor-${monitorDetails.tcp.name.value}"]`).click();

cy.get('[id="monitor-edit-button"]').click();

cy.typeText(monitorDetails.tcp.name.id, '-Edited');

cy.get('[id="monitor-create-button"]').click();

cy.equals(
'[id="monitor-view-menu-name"]',
`${monitorDetails.tcp.name.value}-Edited`
);
});
});

context('delete a monitor', () => {
beforeEach(() => {
const { email, password } = loginDetails.ownerUser;

cy.clearCookies();
cy.loginUser(email, password);
cy.visit('/');
});

it('should delete a monitor', () => {
cy.get(`[id="monitor-${monitorDetails.tcp.name.value}-Edited"]`).click();

cy.get('[id="monitor-delete-button"]').click();

cy.get('[id="monitor-delete-confirm-button"]').click();

cy.get(`[id="monitor-${monitorDetails.tcp.name.value}-Edited"]`).should(
'not.exist'
);
});
});
});
Loading

0 comments on commit cc1d964

Please sign in to comment.