31fa046ea05ae5ef4ae8fb8aa24b1469c8139ed5
[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 { ApiClientAuthorizationService } from "./api-client-authorization-service";
7
8
9 describe('ApiClientAuthorizationService', () => {
10     let apiClientAuthorizationService: ApiClientAuthorizationService;
11     let serverApi: AxiosInstance;
12     let actions;
13
14     beforeEach(() => {
15         serverApi = axios.create();
16         actions = {
17             progressFn: jest.fn(),
18         } as any;
19         apiClientAuthorizationService = new ApiClientAuthorizationService(serverApi, actions);
20     });
21
22     describe('createCollectionSharingToken', () => {
23         it('should return error on invalid collection uuid', () => {
24             expect(() => apiClientAuthorizationService.createCollectionSharingToken("foo")).toThrowError("UUID foo is not a collection");
25         });
26
27         it('should make a create request with proper scopes', async () => {
28             serverApi.post = jest.fn(() => Promise.resolve(
29                 { data: { uuid: 'zzzzz-4zz18-0123456789abcde' } }
30             ));
31             const uuid = 'zzzzz-4zz18-0123456789abcde'
32             await apiClientAuthorizationService.createCollectionSharingToken(uuid);
33             expect(serverApi.post).toHaveBeenCalledWith(
34                 '/api_client_authorizations', {
35                     scopes: [
36                         `GET /arvados/v1/collections/${uuid}`,
37                         `GET /arvados/v1/collections/${uuid}/`,
38                         `GET /arvados/v1/keep_services/accessible`,
39                     ]
40                 }
41             );
42         });
43     });
44
45     describe('listCollectionSharingToken', () => {
46         it('should return error on invalid collection uuid', () => {
47             expect(() => apiClientAuthorizationService.listCollectionSharingTokens("foo")).toThrowError("UUID foo is not a collection");
48         });
49
50         it('should make a list request with proper scopes', async () => {
51             serverApi.get = jest.fn(() => Promise.resolve(
52                 { data: { items: [{}] } }
53             ));
54             const uuid = 'zzzzz-4zz18-0123456789abcde'
55             await apiClientAuthorizationService.listCollectionSharingTokens(uuid);
56             expect(serverApi.get).toHaveBeenCalledWith(
57                 `/api_client_authorizations`, {params: {
58                     filters: '[["scopes","=","' + JSON.stringify([
59                         `GET /arvados/v1/collections/${uuid}`,
60                         `GET /arvados/v1/collections/${uuid}/`,
61                         `GET /arvados/v1/keep_services/accessible`,
62                     ]) + '"]]',
63                     select: undefined,
64                 }}
65             );
66         });
67     });
68 });