ExtensionStrategy-NoProxy.spec.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import extensionStrategy, {
  2. hasExtensionInstalled,
  3. hasChromeExtensionInstalled,
  4. hasFirefoxExtensionInstalled,
  5. cancelRunningExtensionRequest,
  6. } from "../ExtensionStrategy"
  7. jest.mock("../../utils/b64", () => ({
  8. __esModule: true,
  9. decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
  10. }))
  11. jest.mock("~/newstore/settings", () => {
  12. return {
  13. __esModule: true,
  14. settingsStore: {
  15. value: {
  16. EXTENSIONS_ENABLED: true,
  17. PROXY_ENABLED: false,
  18. },
  19. },
  20. }
  21. })
  22. describe("hasExtensionInstalled", () => {
  23. test("returns true if extension is present and hooked", () => {
  24. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  25. expect(hasExtensionInstalled()).toEqual(true)
  26. })
  27. test("returns false if extension not present or not hooked", () => {
  28. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  29. expect(hasExtensionInstalled()).toEqual(false)
  30. })
  31. })
  32. describe("hasChromeExtensionInstalled", () => {
  33. test("returns true if extension is hooked and browser is chrome", () => {
  34. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  35. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  36. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  37. expect(hasChromeExtensionInstalled()).toEqual(true)
  38. })
  39. test("returns false if extension is hooked and browser is not chrome", () => {
  40. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  41. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  42. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  43. expect(hasChromeExtensionInstalled()).toEqual(false)
  44. })
  45. test("returns false if extension not installed and browser is chrome", () => {
  46. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  47. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  48. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  49. expect(hasChromeExtensionInstalled()).toEqual(false)
  50. })
  51. test("returns false if extension not installed and browser is not chrome", () => {
  52. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  53. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  54. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  55. expect(hasChromeExtensionInstalled()).toEqual(false)
  56. })
  57. })
  58. describe("hasFirefoxExtensionInstalled", () => {
  59. test("returns true if extension is hooked and browser is firefox", () => {
  60. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  61. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  62. expect(hasFirefoxExtensionInstalled()).toEqual(true)
  63. })
  64. test("returns false if extension is hooked and browser is not firefox", () => {
  65. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  66. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  67. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  68. })
  69. test("returns false if extension not installed and browser is firefox", () => {
  70. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  71. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  72. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  73. })
  74. test("returns false if extension not installed and browser is not firefox", () => {
  75. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  76. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  77. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  78. })
  79. })
  80. describe("cancelRunningExtensionRequest", () => {
  81. const cancelFunc = jest.fn()
  82. beforeEach(() => {
  83. cancelFunc.mockClear()
  84. })
  85. test("cancels request if extension installed and function present in hook", () => {
  86. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  87. cancelRunningRequest: cancelFunc,
  88. }
  89. cancelRunningExtensionRequest()
  90. expect(cancelFunc).toHaveBeenCalledTimes(1)
  91. })
  92. test("does not cancel request if extension not installed", () => {
  93. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  94. cancelRunningExtensionRequest()
  95. expect(cancelFunc).not.toHaveBeenCalled()
  96. })
  97. })
  98. describe("extensionStrategy", () => {
  99. const sendReqFunc = jest.fn()
  100. beforeEach(() => {
  101. sendReqFunc.mockClear()
  102. })
  103. describe("Non-Proxy Requests", () => {
  104. test("ask extension to send request", async () => {
  105. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  106. sendRequest: sendReqFunc,
  107. }
  108. sendReqFunc.mockResolvedValue({
  109. data: '{"success":true,"data":""}',
  110. })
  111. await extensionStrategy({})()
  112. expect(sendReqFunc).toHaveBeenCalledTimes(1)
  113. })
  114. test("sends request to the actual sender if proxy disabled", async () => {
  115. let passedUrl
  116. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  117. sendRequest: sendReqFunc,
  118. }
  119. sendReqFunc.mockImplementation(({ url }) => {
  120. passedUrl = url
  121. return Promise.resolve({
  122. data: '{"success":true,"data":""}',
  123. })
  124. })
  125. await extensionStrategy({ url: "test" })()
  126. expect(passedUrl).toEqual("test")
  127. })
  128. test("asks extension to get binary data", async () => {
  129. let passedFields
  130. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  131. sendRequest: sendReqFunc,
  132. }
  133. sendReqFunc.mockImplementation((fields) => {
  134. passedFields = fields
  135. return Promise.resolve({
  136. data: '{"success":true,"data":""}',
  137. })
  138. })
  139. await extensionStrategy({})()
  140. expect(passedFields).toHaveProperty("wantsBinary")
  141. })
  142. test("rights successful requests", async () => {
  143. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  144. sendRequest: sendReqFunc,
  145. }
  146. sendReqFunc.mockResolvedValue({
  147. data: '{"success":true,"data":""}',
  148. })
  149. expect(await extensionStrategy({})()).toBeRight()
  150. })
  151. test("rejects errors as-is", async () => {
  152. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  153. sendRequest: sendReqFunc,
  154. }
  155. sendReqFunc.mockRejectedValue("err")
  156. expect(await extensionStrategy({})()).toEqualLeft("err")
  157. })
  158. })
  159. })