Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Dec 4, 2024
1 parent af13ff7 commit 244a675
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 34 deletions.
10 changes: 2 additions & 8 deletions frontend/common/services/useRolePermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,12 @@ export async function getRoleProjectPermissions(
typeof rolePermissionService.endpoints.getRoleProjectPermissions.initiate
>[1],
) {
store.dispatch(
return store.dispatch(
rolePermissionService.endpoints.getRoleProjectPermissions.initiate(
data,
options,
),
)
return Promise.all(
store.dispatch(rolePermissionService.util.getRunningQueriesThunk()),
)
}

export async function getRoleEnvironmentPermissions(
Expand All @@ -139,15 +136,12 @@ export async function getRoleEnvironmentPermissions(
typeof rolePermissionService.endpoints.getRoleEnvironmentPermissions.initiate
>[1],
) {
store.dispatch(
return store.dispatch(
rolePermissionService.endpoints.getRoleEnvironmentPermissions.initiate(
data,
options,
),
)
return Promise.all(
store.dispatch(rolePermissionService.util.getRunningQueriesThunk()),
)
}

export async function createRolePermissions(
Expand Down
3 changes: 3 additions & 0 deletions frontend/common/stores/account-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Constants from 'common/constants'
import dataRelay from 'data-relay'
import { sortBy } from 'lodash'
import Project from 'common/project'
import { getStore } from 'common/store'
import { service } from "common/service";

const controller = {
acceptInvite: (id) => {
Expand Down Expand Up @@ -341,6 +343,7 @@ const controller = {
API.reset().finally(() => {
store.model = user
store.organisation = null
getStore().dispatch(service.util.resetApiState())
store.trigger('logout')
})
})
Expand Down
18 changes: 9 additions & 9 deletions frontend/e2e/init.cafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@ fixture`E2E Tests`.requestHooks(logger).before(async () => {
// await logout()
// })
//
test('Organisation-permission', async () => {
await organisationPermissionTest()
await logout()
})
// test('Organisation-permission', async () => {
// await organisationPermissionTest()
// await logout()
// })

// test('Project-permission', async () => {
// await projectPermissionTest()
// await logout()
// })
//
// test('Environment-permission', async () => {
// await environmentPermissionTest()
// await logout()
// })

test('Environment-permission', async () => {
await environmentPermissionTest()
await logout()
})

// test('Roles', async () => {
// await rolesTest()
Expand Down
7 changes: 5 additions & 2 deletions frontend/e2e/tests/environment-permission-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
gotoTraits,
log,
login,
toggleFeature,
} from '../helpers.cafe'
toggleFeature, waitForElementNotExist
} from "../helpers.cafe";
import { PASSWORD, E2E_NON_ADMIN_USER_WITH_ENV_PERMISSIONS } from '../config'
import { Selector, t } from 'testcafe'

Expand All @@ -25,4 +25,7 @@ export default async function () {
await gotoTraits()
const createTraitBtn = Selector(byId('add-trait'))
await t.expect(createTraitBtn.hasAttribute('disabled')).ok()
log('User without permissions cannot see audit logs')
await waitForElementNotExist(byId('audit-log-link'))

}
4 changes: 1 addition & 3 deletions frontend/e2e/tests/organisation-permission-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { Selector, t } from 'testcafe'

export default async function () {
const projectName = 'Test Project'
log('Login')
await login(E2E_NON_ADMIN_USER_WITH_ORG_PERMISSIONS, PASSWORD)
log('User without permissions cannot see any Project')
Expand All @@ -29,9 +28,8 @@ export default async function () {
await click(byId('users-and-permissions'))
await clickByText('Groups')
await waitForElementClickable("#btn-invite-groups")

log('Login as project user')
await logout(t)
log('Login as project user')
await login(E2E_NON_ADMIN_USER_WITH_PROJECT_PERMISSIONS, PASSWORD)
log('User cannot manage users or groups')
await click(byId('users-and-permissions'))
Expand Down
5 changes: 3 additions & 2 deletions frontend/e2e/tests/project-permission-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from '../helpers.cafe';
import {
PASSWORD,
E2E_NON_ADMIN_USER_WITH_PROJECT_PERMISSIONS,
} from '../config'
E2E_NON_ADMIN_USER_WITH_PROJECT_PERMISSIONS, E2E_NON_ADMIN_USER_WITH_ENV_PERMISSIONS
} from "../config";
import { Selector, t } from 'testcafe'

export default async function () {
Expand Down Expand Up @@ -40,4 +40,5 @@ export default async function () {
await t.expect(createSegmentBtn.hasAttribute('disabled')).ok()
log('User with permissions can see the Audit Logs')
await click(byId('audit-log-link'))
log('Login')
}
19 changes: 16 additions & 3 deletions frontend/web/components/pages/EnvironmentSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Icon from 'components/Icon'
import PageTitle from 'components/PageTitle'
import { getStore } from 'common/store'
import { getRoles } from 'common/services/useRole'
import { getRoleEnvironmentPermissions } from 'common/services/useRolePermission'
import AccountStore from 'common/stores/account-store'
import { Link } from 'react-router-dom'
import { enableFeatureVersioning } from 'common/services/useEnableFeatureVersioning'
Expand Down Expand Up @@ -66,9 +67,21 @@ const EnvironmentSettingsPage = class extends Component {
{ organisation_id: AccountStore.getOrganisation().id },
{ forceRefetch: true },
).then((roles) => {
if (roles.error) {
return
}
getRoleEnvironmentPermissions(
getStore(),
{
env_id: env.id,
organisation_id: AccountStore.getOrganisation().id,
role_id: roles.data.results[0].id,
},
{ forceRefetch: true },
).then((res) => {
debugger
const matchingItems = roles.data.results.filter((item1) =>
res.data.results.some((item2) => item2.role === item1.id),
)
this.setState({ roles: matchingItems })
})
})

if (Utils.getPlansPermission('METADATA')) {
Expand Down
18 changes: 17 additions & 1 deletion frontend/web/components/pages/ProjectSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Link } from 'react-router-dom'
import Setting from 'components/Setting'
import PlanBasedBanner from 'components/PlanBasedAccess'
import classNames from 'classnames'
import { getRoleProjectPermissions } from 'common/services/useRolePermission'

const ProjectSettingsPage = class extends Component {
static displayName = 'ProjectSettingsPage'
Expand Down Expand Up @@ -58,7 +59,22 @@ const ProjectSettingsPage = class extends Component {
getStore(),
{ organisation_id: AccountStore.getOrganisation().id },
{ forceRefetch: true },
)
).then((roles) => {
getRoleProjectPermissions(
getStore(),
{
organisation_id: AccountStore.getOrganisation().id,
project_id: this.props.match.params.projectId,
role_id: roles.data.results[0].id,
},
{ forceRefetch: true },
).then((res) => {
const matchingItems = roles.data.results.filter((item1) =>
res.data.results.some((item2) => item2.role === item1.id),
)
this.setState({ roles: matchingItems })
})
})
}

onSave = () => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/components/pages/UsersAndPermissionsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ const UsersAndPermissionsInner: FC<UsersAndPermissionsInnerType> = ({
<Row space className='mt-4 mb-1'>
<h5 className='mb-0'>User Groups</h5>
{Utils.renderWithPermission(
!manageGroupsPermission.permission,
manageGroupsPermission.permission,
Constants.organisationPermissions(
'Manage User Groups',
),
Expand Down
34 changes: 29 additions & 5 deletions frontend/web/components/tags/TagValues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import React, { FC, Fragment, ReactNode } from 'react'
import Button from 'components/base/forms/Button'
import Tag from './Tag'
import { useGetTagsQuery } from 'common/services/useTag'
import Utils from 'common/utils/utils'
import Constants from 'common/constants'
import { useHasPermission } from 'common/providers/Permission'

type TagValuesType = {
onAdd?: () => void
Expand All @@ -22,6 +25,16 @@ const TagValues: FC<TagValuesType> = ({
}) => {
const { data: tags } = useGetTagsQuery({ projectId })
const Wrapper = inline ? Fragment : Row
const permissionType = Utils.getFlagsmithHasFeature('manage_tags_permission')
? 'MANAGE_TAGS'
: 'ADMIN'

const { permission: createEditTagPermission } = useHasPermission({
id: projectId,
level: 'project',
permission: permissionType,
})

return (
<Wrapper className='tag-values align-content-center'>
{children}
Expand All @@ -36,11 +49,22 @@ const TagValues: FC<TagValuesType> = ({
/>
),
)}
{!!onAdd && (
<Button size='xSmall' onClick={onAdd} type='button' theme='outline'>
Add Tag
</Button>
)}
{!!onAdd &&
Utils.renderWithPermission(
createEditTagPermission,
Constants.projectPermissions(
permissionType === 'ADMIN' ? 'Admin' : 'Manage Tags',
),
<Button
disabled={!createEditTagPermission}
size='xSmall'
onClick={onAdd}
type='button'
theme='outline'
>
Add Tag
</Button>,
)}
</Wrapper>
)
}
Expand Down

0 comments on commit 244a675

Please sign in to comment.