arocapi 5.0.0-beta.2 → 5.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -824
- package/dist/app.d.ts +20 -7
- package/dist/app.js +42 -28
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +41 -56
- package/dist/app.test.js.map +1 -1
- package/dist/errorHandling.test.d.ts +1 -0
- package/dist/errorHandling.test.js +222 -0
- package/dist/errorHandling.test.js.map +1 -0
- package/dist/index.dev.js +4 -3
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/capabilities.test.js +8 -8
- package/dist/routes/capabilities.test.js.map +1 -1
- package/dist/routes/crate.test.js +67 -19
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +31 -49
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +16 -12
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +14 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +19 -13
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +6 -0
- package/dist/routes/file.js +67 -59
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +135 -25
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +44 -21
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/metadata.d.ts +6 -0
- package/dist/routes/metadata.js +49 -54
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +6 -2
- package/dist/routes/search.js +95 -99
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +85 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.js +2 -3
- package/dist/test/helpers/fastify.js.map +1 -1
- package/dist/test/integration.setup.d.ts +18 -0
- package/dist/test/integration.setup.js +15 -7
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +368 -2
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +10 -18
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +109 -47
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +12 -34
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +5 -9
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +5 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +13 -4
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/errors.test.js +14 -1
- package/dist/utils/errors.test.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +6 -4
- package/src/app.test.ts +45 -66
- package/src/app.ts +85 -54
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +8 -8
- package/src/routes/crate.test.ts +80 -19
- package/src/routes/entities.test.ts +16 -12
- package/src/routes/entities.ts +40 -59
- package/src/routes/entity.test.ts +19 -13
- package/src/routes/entity.ts +22 -35
- package/src/routes/file.test.ts +156 -25
- package/src/routes/file.ts +84 -62
- package/src/routes/files.test.ts +52 -26
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +64 -58
- package/src/routes/search.test.ts +88 -39
- package/src/routes/search.ts +123 -119
- package/src/test/helpers/fastify.ts +2 -3
- package/src/test/integration.setup.ts +18 -7
- package/src/test/integration.test.ts +472 -1
- package/src/transformers/default.test.ts +139 -47
- package/src/transformers/default.ts +103 -83
- package/src/transformers/transformer.test.ts +18 -44
- package/src/types/access.ts +20 -0
- package/src/types/context.ts +15 -0
- package/src/types/fileHandlers.ts +5 -13
- package/src/types/transformers.ts +4 -32
- package/src/types/visibility.ts +18 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.test.ts +16 -1
- package/src/utils/errors.ts +67 -10
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { AllVisibleSearchFilter, LicenceAccess } from '../app.js';
|
|
4
|
+
import { Prisma } from '../generated/prisma/client.js';
|
|
2
5
|
import type { AuthorisedEntity, AuthorisedFile } from '../transformers/default.js';
|
|
3
6
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
4
7
|
import {
|
|
8
|
+
buildTestApp,
|
|
5
9
|
cleanupTestData,
|
|
6
10
|
getTestApp,
|
|
7
11
|
seedTestData,
|
|
@@ -479,4 +483,471 @@ describe('Integration Tests', () => {
|
|
|
479
483
|
expect(response.statusCode).toBe(400);
|
|
480
484
|
});
|
|
481
485
|
});
|
|
486
|
+
|
|
487
|
+
describe('Visibility', () => {
|
|
488
|
+
const entityUrl = (n: number) => encodeURIComponent(`http://example.com/entity/${n}`);
|
|
489
|
+
const hideDrafts = {
|
|
490
|
+
OR: [{ meta: { path: '$.draft', equals: Prisma.AnyNull } }, { meta: { path: '$.draft', not: true } }],
|
|
491
|
+
};
|
|
492
|
+
const hideDraftsSearch = { bool: { must_not: { term: { 'meta.draft': true } } } };
|
|
493
|
+
const stubHandlers = {
|
|
494
|
+
fileHandler: {
|
|
495
|
+
get: async () => ({ type: 'redirect' as const, url: 'https://files.example.com/file' }),
|
|
496
|
+
head: async () => ({ contentType: 'audio/wav', contentLength: 2048 }),
|
|
497
|
+
},
|
|
498
|
+
roCrateHandler: {
|
|
499
|
+
get: async () => ({ type: 'redirect' as const, url: 'https://files.example.com/crate' }),
|
|
500
|
+
head: async () => ({ contentType: 'application/ld+json', contentLength: 100 }),
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// Declared separately: the OpenSearch typings omit `field` from terms aggregations
|
|
505
|
+
const entityTypeFacet = { entityType: { terms: { field: 'entityType', size: 20 } } };
|
|
506
|
+
|
|
507
|
+
let app: FastifyInstance;
|
|
508
|
+
|
|
509
|
+
beforeAll(async () => {
|
|
510
|
+
app = await buildTestApp({
|
|
511
|
+
...stubHandlers,
|
|
512
|
+
entityVisibilityFilter: () => hideDrafts,
|
|
513
|
+
searchVisibilityFilter: () => hideDraftsSearch,
|
|
514
|
+
isAuthenticated: ({ request }) => request.headers['x-user'] !== undefined,
|
|
515
|
+
promptLoginForHidden: true,
|
|
516
|
+
queryBuilderOptions: { aggregations: entityTypeFacet },
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
afterAll(async () => {
|
|
521
|
+
await app.close();
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
const ids = (entities: Array<{ id: string }>) => entities.map((e) => e.id).sort();
|
|
525
|
+
const signedIn = { 'x-user': 'alice' };
|
|
526
|
+
|
|
527
|
+
describe('single entity routes', () => {
|
|
528
|
+
it('treats a hidden entity like a missing one for an authenticated request', async () => {
|
|
529
|
+
const hidden = await app.inject({ method: 'GET', url: `/entity/${entityUrl(2)}`, headers: signedIn });
|
|
530
|
+
const missing = await app.inject({ method: 'GET', url: `/entity/${entityUrl(99)}`, headers: signedIn });
|
|
531
|
+
|
|
532
|
+
expect(hidden.statusCode).toBe(404);
|
|
533
|
+
expect(missing.statusCode).toBe(404);
|
|
534
|
+
const { error: hiddenError } = hidden.json<StandardErrorResponse>();
|
|
535
|
+
const { error: missingError } = missing.json<StandardErrorResponse>();
|
|
536
|
+
expect(hiddenError.code).toBe(missingError.code);
|
|
537
|
+
expect(hiddenError.message).toBe(missingError.message);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it('prompts an anonymous request for a hidden entity to log in', async () => {
|
|
541
|
+
const response = await app.inject({ method: 'GET', url: `/entity/${entityUrl(2)}` });
|
|
542
|
+
|
|
543
|
+
expect(response.statusCode).toBe(401);
|
|
544
|
+
expect(response.json<StandardErrorResponse>().error.code).toBe('UNAUTHORIZED');
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('still returns 404 for a missing entity to an anonymous request', async () => {
|
|
548
|
+
const response = await app.inject({ method: 'GET', url: `/entity/${entityUrl(99)}` });
|
|
549
|
+
|
|
550
|
+
expect(response.statusCode).toBe(404);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('resolves parent references regardless of visibility', async () => {
|
|
554
|
+
const response = await app.inject({ method: 'GET', url: `/entity/${entityUrl(4)}` });
|
|
555
|
+
const body = JSON.parse(response.body) as AuthorisedEntity;
|
|
556
|
+
|
|
557
|
+
expect(response.statusCode).toBe(200);
|
|
558
|
+
expect(body.memberOf).toEqual({ id: 'http://example.com/entity/2', name: 'Test Object' });
|
|
559
|
+
expect(body.rootCollection).toEqual({ id: 'http://example.com/entity/1', name: 'Test Collection' });
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
it.each(['GET', 'HEAD'] as const)(
|
|
563
|
+
'prompts login for a hidden entity on %s /entity/:id/metadata',
|
|
564
|
+
async (method) => {
|
|
565
|
+
const hidden = await app.inject({ method, url: `/entity/${entityUrl(2)}/metadata` });
|
|
566
|
+
const signedInHidden = await app.inject({
|
|
567
|
+
method,
|
|
568
|
+
url: `/entity/${entityUrl(2)}/metadata`,
|
|
569
|
+
headers: signedIn,
|
|
570
|
+
});
|
|
571
|
+
const visible = await app.inject({ method, url: `/entity/${entityUrl(1)}/metadata` });
|
|
572
|
+
|
|
573
|
+
expect(hidden.statusCode).toBe(401);
|
|
574
|
+
expect(signedInHidden.statusCode).toBe(404);
|
|
575
|
+
expect(visible.statusCode).toBe(method === 'GET' ? 302 : 200);
|
|
576
|
+
},
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
it.each(['GET', 'HEAD'] as const)('prompts login for a hidden file on %s /file/:id', async (method) => {
|
|
580
|
+
const hidden = await app.inject({ method, url: `/file/${entityUrl(5)}` });
|
|
581
|
+
const missing = await app.inject({ method, url: `/file/${entityUrl(99)}` });
|
|
582
|
+
const visible = await app.inject({ method, url: `/file/${entityUrl(4)}` });
|
|
583
|
+
|
|
584
|
+
expect(hidden.statusCode).toBe(401);
|
|
585
|
+
expect(missing.statusCode).toBe(404);
|
|
586
|
+
expect(visible.statusCode).toBe(method === 'GET' ? 302 : 200);
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
it('returns 404 for a hidden entity to anonymous requests without the login prompt', async () => {
|
|
590
|
+
const defaultApp = await buildTestApp({
|
|
591
|
+
entityVisibilityFilter: () => hideDrafts,
|
|
592
|
+
isAuthenticated: () => false,
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
const hidden = await defaultApp.inject({ method: 'GET', url: `/entity/${entityUrl(2)}` });
|
|
596
|
+
await defaultApp.close();
|
|
597
|
+
|
|
598
|
+
expect(hidden.statusCode).toBe(404);
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
describe('list routes', () => {
|
|
603
|
+
it('excludes hidden entities from /entities and its total', async () => {
|
|
604
|
+
const response = await app.inject({ method: 'GET', url: '/entities?limit=2' });
|
|
605
|
+
const body = JSON.parse(response.body) as { total: number; entities: AuthorisedEntity[] };
|
|
606
|
+
|
|
607
|
+
expect(body.total).toBe(3);
|
|
608
|
+
expect(ids(body.entities)).toEqual(['http://example.com/entity/1', 'http://example.com/entity/3']);
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
it('combines memberOf with visibility on /entities', async () => {
|
|
612
|
+
const response = await app.inject({ method: 'GET', url: `/entities?memberOf=${entityUrl(1)}` });
|
|
613
|
+
const body = JSON.parse(response.body) as { total: number; entities: AuthorisedEntity[] };
|
|
614
|
+
|
|
615
|
+
expect(body.total).toBe(1);
|
|
616
|
+
expect(ids(body.entities)).toEqual(['http://example.com/entity/3']);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
it('combines entityType with visibility on /entities', async () => {
|
|
620
|
+
const response = await app.inject({
|
|
621
|
+
method: 'GET',
|
|
622
|
+
url: `/entities?entityType=${encodeURIComponent('http://schema.org/MediaObject')}`,
|
|
623
|
+
});
|
|
624
|
+
const body = JSON.parse(response.body) as { total: number; entities: AuthorisedEntity[] };
|
|
625
|
+
|
|
626
|
+
expect(body.total).toBe(1);
|
|
627
|
+
expect(ids(body.entities)).toEqual(['http://example.com/entity/4']);
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it('excludes files of hidden entities from /files and its total', async () => {
|
|
631
|
+
const response = await app.inject({ method: 'GET', url: '/files' });
|
|
632
|
+
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
|
|
633
|
+
|
|
634
|
+
expect(body.total).toBe(1);
|
|
635
|
+
expect(ids(body.files)).toEqual(['http://example.com/entity/4']);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it('excludes hidden entities from /search results, total and facets', async () => {
|
|
639
|
+
const response = await app.inject({ method: 'POST', url: '/search', payload: { query: '' } });
|
|
640
|
+
const body = JSON.parse(response.body) as {
|
|
641
|
+
total: number;
|
|
642
|
+
entities: AuthorisedEntity[];
|
|
643
|
+
facets: Record<string, Array<{ name: string; count: number }>>;
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
expect(body.total).toBe(3);
|
|
647
|
+
expect(ids(body.entities)).toEqual([
|
|
648
|
+
'http://example.com/entity/1',
|
|
649
|
+
'http://example.com/entity/3',
|
|
650
|
+
'http://example.com/entity/4',
|
|
651
|
+
]);
|
|
652
|
+
expect(body.facets.entityType.reduce((sum, bucket) => sum + bucket.count, 0)).toBe(3);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
it('paginates /search over visible entities only', async () => {
|
|
656
|
+
const response = await app.inject({
|
|
657
|
+
method: 'POST',
|
|
658
|
+
url: '/search',
|
|
659
|
+
payload: { query: '', sort: 'id', limit: 2, offset: 2 },
|
|
660
|
+
});
|
|
661
|
+
const body = JSON.parse(response.body) as { total: number; entities: AuthorisedEntity[] };
|
|
662
|
+
|
|
663
|
+
expect(body.total).toBe(3);
|
|
664
|
+
expect(ids(body.entities)).toEqual(['http://example.com/entity/4']);
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it('drops search hits the entity visibility filter hides when the filters disagree', async () => {
|
|
668
|
+
const mismatchedApp = await buildTestApp({
|
|
669
|
+
entityVisibilityFilter: () => hideDrafts,
|
|
670
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
const response = await mismatchedApp.inject({ method: 'POST', url: '/search', payload: { query: '' } });
|
|
674
|
+
await mismatchedApp.close();
|
|
675
|
+
const body = JSON.parse(response.body) as { total: number; entities: AuthorisedEntity[] };
|
|
676
|
+
|
|
677
|
+
expect(body.total).toBe(5);
|
|
678
|
+
expect(ids(body.entities)).toEqual([
|
|
679
|
+
'http://example.com/entity/1',
|
|
680
|
+
'http://example.com/entity/3',
|
|
681
|
+
'http://example.com/entity/4',
|
|
682
|
+
]);
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
describe('Access', () => {
|
|
688
|
+
const entityUrl = (n: number) => encodeURIComponent(`http://example.com/entity/${n}`);
|
|
689
|
+
const restrictedFile = 'http://example.com/entity/5';
|
|
690
|
+
const restrictedMetadata = 'http://example.com/entity/2';
|
|
691
|
+
const fileHandler = {
|
|
692
|
+
get: vi.fn(async () => ({ type: 'redirect' as const, url: 'https://files.example.com/file' })),
|
|
693
|
+
head: vi.fn(async () => ({ contentType: 'audio/wav', contentLength: 2048 })),
|
|
694
|
+
};
|
|
695
|
+
const roCrateHandler = {
|
|
696
|
+
get: vi.fn(async () => ({ type: 'redirect' as const, url: 'https://files.example.com/crate' })),
|
|
697
|
+
head: vi.fn(async () => ({ contentType: 'application/ld+json', contentLength: 100 })),
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
let app: FastifyInstance;
|
|
701
|
+
|
|
702
|
+
beforeAll(async () => {
|
|
703
|
+
app = await buildTestApp({
|
|
704
|
+
fileHandler,
|
|
705
|
+
roCrateHandler,
|
|
706
|
+
access: (entity) => ({
|
|
707
|
+
metadata: entity.id !== restrictedMetadata,
|
|
708
|
+
content: entity.id !== restrictedFile,
|
|
709
|
+
...(entity.id === restrictedFile && { contentAuthorizationUrl: 'https://rems.example.com/apply' }),
|
|
710
|
+
}),
|
|
711
|
+
entityTransformers: [(entity) => ({ ...entity, canDownload: entity.access.content })],
|
|
712
|
+
});
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
afterAll(async () => {
|
|
716
|
+
await app.close();
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
beforeEach(() => {
|
|
720
|
+
vi.clearAllMocks();
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
describe('Content', () => {
|
|
724
|
+
it('denies GET /file/:id with 403 without calling the file handler', async () => {
|
|
725
|
+
const denied = await app.inject({ method: 'GET', url: `/file/${entityUrl(5)}` });
|
|
726
|
+
const body = denied.json<StandardErrorResponse>();
|
|
727
|
+
|
|
728
|
+
expect(denied.statusCode).toBe(403);
|
|
729
|
+
expect(body.error.code).toBe('FORBIDDEN');
|
|
730
|
+
expect(fileHandler.get).not.toHaveBeenCalled();
|
|
731
|
+
|
|
732
|
+
const granted = await app.inject({ method: 'GET', url: `/file/${entityUrl(4)}` });
|
|
733
|
+
|
|
734
|
+
expect(granted.statusCode).toBe(302);
|
|
735
|
+
expect(fileHandler.get).toHaveBeenCalledOnce();
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
it('denies HEAD /file/:id without calling the file handler', async () => {
|
|
739
|
+
const denied = await app.inject({ method: 'HEAD', url: `/file/${entityUrl(5)}` });
|
|
740
|
+
|
|
741
|
+
expect(denied.statusCode).toBe(403);
|
|
742
|
+
expect(fileHandler.head).not.toHaveBeenCalled();
|
|
743
|
+
|
|
744
|
+
const granted = await app.inject({ method: 'HEAD', url: `/file/${entityUrl(4)}` });
|
|
745
|
+
|
|
746
|
+
expect(granted.statusCode).toBe(200);
|
|
747
|
+
expect(fileHandler.head).toHaveBeenCalledOnce();
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
it('denies the noRedirect variant without revealing the location', async () => {
|
|
751
|
+
const denied = await app.inject({ method: 'GET', url: `/file/${entityUrl(5)}?noRedirect=true` });
|
|
752
|
+
|
|
753
|
+
expect(denied.statusCode).toBe(403);
|
|
754
|
+
expect(denied.body).not.toContain('files.example.com');
|
|
755
|
+
expect(fileHandler.get).not.toHaveBeenCalled();
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
describe('Metadata', () => {
|
|
760
|
+
it.each([
|
|
761
|
+
['GET', 'get', 302],
|
|
762
|
+
['HEAD', 'head', 200],
|
|
763
|
+
] as const)(
|
|
764
|
+
'denies %s /entity/:id/metadata without calling the RO-Crate handler',
|
|
765
|
+
async (method, handler, grantedStatus) => {
|
|
766
|
+
const denied = await app.inject({ method, url: `/entity/${entityUrl(2)}/metadata` });
|
|
767
|
+
|
|
768
|
+
expect(denied.statusCode).toBe(403);
|
|
769
|
+
expect(roCrateHandler[handler]).not.toHaveBeenCalled();
|
|
770
|
+
|
|
771
|
+
const granted = await app.inject({ method, url: `/entity/${entityUrl(1)}/metadata` });
|
|
772
|
+
|
|
773
|
+
expect(granted.statusCode).toBe(grantedStatus);
|
|
774
|
+
expect(roCrateHandler[handler]).toHaveBeenCalledOnce();
|
|
775
|
+
},
|
|
776
|
+
);
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
describe('access signal', () => {
|
|
780
|
+
const restrictedFileAccess = {
|
|
781
|
+
metadata: true,
|
|
782
|
+
content: false,
|
|
783
|
+
contentAuthorizationUrl: 'https://rems.example.com/apply',
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
it('attaches access to /entity/:id before entity transformers run', async () => {
|
|
787
|
+
const response = await app.inject({ method: 'GET', url: `/entity/${entityUrl(5)}` });
|
|
788
|
+
const body = response.json<AuthorisedEntity & { canDownload: boolean }>();
|
|
789
|
+
|
|
790
|
+
expect(body.access).toEqual(restrictedFileAccess);
|
|
791
|
+
expect(body.canDownload).toBe(false);
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it('attaches access to every entity in /entities and /search', async () => {
|
|
795
|
+
const listing = await app.inject({ method: 'GET', url: '/entities' });
|
|
796
|
+
const search = await app.inject({ method: 'POST', url: '/search', payload: { query: '' } });
|
|
797
|
+
|
|
798
|
+
for (const response of [listing, search]) {
|
|
799
|
+
const { entities } = response.json<{ entities: AuthorisedEntity[] }>();
|
|
800
|
+
const byId = new Map(entities.map((e) => [e.id, e.access]));
|
|
801
|
+
|
|
802
|
+
expect(byId.get('http://example.com/entity/1')).toEqual({ metadata: true, content: true });
|
|
803
|
+
expect(byId.get(restrictedMetadata)).toEqual({ metadata: false, content: true });
|
|
804
|
+
expect(byId.get(restrictedFile)).toEqual(restrictedFileAccess);
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
it("gives each file in /files its file entity's content access", async () => {
|
|
809
|
+
const response = await app.inject({ method: 'GET', url: '/files' });
|
|
810
|
+
const { files } = response.json<{ files: AuthorisedFile[] }>();
|
|
811
|
+
const byId = new Map(files.map((f) => [f.id, f.access]));
|
|
812
|
+
|
|
813
|
+
expect(byId.get('http://example.com/entity/4')).toEqual({ content: true });
|
|
814
|
+
expect(byId.get(restrictedFile)).toEqual({
|
|
815
|
+
content: false,
|
|
816
|
+
contentAuthorizationUrl: 'https://rems.example.com/apply',
|
|
817
|
+
});
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
it('ignores anything but access returned by the access function', async () => {
|
|
821
|
+
const leakyApp = await buildTestApp({
|
|
822
|
+
access: (entity) => ({ ...entity, name: 'Redacted', metadata: true, content: true }),
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
const response = await leakyApp.inject({ method: 'GET', url: `/entity/${entityUrl(1)}` });
|
|
826
|
+
await leakyApp.close();
|
|
827
|
+
const body = response.json<AuthorisedEntity>();
|
|
828
|
+
|
|
829
|
+
expect(body.name).toBe('Test Collection');
|
|
830
|
+
expect(body.access).toEqual({ metadata: true, content: true });
|
|
831
|
+
});
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
describe('licence access', () => {
|
|
835
|
+
const licence = 'https://creativecommons.org/licenses/by/4.0/';
|
|
836
|
+
const restrictedLicence = 'https://example.com/licences/restricted';
|
|
837
|
+
const hasLicence = vi.fn();
|
|
838
|
+
let licenceApp: FastifyInstance;
|
|
839
|
+
|
|
840
|
+
beforeAll(async () => {
|
|
841
|
+
licenceApp = await buildTestApp({
|
|
842
|
+
access: LicenceAccess({
|
|
843
|
+
hasLicence,
|
|
844
|
+
authorisationUrl: (licenceId) => `https://rems.example.com/apply?licence=${encodeURIComponent(licenceId)}`,
|
|
845
|
+
}),
|
|
846
|
+
});
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
afterAll(async () => {
|
|
850
|
+
await licenceApp.close();
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
it('checks each licence once per request across a listing', async () => {
|
|
854
|
+
hasLicence.mockImplementation(async (licenceId: string) => licenceId === licence);
|
|
855
|
+
|
|
856
|
+
const first = await licenceApp.inject({ method: 'GET', url: '/entities' });
|
|
857
|
+
const second = await licenceApp.inject({ method: 'GET', url: '/entities' });
|
|
858
|
+
const byId = new Map(first.json<{ entities: AuthorisedEntity[] }>().entities.map((e) => [e.id, e.access]));
|
|
859
|
+
|
|
860
|
+
expect(byId.size).toBe(5);
|
|
861
|
+
expect(byId.get('http://example.com/entity/4')).toMatchObject({ metadata: true, content: false });
|
|
862
|
+
expect(byId.get('http://example.com/entity/5')).toMatchObject({ metadata: true, content: true });
|
|
863
|
+
expect(second.statusCode).toBe(200);
|
|
864
|
+
expect(hasLicence).toHaveBeenCalledTimes(4);
|
|
865
|
+
expect(hasLicence).toHaveBeenCalledWith(licence, expect.objectContaining({ request: expect.anything() }));
|
|
866
|
+
expect(hasLicence).toHaveBeenCalledWith(restrictedLicence, expect.anything());
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
it('grants access without authorisation URLs when the licence is held', async () => {
|
|
870
|
+
hasLicence.mockReturnValue(true);
|
|
871
|
+
|
|
872
|
+
const response = await licenceApp.inject({ method: 'GET', url: `/entity/${entityUrl(1)}` });
|
|
873
|
+
|
|
874
|
+
expect(response.json<AuthorisedEntity>().access).toEqual({ metadata: true, content: true });
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
it('denies access with authorisation URLs when the licence is not held', async () => {
|
|
878
|
+
hasLicence.mockResolvedValue(false);
|
|
879
|
+
const applyUrl = (licenceId: string) =>
|
|
880
|
+
`https://rems.example.com/apply?licence=${encodeURIComponent(licenceId)}`;
|
|
881
|
+
|
|
882
|
+
const entity = await licenceApp.inject({ method: 'GET', url: `/entity/${entityUrl(4)}` });
|
|
883
|
+
const content = await licenceApp.inject({ method: 'GET', url: `/file/${entityUrl(4)}` });
|
|
884
|
+
|
|
885
|
+
expect(entity.json<AuthorisedEntity>().access).toEqual({
|
|
886
|
+
metadata: false,
|
|
887
|
+
content: false,
|
|
888
|
+
metadataAuthorizationUrl: applyUrl(licence),
|
|
889
|
+
contentAuthorizationUrl: applyUrl(restrictedLicence),
|
|
890
|
+
});
|
|
891
|
+
expect(content.statusCode).toBe(403);
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
it('omits authorisation URLs when no builder is given', async () => {
|
|
895
|
+
const bareApp = await buildTestApp({ access: LicenceAccess({ hasLicence: () => false }) });
|
|
896
|
+
|
|
897
|
+
const response = await bareApp.inject({ method: 'GET', url: `/entity/${entityUrl(1)}` });
|
|
898
|
+
await bareApp.close();
|
|
899
|
+
|
|
900
|
+
expect(response.json<AuthorisedEntity>().access).toEqual({ metadata: false, content: false });
|
|
901
|
+
});
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
describe('denied requests', () => {
|
|
905
|
+
const signedIn = { 'x-user': 'alice' };
|
|
906
|
+
let deniedApp: FastifyInstance;
|
|
907
|
+
|
|
908
|
+
beforeAll(async () => {
|
|
909
|
+
deniedApp = await buildTestApp({
|
|
910
|
+
fileHandler,
|
|
911
|
+
roCrateHandler,
|
|
912
|
+
entityVisibilityFilter: () => ({ id: { not: 'http://example.com/entity/3' } }),
|
|
913
|
+
access: () => ({ metadata: false, content: false }),
|
|
914
|
+
isAuthenticated: ({ request }) => request.headers['x-user'] !== undefined,
|
|
915
|
+
});
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
afterAll(async () => {
|
|
919
|
+
await deniedApp.close();
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
it.each([
|
|
923
|
+
['an anonymous', {}, 401, 'UNAUTHORIZED'],
|
|
924
|
+
['an authenticated', signedIn, 403, 'FORBIDDEN'],
|
|
925
|
+
])('refuses %s request', async (_who, headers, status, code) => {
|
|
926
|
+
const metadata = await deniedApp.inject({ method: 'GET', url: `/entity/${entityUrl(1)}/metadata`, headers });
|
|
927
|
+
const content = await deniedApp.inject({ method: 'GET', url: `/file/${entityUrl(4)}`, headers });
|
|
928
|
+
|
|
929
|
+
expect(metadata.statusCode).toBe(status);
|
|
930
|
+
expect(metadata.json<StandardErrorResponse>().error.code).toBe(code);
|
|
931
|
+
expect(content.statusCode).toBe(status);
|
|
932
|
+
expect(content.json<StandardErrorResponse>().error.code).toBe(code);
|
|
933
|
+
});
|
|
934
|
+
|
|
935
|
+
it('refuses with 403 when the repository has no logins', async () => {
|
|
936
|
+
const noLoginApp = await buildTestApp({ roCrateHandler, access: () => ({ metadata: false, content: false }) });
|
|
937
|
+
|
|
938
|
+
const response = await noLoginApp.inject({ method: 'GET', url: `/entity/${entityUrl(1)}/metadata` });
|
|
939
|
+
await noLoginApp.close();
|
|
940
|
+
|
|
941
|
+
expect(response.statusCode).toBe(403);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it('gives hidden and missing entities 404, not a denied response', async () => {
|
|
945
|
+
const hidden = await deniedApp.inject({ method: 'GET', url: `/entity/${entityUrl(3)}/metadata` });
|
|
946
|
+
const missing = await deniedApp.inject({ method: 'GET', url: `/file/${entityUrl(99)}` });
|
|
947
|
+
|
|
948
|
+
expect(hidden.statusCode).toBe(404);
|
|
949
|
+
expect(missing.statusCode).toBe(404);
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
});
|
|
482
953
|
});
|