16115: Adds sharing token management methods to ACA service, with tests.
authorLucas Di Pentima <lucas.dipentima@curii.com>
Mon, 18 Apr 2022 19:43:39 +0000 (16:43 -0300)
committerLucas Di Pentima <lucas.dipentima@curii.com>
Wed, 20 Apr 2022 17:06:58 +0000 (14:06 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima@curii.com>

src/services/api-client-authorization-service/api-client-authorization-service.test.ts [new file with mode: 0644]
src/services/api-client-authorization-service/api-client-authorization-service.ts

diff --git a/src/services/api-client-authorization-service/api-client-authorization-service.test.ts b/src/services/api-client-authorization-service/api-client-authorization-service.test.ts
new file mode 100644 (file)
index 0000000..cc1942c
--- /dev/null
@@ -0,0 +1,70 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import axios, { AxiosInstance } from "axios";
+// import MockAdapter from 'axios-mock-adapter';
+import { ApiClientAuthorizationService } from "./api-client-authorization-service";
+
+
+describe('ApiClientAuthorizationService', () => {
+    let apiClientAuthorizationService: ApiClientAuthorizationService;
+    let serverApi: AxiosInstance;
+    let actions;
+
+
+    beforeEach(() => {
+        serverApi = axios.create();
+        actions = {
+            progressFn: jest.fn(),
+        } as any;
+        apiClientAuthorizationService = new ApiClientAuthorizationService(serverApi, actions);
+    });
+
+    describe('createCollectionSharingToken', () => {
+        it('should return error on invalid collection uuid', () => {
+            expect(() => apiClientAuthorizationService.createCollectionSharingToken("foo")).toThrowError("UUID foo is not a collection");
+        });
+
+        it('should make a create request with proper scopes', async () => {
+            serverApi.post = jest.fn(() => Promise.resolve(
+                { data: { uuid: 'zzzzz-4zz18-0123456789abcde' } }
+            ));
+            const uuid = 'zzzzz-4zz18-0123456789abcde'
+            await apiClientAuthorizationService.createCollectionSharingToken(uuid);
+            expect(serverApi.post).toHaveBeenCalledWith(
+                '/api_client_authorizations', {
+                    scopes: [
+                        `GET /arvados/v1/collections/${uuid}`,
+                        `GET /arvados/v1/collections/${uuid}/`,
+                        `GET /arvados/v1/keep_services/accessible`,
+                    ]
+                }
+            );
+        });
+    });
+
+    describe('listCollectionSharingToken', () => {
+        it('should return error on invalid collection uuid', () => {
+            expect(() => apiClientAuthorizationService.listCollectionSharingTokens("foo")).toThrowError("UUID foo is not a collection");
+        });
+
+        it('should make a list request with proper scopes', async () => {
+            serverApi.get = jest.fn(() => Promise.resolve(
+                { data: { items: [{}] } }
+            ));
+            const uuid = 'zzzzz-4zz18-0123456789abcde'
+            await apiClientAuthorizationService.listCollectionSharingTokens(uuid);
+            expect(serverApi.get).toHaveBeenCalledWith(
+                `/api_client_authorizations`, {params: {
+                    filters: '[["scopes","=","' + JSON.stringify([
+                        `GET /arvados/v1/collections/${uuid}`,
+                        `GET /arvados/v1/collections/${uuid}/`,
+                        `GET /arvados/v1/keep_services/accessible`,
+                    ]) + '"]]',
+                    select: undefined,
+                }}
+            );
+        });
+    });
+});
\ No newline at end of file
index 386c974755f720b931ddc75b9d415eeed6e7c309..012fdb15c927364b3a03f4eca356a18bbb3a6bd2 100644 (file)
@@ -5,10 +5,39 @@
 import { AxiosInstance } from "axios";
 import { ApiActions } from 'services/api/api-actions';
 import { ApiClientAuthorization } from 'models/api-client-authorization';
-import { CommonService } from 'services/common-service/common-service';
+import { CommonService, ListResults } from 'services/common-service/common-service';
+import { extractUuidObjectType, ResourceObjectType } from "models/resource";
+import { FilterBuilder } from "services/api/filter-builder";
 
 export class ApiClientAuthorizationService extends CommonService<ApiClientAuthorization> {
     constructor(serverApi: AxiosInstance, actions: ApiActions) {
         super(serverApi, "api_client_authorizations", actions);
     }
-} 
\ No newline at end of file
+
+    createCollectionSharingToken(uuid: string): Promise<ApiClientAuthorization> {
+        if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
+            throw new Error(`UUID ${uuid} is not a collection`);
+        }
+        return this.create({
+            scopes: [
+                `GET /arvados/v1/collections/${uuid}`,
+                `GET /arvados/v1/collections/${uuid}/`,
+                `GET /arvados/v1/keep_services/accessible`,
+            ]
+        });
+    }
+
+    listCollectionSharingTokens(uuid: string): Promise<ListResults<ApiClientAuthorization>> {
+        if (extractUuidObjectType(uuid) !== ResourceObjectType.COLLECTION) {
+            throw new Error(`UUID ${uuid} is not a collection`);
+        }
+        return this.list({
+            filters: new FilterBuilder()
+                .addEqual("scopes", JSON.stringify([
+                    `GET /arvados/v1/collections/${uuid}`,
+                    `GET /arvados/v1/collections/${uuid}/`,
+                    "GET /arvados/v1/keep_services/accessible",
+                ])).getFilters()
+        });
+    }
+}
\ No newline at end of file