forked from artifacthub/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some missing tests for authz view in UI (artifacthub#677)
Signed-off-by: Cintia Sanchez Garcia <cynthiasg@icloud.com>
- Loading branch information
1 parent
10e6a40
commit f8b9779
Showing
31 changed files
with
1,996 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorization_enabled": true, | ||
"predefined_policy": null, | ||
"custom_policy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]", | ||
"policy_data": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["addOrganizationMember", "addOrganizationRepository", "deleteOrganizationMember", "deleteOrganizationRepository"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"result": "https://play.openpolicyagent.org/p/wkFwnwti9Z" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { AppCtx } from '../../context/AppCtx'; | ||
import { AuthorizerAction, AuthorizerInput } from '../../types'; | ||
import ActionBtn from './ActionBtn'; | ||
|
||
jest.mock('../../utils/authorizer', () => ({ | ||
check: (params: AuthorizerInput) => { | ||
if (params.user === 'member') { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
})); | ||
|
||
const onClickMock = jest.fn(); | ||
|
||
const defaultProps = { | ||
testId: 'test', | ||
onClick: onClickMock, | ||
action: AuthorizerAction.AddOrganizationMember, | ||
}; | ||
|
||
const mockCtx = { | ||
user: { alias: 'test', email: 'test@test.com' }, | ||
prefs: { | ||
controlPanel: { selectedOrg: 'orgTest' }, | ||
search: { limit: 25 }, | ||
theme: { | ||
configured: 'light', | ||
automatic: false, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('ActionBtn', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('renders correctly', () => { | ||
const result = render( | ||
<AppCtx.Provider value={{ ctx: mockCtx, dispatch: jest.fn() }}> | ||
<ActionBtn {...defaultProps}> | ||
<div>button content</div> | ||
</ActionBtn> | ||
</AppCtx.Provider> | ||
); | ||
expect(result.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders enabled button', () => { | ||
const { getByTestId, getByText } = render( | ||
<AppCtx.Provider value={{ ctx: mockCtx, dispatch: jest.fn() }}> | ||
<ActionBtn {...defaultProps}> | ||
<div>button content</div> | ||
</ActionBtn> | ||
</AppCtx.Provider> | ||
); | ||
|
||
const btn = getByTestId(defaultProps.testId); | ||
expect(btn).toBeInTheDocument(); | ||
expect(btn).not.toHaveClass('disabled'); | ||
|
||
fireEvent.click(btn); | ||
expect(onClickMock).toHaveBeenCalledTimes(1); | ||
|
||
expect(getByText('button content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders disabled button', () => { | ||
const { getByTestId, getByText } = render( | ||
<AppCtx.Provider | ||
value={{ | ||
ctx: { | ||
...mockCtx, | ||
user: { alias: 'member', email: 'test@test.com' }, | ||
}, | ||
dispatch: jest.fn(), | ||
}} | ||
> | ||
<ActionBtn {...defaultProps}> | ||
<div>button content</div> | ||
</ActionBtn> | ||
</AppCtx.Provider> | ||
); | ||
|
||
const btn = getByTestId(defaultProps.testId); | ||
expect(btn).toBeInTheDocument(); | ||
expect(btn).toHaveClass('disabled'); | ||
|
||
fireEvent.click(btn); | ||
expect(onClickMock).toHaveBeenCalledTimes(0); | ||
|
||
expect(getByText('button content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays tooltip', async () => { | ||
const { getByTestId, getByRole, getByText } = render( | ||
<AppCtx.Provider | ||
value={{ | ||
ctx: { | ||
...mockCtx, | ||
user: { alias: 'member', email: 'test@test.com' }, | ||
}, | ||
dispatch: jest.fn(), | ||
}} | ||
> | ||
<ActionBtn {...defaultProps}> | ||
<div>button content</div> | ||
</ActionBtn> | ||
</AppCtx.Provider> | ||
); | ||
|
||
const btn = getByTestId(defaultProps.testId); | ||
fireEvent.mouseEnter(btn); | ||
|
||
await waitFor(() => { | ||
expect(getByRole('tooltip')).toBeInTheDocument(); | ||
expect(getByText('You are not allowed to perform this action')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
web/src/layout/controlPanel/__snapshots__/ActionBtn.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": false, | ||
"predefinedPolicy": null, | ||
"customPolicy": null, | ||
"policyData": null | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/10.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": null, | ||
"customPolicy": "package artifacthub.authz↵↵allow = true↵allowed_actions = [\"all\"]", | ||
"policyData": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/11.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": null, | ||
"customPolicy": "package artifacthub.authz↵↵allow = true↵allowed_actions = [\"all\"]", | ||
"policyData": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": false, | ||
"predefinedPolicy": null, | ||
"customPolicy": null, | ||
"policyData": null | ||
} |
26 changes: 26 additions & 0 deletions
26
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/3.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": "rbac.v1", | ||
"customPolicy": null, | ||
"policyData": { | ||
"roles": { | ||
"owner": { | ||
"users": ["demo", "cintia"] | ||
}, | ||
"customRole1": { | ||
"users": [], | ||
"allowed_actions": [ | ||
"addOrganizationMember", | ||
"addOrganizationRepository", | ||
"deleteOrganizationMember", | ||
"deleteOrganizationRepository", | ||
"getAuthorizationPolicy", | ||
"transferOrganizationRepository", | ||
"updateAuthorizationPolicy", | ||
"updateOrganization", | ||
"updateOrganizationRepository" | ||
] | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/4.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": null, | ||
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]", | ||
"policyData": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/5.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": null, | ||
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]", | ||
"policyData": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
web/src/layout/controlPanel/settings/orgSettings/authorization/__fixtures__/index/6.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"authorizationEnabled": true, | ||
"predefinedPolicy": null, | ||
"customPolicy": "package artifacthub.authz\n\nallow = true\nallowed_actions = [\"all\"]", | ||
"policyData": {} | ||
} |
Oops, something went wrong.