SecondaryLoginService.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Licensed Materials - Property of IBM
  3. * IBM Cognos Products: Glass
  4. * (C) Copyright IBM Corp. 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure
  6. * restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. import secondaryLogin, {
  9. SecondaryLoginConstants,
  10. postSecondaryLogin
  11. } from "cawebpack/services/SecondaryLoginService";
  12. import BrowserUtils from "baglass/core-client/js/core-client/utils/BrowserUtils";
  13. jest.mock("caglass/nls/StringResources", () => {
  14. return {
  15. get: jest.fn()
  16. };
  17. });
  18. jest.mock("baglass/core-client/js/core-client/utils/UrlUtils", () => {
  19. return {
  20. base64Encode: jest.fn(url => url)
  21. };
  22. });
  23. jest.mock("baglass/core-client/js/core-client/utils/BrowserUtils", () => {
  24. return {
  25. isIE: jest.fn().mockReturnValue(false)
  26. };
  27. });
  28. jest.mock("baglass/core-client/js/core-client/utils/Utils", () => {
  29. return {
  30. getCurrentWindow: jest.fn().mockReturnValue(window)
  31. };
  32. });
  33. let originalWindowOpen: any;
  34. const mockGlassContext = {
  35. appController: {
  36. showMessage: jest.fn()
  37. }
  38. };
  39. beforeEach(() => {
  40. originalWindowOpen = window.open;
  41. mockGlassContext.appController.showMessage.mockReset();
  42. });
  43. afterEach(() => {
  44. window.open = originalWindowOpen;
  45. });
  46. describe("SecondaryLoginService", () => {
  47. it("opens a new window with the secondaryLogin query string", () => {
  48. const mockOpen = jest.fn();
  49. window.open = mockOpen;
  50. secondaryLogin(mockGlassContext);
  51. const windowURL = new URL(mockOpen.mock.calls[0][0]);
  52. expect(windowURL.pathname).toEqual(
  53. `/bi${SecondaryLoginConstants.path}`
  54. );
  55. expect(windowURL.search).toEqual(
  56. `?${SecondaryLoginConstants.parameters}`
  57. );
  58. });
  59. it("closes the secondary window once the login promise resolves", async () => {
  60. const mockWindowClose = jest.fn();
  61. const mockSecondaryLoginWindow = {
  62. close: mockWindowClose
  63. };
  64. window.open = jest.fn().mockReturnValueOnce(mockSecondaryLoginWindow);
  65. const mockEvent: any = {
  66. data: {
  67. mockData: "mock"
  68. },
  69. source: mockSecondaryLoginWindow
  70. };
  71. const mockAddEventListener = jest.fn((type: string, callback: any) => {
  72. callback(mockEvent);
  73. });
  74. window.addEventListener = mockAddEventListener;
  75. const eventData = await secondaryLogin(mockGlassContext);
  76. expect(mockAddEventListener.mock.calls[0][0]).toEqual("message");
  77. expect(eventData).toEqual(mockEvent.data);
  78. expect(mockWindowClose).toHaveBeenCalled();
  79. expect(mockGlassContext.appController.showMessage).toHaveBeenCalled();
  80. });
  81. it("adds a storage event listener if using IE", async () => {
  82. const mockWindowClose = jest.fn();
  83. const mockSecondaryLoginWindow = {
  84. close: mockWindowClose
  85. };
  86. window.open = jest.fn().mockReturnValueOnce(mockSecondaryLoginWindow);
  87. BrowserUtils.isIE.mockReturnValueOnce(true);
  88. const mockEvent: any = {
  89. key: "secondaryLoginResult",
  90. newValue: JSON.stringify({
  91. result: {}
  92. })
  93. };
  94. const mockAddEventListener = jest.fn((type: string, callback: any) => {
  95. callback(mockEvent);
  96. });
  97. window.addEventListener = mockAddEventListener;
  98. window.removeEventListener = jest.fn();
  99. await secondaryLogin(mockGlassContext);
  100. expect(mockAddEventListener.mock.calls[0][0]).toEqual("storage");
  101. expect(mockWindowClose).toHaveBeenCalled();
  102. expect(window.removeEventListener).toHaveBeenCalled();
  103. });
  104. describe("after succesfully logging in", () => {
  105. it("writes to localStorage if using IE", () => {
  106. BrowserUtils.isIE.mockReturnValueOnce(true);
  107. const mockResult = {
  108. result: "test"
  109. };
  110. postSecondaryLogin(window, mockResult);
  111. expect(window.localStorage.secondaryLoginResult).toEqual(
  112. JSON.stringify(mockResult)
  113. );
  114. });
  115. it("sends a message to the origin window if its defined and not using IE", () => {
  116. const mockWindow = {
  117. postMessage: jest.fn()
  118. };
  119. window.opener = mockWindow;
  120. const mockResult = {
  121. result: "test"
  122. };
  123. postSecondaryLogin(window, mockResult);
  124. expect(mockWindow.postMessage).toHaveBeenCalledWith(mockResult);
  125. });
  126. it("closes the window if the origin window no longer exists", () => {
  127. window.close = jest.fn();
  128. window.opener = undefined;
  129. postSecondaryLogin(window, {});
  130. expect(window.close).toHaveBeenCalled();
  131. });
  132. });
  133. it("adds the CAM error to the Url", () => {
  134. const mockError: any = {
  135. type: "bus:promptInfo",
  136. captions: ["Please type your credentials for authentication."],
  137. displayObjects: [
  138. {
  139. type: "hidden",
  140. name: "CAMNamespace",
  141. promptOptions: [],
  142. value: "LDAP"
  143. },
  144. {
  145. type: "display",
  146. caption: "Namespace:",
  147. name: "CAMNamespaceDisplayName",
  148. promptOptions: [],
  149. value: "LDAP"
  150. },
  151. {
  152. type: "text",
  153. caption: "User ID:",
  154. name: "CAMUsername",
  155. promptOptions: []
  156. },
  157. {
  158. type: "textnoecho",
  159. caption: "Password:",
  160. name: "CAMPassword",
  161. promptOptions: []
  162. },
  163. {
  164. type: "hidden",
  165. name: "h_CAM_action",
  166. promptOptions: [],
  167. value: "logonAs"
  168. }
  169. ]
  170. };
  171. const mockOpen = jest.fn();
  172. window.open = mockOpen;
  173. secondaryLogin(mockGlassContext, mockError);
  174. const windowURL = mockOpen.mock.calls[0][0];
  175. const stateParam = windowURL.split("&")[1];
  176. expect(stateParam).toEqual(`state=${JSON.stringify(mockError)}`);
  177. });
  178. it("redirects to correct url with gateway configured", () => {
  179. const mockOpen = jest.fn();
  180. window.open = mockOpen;
  181. const mockContextWithGateway: any = { ...mockGlassContext };
  182. mockContextWithGateway.gateway = "/gateway/bi";
  183. secondaryLogin(mockContextWithGateway);
  184. const windowURL = new URL(mockOpen.mock.calls[0][0]);
  185. expect(windowURL.pathname).toEqual("/gateway/bi/login");
  186. expect(windowURL.search).toEqual(
  187. `?${SecondaryLoginConstants.parameters}`
  188. );
  189. });
  190. });