21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / workbench2 / 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 and no expiration date', 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         it('should make a create request with proper scopes and expiration date', async () => {
45             serverApi.post = jest.fn(() => Promise.resolve(
46                 { data: { uuid: 'zzzzz-4zz18-0123456789abcde' } }
47             ));
48             const uuid = 'zzzzz-4zz18-0123456789abcde'
49             const expDate = new Date(2022, 8, 28, 12, 0, 0);
50             await apiClientAuthorizationService.createCollectionSharingToken(uuid, expDate);
51             expect(serverApi.post).toHaveBeenCalledWith(
52                 '/api_client_authorizations', {
53                     scopes: [
54                         `GET /arvados/v1/collections/${uuid}`,
55                         `GET /arvados/v1/collections/${uuid}/`,
56                         `GET /arvados/v1/keep_services/accessible`,
57                     ],
58                     expires_at: expDate.toUTCString()
59                 }
60             );
61         });
62     });
63
64     describe('listCollectionSharingToken', () => {
65         it('should return error on invalid collection uuid', () => {
66             expect(() => apiClientAuthorizationService.listCollectionSharingTokens("foo")).toThrowError("UUID foo is not a collection");
67         });
68
69         it('should make a list request with proper scopes', async () => {
70             serverApi.get = jest.fn(() => Promise.resolve(
71                 { data: { items: [{}] } }
72             ));
73             const uuid = 'zzzzz-4zz18-0123456789abcde'
74             await apiClientAuthorizationService.listCollectionSharingTokens(uuid);
75             expect(serverApi.get).toHaveBeenCalledWith(
76                 `/api_client_authorizations`, {params: {
77                     filters: JSON.stringify([["scopes","=",[
78                         `GET /arvados/v1/collections/${uuid}`,
79                         `GET /arvados/v1/collections/${uuid}/`,
80                         'GET /arvados/v1/keep_services/accessible',
81                     ]]]),
82                     select: undefined,
83                 }}
84             );
85         });
86     });
87 });