123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import extensionStrategy, {
- hasExtensionInstalled,
- hasChromeExtensionInstalled,
- hasFirefoxExtensionInstalled,
- cancelRunningExtensionRequest,
- } from "../ExtensionStrategy"
- jest.mock("../../utils/b64", () => ({
- __esModule: true,
- decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
- }))
- jest.mock("~/newstore/settings", () => {
- return {
- __esModule: true,
- settingsStore: {
- value: {
- EXTENSIONS_ENABLED: true,
- PROXY_ENABLED: true,
- PROXY_URL: "test",
- },
- },
- }
- })
- describe("hasExtensionInstalled", () => {
- test("returns true if extension is present and hooked", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- expect(hasExtensionInstalled()).toEqual(true)
- })
- test("returns false if extension not present or not hooked", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- expect(hasExtensionInstalled()).toEqual(false)
- })
- })
- describe("hasChromeExtensionInstalled", () => {
- test("returns true if extension is hooked and browser is chrome", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
- jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
- expect(hasChromeExtensionInstalled()).toEqual(true)
- })
- test("returns false if extension is hooked and browser is not chrome", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
- jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
- expect(hasChromeExtensionInstalled()).toEqual(false)
- })
- test("returns false if extension not installed and browser is chrome", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
- jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
- expect(hasChromeExtensionInstalled()).toEqual(false)
- })
- test("returns false if extension not installed and browser is not chrome", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
- jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
- expect(hasChromeExtensionInstalled()).toEqual(false)
- })
- })
- describe("hasFirefoxExtensionInstalled", () => {
- test("returns true if extension is hooked and browser is firefox", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
- expect(hasFirefoxExtensionInstalled()).toEqual(true)
- })
- test("returns false if extension is hooked and browser is not firefox", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
- expect(hasFirefoxExtensionInstalled()).toEqual(false)
- })
- test("returns false if extension not installed and browser is firefox", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
- expect(hasFirefoxExtensionInstalled()).toEqual(false)
- })
- test("returns false if extension not installed and browser is not firefox", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
- expect(hasFirefoxExtensionInstalled()).toEqual(false)
- })
- })
- describe("cancelRunningExtensionRequest", () => {
- const cancelFunc = jest.fn()
- beforeEach(() => {
- cancelFunc.mockClear()
- })
- test("cancels request if extension installed and function present in hook", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- cancelRunningRequest: cancelFunc,
- }
- cancelRunningExtensionRequest()
- expect(cancelFunc).toHaveBeenCalledTimes(1)
- })
- test("does not cancel request if extension not installed", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
- cancelRunningExtensionRequest()
- expect(cancelFunc).not.toHaveBeenCalled()
- })
- test("does not cancel request if extension installed but function not present", () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {}
- cancelRunningExtensionRequest()
- expect(cancelFunc).not.toHaveBeenCalled()
- })
- })
- describe("extensionStrategy", () => {
- const sendReqFunc = jest.fn()
- beforeEach(() => {
- sendReqFunc.mockClear()
- })
- describe("Proxy Requests", () => {
- test("asks extension to send request", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success":true,"data":""}',
- })
- await extensionStrategy({})
- expect(sendReqFunc).toHaveBeenCalledTimes(1)
- })
- test("sends POST request to proxy if proxy is enabled", async () => {
- let passedUrl
- let passedMethod
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockImplementation(({ method, url }) => {
- passedUrl = url
- passedMethod = method
- return Promise.resolve({
- data: '{"success":true,"data":""}',
- })
- })
- await extensionStrategy({})
- expect(passedUrl).toEqual("test")
- expect(passedMethod).toEqual("post")
- })
- test("passes request fields properly", async () => {
- const reqFields = {
- testA: "testA",
- testB: "testB",
- testC: "testC",
- }
- let passedFields
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockImplementation(({ data }) => {
- passedFields = data
- return Promise.resolve({
- data: '{"success":true,"data":""}',
- })
- })
- await extensionStrategy(reqFields)
- expect(passedFields).toMatchObject(reqFields)
- })
- test("passes wantsBinary field", async () => {
- let passedFields
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockImplementation(({ data }) => {
- passedFields = data
- return Promise.resolve({
- data: '{"success":true,"data":""}',
- })
- })
- await extensionStrategy({})
- expect(passedFields).toHaveProperty("wantsBinary")
- })
- test("checks for proxy response success field and throws error message for non-success", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success":false,"data": { "message": "testerr" } }',
- })
- await expect(extensionStrategy({})).rejects.toThrow("testerr")
- })
- test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success":false,"data": {} }',
- })
- await expect(extensionStrategy({})).rejects.toThrow("Proxy Error")
- })
- test("checks for proxy response success and doesn't throw for success", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success":true,"data": {} }',
- })
- await expect(extensionStrategy({})).resolves.toBeDefined()
- })
- test("checks isBinary response field and resolve with the converted value if so", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success": true, "isBinary": true, "data": "testdata" }',
- })
- await expect(extensionStrategy({})).resolves.toMatchObject({
- data: "testdata-converted",
- })
- })
- test("checks isBinary response field and resolve with the actual value if not so", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockResolvedValue({
- data: '{"success": true, "isBinary": false, "data": "testdata" }',
- })
- await expect(extensionStrategy({})).resolves.toMatchObject({
- data: "testdata",
- })
- })
- test("failed request errors are thrown as-is", async () => {
- global.__POSTWOMAN_EXTENSION_HOOK__ = {
- sendRequest: sendReqFunc,
- }
- sendReqFunc.mockRejectedValue("err")
- await expect(extensionStrategy({})).rejects.toBe("err")
- })
- })
- })
|