|
| 1 | +/** |
| 2 | + * Passbolt ~ Open source password manager for teams |
| 3 | + * Copyright (c) Passbolt SA (https://www.passbolt.com) |
| 4 | + * |
| 5 | + * Licensed under GNU Affero General Public License version 3 of the or any later version. |
| 6 | + * For full copyright and license information, please see the LICENSE.txt |
| 7 | + * Redistributions of files must retain the above copyright notice. |
| 8 | + * |
| 9 | + * @copyright Copyright (c) Passbolt SA (https://www.passbolt.com) |
| 10 | + * @license https://opensource.org/licenses/AGPL-3.0 AGPL License |
| 11 | + * @link https://www.passbolt.com Passbolt(tm) |
| 12 | + * @since 4.7.0 |
| 13 | + */ |
| 14 | + |
| 15 | +import each from "jest-each"; |
| 16 | +import Validator from "validator"; |
| 17 | +import {enableFetchMocks} from "jest-fetch-mock"; |
| 18 | +import { |
| 19 | + IS_FETCH_OFFSCREEN_PREFERRED_STORAGE_KEY, |
| 20 | + RequestFetchOffscreenService |
| 21 | +} from "./requestFetchOffscreenService"; |
| 22 | +import {SEND_MESSAGE_TARGET_FETCH_OFFSCREEN} from "../../../offscreens/service/network/fetchOffscreenService"; |
| 23 | +import {fetchOptionsWithBodyFormData, fetchOptionWithBodyData} from "./requestFetchOffscreenService.test.data"; |
| 24 | + |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + enableFetchMocks(); |
| 28 | + fetch.resetMocks(); |
| 29 | + jest.clearAllMocks(); |
| 30 | + // Flush preferred strategy runtime cache. |
| 31 | + RequestFetchOffscreenService.isFetchOffscreenPreferredCache = null; |
| 32 | +}); |
| 33 | + |
| 34 | +describe("RequestFetchOffscreenService", () => { |
| 35 | + describe("::isFetchOffscreenPreferred", () => { |
| 36 | + it("should return false if no value found in runtime or session storage caches", async() => { |
| 37 | + expect.assertions(1); |
| 38 | + expect(await RequestFetchOffscreenService.isFetchOffscreenPreferred()).toBeFalsy(); |
| 39 | + }); |
| 40 | + |
| 41 | + each([ |
| 42 | + {label: "true", value: true}, |
| 43 | + {label: "false", value: false}, |
| 44 | + ]).describe("should return the runtime cached value", scenario => { |
| 45 | + it(`should return the runtime cached value for scenario: ${scenario.label}`, async() => { |
| 46 | + expect.assertions(1); |
| 47 | + // Mock runtime cached value. |
| 48 | + RequestFetchOffscreenService.isFetchOffscreenPreferredCache = scenario.value; |
| 49 | + expect(await RequestFetchOffscreenService.isFetchOffscreenPreferred()).toEqual(scenario.value); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + each([ |
| 54 | + {label: "true", value: true}, |
| 55 | + {label: "false", value: false}, |
| 56 | + ]).describe("should return the session storage value if no runtime value is present", scenario => { |
| 57 | + it(`should return the runtime cached value for scenario: ${scenario.label}`, async() => { |
| 58 | + expect.assertions(1); |
| 59 | + // Mock session storage cached value. |
| 60 | + browser.storage.session.set({[IS_FETCH_OFFSCREEN_PREFERRED_STORAGE_KEY]: scenario.value}); |
| 61 | + expect(await RequestFetchOffscreenService.isFetchOffscreenPreferred()).toEqual(scenario.value); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + each([ |
| 66 | + {label: "true", value: true}, |
| 67 | + {label: "false", value: false}, |
| 68 | + ]).describe("should return the runtime cached value if set and if the session storage value is also set", scenario => { |
| 69 | + it(`should return the runtime cached value for scenario: ${scenario.label}`, async() => { |
| 70 | + expect.assertions(1); |
| 71 | + // Mock runtime cached value. |
| 72 | + RequestFetchOffscreenService.isFetchOffscreenPreferredCache = scenario.value; |
| 73 | + browser.storage.session.set({[IS_FETCH_OFFSCREEN_PREFERRED_STORAGE_KEY]: !scenario.value}); |
| 74 | + expect(await RequestFetchOffscreenService.isFetchOffscreenPreferred()).toEqual(scenario.value); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("::createIfNotExistOffscreenDocument", () => { |
| 80 | + it("should create the offscreen document if it does not exist yet ", async() => { |
| 81 | + expect.assertions(2); |
| 82 | + jest.spyOn(chrome.runtime, "getContexts").mockImplementationOnce(() => []); |
| 83 | + await RequestFetchOffscreenService.createIfNotExistOffscreenDocument(); |
| 84 | + |
| 85 | + const expectedGetContextsData = { |
| 86 | + contextTypes: ["OFFSCREEN_DOCUMENT"], |
| 87 | + documentUrls: ["chrome-extension://didegimhafipceonhjepacocaffmoppf/offscreens/fetch.html"] |
| 88 | + }; |
| 89 | + const expectedCreateDocumentData = { |
| 90 | + url: "offscreens/fetch.html", |
| 91 | + reasons: ["WORKERS"], |
| 92 | + justification: "Used to perform fetch to services such as the passbolt API serving invalid certificate." |
| 93 | + }; |
| 94 | + expect(chrome.runtime.getContexts).toHaveBeenCalledWith(expectedGetContextsData); |
| 95 | + expect(chrome.offscreen.createDocument).toHaveBeenCalledWith(expectedCreateDocumentData); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should not create the offscreen document if it already exist ", async() => { |
| 99 | + expect.assertions(1); |
| 100 | + jest.spyOn(chrome.runtime, "getContexts").mockImplementationOnce(() => ["shallow-offscreen-document-mock"]); |
| 101 | + await RequestFetchOffscreenService.createIfNotExistOffscreenDocument(); |
| 102 | + expect(chrome.offscreen.createDocument).not.toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe("::buildOffscreenData", () => { |
| 107 | + it("should build data to send to the offscreen document", async() => { |
| 108 | + expect.assertions(1); |
| 109 | + const id = crypto.randomUUID(); |
| 110 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 111 | + const options = fetchOptionWithBodyData(); |
| 112 | + const offscreenData = RequestFetchOffscreenService.buildOffscreenData(id, resource, options); |
| 113 | + // Ensure body remains a form data after serialization. |
| 114 | + expect(offscreenData).toEqual({id, resource, options}); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should ensure given fetch options body will not be altered", async() => { |
| 118 | + expect.assertions(1); |
| 119 | + const id = crypto.randomUUID(); |
| 120 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 121 | + const fetchOptions = fetchOptionsWithBodyFormData(); |
| 122 | + RequestFetchOffscreenService.buildOffscreenData(id, resource, fetchOptions); |
| 123 | + // Ensure body remains a form data after serialization. |
| 124 | + expect(fetchOptions.body).toBeInstanceOf(FormData); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should transform FormData body into serialized encoded url parameters", async() => { |
| 128 | + expect.assertions(1); |
| 129 | + const id = crypto.randomUUID(); |
| 130 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 131 | + const options = fetchOptionsWithBodyFormData(); |
| 132 | + |
| 133 | + const offscreenData = RequestFetchOffscreenService.buildOffscreenData(id, resource, options); |
| 134 | + // eslint-disable-next-line object-shorthand |
| 135 | + const expectedOffscreenMessageData = { |
| 136 | + id, |
| 137 | + resource, |
| 138 | + options: { |
| 139 | + ...options, |
| 140 | + headers: { |
| 141 | + ...options.headers, |
| 142 | + "Content-type": "application/x-www-form-urlencoded" // ensure the content type reflect the body type parameter. |
| 143 | + }, |
| 144 | + body: "prop1=value%201&prop1=value%201" // ensure the body is serialized as url encoded parameter |
| 145 | + } |
| 146 | + }; |
| 147 | + expect(offscreenData).toEqual(expectedOffscreenMessageData); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe("::sendOffscreenMessage", () => { |
| 152 | + it("should send a message to the offscreen document", async() => { |
| 153 | + expect.assertions(1); |
| 154 | + const data = {prop1: "value1"}; |
| 155 | + await RequestFetchOffscreenService.sendOffscreenMessage(data); |
| 156 | + const target = SEND_MESSAGE_TARGET_FETCH_OFFSCREEN; |
| 157 | + expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({target, data}); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe("::fetchOffscreen", () => { |
| 162 | + it("should send a message to the offscreen document and stack the response callback handlers", async() => { |
| 163 | + expect.assertions(4); |
| 164 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 165 | + const options = fetchOptionsWithBodyFormData(); |
| 166 | + jest.spyOn(chrome.runtime, "sendMessage").mockImplementationOnce(message => { |
| 167 | + expect(Validator.isUUID(message.data.id)).toBe(true); |
| 168 | + const expectedMessageData = { |
| 169 | + target: SEND_MESSAGE_TARGET_FETCH_OFFSCREEN, |
| 170 | + data: { |
| 171 | + ...message.data, |
| 172 | + options: { |
| 173 | + ...message.data.options, |
| 174 | + headers: { |
| 175 | + ...message.data.options.headers, |
| 176 | + "Content-type": "application/x-www-form-urlencoded" // ensure the content type reflect the body type parameter. |
| 177 | + }, |
| 178 | + body: "prop1=value%201&prop1=value%201" // ensure the body is serialized as url encoded parameter |
| 179 | + } |
| 180 | + }, |
| 181 | + }; |
| 182 | + expect(message).toEqual(expectedMessageData); |
| 183 | + RequestFetchOffscreenService.offscreenRequestsPromisesCallbacks[message.data.id].resolve(); |
| 184 | + }); |
| 185 | + const requestPromise = RequestFetchOffscreenService.fetchOffscreen(resource, options); |
| 186 | + expect(requestPromise).toBeInstanceOf(Promise); |
| 187 | + await expect(requestPromise).resolves.not.toThrow(); |
| 188 | + }); |
| 189 | + |
| 190 | + it("should throw if the message cannot be sent to the offscreen document for unexpected reason", async() => { |
| 191 | + expect.assertions(2); |
| 192 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 193 | + const options = fetchOptionsWithBodyFormData(); |
| 194 | + jest.spyOn(chrome.runtime, "sendMessage").mockImplementationOnce(() => { |
| 195 | + throw new Error("Test error"); |
| 196 | + }); |
| 197 | + const requestPromise = RequestFetchOffscreenService.fetchOffscreen(resource, options); |
| 198 | + expect(requestPromise).toBeInstanceOf(Promise); |
| 199 | + await expect(requestPromise).rejects.toThrow(); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe("::fetchNative", () => { |
| 204 | + it("should call the native fetch API", async() => { |
| 205 | + expect.assertions(2); |
| 206 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 207 | + const options = fetchOptionsWithBodyFormData(); |
| 208 | + fetch.doMockOnce(() => Promise.resolve({})); |
| 209 | + const requestPromise = RequestFetchOffscreenService.fetchNative(resource, options); |
| 210 | + await expect(requestPromise).resolves.not.toThrow(); |
| 211 | + expect(fetch).toHaveBeenCalledWith(resource, options); |
| 212 | + }); |
| 213 | + |
| 214 | + it("should fallback on fetchOffscreen if an unexpected error occurred", async() => { |
| 215 | + expect.assertions(3); |
| 216 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 217 | + const options = fetchOptionsWithBodyFormData(); |
| 218 | + fetch.doMockOnce(() => Promise.reject({})); |
| 219 | + jest.spyOn(RequestFetchOffscreenService, "fetchOffscreen").mockImplementationOnce(() => jest.fn); |
| 220 | + const requestPromise = RequestFetchOffscreenService.fetchNative(resource, options); |
| 221 | + await expect(requestPromise).resolves.not.toThrow(); |
| 222 | + expect(RequestFetchOffscreenService.isFetchOffscreenPreferredCache).toBeTruthy(); |
| 223 | + expect(RequestFetchOffscreenService.fetchOffscreen).toHaveBeenCalledWith(resource, options); |
| 224 | + }); |
| 225 | + |
| 226 | + it("should throw and not fallback on fetchOffscreen if the navigator is not online", async() => { |
| 227 | + expect.assertions(1); |
| 228 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 229 | + const options = fetchOptionsWithBodyFormData(); |
| 230 | + fetch.doMockOnce(() => Promise.reject({})); |
| 231 | + jest.spyOn(navigator, 'onLine', 'get').mockReturnValueOnce(false); |
| 232 | + const requestPromise = RequestFetchOffscreenService.fetchNative(resource, options); |
| 233 | + await expect(requestPromise).rejects.toThrow(); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + describe("::fetch", () => { |
| 238 | + it("should call the native fetch API if offscreen strategy has not been marked as preferred", async() => { |
| 239 | + expect.assertions(1); |
| 240 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 241 | + const options = fetchOptionsWithBodyFormData(); |
| 242 | + jest.spyOn(RequestFetchOffscreenService, "fetchNative").mockImplementationOnce(() => jest.fn); |
| 243 | + await RequestFetchOffscreenService.fetch(resource, options); |
| 244 | + expect(RequestFetchOffscreenService.fetchNative).toHaveBeenCalledWith(resource, options); |
| 245 | + }); |
| 246 | + |
| 247 | + it("should call the native fetch offscreen if this strategy has been marked as preferred", async() => { |
| 248 | + expect.assertions(1); |
| 249 | + const resource = "https://test.passbolt.com/passbolt-unit-test/test.json"; |
| 250 | + const options = fetchOptionsWithBodyFormData(); |
| 251 | + jest.spyOn(RequestFetchOffscreenService, "fetchOffscreen").mockImplementationOnce(() => jest.fn); |
| 252 | + RequestFetchOffscreenService.isFetchOffscreenPreferredCache = true; |
| 253 | + await RequestFetchOffscreenService.fetch(resource, options); |
| 254 | + expect(RequestFetchOffscreenService.fetchOffscreen).toHaveBeenCalledWith(resource, options); |
| 255 | + }); |
| 256 | + }); |
| 257 | +}); |
0 commit comments