From 09494e688c3e48b7e2e139c3d0fd8aa4568d75d8 Mon Sep 17 00:00:00 2001 From: VIVEK P A Date: Sun, 9 Jun 2024 20:27:13 +0530 Subject: [PATCH] Fix issue with tests not being run in 'client/app/utils' due to filename, add tests to cover all util methods --- client/app/utils/__tests__/index.spec.jsx | 98 +++++++++++++++++++++++ client/app/utils/__tests__/index.test.js | 20 ----- 2 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 client/app/utils/__tests__/index.spec.jsx delete mode 100644 client/app/utils/__tests__/index.test.js diff --git a/client/app/utils/__tests__/index.spec.jsx b/client/app/utils/__tests__/index.spec.jsx new file mode 100644 index 0000000000..1a26ae7d5c --- /dev/null +++ b/client/app/utils/__tests__/index.spec.jsx @@ -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 = '

THIS IS A HEADING

'; + 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 = ''; + const imageObject = Utils.renderContent(imageHTMLString); + expect(imageObject.type).toEqual('img'); + expect(imageObject.props.src).toEqual('img.jpg'); + expect(imageObject.props.onError).toBe(undefined); + }); + }); +}); diff --git a/client/app/utils/__tests__/index.test.js b/client/app/utils/__tests__/index.test.js deleted file mode 100644 index 34ee222b75..0000000000 --- a/client/app/utils/__tests__/index.test.js +++ /dev/null @@ -1,20 +0,0 @@ -import Utils from '../index'; - -describe('Utils', () => { - describe('randomString ', () => { - it('should return random string', () => { - expect(typeof Utils.randomString()).toBe('string'); - }); - }); - - describe('getPusher', () => { - 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); - }); - }); -});