gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant-backoffice] 01/03: fix and test for auth update


From: gnunet
Subject: [taler-merchant-backoffice] 01/03: fix and test for auth update
Date: Mon, 09 May 2022 18:30:33 +0200

This is an automated email from the git hooks/post-receive script.

sebasjm pushed a commit to branch master
in repository merchant-backoffice.

commit 1a59c8ff4a3b1550ae886e8bd993801e4ae3c137
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Mon May 9 13:28:37 2022 -0300

    fix and test for auth update
---
 packages/merchant-backoffice/src/hooks/instance.ts |   3 +-
 packages/merchant-backoffice/tests/axiosMock.ts    |  19 +++-
 .../tests/context/backend.test.tsx                 | 102 +++++++++++++++++++++
 3 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/packages/merchant-backoffice/src/hooks/instance.ts 
b/packages/merchant-backoffice/src/hooks/instance.ts
index 9153e19..71c9102 100644
--- a/packages/merchant-backoffice/src/hooks/instance.ts
+++ b/packages/merchant-backoffice/src/hooks/instance.ts
@@ -85,7 +85,7 @@ export interface AdminAPI {
 
 export function useManagementAPI(instanceId: string): InstanceAPI {
   const mutateAll = useMatchMutate();
-  const { url, token } = useBackendContext();
+  const { url, token, updateLoginStatus } = useBackendContext();
 
   const updateInstance = async (
     instance: MerchantBackend.Instances.InstanceReconfigurationMessage
@@ -125,6 +125,7 @@ export function useManagementAPI(instanceId: string): 
InstanceAPI {
       data: { method: "token", token: newToken },
     });
 
+    updateLoginStatus(url, newToken)
     mutateAll(/\/management\/instances/);
   };
 
diff --git a/packages/merchant-backoffice/tests/axiosMock.ts 
b/packages/merchant-backoffice/tests/axiosMock.ts
index 412d2a0..13ddab5 100644
--- a/packages/merchant-backoffice/tests/axiosMock.ts
+++ b/packages/merchant-backoffice/tests/axiosMock.ts
@@ -36,7 +36,9 @@ interface PatchQuery { patch: string }
 
 const JEST_DEBUG_LOG = process.env['JEST_DEBUG_LOG'] !== undefined
 
-type TestValues = [axios.AxiosRequestConfig | undefined, { query: Query<any, 
any>; params?: { request?: any, qparam?: any, response?: any } } | undefined]
+type ExpectationValues = { query: Query<any, any>; params?: { auth?: string, 
request?: any, qparam?: any, response?: any } }
+
+type TestValues = [axios.AxiosRequestConfig | undefined, ExpectationValues | 
undefined]
 
 const defaultCallback = (actualQuery?: axios.AxiosRequestConfig): 
axios.AxiosPromise<any> => {
   if (JEST_DEBUG_LOG) {
@@ -50,17 +52,22 @@ setAxiosRequestAsTestingEnvironment(
 );
 
 export class AxiosMockEnvironment {
-  expectations: Array<{ query: Query<any, any>, params?: { request?: any, 
qparam?: any, response?: any }, result: { args: axios.AxiosRequestConfig | 
undefined } } | undefined> = []
+  expectations: Array<{
+    query: Query<any, any>,
+    auth?: string,
+    params?: { request?: any, qparam?: any, response?: any },
+    result: { args: axios.AxiosRequestConfig | undefined }
+  } | undefined> = []
   // axiosMock: jest.MockedFunction<axios.AxiosStatic>
 
-  addRequestExpectation<RequestType, ResponseType>(expectedQuery: 
Query<RequestType, ResponseType>, params: { request?: RequestType, qparam?: 
any, response?: ResponseType }): void {
+  addRequestExpectation<RequestType, ResponseType>(expectedQuery: 
Query<RequestType, ResponseType>, params: { auth?: string, request?: 
RequestType, qparam?: any, response?: ResponseType }): void {
     const result = mockAxiosOnce(function (actualQuery?: 
axios.AxiosRequestConfig): axios.AxiosPromise {
 
       if (JEST_DEBUG_LOG) {
         console.log('query to the backend is made', actualQuery)
       }
       if (!expectedQuery) {
-        return Promise.reject()
+        return Promise.reject("a query was made but it was not expected")
       }
       if (JEST_DEBUG_LOG) {
         console.log('expected query:', params?.request)
@@ -160,6 +167,10 @@ export function assertNextRequest(env: 
AxiosMockEnvironment): void {
     expect(actualQuery.params).toMatchObject(expectedQuery.params.qparam)
   }
 
+  if (expectedQuery.params?.auth) {
+    expect(actualQuery.headers.Authorization).toBe(expectedQuery.params?.auth)
+  }
+
 }
 
 ////////////////////
diff --git a/packages/merchant-backoffice/tests/context/backend.test.tsx 
b/packages/merchant-backoffice/tests/context/backend.test.tsx
new file mode 100644
index 0000000..67d59bc
--- /dev/null
+++ b/packages/merchant-backoffice/tests/context/backend.test.tsx
@@ -0,0 +1,102 @@
+/*
+ This file is part of GNU Taler
+ (C) 2021 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+/**
+ *
+ * @author Sebastian Javier Marchano (sebasjm)
+ */
+
+import { renderHook } from "@testing-library/preact-hooks";
+import { ComponentChildren, h, VNode } from "preact";
+import { act } from "preact/test-utils";
+import { BackendContextProvider } from "../../src/context/backend";
+import { MerchantBackend } from "../../src/declaration";
+import {
+  useAdminAPI,
+  useInstanceAPI,
+  useManagementAPI,
+} from "../../src/hooks/instance";
+import {
+  API_CREATE_INSTANCE,
+  API_GET_CURRENT_INSTANCE,
+  API_UPDATE_INSTANCE_AUTH_BY_ID,
+  assertJustExpectedRequestWereMade,
+  AxiosMockEnvironment,
+} from "../axiosMock";
+
+interface TestingContextProps {
+  children?: ComponentChildren;
+}
+function TestingContext({ children }: TestingContextProps): VNode {
+  return (
+    <BackendContextProvider defaultUrl="http://backend"; initialToken="token">
+      {children}
+    </BackendContextProvider>
+  );
+}
+
+describe("backend context api ", () => {
+  it("should use new token after updating the instance token in the settings", 
async () => {
+    const env = new AxiosMockEnvironment();
+
+    const { result, waitForNextUpdate } = renderHook(
+      () => {
+        const instance = useInstanceAPI();
+        const management = useManagementAPI("default");
+        const admin = useAdminAPI();
+
+        return { instance, management, admin };
+      },
+      { wrapper: TestingContext }
+    );
+
+    if (!result.current) {
+      expect(result.current).toBeDefined();
+      return;
+    }
+
+    env.addRequestExpectation(API_UPDATE_INSTANCE_AUTH_BY_ID("default"), {
+      request: {
+        method: "token",
+        token: "another_token",
+      },
+      response: {
+        name: "instance_name",
+      } as MerchantBackend.Instances.QueryInstancesResponse,
+    });
+
+    act(async () => {
+      await result.current?.management.setNewToken("another_token");
+    });
+
+    await waitForNextUpdate({ timeout: 1 });
+
+    assertJustExpectedRequestWereMade(env);
+
+    env.addRequestExpectation(API_CREATE_INSTANCE, {
+      auth: "Bearer another_token",
+      request: {
+        id: "new_instance_id",
+      } as MerchantBackend.Instances.InstanceConfigurationMessage,
+    });
+
+    result.current.admin.createInstance({
+      id: "new_instance_id",
+    } as MerchantBackend.Instances.InstanceConfigurationMessage);
+
+    assertJustExpectedRequestWereMade(env);
+  });
+});

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]