Repository navigation
Add an Edit link and a placeholder edit page #107
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <script setup lang="ts"> | ||
| import { computed, watch } from 'vue' | ||
| import { useRouter } from 'vue-router' | ||
| import { useResourceView } from '../composables/useResourceView' | ||
| import { useMeta } from '../composables/useMeta' | ||
| import { internalHref } from '../composables/urlUtils' | ||
|
|
||
| const { loading, error, resource, resourceUri, title, breadcrumbs, resourceLabel } = | ||
| useResourceView() | ||
| const { canEdit, loaded, error: metaError } = useMeta(resource) | ||
| const router = useRouter() | ||
|
|
||
| const resourcePath = computed(() => internalHref(resourceUri.value)) | ||
|
|
||
| // Without edit access, go back to the resource, as the legacy client does. | ||
| watch( | ||
| [loaded, canEdit], | ||
| ([isLoaded, allowed]) => { | ||
| if (isLoaded && !allowed) void router.replace(resourcePath.value) | ||
| }, | ||
| { immediate: true }, | ||
| ) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div> | ||
| <nav v-if="breadcrumbs.length > 0" class="breadcrumbs" aria-label="Breadcrumb"> | ||
| <div class="breadcrumbs__inner"> | ||
| <template v-for="item in breadcrumbs" :key="item.uri"> | ||
| <router-link :to="internalHref(item.uri)" class="breadcrumb-link">{{ | ||
| item.text | ||
| }}</router-link> | ||
| <span class="breadcrumb-sep">/</span> | ||
| </template> | ||
| <span class="breadcrumb-current">Edit</span> | ||
| </div> | ||
| </nav> | ||
|
|
||
| <main class="page-container"> | ||
| <p v-if="error || metaError" class="alert alert-danger">Error: {{ error ?? metaError }}</p> | ||
| <p v-else-if="loading || !loaded">Loading…</p> | ||
| <template v-else> | ||
| <h1 class="resource-title">Edit {{ title ?? resourceLabel(resourceUri) }}</h1> | ||
| <p>Editing is not implemented yet.</p> | ||
| </template> | ||
| <router-link :to="resourcePath" class="text-link">Cancel</router-link> | ||
| </main> | ||
| </div> | ||
| </template> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { useRawFormat, formats } from '../composables/useRawFormat' | |
| import { internalHref } from '../composables/urlUtils' | ||
| import RdfGraph from '../components/RdfGraph.vue' | ||
| import MembershipBadge from '../components/MembershipBadge.vue' | ||
| import IconEdit from '../assets/icons/edit.svg?component' | ||
| import IconTrashAlt from '../assets/icons/trash-alt.svg?component' | ||
|
|
||
| const { | ||
|
|
@@ -33,7 +34,10 @@ const { | |
| resourceLabel, | ||
| } = useResourceView() | ||
|
|
||
| const { membershipName: metaMembershipName } = useMeta(resource) | ||
| const { membershipName: metaMembershipName, canEdit } = useMeta(resource) | ||
| const editPath = computed(() => | ||
| resource.value ? `/${resource.value.resourceType}/${resource.value.id}/edit` : '/edit', | ||
| ) | ||
| const { | ||
| currentMembershipName, | ||
| otherOwnerNames, | ||
|
|
@@ -146,8 +150,13 @@ const { | |
| :resource-type="resource?.resourceType ?? null" | ||
| /> | ||
| </div> | ||
| <div v-if="deleteAvailable" class="resource-actions"> | ||
| <div v-if="canEdit || deleteAvailable" class="resource-actions"> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use consistent names here, as in |
||
| <router-link v-if="canEdit" :to="editPath" class="resource-action"> | ||
| <IconEdit /> | ||
| Edit | ||
| </router-link> | ||
| <button | ||
| v-if="deleteAvailable" | ||
| type="button" | ||
| class="resource-action resource-action--danger" | ||
| :disabled="deleting" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,25 @@ describe('resource operations', () => { | |
| }) | ||
| }) | ||
|
|
||
| it('names the PUT after the prefix, and reads the root PUT from the api-docs', async () => { | ||
| vi.stubGlobal('fetch', mockApiFetch(okJson(), resourceApiDocs)) | ||
| const { putOperationId } = await import('../../src/composables/fdpApi') | ||
| expect(await putOperationId({ resourceType: 'catalog', id: 'abc' })).toBe('putCatalog') | ||
| // resourceApiDocs advertises no PUT /. | ||
| expect(await putOperationId(null)).toBeNull() | ||
|
|
||
| vi.resetModules() | ||
| vi.stubGlobal( | ||
| 'fetch', | ||
| mockApiFetch(okJson(), { | ||
| openapi: '3.0.1', | ||
| paths: { '/': { put: { operationId: 'putResearch Hub' } } }, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Or perhaps use a variable, also because it is used twice: const mockOperationId = 'some operation id' |
||
| }), | ||
| ) | ||
| const { putOperationId: rootPutOperationId } = await import('../../src/composables/fdpApi') | ||
| expect(await rootPutOperationId(null)).toBe('putResearch Hub') | ||
| }) | ||
|
|
||
| it('refuses an unadvertised operation even if a conventional endpoint path exists', async () => { | ||
| const handleRequest = vi.fn(okJson(meta)) | ||
| vi.stubGlobal( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,19 +3,30 @@ import { effectScope, nextTick, ref, type EffectScope } from 'vue' | |
| import { useMeta } from '../../src/composables/useMeta' | ||
| import { fetchMeta, type ResourceMeta } from '../../src/composables/fdpApi' | ||
|
|
||
| const auth = vi.hoisted(() => ({ isLoggedIn: { value: false } })) | ||
|
|
||
| vi.mock('../../src/composables/fdpApi', () => ({ fetchMeta: vi.fn() })) | ||
| const auth = vi.hoisted(() => ({ isLoggedIn: { value: false }, isAdmin: { value: false } })) | ||
| const offered = vi.hoisted(() => new Set<string>()) | ||
|
|
||
| vi.mock('../../src/composables/fdpApi', () => ({ | ||
| fetchMeta: vi.fn(), | ||
| putOperationId: async (resource: { resourceType: string } | null) => | ||
| resource ? `put${resource.resourceType}` : null, | ||
| })) | ||
| vi.mock('../../src/composables/apiDocs', () => ({ | ||
| isOperationOffered: (operationId: string) => offered.has(operationId), | ||
| })) | ||
| vi.mock('../../src/composables/useAuth', async () => { | ||
| const { ref } = await import('vue') | ||
| auth.isLoggedIn = ref(false) | ||
| auth.isAdmin = ref(false) | ||
| return { useAuth: () => auth } | ||
| }) | ||
|
|
||
| const flushPromises = () => new Promise<void>((resolve) => setTimeout(resolve, 0)) | ||
|
|
||
| const metaWith = (name: string | null): ResourceMeta => ({ | ||
| member: { membership: name ? { name } : null }, | ||
| const metaWith = (name: string | null, codes: string[] = []): ResourceMeta => ({ | ||
| member: { | ||
| membership: name ? { name, permissions: codes.map((code) => ({ code })) } : null, | ||
| }, | ||
| }) | ||
|
|
||
| let scope: EffectScope | ||
|
|
@@ -24,6 +35,8 @@ beforeEach(() => { | |
| scope = effectScope() | ||
| vi.mocked(fetchMeta).mockReset() | ||
| auth.isLoggedIn.value = false | ||
| auth.isAdmin.value = false | ||
| offered.clear() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
|
|
@@ -118,3 +131,56 @@ describe('useMeta membershipName', () => { | |
| expect(membershipName.value).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('useMeta canEdit', () => { | ||
| const catalog = { resourceType: 'catalog', id: 'abc' } | ||
|
|
||
| /** Runs useMeta on the catalog, logged in, and waits for meta. */ | ||
| async function editAccess(meta: ResourceMeta) { | ||
| auth.isLoggedIn.value = true | ||
| vi.mocked(fetchMeta).mockResolvedValue(meta) | ||
| const result = scope.run(() => useMeta(ref(catalog)))! | ||
| await flushPromises() | ||
| return result | ||
| } | ||
|
|
||
| it('allows members with W when the PUT is advertised', async () => { | ||
| offered.add('putcatalog') | ||
| const { canEdit, loaded } = await editAccess(metaWith('Owner', ['C', 'W', 'D', 'A'])) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we could introduce Enums for the membership name and permission codes (not just here, but in the app code)? For example: enum MembershipName {
Owner = 'Owner',
DataProvider = 'Data Provider',
}
enum PermissionCode {
Administration = 'A',
Create = 'C',
Delete = 'D',
Insert = 'I',
Read = 'R',
Write = 'W',
} |
||
| expect(loaded.value).toBe(true) | ||
| expect(canEdit.value).toBe(true) | ||
| }) | ||
|
|
||
| it('denies members without W', async () => { | ||
| offered.add('putcatalog') | ||
| const { canEdit } = await editAccess(metaWith('Data Provider', ['C'])) | ||
| expect(canEdit.value).toBe(false) | ||
| }) | ||
|
|
||
| it('allows admins without a membership', async () => { | ||
| offered.add('putcatalog') | ||
| auth.isAdmin.value = true | ||
| const { canEdit } = await editAccess(metaWith(null)) | ||
| expect(canEdit.value).toBe(true) | ||
| }) | ||
|
|
||
| it('denies everyone when the PUT is not advertised', async () => { | ||
| auth.isAdmin.value = true | ||
| const { canEdit } = await editAccess(metaWith('Owner', ['W'])) | ||
| expect(canEdit.value).toBe(false) | ||
| }) | ||
|
|
||
| it('reports a failed meta request instead of denying', async () => { | ||
| offered.add('putcatalog') | ||
| auth.isAdmin.value = true | ||
| auth.isLoggedIn.value = true | ||
| vi.mocked(fetchMeta).mockRejectedValue(new Error('HTTP 500')) | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| const { canEdit, loaded, error } = scope.run(() => useMeta(ref(catalog)))! | ||
| await flushPromises() | ||
| expect(error.value).toBe('HTTP 500') | ||
| expect(loaded.value).toBe(false) | ||
| expect(canEdit.value).toBe(false) | ||
| warn.mockRestore() | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and so on