Skip to content

Commit

Permalink
fix: show message to users with no authority to approve data (#102)
Browse files Browse the repository at this point in the history
* fix: show message to users with no authority to approve data

* chore: remove unused import
  • Loading branch information
HendrikThePendric authored Sep 30, 2021
1 parent 88c2e06 commit b4fae37
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
24 changes: 14 additions & 10 deletions src/auth/auth-wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import { ErrorMessage } from '../shared/index.js'
import { useIsAuthorized } from './use-is-authorized.js'

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

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>
)
if (hasAppAccess && hasApprovalAuthorities) {
return children
}

return children
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>
)
}

AuthWall.propTypes = {
Expand Down
27 changes: 25 additions & 2 deletions src/auth/auth-wall.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,38 @@ afterEach(() => {

describe('<AuthWall>', () => {
it('shows a noticebox for unauthorized users', () => {
useIsAuthorized.mockImplementation(() => false)
useIsAuthorized.mockImplementation(() => ({
hasAppAccess: false,
hasApprovalAuthorities: 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(() => true)
useIsAuthorized.mockImplementation(() => ({
hasAppAccess: true,
hasApprovalAuthorities: true,
}))

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

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

export const useIsAuthorized = () => {
const { authorities } = useAppContext()
return authorities.some(
const hasAppAccess = 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: 49 additions & 6 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 false for unauthorised users', () => {
it('returns the correct object for unauthorised users', () => {
const value = {
authorities: ['dummy'],
}
Expand All @@ -15,10 +15,13 @@ describe('useIsAuthorized', () => {

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

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

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

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

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

it('returns true for superusers', () => {
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', () => {
const value = {
authorities: ['ALL'],
}
Expand All @@ -43,6 +83,9 @@ describe('useIsAuthorized', () => {

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

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

0 comments on commit b4fae37

Please sign in to comment.