-
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.
* scope editor base version * Display session scopes * Added authorizedScopes field to Fragment * scopeEditor shows authorizedScopes of the fragment * * Added tests to ScopeEditor *Updated button for updating fragments * Update test for FragmentService * Removed li elements Modified the namings of checkboxes
- Loading branch information
Showing
8 changed files
with
194 additions
and
3 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
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
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,73 @@ | ||
import React from 'react' | ||
import { render, screen, fireEvent } from '@testing-library/react' | ||
import '@testing-library/jest-dom/extend-expect' | ||
import ScopeEditor from './ScopeEditor' | ||
import { Fragment } from 'fragmentarium/domain/fragment' | ||
import MemorySession from 'auth/Session' | ||
|
||
describe('ScopeEditor', () => { | ||
const fragment: Fragment = { | ||
authorizedScopes: ['read:CAIC-fragments'], | ||
} as Fragment | ||
|
||
const session = new MemorySession([ | ||
'read:CAIC-fragments', | ||
'read:ITALIANNINEVEH-fragments', | ||
]) | ||
|
||
const updateScopes = jest.fn().mockResolvedValue(undefined) | ||
|
||
test('renders the component correctly', () => { | ||
render( | ||
<ScopeEditor | ||
fragment={fragment} | ||
session={session} | ||
updateScopes={updateScopes} | ||
/> | ||
) | ||
|
||
expect(screen.getByText('Permissions')).toBeInTheDocument() | ||
expect( | ||
screen.getByText( | ||
'Records with added permissions are visible only to users who have those permissions.' | ||
) | ||
).toBeInTheDocument() | ||
}) | ||
|
||
test('toggles scopes', () => { | ||
render( | ||
<ScopeEditor | ||
fragment={fragment} | ||
session={session} | ||
updateScopes={updateScopes} | ||
/> | ||
) | ||
|
||
const checkboxes = screen.getAllByRole('checkbox') | ||
expect(checkboxes).toHaveLength(2) | ||
|
||
expect(checkboxes[0]).toBeChecked() | ||
|
||
fireEvent.click(checkboxes[0]) | ||
expect(checkboxes[0]).not.toBeChecked() | ||
|
||
fireEvent.click(checkboxes[0]) | ||
expect(checkboxes[0]).toBeChecked() | ||
}) | ||
|
||
test('submits selected scopes', async () => { | ||
render( | ||
<ScopeEditor | ||
fragment={fragment} | ||
session={session} | ||
updateScopes={updateScopes} | ||
/> | ||
) | ||
|
||
const button = screen.getByText('Update Permissions') | ||
|
||
fireEvent.click(button) | ||
|
||
expect(updateScopes).toHaveBeenCalledWith(['read:CAIC-fragments']) | ||
}) | ||
}) |
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,80 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { Fragment } from 'fragmentarium/domain/fragment' | ||
import MemorySession, { Session } from 'auth/Session' | ||
import { Button } from 'react-bootstrap' | ||
interface ScopeEditorProps { | ||
fragment: Fragment | ||
session: Session | ||
updateScopes: (scopes: string[]) => Promise<void> | ||
} | ||
|
||
function isMemorySession(session: Session): session is MemorySession { | ||
return !session.isGuestSession() | ||
} | ||
|
||
const ScopeEditor: React.FC<ScopeEditorProps> = ({ | ||
fragment, | ||
session, | ||
updateScopes, | ||
}) => { | ||
const SCOPES = [ | ||
'CAIC', | ||
'ItalianNineveh', | ||
'SipparLibrary', | ||
'UrukLBU', | ||
'SipparIstanbul', | ||
] | ||
const [selectedScopes, setSelectedScopes] = useState<string[]>( | ||
fragment.authorizedScopes || [] | ||
) | ||
const [fragmentScopes, setFragmentScopes] = useState<string[]>([]) | ||
useEffect(() => { | ||
if (isMemorySession(session)) { | ||
const scopes = SCOPES.filter((scope) => | ||
session.hasApplicationScope('read' + scope + 'Fragments') | ||
) | ||
const upperCaseScopes = scopes.map((scope) => scope.toUpperCase()) | ||
const reformattedScopes = upperCaseScopes.map( | ||
(scope) => 'read:' + scope + '-fragments' | ||
) | ||
setFragmentScopes(reformattedScopes) | ||
} | ||
}, [session]) | ||
const handleScopeChange = (scope: string) => { | ||
setSelectedScopes((prevScopes) => | ||
prevScopes.includes(scope) | ||
? prevScopes.filter((s) => s !== scope) | ||
: [...prevScopes, scope] | ||
) | ||
} | ||
|
||
const handleSubmit = async () => { | ||
await updateScopes(selectedScopes) | ||
} | ||
const getDisplayScope = (scope: string) => { | ||
return scope.replace('read:', '').replace('-fragments', '') | ||
} | ||
return ( | ||
<div> | ||
<h3>Permissions</h3> | ||
<p> | ||
Records with added permissions are visible only to users who have those | ||
permissions. | ||
</p> | ||
{fragmentScopes.map((scope) => ( | ||
<div key={scope}> | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={selectedScopes.includes(scope)} | ||
onChange={() => handleScopeChange(scope)} | ||
/> | ||
Restrict it to users with {getDisplayScope(scope)} permissions | ||
</label> | ||
</div> | ||
))} | ||
<Button onClick={handleSubmit}>Update Permissions</Button> | ||
</div> | ||
) | ||
} | ||
export default ScopeEditor |