16848: Avoids showing API errors when the extra token creation fails.
[arvados-workbench2.git] / src / services / common-service / common-resource-service.test.ts
index 41a584fd78582056a279b2cd977b4731109e9bd8..038c6943fbf9df52b6fb491205c85de777f3dd8d 100644 (file)
@@ -23,11 +23,12 @@ export const mockResourceService = <R extends Resource, C extends CommonResource
 };
 
 describe("CommonResourceService", () => {
-    const axiosInstance = axios.create();
-    const axiosMock = new MockAdapter(axiosInstance);
+    let axiosInstance: AxiosInstance;
+    let axiosMock: MockAdapter;
 
     beforeEach(() => {
-        axiosMock.reset();
+        axiosInstance = axios.create();
+        axiosMock = new MockAdapter(axiosInstance);
     });
 
     it("#create", async () => {
@@ -47,6 +48,22 @@ describe("CommonResourceService", () => {
         expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
     });
 
+    it("#create ignores fields listed as readonly", async () => {
+        axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+        // UUID fields are read-only on all resources.
+        await commonResourceService.create({ uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
+        expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
+    });
+
+    it("#update ignores fields listed as readonly", async () => {
+        axiosInstance.put = jest.fn(() => Promise.resolve({data: {}}));
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+        // UUID fields are read-only on all resources.
+        await commonResourceService.update('resource-uuid', { uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
+        expect(axiosInstance.put).toHaveBeenCalledWith("/resource/resource-uuid", {owner_uuid: "ownerUuidValue"});
+    });
+
     it("#delete", async () => {
         axiosMock
             .onDelete("/resource/uuid")
@@ -110,4 +127,29 @@ describe("CommonResourceService", () => {
             itemsAvailable: 20
         });
     });
+
+    it("#list using POST when query string is too big", async () => {
+        axiosMock
+            .onAny("/resource")
+            .reply(200);
+        const tooBig = 'x'.repeat(1500);
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+        await commonResourceService.list({ filters: tooBig });
+        expect(axiosMock.history.get.length).toBe(0);
+        expect(axiosMock.history.post.length).toBe(1);
+        expect(axiosMock.history.post[0].data.get('filters')).toBe(`[${tooBig}]`);
+        expect(axiosMock.history.post[0].params._method).toBe('GET');
+    });
+
+    it("#list using GET when query string is not too big", async () => {
+        axiosMock
+            .onAny("/resource")
+            .reply(200);
+        const notTooBig = 'x'.repeat(1480);
+        const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
+        await commonResourceService.list({ filters: notTooBig });
+        expect(axiosMock.history.post.length).toBe(0);
+        expect(axiosMock.history.get.length).toBe(1);
+        expect(axiosMock.history.get[0].params.filters).toBe(`[${notTooBig}]`);
+    });
 });