1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { CommonResourceService } from "./common-resource-service";
6 import axios, { AxiosInstance } from "axios";
7 import MockAdapter from "axios-mock-adapter";
8 import { Resource } from "models/resource";
9 import { ApiActions } from "services/api/api-actions";
11 const actions: ApiActions = {
12 progressFn: (id: string, working: boolean) => {},
13 errorFn: (id: string, message: string) => {}
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 service = new Service(axiosInstance, actions);
20 Object.keys(service).map(key => service[key] = jest.fn());
24 describe("CommonResourceService", () => {
25 let axiosInstance: AxiosInstance;
26 let axiosMock: MockAdapter;
29 axiosInstance = axios.create();
30 axiosMock = new MockAdapter(axiosInstance);
33 it("#create", async () => {
36 .reply(200, { owner_uuid: "ownerUuidValue" });
38 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
39 const resource = await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
40 expect(resource).toEqual({ ownerUuid: "ownerUuidValue" });
43 it("#create maps request params to snake case", async () => {
44 axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
45 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
46 await commonResourceService.create({ ownerUuid: "ownerUuidValue" });
47 expect(axiosInstance.post).toHaveBeenCalledWith("/resources", {resource: {owner_uuid: "ownerUuidValue"}});
50 it("#create ignores fields listed as readonly", async () => {
51 axiosInstance.post = jest.fn(() => Promise.resolve({data: {}}));
52 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
53 // UUID fields are read-only on all resources.
54 await commonResourceService.create({ uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
55 expect(axiosInstance.post).toHaveBeenCalledWith("/resources", {resource: {owner_uuid: "ownerUuidValue"}});
58 it("#update ignores fields listed as readonly", async () => {
59 axiosInstance.put = jest.fn(() => Promise.resolve({data: {}}));
60 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
61 // UUID fields are read-only on all resources.
62 await commonResourceService.update('resource-uuid', { uuid: "this should be ignored", ownerUuid: "ownerUuidValue" });
63 expect(axiosInstance.put).toHaveBeenCalledWith("/resources/resource-uuid", {resource: {owner_uuid: "ownerUuidValue"}});
66 it("#delete", async () => {
68 .onDelete("/resources/uuid")
69 .reply(200, { deleted_at: "now" });
71 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
72 const resource = await commonResourceService.delete("uuid");
73 expect(resource).toEqual({ deletedAt: "now" });
76 it("#get", async () => {
78 .onGet("/resources/uuid")
82 responsible_owner_uuid: "another_owner"
86 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
87 const resource = await commonResourceService.get("uuid");
88 // Only first level keys are mapped to camel case
89 expect(resource).toEqual({
92 responsible_owner_uuid: "another_owner"
97 it("#list", async () => {
113 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
114 const resource = await commonResourceService.list({ limit: 10, offset: 1 });
115 // First level keys are mapped to camel case inside "items" arrays
116 expect(resource).toEqual({
130 it("#list using POST when query string is too big", async () => {
134 const tooBig = 'x'.repeat(1500);
135 const commonResourceService = new CommonResourceService(axiosInstance, "resources", actions);
136 await commonResourceService.list({ filters: tooBig });
137 expect(axiosMock.history.get.length).toBe(0);
138 expect(axiosMock.history.post.length).toBe(1);
139 const postParams = new URLSearchParams(axiosMock.history.post[0].data);
140 expect(postParams.get('filters')).toBe(`[${tooBig}]`);
141 expect(postParams.get('_method')).toBe('GET');
144 it("#list using GET when query string is not too big", async () => {
148 const notTooBig = 'x'.repeat(1480);
149 const commonResourceService = new CommonResourceService(axiosInstance, "resources", 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}]`);