13494: Merge branch 'master' into 13494-collection-version-browser
[arvados-workbench2.git] / src / services / common-service / common-resource-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CommonResourceService } from "./common-resource-service";
6 import axios, { AxiosInstance } from "axios";
7 import MockAdapter from "axios-mock-adapter";
8 import { Resource } from "src/models/resource";
9 import { ApiActions } from "~/services/api/api-actions";
10
11 const actions: ApiActions = {
12     progressFn: (id: string, working: boolean) => {},
13     errorFn: (id: string, message: string) => {}
14 };
15
16 export const mockResourceService = <R extends Resource, C extends CommonResourceService<R>>(
17     Service: new (client: AxiosInstance, actions: ApiActions) => C) => {
18     const axiosInstance = axios.create();
19     const axiosMock = new MockAdapter(axiosInstance);
20     const service = new Service(axiosInstance, actions);
21     Object.keys(service).map(key => service[key] = jest.fn());
22     return service;
23 };
24
25 describe("CommonResourceService", () => {
26     let axiosInstance: AxiosInstance;
27     let axiosMock: MockAdapter;
28
29     beforeEach(() => {
30         axiosInstance = axios.create();
31         axiosMock = new MockAdapter(axiosInstance);
32     });
33
34     it("#create", async () => {
35         axiosMock
36             .onPost("/resource")
37             .reply(200, { owner_uuid: "ownerUuidValue" });
38
39         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
40         const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
41         expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
42     });
43
44     it("#create maps request params to snake case", async () => {
45         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
46         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
47         await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
48         expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
49     });
50
51     it("#create ignores fields listed as readonly", async () => {
52         axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
53         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
54         // UUID fields are read-only on all resources.
55         await commonResourceService.create({ uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
56         expect(axiosInstance.post).toHaveBeenCalledWith("/resource", {owner_uuid: "ownerUuidValue"});
57     });
58
59     it("#update ignores fields listed as readonly", async () => {
60         axiosInstance.put = jest.fn(() => Promise.resolve({data: {}}));
61         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
62         // UUID fields are read-only on all resources.
63         await commonResourceService.update('resource-uuid', { uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
64         expect(axiosInstance.put).toHaveBeenCalledWith("/resource/resource-uuid", {owner_uuid: "ownerUuidValue"});
65     });
66
67     it("#delete", async () => {
68         axiosMock
69             .onDelete("/resource/uuid")
70             .reply(200, { deleted_at: "now" });
71
72         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
73         const resource = await commonResourceService.delete("uuid");
74         expect(resource).toEqual({ deletedAt: "now" });
75     });
76
77     it("#get", async () => {
78         axiosMock
79             .onGet("/resource/uuid")
80             .reply(200, {
81                 modified_at: "now",
82                 properties: {
83                     responsible_owner_uuid: "another_owner"
84                 }
85             });
86
87         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
88         const resource = await commonResourceService.get("uuid");
89         // Only first level keys are mapped to camel case
90         expect(resource).toEqual({
91             modifiedAt: "now",
92             properties: {
93                 responsible_owner_uuid: "another_owner"
94             }
95         });
96     });
97
98     it("#list", async () => {
99         axiosMock
100             .onGet("/resource")
101             .reply(200, {
102                 kind: "kind",
103                 offset: 2,
104                 limit: 10,
105                 items: [{
106                     modified_at: "now",
107                     properties: {
108                         is_active: true
109                     }
110                 }],
111                 items_available: 20
112             });
113
114         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
115         const resource = await commonResourceService.list({ limit: 10, offset: 1 });
116         // First level keys are mapped to camel case inside "items" arrays
117         expect(resource).toEqual({
118             kind: "kind",
119             offset: 2,
120             limit: 10,
121             items: [{
122                 modifiedAt: "now",
123                 properties: {
124                     is_active: true
125                 }
126             }],
127             itemsAvailable: 20
128         });
129     });
130
131     it("#list using POST when query string is too big", async () => {
132         axiosMock
133             .onAny("/resource")
134             .reply(200);
135         const tooBig = 'x'.repeat(1500);
136         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
137         await commonResourceService.list({ filters: tooBig });
138         expect(axiosMock.history.get.length).toBe(0);
139         expect(axiosMock.history.post.length).toBe(1);
140         expect(axiosMock.history.post[0].data.get('filters')).toBe(`[${tooBig}]`);
141         expect(axiosMock.history.post[0].params._method).toBe('GET');
142     });
143
144     it("#list using GET when query string is not too big", async () => {
145         axiosMock
146             .onAny("/resource")
147             .reply(200);
148         const notTooBig = 'x'.repeat(1480);
149         const commonResourceService = new CommonResourceService(axiosInstance, "resource", actions);
150         await commonResourceService.list({ filters: notTooBig });
151         expect(axiosMock.history.post.length).toBe(0);
152         expect(axiosMock.history.get.length).toBe(1);
153         expect(axiosMock.history.get[0].params.filters).toBe(`[${notTooBig}]`);
154     });
155 });