16812: Handoff token using query param
[arvados-workbench2.git] / src / common / redirect-to.test.ts
index 94000a98c489beb49f990987bd52b31efb318600..c7d3a84e8cec8ca42e4c0233117a760478b0524c 100644 (file)
@@ -5,16 +5,38 @@
 import { storeRedirects, handleRedirects } from './redirect-to';
 
 describe('redirect-to', () => {
-    const redirectTo = 'http://localhost/test123';
+    const { location } = window;
+    const config: any = {
+        keepWebServiceUrl: 'http://localhost'
+    };
+    const redirectTo = '/test123';
+    const locationTemplate = {
+        hash: '',
+        hostname: '',
+        origin: '',
+        host: '',
+        pathname: '',
+        port: '80',
+        protocol: 'http',
+        search: '',
+        reload: () => { },
+        replace: () => { },
+        assign: () => { },
+        ancestorOrigins: [],
+        href: '',
+    };
+
+    afterAll((): void => {
+        window.location = location;
+    });
 
     describe('storeRedirects', () => {
         beforeEach(() => {
-            Object.defineProperty(window, 'location', {
-                value: {
-                    href: `${window.location.href}?redirectTo=${redirectTo}`
-                },
-                writable: true
-            });
+            delete window.location;
+            window.location = {
+                ...locationTemplate,
+                href: `${location.href}?redirectTo=${redirectTo}`,
+            } as any;
             Object.defineProperty(window, 'sessionStorage', {
                 value: {
                     setItem: jest.fn(),
@@ -34,12 +56,11 @@ describe('redirect-to', () => {
 
     describe('handleRedirects', () => {
         beforeEach(() => {
-            Object.defineProperty(window, 'location', {
-                value: {
-                    href: ''
-                },
-                writable: true
-            });
+            delete window.location;
+            window.location = {
+                ...locationTemplate,
+                href: `${location.href}?redirectTo=${redirectTo}`,
+            } as any;;
             Object.defineProperty(window, 'sessionStorage', {
                 value: {
                     getItem: () => redirectTo,
@@ -50,14 +71,11 @@ describe('redirect-to', () => {
         });
 
         it('should redirect to page when it is present in session storage', () => {
-            // given
-            const token = 'testToken';
-
             // when
-            handleRedirects(token);
+            handleRedirects("abcxyz", config);
 
             // then
-            expect(window.location.href).toBe(`${redirectTo}?api_token=${token}`);
+            expect(window.location.href).toBe(`${config.keepWebServiceUrl}${redirectTo}?api_token=abcxyz`);
         });
     });
-});
\ No newline at end of file
+});