16115: Adds sharing token management methods to ACA service, with tests.
[arvados-workbench2.git] / src / services / api-client-authorization-service / api-client-authorization-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import axios, { AxiosInstance } from "axios";
6 // import MockAdapter from 'axios-mock-adapter';
7 import { ApiClientAuthorizationService } from "./api-client-authorization-service";
8
9
10 describe('ApiClientAuthorizationService', () => {
11     let apiClientAuthorizationService: ApiClientAuthorizationService;
12     let serverApi: AxiosInstance;
13     let actions;
14
15
16     beforeEach(() => {
17         serverApi = axios.create();
18         actions = {
19             progressFn: jest.fn(),
20         } as any;
21         apiClientAuthorizationService = new ApiClientAuthorizationService(serverApi, actions);
22     });
23
24     describe('createCollectionSharingToken', () => {
25         it('should return error on invalid collection uuid', () => {
26             expect(() => apiClientAuthorizationService.createCollectionSharingToken("foo")).toThrowError("UUID foo is not a collection");
27         });
28
29         it('should make a create request with proper scopes', async () => {
30             serverApi.post = jest.fn(() => Promise.resolve(
31                 { data: { uuid: 'zzzzz-4zz18-0123456789abcde' } }
32             ));
33             const uuid = 'zzzzz-4zz18-0123456789abcde'
34             await apiClientAuthorizationService.createCollectionSharingToken(uuid);
35             expect(serverApi.post).toHaveBeenCalledWith(
36                 '/api_client_authorizations', {
37                     scopes: [
38                         `GET /arvados/v1/collections/${uuid}`,
39                         `GET /arvados/v1/collections/${uuid}/`,
40                         `GET /arvados/v1/keep_services/accessible`,
41                     ]
42                 }
43             );
44         });
45     });
46
47     describe('listCollectionSharingToken', () => {
48         it('should return error on invalid collection uuid', () => {
49             expect(() => apiClientAuthorizationService.listCollectionSharingTokens("foo")).toThrowError("UUID foo is not a collection");
50         });
51
52         it('should make a list request with proper scopes', async () => {
53             serverApi.get = jest.fn(() => Promise.resolve(
54                 { data: { items: [{}] } }
55             ));
56             const uuid = 'zzzzz-4zz18-0123456789abcde'
57             await apiClientAuthorizationService.listCollectionSharingTokens(uuid);
58             expect(serverApi.get).toHaveBeenCalledWith(
59                 `/api_client_authorizations`, {params: {
60                     filters: '[["scopes","=","' + JSON.stringify([
61                         `GET /arvados/v1/collections/${uuid}`,
62                         `GET /arvados/v1/collections/${uuid}/`,
63                         `GET /arvados/v1/keep_services/accessible`,
64                     ]) + '"]]',
65                     select: undefined,
66                 }}
67             );
68         });
69     });
70 });