Skip to content

Commit

Permalink
Fix issue with tests not being run in 'client/app/utils' due to filen…
Browse files Browse the repository at this point in the history
…ame, add tests to cover all util methods
  • Loading branch information
VIVEK P A authored and VIVEK P A committed Jun 9, 2024
1 parent 391f829 commit 09494e6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
98 changes: 98 additions & 0 deletions client/app/utils/__tests__/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import axios from 'axios';
import { Utils } from '../index';

describe('Utils', () => {
describe('randomString ', () => {
it('should return random string', () => {
expect(typeof Utils.randomString()).toBe('string');
});
});

describe('getPusher', () => {
beforeAll(() => {
const metaElementOne = document.createElement('meta');
const metaElementTwo = document.createElement('meta');

metaElementOne.setAttribute('name', 'pusher-key');
metaElementOne.setAttribute('content', 'sample content');

metaElementTwo.setAttribute('name', 'pusher-cluster');
metaElementTwo.setAttribute('content', 'sample-cluster');

jest.spyOn(document, 'getElementsByTagName').mockImplementation(() => [metaElementOne, metaElementTwo]);
});

it('should return null if window.Pusher is not defined', () => {
expect(Utils.getPusher()).toBe(null);
});

it('should return new window.Pusher if window.Pusher is defined', () => {
window.Pusher = jest.fn();
const pusher = Utils.getPusher();
expect(pusher).toBeInstanceOf(window.Pusher);
expect(window.Pusher).toHaveBeenCalledWith('sample content', { cluster: 'sample-cluster' });
});
});

describe('setCsrfToken', () => {
it('should verify that csrf token will be undefined if not present in meta tag', () => {
Utils.setCsrfToken();
expect(axios.defaults.headers.common['X-CSRF-Token']).toBe(undefined);
});

it('should verify that axios makes use of the CSRF token present in meta tag if defined', () => {
const metaElementOne = document.createElement('meta');
metaElementOne.setAttribute('name', 'csrf-token');
metaElementOne.setAttribute('content', 'TOKEN-TEST-VALUE');

jest.spyOn(document, 'getElementsByTagName').mockImplementation(() => [metaElementOne]);

Utils.setCsrfToken();
expect(axios.defaults.headers.common['X-CSRF-Token']).toBe('TOKEN-TEST-VALUE');
});
});

describe('renderContent', () => {
it('should verify that render content returns the content passed in without any modification if its not a string', () => {
const objectValue = { a: 1, b: 2 };
const objectValueRespone = Utils.renderContent(objectValue);
expect(objectValueRespone).toBe(objectValue);

const integerValue = 12;
const integerValueResponse = Utils.renderContent(integerValue);
expect(integerValueResponse).toBe(integerValue);

const booleanValue = false;
const booleanValueResponse = Utils.renderContent(booleanValue);
expect(booleanValueResponse).toBe(booleanValue);

const listValue = ['VALUE1', 'VALUE2'];
const listValueResponse = Utils.renderContent(listValue);
expect(listValueResponse).toBe(listValue);

const nullValue = null;
const nullValueResponse = Utils.renderContent(nullValue);
expect(nullValueResponse).toBe(nullValue);

const undefinedValue = undefined;
const undefinedValueResponse = Utils.renderContent(undefinedValue);
expect(undefinedValueResponse).toBe(undefinedValue);
});

it('should verify that for html strings without any issues, proper object representation is created without any value missing', () => {
const h1HeadingString = '<h1 id="main-heading">THIS IS A HEADING</h1>';
const h1Object = Utils.renderContent(h1HeadingString);
expect(h1Object.type).toEqual('h1');
expect(h1Object.props.id).toEqual('main-heading');
expect(h1Object.props.children[0]).toEqual('THIS IS A HEADING');
});

it('should verify that for html strings with vulnerable code, it gets sanitized in object representation', () => {
const imageHTMLString = '<img src=img.jpg onerror=alert(1)>';
const imageObject = Utils.renderContent(imageHTMLString);
expect(imageObject.type).toEqual('img');
expect(imageObject.props.src).toEqual('img.jpg');
expect(imageObject.props.onError).toBe(undefined);
});
});
});
20 changes: 0 additions & 20 deletions client/app/utils/__tests__/index.test.js

This file was deleted.

0 comments on commit 09494e6

Please sign in to comment.