Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert pr#102 because the bug was actually expected behavior #104

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/auth/auth-wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ import { ErrorMessage } from '../shared/index.js'
import { useIsAuthorized } from './use-is-authorized.js'

const AuthWall = ({ children }) => {
const { hasAppAccess, hasApprovalAuthorities } = useIsAuthorized()
const isAuthorized = useIsAuthorized()

if (hasAppAccess && hasApprovalAuthorities) {
return children
if (!isAuthorized) {
return (
<ErrorMessage title={i18n.t('Not authorized')}>
{i18n.t(
"You don't have access to the Data Approval App. Contact a system administrator to request access."
)}
</ErrorMessage>
)
}

const message = !hasAppAccess
? i18n.t(
"You don't have access to the Data Approval App. Contact a system administrator to request access."
)
: i18n.t(
'You are not allowed to approve data. Contact a system administrator to request the appropriate authorities.'
)

return (
<ErrorMessage title={i18n.t('Not authorized')}>{message}</ErrorMessage>
)
return children
}

AuthWall.propTypes = {
Expand Down
28 changes: 3 additions & 25 deletions src/auth/auth-wall.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NoticeBox } from '@dhis2/ui'
import { shallow } from 'enzyme'
import React from 'react'
import { ErrorMessage } from '../shared/index.js'
Expand All @@ -14,38 +15,15 @@ afterEach(() => {

describe('<AuthWall>', () => {
it('shows a noticebox for unauthorized users', () => {
useIsAuthorized.mockImplementation(() => ({
hasAppAccess: false,
hasApprovalAuthorities: false,
}))
useIsAuthorized.mockImplementation(() => false)

const wrapper = shallow(<AuthWall>Child</AuthWall>)

expect(wrapper.find(ErrorMessage)).toHaveLength(1)
expect(wrapper.prop('children')).toBe(
"You don't have access to the Data Approval App. Contact a system administrator to request access."
)
})

it('shows a noticebox for users without appropriate authorities', () => {
useIsAuthorized.mockImplementation(() => ({
hasAppAccess: true,
hasApprovalAuthorities: false,
}))

const wrapper = shallow(<AuthWall>Child</AuthWall>)

expect(wrapper.find(ErrorMessage)).toHaveLength(1)
expect(wrapper.prop('children')).toBe(
'You are not allowed to approve data. Contact a system administrator to request the appropriate authorities.'
)
})

it('renders the children for authorised users', () => {
useIsAuthorized.mockImplementation(() => ({
hasAppAccess: true,
hasApprovalAuthorities: true,
}))
useIsAuthorized.mockImplementation(() => true)

const wrapper = shallow(<AuthWall>Child</AuthWall>)

Expand Down
10 changes: 1 addition & 9 deletions src/auth/use-is-authorized.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import { useAppContext } from '../app-context/index.js'

export const useIsAuthorized = () => {
const { authorities } = useAppContext()
const hasAppAccess = authorities.some(
return authorities.some(
authority => authority === 'ALL' || authority === 'M_dhis-web-approval'
)
const hasApprovalAuthorities = authorities.some(
authority =>
authority === 'ALL' ||
authority === 'F_APPROVE_DATA' ||
authority === 'F_APPROVE_DATA_LOWER_LEVELS'
)

return { hasAppAccess, hasApprovalAuthorities }
}
55 changes: 6 additions & 49 deletions src/auth/use-is-authorized.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppContext } from '../app-context/index.js'
import { useIsAuthorized } from './use-is-authorized.js'

describe('useIsAuthorized', () => {
it('returns the correct object for unauthorised users', () => {
it('returns false for unauthorised users', () => {
const value = {
authorities: ['dummy'],
}
Expand All @@ -15,13 +15,10 @@ describe('useIsAuthorized', () => {

const { result } = renderHook(() => useIsAuthorized(), { wrapper })

expect(result.current).toEqual({
hasAppAccess: false,
hasApprovalAuthorities: false,
})
expect(result.current).toEqual(false)
})

it('returns the correct object for authorised users', () => {
it('returns true for authorised users', () => {
const value = {
authorities: ['M_dhis-web-approval'],
}
Expand All @@ -32,47 +29,10 @@ describe('useIsAuthorized', () => {

const { result } = renderHook(() => useIsAuthorized(), { wrapper })

expect(result.current).toEqual({
hasAppAccess: true,
hasApprovalAuthorities: false,
})
expect(result.current).toEqual(true)
})

it('returns the correct object for authorised users with F_APPROVE_DATA authority', () => {
const value = {
authorities: ['M_dhis-web-approval', 'F_APPROVE_DATA'],
}

const wrapper = ({ children }) => (
<AppContext.Provider value={value}>{children}</AppContext.Provider>
)

const { result } = renderHook(() => useIsAuthorized(), { wrapper })

expect(result.current).toEqual({
hasAppAccess: true,
hasApprovalAuthorities: true,
})
})

it('returns the correct object for authorised users with F_APPROVE_DATA_LOWER_LEVELS authority', () => {
const value = {
authorities: ['M_dhis-web-approval', 'F_APPROVE_DATA_LOWER_LEVELS'],
}

const wrapper = ({ children }) => (
<AppContext.Provider value={value}>{children}</AppContext.Provider>
)

const { result } = renderHook(() => useIsAuthorized(), { wrapper })

expect(result.current).toEqual({
hasAppAccess: true,
hasApprovalAuthorities: true,
})
})

it('returns the correct object for superusers', () => {
it('returns true for superusers', () => {
const value = {
authorities: ['ALL'],
}
Expand All @@ -83,9 +43,6 @@ describe('useIsAuthorized', () => {

const { result } = renderHook(() => useIsAuthorized(), { wrapper })

expect(result.current).toEqual({
hasAppAccess: true,
hasApprovalAuthorities: true,
})
expect(result.current).toEqual(true)
})
})