Merge branch '17319-service-layer-uuid-validation'
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 22 Feb 2021 20:34:06 +0000 (17:34 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Mon, 22 Feb 2021 20:34:06 +0000 (17:34 -0300)
Closes #17319

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/services/common-service/common-resource-service.test.ts
src/services/common-service/common-service.test.ts [new file with mode: 0644]
src/services/common-service/common-service.ts

index 038c6943fbf9df52b6fb491205c85de777f3dd8d..d00412b8a69baa3941ec0bccca56f882928e0a6a 100644 (file)
@@ -15,12 +15,11 @@ const actions: ApiActions = {
 
 export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(
     Service: new (client: AxiosInstance, actions: ApiActions) => C) => {
-    const axiosInstance = axios.create();
-    const axiosMock = new MockAdapter(axiosInstance);
-    const service = new Service(axiosInstance, actions);
-    Object.keys(service).map(key => service[key] = jest.fn());
-    return service;
-};
+        const axiosInstance = axios.create();
+        const service = new Service(axiosInstance, actions);
+        Object.keys(service).map(key => service[key] = jest.fn());
+        return service;
+    };
 
 describe("CommonResourceService", () => {
     let axiosInstance: AxiosInstance;
diff --git a/src/services/common-service/common-service.test.ts b/src/services/common-service/common-service.test.ts
new file mode 100644 (file)
index 0000000..9a13d60
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import axios, { AxiosInstance } from "axios";
+import { ApiActions } from "~/services/api/api-actions";
+import { CommonService } from "./common-service";
+
+const actions: ApiActions = {
+    progressFn: (id: string, working: boolean) => {},
+    errorFn: (id: string, message: string) => {}
+};
+
+describe("CommonService", () => {
+    let commonService: CommonService<any>;
+
+    beforeEach(() => {
+        commonService = new CommonService<any>({} as AxiosInstance, "resource", actions);
+    });
+
+    it("throws an exception when passing uuid as empty string to get()", () => {
+        expect(() => commonService.get("")).toThrowError("UUID cannot be empty string");
+    });
+
+    it("throws an exception when passing uuid as empty string to update()", () => {
+        expect(() => commonService.update("", {})).toThrowError("UUID cannot be empty string");
+    });
+
+    it("throws an exception when passing uuid as empty string to delete()", () => {
+        expect(() => commonService.delete("")).toThrowError("UUID cannot be empty string");
+    });
+});
\ No newline at end of file
index 8e00c4ad1abd42897f84cc1634d34a22b5bd48f0..54c0edf6bf2c684346e0309103ad085c4f38a541 100644 (file)
@@ -68,6 +68,12 @@ export class CommonService<T> {
             }
         }
 
+    private validateUuid(uuid: string) {
+        if (uuid === "") {
+            throw new Error('UUID cannot be empty string');
+        }
+    }
+
     static defaultResponse<R>(promise: AxiosPromise<R>, actions: ApiActions, mapKeys = true, showErrors = true): Promise<R> {
         const reqId = uuid();
         actions.progressFn(reqId, true);
@@ -97,6 +103,7 @@ export class CommonService<T> {
     }
 
     delete(uuid: string): Promise<T> {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .delete(this.resourceType + '/' + uuid),
@@ -105,6 +112,7 @@ export class CommonService<T> {
     }
 
     get(uuid: string, showErrors?: boolean) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .get<T>(this.resourceType + '/' + uuid),
@@ -148,6 +156,7 @@ export class CommonService<T> {
     }
 
     update(uuid: string, data: Partial<T>) {
+        this.validateUuid(uuid);
         return CommonService.defaultResponse(
             this.serverApi
                 .put<T>(this.resourceType + '/' + uuid, data && CommonService.mapKeys(_.snakeCase)(data)),