From fc06dc99a276e182dfa2ff706839c8627c2a9576 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Fri, 26 Nov 2021 10:21:38 -0500 Subject: [PATCH] [MM-40328] Unit tests for main/server (#1880) --- src/main/server/serverAPI.test.js | 205 ++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 src/main/server/serverAPI.test.js diff --git a/src/main/server/serverAPI.test.js b/src/main/server/serverAPI.test.js new file mode 100644 index 00000000..f500ff6b --- /dev/null +++ b/src/main/server/serverAPI.test.js @@ -0,0 +1,205 @@ +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +'use strict'; + +import {net, session} from 'electron'; + +import {getServerAPI} from './serverAPI'; + +const validURL = 'http://server-1.com/api/endpoint'; +const badDataURL = 'http://server-1.com/api/bad/endpoint'; +const testData = { + name: 'some data', + value: 'some more data', +}; + +jest.mock('electron-log', () => ({ + error: jest.fn(), +})); + +jest.mock('electron', () => ({ + net: { + request: jest.fn().mockImplementation(({url}) => ({ + on: jest.fn().mockImplementation((_, requestCallback) => { + requestCallback({ + on: jest.fn().mockImplementation((event, responseCallback) => { + if (event === 'data') { + responseCallback(url === badDataURL ? '98&H09986t&(*6BV789RhN^t97rb6Ev^*e5v89 re5bg^&' : JSON.stringify(testData)); + } + }), + statusCode: (url === validURL || url === badDataURL) ? 200 : 404, + }); + }), + end: jest.fn(), + })), + }, + session: { + defaultSession: { + cookies: { + get: jest.fn(), + }, + }, + }, +})); + +describe('main/server/serverAPI', () => { + it('should call onSuccess with parsed data when successful', async () => { + const successFn = jest.fn(); + await getServerAPI( + validURL, + false, + successFn, + ); + expect(successFn).toHaveBeenCalledWith(testData); + }); + + it('should call onError when bad status code received', async () => { + const successFn = jest.fn(); + const errorFn = jest.fn(); + await getServerAPI( + 'http://badurl.com', + false, + successFn, + null, + errorFn, + ); + expect(successFn).not.toHaveBeenCalled(); + expect(errorFn).toHaveBeenCalled(); + }); + + it('should call onError when data parsing fails', async () => { + const successFn = jest.fn(); + const errorFn = jest.fn(); + await getServerAPI( + badDataURL, + false, + successFn, + null, + errorFn, + ); + expect(successFn).not.toHaveBeenCalled(); + expect(errorFn).toHaveBeenCalled(); + }); + + it('should call onError when response encounters an error', async () => { + net.request.mockImplementation(({url}) => ({ + on: jest.fn().mockImplementation((_, requestCallback) => { + requestCallback({ + on: jest.fn().mockImplementation((event, responseCallback) => { + if (event === 'error') { + responseCallback(); + } + }), + statusCode: (url === validURL || url === badDataURL) ? 200 : 404, + }); + }), + end: jest.fn(), + })); + + const successFn = jest.fn(); + const errorFn = jest.fn(); + await getServerAPI( + validURL, + false, + successFn, + null, + errorFn, + ); + expect(errorFn).toHaveBeenCalled(); + }); + + it('should call onAbort when request aborts', async () => { + net.request.mockImplementation(() => ({ + on: jest.fn().mockImplementation((event, requestCallback) => { + if (event === 'abort') { + requestCallback(); + } + }), + end: jest.fn(), + })); + + const successFn = jest.fn(); + const abortFn = jest.fn(); + await getServerAPI( + validURL, + false, + successFn, + abortFn, + null, + ); + expect(abortFn).toHaveBeenCalled(); + }); + + it('should call onError when request errors', async () => { + net.request.mockImplementation(() => ({ + on: jest.fn().mockImplementation((event, requestCallback) => { + if (event === 'error') { + requestCallback(); + } + }), + end: jest.fn(), + })); + + const successFn = jest.fn(); + const errorFn = jest.fn(); + await getServerAPI( + validURL, + false, + successFn, + null, + errorFn, + ); + expect(errorFn).toHaveBeenCalled(); + }); + + it('should do nothing when all cookies are missing for authenticated request', async () => { + const successFn = jest.fn(); + await getServerAPI( + validURL, + true, + successFn, + ); + expect(net.request).not.toBeCalled(); + }); + + it('should do nothing when some cookies are missing for authenticated request', async () => { + session.defaultSession.cookies.get.mockImplementation(() => ([ + { + domain: 'http://server-1.com', + name: 'MMUSERID', + }, + ])); + const successFn = jest.fn(); + await getServerAPI( + validURL, + true, + successFn, + ); + expect(net.request).not.toBeCalled(); + }); + + it('should continue when all cookies are present', async () => { + session.defaultSession.cookies.get.mockImplementation(() => ([ + { + domain: 'http://server-1.com', + name: 'MMUSERID', + }, + { + domain: 'http://server-1.com', + name: 'MMCSRF', + }, + { + domain: 'http://server-1.com', + name: 'MMAUTHTOKEN', + }, + ])); + const successFn = jest.fn(); + await getServerAPI( + validURL, + true, + successFn, + ); + expect(net.request).toBeCalled(); + }); +});