diff --git a/ui/mocks/dummyData.ts b/ui/mocks/dummyData.ts index 54284d6e57..5611f3bd63 100644 --- a/ui/mocks/dummyData.ts +++ b/ui/mocks/dummyData.ts @@ -25,6 +25,10 @@ import { Task as TaskApi, Organization, Variable, + Authorization, + AuthorizationUpdateRequest, + Permission, + PermissionResource, } from '@influxdata/influx' export const links: Links = { @@ -679,3 +683,237 @@ export const scraperTargets = [ url: 'http://localhost:9999/metrics', }, ] + +export const auth: Authorization = { + id: '03c03a8a64728000', + token: + 'RcW2uWiD-vfxujKyJCirK8un3lJsWPfiA6ulmWY_SlSITUal7Z180OwExiKKfrO98X8W6qGrd5hSGdag-hEpWw==', + status: AuthorizationUpdateRequest.StatusEnum.Active, + description: 'My token', + orgID: '039edab314789000', + org: 'a', + userID: '039edab303789000', + user: 'adminuser', + permissions: [ + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Orgs, + id: '039edab314789000', + name: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Authorizations, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Authorizations, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Buckets, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Buckets, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Dashboards, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Dashboards, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Sources, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Sources, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Tasks, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Tasks, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Telegrafs, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Telegrafs, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Users, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Users, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Variables, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Variables, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Scrapers, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Scrapers, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Secrets, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Secrets, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Labels, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Labels, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Views, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Views, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Read, + resource: { + type: PermissionResource.TypeEnum.Documents, + orgID: '039edab314789000', + org: 'a', + }, + }, + { + action: Permission.ActionEnum.Write, + resource: { + type: PermissionResource.TypeEnum.Documents, + orgID: '039edab314789000', + org: 'a', + }, + }, + ], + links: { + self: '/api/v2/authorizations/03c03a8a64728000', + user: '/api/v2/users/039edab303789000', + }, +} diff --git a/ui/src/authorizations/components/ViewTokenOverlay.test.tsx b/ui/src/authorizations/components/ViewTokenOverlay.test.tsx index 2828b87304..36db2fcf41 100644 --- a/ui/src/authorizations/components/ViewTokenOverlay.test.tsx +++ b/ui/src/authorizations/components/ViewTokenOverlay.test.tsx @@ -6,7 +6,8 @@ import {shallow} from 'enzyme' import ViewTokenOverlay from 'src/authorizations/components/ViewTokenOverlay' // Fixtures -import {authorization as auth} from 'src/authorizations/apis/__mocks__/data' +import {auth} from 'mocks/dummyData' +import {permissions} from 'src/utils/permissions' const setup = (override?) => { const props = { @@ -14,18 +15,41 @@ const setup = (override?) => { ...override, } - const wrapper = shallow() - - return {wrapper} + return shallow() } describe('Account', () => { describe('rendering', () => { it('renders!', () => { - const {wrapper} = setup() + const wrapper = setup() expect(wrapper.exists()).toBe(true) expect(wrapper).toMatchSnapshot() }) }) + + describe('if there is all access tokens', () => { + it('renders permissions correctly', () => { + const actual = permissions(auth.permissions) + + const expected = { + 'orgs-a': ['read'], + authorizations: ['read', 'write'], + buckets: ['read', 'write'], + dashboards: ['read', 'write'], + sources: ['read', 'write'], + tasks: ['read', 'write'], + telegrafs: ['read', 'write'], + users: ['read', 'write'], + variables: ['read', 'write'], + scrapers: ['read', 'write'], + secrets: ['read', 'write'], + labels: ['read', 'write'], + views: ['read', 'write'], + documents: ['read', 'write'], + } + + expect(actual).toEqual(expected) + }) + }) }) diff --git a/ui/src/authorizations/components/ViewTokenOverlay.tsx b/ui/src/authorizations/components/ViewTokenOverlay.tsx index 1ebf8ac655..d11795b67e 100644 --- a/ui/src/authorizations/components/ViewTokenOverlay.tsx +++ b/ui/src/authorizations/components/ViewTokenOverlay.tsx @@ -62,16 +62,18 @@ export default class ViewTokenOverlay extends PureComponent { const p = this.props.auth.permissions.reduce((acc, {action, resource}) => { const {type} = resource const name = get(resource, 'name', '') - - let key = `${type}-${name}` - let actions = get(resource, key, []) - + let key = `${type}` if (name) { + key = `${type}-${name}` + } + + let actions = get(acc, key, []) + + if (name && actions) { return {...acc, [key]: [...actions, action]} } - actions = get(resource, type, []) - + actions = get(acc, key || resource.type, []) return {...acc, [type]: [...actions, action]} }, {}) diff --git a/ui/src/authorizations/components/__snapshots__/ViewTokenOverlay.test.tsx.snap b/ui/src/authorizations/components/__snapshots__/ViewTokenOverlay.test.tsx.snap index f8093bac55..732279a86c 100644 --- a/ui/src/authorizations/components/__snapshots__/ViewTokenOverlay.test.tsx.snap +++ b/ui/src/authorizations/components/__snapshots__/ViewTokenOverlay.test.tsx.snap @@ -6,11 +6,11 @@ exports[`Account rendering renders! 1`] = ` > + + + + @@ -37,8 +56,223 @@ exports[`Account rendering renders! 1`] = ` title="buckets" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ui/src/utils/permissions.ts b/ui/src/utils/permissions.ts new file mode 100644 index 0000000000..1b730c3698 --- /dev/null +++ b/ui/src/utils/permissions.ts @@ -0,0 +1,25 @@ +import {Permission} from '@influxdata/influx' +import {get} from 'lodash' + +export const permissions = ( + permissions: Permission[] +): {[x: string]: Permission.ActionEnum[]} => { + const p = permissions.reduce((acc, {action, resource}) => { + const {type} = resource + const name = get(resource, 'name', '') + let key = `${type}` + if (name) { + key = `${type}-${name}` + } + + let actions = get(acc, key, []) + + if (name && actions) { + return {...acc, [key]: [...actions, action]} + } + + actions = get(acc, key || resource.type, []) + return {...acc, [type]: [...actions, action]} + }, {}) + return p +}