ExtensionStrategy-Proxy.spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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: true,
  18. PROXY_URL: "test",
  19. },
  20. },
  21. }
  22. })
  23. describe("hasExtensionInstalled", () => {
  24. test("returns true if extension is present and hooked", () => {
  25. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  26. expect(hasExtensionInstalled()).toEqual(true)
  27. })
  28. test("returns false if extension not present or not hooked", () => {
  29. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  30. expect(hasExtensionInstalled()).toEqual(false)
  31. })
  32. })
  33. describe("hasChromeExtensionInstalled", () => {
  34. test("returns true if extension is hooked and browser is chrome", () => {
  35. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  36. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  37. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  38. expect(hasChromeExtensionInstalled()).toEqual(true)
  39. })
  40. test("returns false if extension is hooked and browser is not chrome", () => {
  41. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  42. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  43. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  44. expect(hasChromeExtensionInstalled()).toEqual(false)
  45. })
  46. test("returns false if extension not installed and browser is chrome", () => {
  47. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  48. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  49. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  50. expect(hasChromeExtensionInstalled()).toEqual(false)
  51. })
  52. test("returns false if extension not installed and browser is not chrome", () => {
  53. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  54. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  55. jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
  56. expect(hasChromeExtensionInstalled()).toEqual(false)
  57. })
  58. })
  59. describe("hasFirefoxExtensionInstalled", () => {
  60. test("returns true if extension is hooked and browser is firefox", () => {
  61. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  62. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  63. expect(hasFirefoxExtensionInstalled()).toEqual(true)
  64. })
  65. test("returns false if extension is hooked and browser is not firefox", () => {
  66. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  67. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  68. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  69. })
  70. test("returns false if extension not installed and browser is firefox", () => {
  71. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  72. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
  73. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  74. })
  75. test("returns false if extension not installed and browser is not firefox", () => {
  76. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  77. jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
  78. expect(hasFirefoxExtensionInstalled()).toEqual(false)
  79. })
  80. })
  81. describe("cancelRunningExtensionRequest", () => {
  82. const cancelFunc = jest.fn()
  83. beforeEach(() => {
  84. cancelFunc.mockClear()
  85. })
  86. test("cancels request if extension installed and function present in hook", () => {
  87. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  88. cancelRunningRequest: cancelFunc,
  89. }
  90. cancelRunningExtensionRequest()
  91. expect(cancelFunc).toHaveBeenCalledTimes(1)
  92. })
  93. test("does not cancel request if extension not installed", () => {
  94. global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
  95. cancelRunningExtensionRequest()
  96. expect(cancelFunc).not.toHaveBeenCalled()
  97. })
  98. test("does not cancel request if extension installed but function not present", () => {
  99. global.__POSTWOMAN_EXTENSION_HOOK__ = {}
  100. cancelRunningExtensionRequest()
  101. expect(cancelFunc).not.toHaveBeenCalled()
  102. })
  103. })
  104. describe("extensionStrategy", () => {
  105. const sendReqFunc = jest.fn()
  106. beforeEach(() => {
  107. sendReqFunc.mockClear()
  108. })
  109. describe("Proxy Requests", () => {
  110. test("asks extension to send request", async () => {
  111. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  112. sendRequest: sendReqFunc,
  113. }
  114. sendReqFunc.mockResolvedValue({
  115. data: '{"success":true,"data":""}',
  116. })
  117. await extensionStrategy({})
  118. expect(sendReqFunc).toHaveBeenCalledTimes(1)
  119. })
  120. test("sends POST request to proxy if proxy is enabled", async () => {
  121. let passedUrl
  122. let passedMethod
  123. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  124. sendRequest: sendReqFunc,
  125. }
  126. sendReqFunc.mockImplementation(({ method, url }) => {
  127. passedUrl = url
  128. passedMethod = method
  129. return Promise.resolve({
  130. data: '{"success":true,"data":""}',
  131. })
  132. })
  133. await extensionStrategy({})
  134. expect(passedUrl).toEqual("test")
  135. expect(passedMethod).toEqual("post")
  136. })
  137. test("passes request fields properly", async () => {
  138. const reqFields = {
  139. testA: "testA",
  140. testB: "testB",
  141. testC: "testC",
  142. }
  143. let passedFields
  144. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  145. sendRequest: sendReqFunc,
  146. }
  147. sendReqFunc.mockImplementation(({ data }) => {
  148. passedFields = data
  149. return Promise.resolve({
  150. data: '{"success":true,"data":""}',
  151. })
  152. })
  153. await extensionStrategy(reqFields)
  154. expect(passedFields).toMatchObject(reqFields)
  155. })
  156. test("passes wantsBinary field", async () => {
  157. let passedFields
  158. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  159. sendRequest: sendReqFunc,
  160. }
  161. sendReqFunc.mockImplementation(({ data }) => {
  162. passedFields = data
  163. return Promise.resolve({
  164. data: '{"success":true,"data":""}',
  165. })
  166. })
  167. await extensionStrategy({})
  168. expect(passedFields).toHaveProperty("wantsBinary")
  169. })
  170. test("checks for proxy response success field and throws error message for non-success", async () => {
  171. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  172. sendRequest: sendReqFunc,
  173. }
  174. sendReqFunc.mockResolvedValue({
  175. data: '{"success":false,"data": { "message": "testerr" } }',
  176. })
  177. await expect(extensionStrategy({})).rejects.toThrow("testerr")
  178. })
  179. test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
  180. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  181. sendRequest: sendReqFunc,
  182. }
  183. sendReqFunc.mockResolvedValue({
  184. data: '{"success":false,"data": {} }',
  185. })
  186. await expect(extensionStrategy({})).rejects.toThrow("Proxy Error")
  187. })
  188. test("checks for proxy response success and doesn't throw for success", async () => {
  189. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  190. sendRequest: sendReqFunc,
  191. }
  192. sendReqFunc.mockResolvedValue({
  193. data: '{"success":true,"data": {} }',
  194. })
  195. await expect(extensionStrategy({})).resolves.toBeDefined()
  196. })
  197. test("checks isBinary response field and resolve with the converted value if so", async () => {
  198. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  199. sendRequest: sendReqFunc,
  200. }
  201. sendReqFunc.mockResolvedValue({
  202. data: '{"success": true, "isBinary": true, "data": "testdata" }',
  203. })
  204. await expect(extensionStrategy({})).resolves.toMatchObject({
  205. data: "testdata-converted",
  206. })
  207. })
  208. test("checks isBinary response field and resolve with the actual value if not so", async () => {
  209. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  210. sendRequest: sendReqFunc,
  211. }
  212. sendReqFunc.mockResolvedValue({
  213. data: '{"success": true, "isBinary": false, "data": "testdata" }',
  214. })
  215. await expect(extensionStrategy({})).resolves.toMatchObject({
  216. data: "testdata",
  217. })
  218. })
  219. test("failed request errors are thrown as-is", async () => {
  220. global.__POSTWOMAN_EXTENSION_HOOK__ = {
  221. sendRequest: sendReqFunc,
  222. }
  223. sendReqFunc.mockRejectedValue("err")
  224. await expect(extensionStrategy({})).rejects.toBe("err")
  225. })
  226. })
  227. })