blob: 3c29076ea8bed2edd269c93fbbf4d06fe06ff6a6 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import sinon from 'sinon';
7
8import * as permissions from './permissions.js';
9import * as example from 'shared/test/constants-permissions.js';
10import * as exampleIssues from 'shared/test/constants-issueV0.js';
11
12import {prpcClient} from 'prpc-client-instance.js';
13
14let dispatch;
15
16describe('permissions reducers', () => {
17 it('root reducer initial state', () => {
18 const actual = permissions.reducer(undefined, {type: null});
19 const expected = {
20 byName: {},
21 requests: {
22 batchGet: {error: null, requesting: false},
23 },
24 };
25 assert.deepEqual(actual, expected);
26 });
27
28 it('byName updates on BATCH_GET_SUCCESS', () => {
29 const action = {
30 type: permissions.BATCH_GET_SUCCESS,
31 permissionSets: [example.PERMISSION_SET_ISSUE],
32 };
33 const actual = permissions.byNameReducer({}, action);
34 const expected = {
35 [example.PERMISSION_SET_ISSUE.resource]: example.PERMISSION_SET_ISSUE,
36 };
37 assert.deepEqual(actual, expected);
38 });
39});
40
41describe('permissions selectors', () => {
42 it('byName', () => {
43 const state = {permissions: {byName: example.BY_NAME}};
44 const actual = permissions.byName(state);
45 assert.deepEqual(actual, example.BY_NAME);
46 });
47});
48
49describe('permissions action creators', () => {
50 beforeEach(() => {
51 sinon.stub(prpcClient, 'call');
52 dispatch = sinon.stub();
53 });
54
55 afterEach(() => {
56 prpcClient.call.restore();
57 });
58
59 describe('batchGet', () => {
60 it('success', async () => {
61 const response = {permissionSets: [example.PERMISSION_SET_ISSUE]};
62 prpcClient.call.returns(Promise.resolve(response));
63
64 await permissions.batchGet([exampleIssues.NAME])(dispatch);
65
66 sinon.assert.calledWith(dispatch, {type: permissions.BATCH_GET_START});
67
68 const args = {names: [exampleIssues.NAME]};
69 sinon.assert.calledWith(
70 prpcClient.call, 'monorail.v3.Permissions',
71 'BatchGetPermissionSets', args);
72
73 const action = {
74 type: permissions.BATCH_GET_SUCCESS,
75 permissionSets: [example.PERMISSION_SET_ISSUE],
76 };
77 sinon.assert.calledWith(dispatch, action);
78 });
79
80 it('failure', async () => {
81 prpcClient.call.throws();
82
83 await permissions.batchGet([exampleIssues.NAME])(dispatch);
84
85 const action = {
86 type: permissions.BATCH_GET_FAILURE,
87 error: sinon.match.any,
88 };
89 sinon.assert.calledWith(dispatch, action);
90 });
91
92 it('fills in permissions field', async () => {
93 const response = {permissionSets: [{resource: exampleIssues.NAME}]};
94 prpcClient.call.returns(Promise.resolve(response));
95
96 await permissions.batchGet([exampleIssues.NAME])(dispatch);
97
98 const action = {
99 type: permissions.BATCH_GET_SUCCESS,
100 permissionSets: [{resource: exampleIssues.NAME, permissions: []}],
101 };
102 sinon.assert.calledWith(dispatch, action);
103 });
104 });
105});