Skip to content

Commit

Permalink
chore: adjust implementation and test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Oct 12, 2023
1 parent 9c8ff4c commit 69e9e5f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
hasAuthority,
getInterpretationAccess,
getCommentAccess,
} from '../getInterpretationAccess.js'
Expand Down Expand Up @@ -34,6 +35,18 @@ const userJane = {
}

describe('interpretation and comment access', () => {
describe('hasAuthority', () => {
it('throws an error when no authority is provided', () => {
expect(() => hasAuthority([], undefined)).toThrow(
'"hasAuthority" requires "authority" to be a populated string but received undefined'
)
})
it('throws an error when authority is not a string', () => {
expect(() => hasAuthority([], 12)).toThrow(
'"hasAuthority" requires "authority" to be a populated string but received 12'
)
})
})
describe('getInterpretationAccess', () => {
it('returns true for all accesses for superuser', () => {
const interpretation = {
Expand Down Expand Up @@ -129,7 +142,7 @@ describe('interpretation and comment access', () => {
})
})

it('returns false for all accesses when no currentUser provided', () => {
it('throws an error for all accesses when no currentUser provided', () => {
const interpretation = {
access: {
write: false,
Expand All @@ -138,15 +151,12 @@ describe('interpretation and comment access', () => {
createdBy: userJane,
}

expect(getInterpretationAccess(interpretation)).toMatchObject({
share: false,
comment: false,
edit: false,
delete: false,
})
expect(() => getInterpretationAccess(interpretation)).toThrow(
'"hasAuthority" requires "authorities" to be an array or set of authorities (strings)'
)
})

it('returns false for all accesses when currentUser missing authorities', () => {
it('throws an error when currentUser is missing authorities', () => {
const interpretation = {
access: {
write: false,
Expand All @@ -155,16 +165,13 @@ describe('interpretation and comment access', () => {
createdBy: userJane,
}

expect(
expect(() =>
getInterpretationAccess(interpretation, {
id: 'usernoauthorties',
})
).toMatchObject({
share: false,
comment: false,
edit: false,
delete: false,
})
).toThrow(
'"hasAuthority" requires "authorities" to be an array or set of authorities (strings)'
)
})
})

Expand Down
23 changes: 14 additions & 9 deletions src/components/Interpretations/common/getInterpretationAccess.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// For backwards compatibility
// accept both Set (from the old d2.currentUser object) and array
const hasAuthority = (authorities, authority) => {
if (!authority || typeof authority !== 'string) {
throw new Error(`"hasAuthority" requires "authority" to be a populated string but received ${authority}`)
} else if (Array.isArray(authorities)) {
return authorities.includes(authority)
} else if (typeof authorities.has === 'function') {
return authorities.has(authority)
} else {
throw new Error(`"hasAuthority" requires "authorities" to be an array or set of authorities (strings)`)
export const hasAuthority = (authorities, authority) => {
if (!authority || typeof authority !== 'string') {
throw new Error(
`"hasAuthority" requires "authority" to be a populated string but received ${authority}`
)
}
if (!(Array.isArray(authorities) || authorities instanceof Set)) {
throw new Error(
`"hasAuthority" requires "authorities" to be an array or set of authorities (strings)`
)
}

return Array.isArray(authorities)
? authorities.includes(authority)
: authorities.has(authority)
}

const isSuperuser = (authorities) => hasAuthority(authorities, 'ALL')
Expand Down

0 comments on commit 69e9e5f

Please sign in to comment.