Files
mattermostest/src/main/Validator.test.js
Devin Binnie 5056ec7ace [MM-40277][MM-40279][MM-40301][MM-40307][MM-40310][MM-40313] Unit tests and refactors for main module (#1876)
* Refactor autoLauncher and remove unnecessary file

* [MM-40277] Unit tests for main/badge

* [MM-40279] Unit tests for main/certificateStore

* [MM-40301] Unit tests for main/contextMenu, also remove workaround

* [MM-40307] Unit tests for main/CriticalErrorHandler

* [MM-40310] Unit tests for main/utils

* [MM-40313] Unit tests for main/Validator

* Lint fix

* Added globals

* More things that should probably already be merged

* PR feedback
2021-11-26 11:14:26 -05:00

206 lines
6.4 KiB
JavaScript

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
'use strict';
import * as Validator from './Validator';
jest.mock('electron-log', () => ({
error: jest.fn(),
}));
describe('main/Validator', () => {
describe('validateV0ConfigData', () => {
const config = {url: 'http://server-1.com'};
it('should return null when not provided object', () => {
expect(Validator.validateV0ConfigData('notanobject')).toBe(null);
});
it('should return complete object when it is valid', () => {
expect(Validator.validateV0ConfigData(config)).toStrictEqual(config);
});
it('should remove fields that arent part of the schema', () => {
const modifiedConfig = {...config, anotherField: 'value'};
expect(Validator.validateV0ConfigData(modifiedConfig)).toStrictEqual(config);
});
});
describe('validateV1ConfigData', () => {
const config = {
autostart: true,
enableHardwareAcceleration: true,
minimizeToTray: false,
showTrayIcon: false,
showUnreadBadge: true,
spellCheckerLocale: 'en-US',
teams: [
{
name: 'server-1',
url: 'http://server-1.com',
},
],
trayIconTheme: 'light',
useSpellChecker: true,
version: 1,
};
it('should remove invalid urls', () => {
const modifiedConfig = {
...config,
teams: [
...config.teams,
{
name: 'server-2',
url: 'a-bad>url',
},
],
};
expect(Validator.validateV1ConfigData(modifiedConfig)).toStrictEqual(config);
});
it('should clean URLs with backslashes', () => {
const modifiedConfig = {
...config,
teams: [
...config.teams,
{
name: 'server-2',
url: 'http:\\\\server-2.com\\subpath',
},
],
};
expect(Validator.validateV1ConfigData(modifiedConfig)).toStrictEqual({
...config,
teams: [
...config.teams,
{
name: 'server-2',
url: 'http://server-2.com/subpath',
},
],
});
});
it('should invalidate bad spell checker locales', () => {
const modifiedConfig = {
...config,
spellCheckerLocale: 'not-a-locale',
};
expect(Validator.validateV1ConfigData(modifiedConfig)).toStrictEqual(null);
});
});
describe('validateV2ConfigData', () => {
const config = {
autostart: true,
darkMode: false,
enableHardwareAcceleration: true,
minimizeToTray: false,
showTrayIcon: false,
showUnreadBadge: true,
spellCheckerLocale: 'en-US',
spellCheckerURL: 'http://spellcheckerservice.com',
teams: [
{
name: 'server-1',
url: 'http://server-1.com',
order: 1,
},
],
trayIconTheme: 'light',
useSpellChecker: true,
version: 2,
};
it('should remove invalid spellchecker URLs', () => {
const modifiedConfig = {
...config,
spellCheckerURL: 'a-bad>url',
};
expect(Validator.validateV2ConfigData(modifiedConfig)).not.toHaveProperty('spellCheckerURL');
});
});
describe('validateV3ConfigData', () => {
const config = {
autostart: true,
darkMode: false,
enableHardwareAcceleration: true,
lastActiveTeam: 0,
minimizeToTray: false,
showTrayIcon: false,
showUnreadBadge: true,
spellCheckerLocales: ['en-US'],
spellCheckerURL: 'http://spellcheckerservice.com',
teams: [
{
lastActiveTab: 0,
name: 'server-1',
url: 'http://server-1.com',
order: 1,
tabs: [
{
name: 'tab-1',
isOpen: true,
},
],
},
],
trayIconTheme: 'light',
useSpellChecker: true,
version: 3,
};
it('should ensure messaging tab is open', () => {
const modifiedConfig = {
...config,
teams: [
{
...config.teams[0],
tabs: [
...config.teams[0].tabs,
{
name: 'TAB_MESSAGING',
isOpen: false,
},
],
},
],
};
expect(Validator.validateV3ConfigData(modifiedConfig)).toStrictEqual({
...config,
teams: [
{
...config.teams[0],
tabs: [
...config.teams[0].tabs,
{
name: 'TAB_MESSAGING',
isOpen: true,
},
],
},
],
});
});
});
describe('validateAllowedProtocols', () => {
const allowedProtocols = [
'spotify:',
'steam:',
'mattermost:',
];
it('should accept valid protocols', () => {
expect(Validator.validateAllowedProtocols(allowedProtocols)).toStrictEqual(allowedProtocols);
});
it('should reject invalid protocols', () => {
expect(Validator.validateAllowedProtocols([...allowedProtocols, 'not-a-protocol'])).toStrictEqual(null);
});
});
});