arocapi 5.0.0-beta.1 → 5.0.0-beta.3
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 -802
- package/dist/app.d.ts +22 -9
- package/dist/app.js +46 -30
- 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.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/capabilities.test.d.ts +1 -0
- package/dist/routes/capabilities.test.js +81 -0
- package/dist/routes/capabilities.test.js.map +1 -0
- 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 +99 -22
- 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 +8 -5
- package/dist/routes/search.js +95 -101
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +92 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.d.ts +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 +19 -1
- 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 +19 -10
- 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/queryBuilder.d.ts +12 -0
- package/dist/utils/queryBuilder.js +9 -0
- package/dist/utils/queryBuilder.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 +34 -33
- package/src/app.test.ts +45 -66
- package/src/app.ts +92 -58
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/capabilities.test.ts.snap +34 -0
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +102 -0
- package/src/routes/capabilities.ts +22 -0
- 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 +117 -22
- 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 +95 -39
- package/src/routes/search.ts +120 -125
- 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 +68 -11
- package/src/utils/queryBuilder.ts +25 -0
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
2
|
|
|
3
3
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
-
import {
|
|
4
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
5
5
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
6
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
6
7
|
import entityRoute from './entity.js';
|
|
7
8
|
|
|
8
9
|
describe('Entity Route', () => {
|
|
9
10
|
beforeEach(async () => {
|
|
10
11
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(entityRoute, {
|
|
12
|
+
await fastify.register(entityRoute, {
|
|
13
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
14
|
+
refusal: { promptLoginForHidden: false },
|
|
15
|
+
prisma,
|
|
16
|
+
access: AllPublicAccess,
|
|
17
|
+
});
|
|
12
18
|
});
|
|
13
19
|
|
|
14
20
|
afterEach(async () => {
|
|
@@ -32,7 +38,7 @@ describe('Entity Route', () => {
|
|
|
32
38
|
meta: {},
|
|
33
39
|
};
|
|
34
40
|
|
|
35
|
-
prisma.entity.
|
|
41
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
36
42
|
|
|
37
43
|
const response = await fastify.inject({
|
|
38
44
|
method: 'GET',
|
|
@@ -42,16 +48,14 @@ describe('Entity Route', () => {
|
|
|
42
48
|
|
|
43
49
|
expect(response.statusCode).toBe(200);
|
|
44
50
|
expect(body).toMatchSnapshot();
|
|
45
|
-
expect(prisma.entity.
|
|
46
|
-
where: {
|
|
47
|
-
id: 'http://example.com/entity/123',
|
|
48
|
-
},
|
|
51
|
+
expect(prisma.entity.findFirst).toHaveBeenCalledWith({
|
|
52
|
+
where: { AND: [{ id: 'http://example.com/entity/123' }, {}] },
|
|
49
53
|
include: { file: { select: { id: true } } },
|
|
50
54
|
});
|
|
51
55
|
});
|
|
52
56
|
|
|
53
57
|
it('should return 404 when entity not found', async () => {
|
|
54
|
-
prisma.entity.
|
|
58
|
+
prisma.entity.findFirst.mockResolvedValue(null);
|
|
55
59
|
|
|
56
60
|
const response = await fastify.inject({
|
|
57
61
|
method: 'GET',
|
|
@@ -64,7 +68,7 @@ describe('Entity Route', () => {
|
|
|
64
68
|
});
|
|
65
69
|
|
|
66
70
|
it('should return 500 when database error occurs', async () => {
|
|
67
|
-
prisma.entity.
|
|
71
|
+
prisma.entity.findFirst.mockRejectedValue(new Error('Database connection failed'));
|
|
68
72
|
|
|
69
73
|
const response = await fastify.inject({
|
|
70
74
|
method: 'GET',
|
|
@@ -84,7 +88,7 @@ describe('Entity Route', () => {
|
|
|
84
88
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
85
89
|
|
|
86
90
|
expect(response.statusCode).toBe(400);
|
|
87
|
-
expect(body.error).toBe('
|
|
91
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
88
92
|
});
|
|
89
93
|
|
|
90
94
|
it('should apply custom entity transformers', async () => {
|
|
@@ -96,8 +100,10 @@ describe('Entity Route', () => {
|
|
|
96
100
|
|
|
97
101
|
await fastifyBefore();
|
|
98
102
|
await fastify.register(entityRoute, {
|
|
103
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
104
|
+
refusal: { promptLoginForHidden: false },
|
|
99
105
|
prisma,
|
|
100
|
-
|
|
106
|
+
access: AllPublicAccess,
|
|
101
107
|
entityTransformers: [customTransformer],
|
|
102
108
|
});
|
|
103
109
|
|
|
@@ -115,7 +121,7 @@ describe('Entity Route', () => {
|
|
|
115
121
|
};
|
|
116
122
|
|
|
117
123
|
// @ts-expect-error TS is looking at the wronf function signature
|
|
118
|
-
prisma.entity.
|
|
124
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
119
125
|
|
|
120
126
|
const response = await fastify.inject({
|
|
121
127
|
method: 'GET',
|
|
@@ -144,7 +150,7 @@ describe('Entity Route', () => {
|
|
|
144
150
|
};
|
|
145
151
|
|
|
146
152
|
// First call returns the entity, second call (for reference resolution) returns empty
|
|
147
|
-
prisma.entity.
|
|
153
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
148
154
|
prisma.entity.findMany.mockResolvedValue([]);
|
|
149
155
|
|
|
150
156
|
const response = await fastify.inject({
|
package/src/routes/entity.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { FastifyPluginAsync } from 'fastify';
|
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
9
|
+
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
8
10
|
|
|
9
11
|
const paramsSchema = z.object({
|
|
10
12
|
id: z.url(),
|
|
@@ -12,12 +14,16 @@ const paramsSchema = z.object({
|
|
|
12
14
|
|
|
13
15
|
type EntityRouteOptions = {
|
|
14
16
|
prisma: PrismaClient;
|
|
15
|
-
|
|
17
|
+
access: AccessFunction;
|
|
16
18
|
entityTransformers?: EntityTransformer[];
|
|
19
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
20
|
+
refusal: RefusalOptions;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) => {
|
|
20
|
-
const { prisma,
|
|
24
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter, refusal } = opts;
|
|
25
|
+
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
26
|
+
|
|
21
27
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
22
28
|
'/entity/:id',
|
|
23
29
|
{
|
|
@@ -28,39 +34,20 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
28
34
|
async (request, reply) => {
|
|
29
35
|
const { id } = request.params;
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!entity) {
|
|
38
|
-
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Resolve memberOf and rootCollection references
|
|
42
|
-
const refMap = await resolveEntityReferences([entity], prisma);
|
|
37
|
+
const context = { request, fastify };
|
|
38
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
39
|
+
const entity = await prisma.entity.findFirst({
|
|
40
|
+
where: { AND: [{ id }, visibilityWhere] },
|
|
41
|
+
include: { file: { select: { id: true } } },
|
|
42
|
+
});
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
48
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
49
|
-
};
|
|
50
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
51
|
-
|
|
52
|
-
let result = authorisedEntity;
|
|
53
|
-
for (const transformer of entityTransformers) {
|
|
54
|
-
result = await transformer(result, { request, fastify });
|
|
55
|
-
}
|
|
44
|
+
if (!entity) {
|
|
45
|
+
return sendEntityNotVisible(reply, id, context);
|
|
46
|
+
}
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
} catch (error) {
|
|
59
|
-
const err = error as Error;
|
|
60
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
48
|
+
const [authorisedEntity] = await authoriseEntities([entity], prisma, access, context);
|
|
61
49
|
|
|
62
|
-
|
|
63
|
-
}
|
|
50
|
+
return applyTransformers(authorisedEntity, entityTransformers, context);
|
|
64
51
|
},
|
|
65
52
|
);
|
|
66
53
|
};
|
package/src/routes/file.test.ts
CHANGED
|
@@ -2,8 +2,11 @@ import { createReadStream } from 'node:fs';
|
|
|
2
2
|
import { Readable } from 'node:stream';
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
5
|
+
import type { AccessFunction } from '../types/access.js';
|
|
5
6
|
import type { FileHandler, FileResult } from '../types/fileHandlers.js';
|
|
7
|
+
import { AllPublicAccess } from '../utils/access.js';
|
|
6
8
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
9
|
+
import { AllVisibleEntityFilter } from '../utils/visibility.js';
|
|
7
10
|
import fileRoute from './file.js';
|
|
8
11
|
|
|
9
12
|
vi.mock('node:fs', () => ({
|
|
@@ -15,11 +18,20 @@ describe('File Route', () => {
|
|
|
15
18
|
get: vi.fn(),
|
|
16
19
|
head: vi.fn(),
|
|
17
20
|
};
|
|
21
|
+
const mockAccess = vi.fn<AccessFunction>();
|
|
18
22
|
|
|
19
23
|
beforeEach(async () => {
|
|
20
24
|
await fastifyBefore();
|
|
21
|
-
await fastify.register(fileRoute, {
|
|
25
|
+
await fastify.register(fileRoute, {
|
|
26
|
+
access: mockAccess,
|
|
27
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
28
|
+
refusal: { promptLoginForHidden: false },
|
|
29
|
+
prisma,
|
|
30
|
+
fileHandler: mockFileHandler,
|
|
31
|
+
});
|
|
22
32
|
vi.clearAllMocks();
|
|
33
|
+
mockAccess.mockImplementation(AllPublicAccess);
|
|
34
|
+
prisma.entity.findMany.mockResolvedValue([]);
|
|
23
35
|
});
|
|
24
36
|
|
|
25
37
|
afterEach(async () => {
|
|
@@ -34,11 +46,25 @@ describe('File Route', () => {
|
|
|
34
46
|
meta: { storagePath: '/data/files/test.wav' },
|
|
35
47
|
createdAt: new Date(),
|
|
36
48
|
updatedAt: new Date(),
|
|
49
|
+
entity: {
|
|
50
|
+
id: 'http://example.com/file/test.wav',
|
|
51
|
+
name: 'test.wav',
|
|
52
|
+
description: 'A test file',
|
|
53
|
+
entityType: 'http://schema.org/MediaObject',
|
|
54
|
+
memberOf: 'http://example.com/object',
|
|
55
|
+
rootCollection: 'http://example.com/collection',
|
|
56
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
57
|
+
contentLicenseId: 'https://example.com/licence/restricted',
|
|
58
|
+
meta: {},
|
|
59
|
+
createdAt: new Date(),
|
|
60
|
+
updatedAt: new Date(),
|
|
61
|
+
},
|
|
37
62
|
};
|
|
63
|
+
const { entity: _entity, ...handlerFile } = mockFile;
|
|
38
64
|
|
|
39
65
|
describe('GET /file/:id', () => {
|
|
40
66
|
it('should stream file content successfully', async () => {
|
|
41
|
-
prisma.file.
|
|
67
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
42
68
|
|
|
43
69
|
const mockStream = Readable.from(['test file content']);
|
|
44
70
|
const mockResult: FileResult = {
|
|
@@ -66,7 +92,7 @@ describe('File Route', () => {
|
|
|
66
92
|
expect(response.body).toBe('test file content');
|
|
67
93
|
|
|
68
94
|
expect(mockFileHandler.get).toHaveBeenCalledWith(
|
|
69
|
-
|
|
95
|
+
handlerFile,
|
|
70
96
|
expect.objectContaining({
|
|
71
97
|
request: expect.any(Object),
|
|
72
98
|
fastify: expect.any(Object),
|
|
@@ -75,7 +101,7 @@ describe('File Route', () => {
|
|
|
75
101
|
});
|
|
76
102
|
|
|
77
103
|
it('should handle attachment disposition', async () => {
|
|
78
|
-
prisma.file.
|
|
104
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
79
105
|
|
|
80
106
|
const mockStream = Readable.from(['test']);
|
|
81
107
|
const mockResult: FileResult = {
|
|
@@ -98,7 +124,7 @@ describe('File Route', () => {
|
|
|
98
124
|
});
|
|
99
125
|
|
|
100
126
|
it('should use custom filename when provided', async () => {
|
|
101
|
-
prisma.file.
|
|
127
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
102
128
|
|
|
103
129
|
const mockStream = Readable.from(['test']);
|
|
104
130
|
const mockResult: FileResult = {
|
|
@@ -121,7 +147,7 @@ describe('File Route', () => {
|
|
|
121
147
|
});
|
|
122
148
|
|
|
123
149
|
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
124
|
-
prisma.file.
|
|
150
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
125
151
|
|
|
126
152
|
const mockResult: FileResult = {
|
|
127
153
|
type: 'redirect',
|
|
@@ -139,7 +165,7 @@ describe('File Route', () => {
|
|
|
139
165
|
});
|
|
140
166
|
|
|
141
167
|
it('should handle redirect response with noRedirect=true', async () => {
|
|
142
|
-
prisma.file.
|
|
168
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
143
169
|
|
|
144
170
|
const mockResult: FileResult = {
|
|
145
171
|
type: 'redirect',
|
|
@@ -158,7 +184,7 @@ describe('File Route', () => {
|
|
|
158
184
|
});
|
|
159
185
|
|
|
160
186
|
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
161
|
-
prisma.file.
|
|
187
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
162
188
|
|
|
163
189
|
const mockStream = Readable.from(['file content']);
|
|
164
190
|
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
@@ -186,7 +212,7 @@ describe('File Route', () => {
|
|
|
186
212
|
});
|
|
187
213
|
|
|
188
214
|
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
189
|
-
prisma.file.
|
|
215
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
190
216
|
|
|
191
217
|
const mockResult: FileResult = {
|
|
192
218
|
type: 'file',
|
|
@@ -214,7 +240,7 @@ describe('File Route', () => {
|
|
|
214
240
|
});
|
|
215
241
|
|
|
216
242
|
it('should return 404 when entity not found', async () => {
|
|
217
|
-
prisma.file.
|
|
243
|
+
prisma.file.findFirst.mockResolvedValue(null);
|
|
218
244
|
|
|
219
245
|
const response = await fastify.inject({
|
|
220
246
|
method: 'GET',
|
|
@@ -229,7 +255,7 @@ describe('File Route', () => {
|
|
|
229
255
|
});
|
|
230
256
|
|
|
231
257
|
it('should return 500 when database error occurs', async () => {
|
|
232
|
-
prisma.file.
|
|
258
|
+
prisma.file.findFirst.mockRejectedValue(new Error('Database connection failed'));
|
|
233
259
|
|
|
234
260
|
const response = await fastify.inject({
|
|
235
261
|
method: 'GET',
|
|
@@ -241,8 +267,62 @@ describe('File Route', () => {
|
|
|
241
267
|
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
242
268
|
});
|
|
243
269
|
|
|
270
|
+
it('should decide access on the file entity without looking up its parents', async () => {
|
|
271
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
272
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue({ type: 'redirect', url: 'https://example.com/test.wav' });
|
|
273
|
+
|
|
274
|
+
await fastify.inject({
|
|
275
|
+
method: 'GET',
|
|
276
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
expect(prisma.file.findFirst).toHaveBeenCalledWith(
|
|
280
|
+
expect.objectContaining({ include: { entity: { omit: { meta: true } } } }),
|
|
281
|
+
);
|
|
282
|
+
expect(mockAccess).toHaveBeenCalledWith(
|
|
283
|
+
expect.objectContaining({
|
|
284
|
+
id: 'http://example.com/file/test.wav',
|
|
285
|
+
contentLicenseId: 'https://example.com/licence/restricted',
|
|
286
|
+
memberOf: 'http://example.com/object',
|
|
287
|
+
rootCollection: 'http://example.com/collection',
|
|
288
|
+
}),
|
|
289
|
+
expect.objectContaining({ request: expect.any(Object), fastify: expect.any(Object) }),
|
|
290
|
+
);
|
|
291
|
+
expect(prisma.entity.findMany).not.toHaveBeenCalled();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should return 403 when the content is not accessible', async () => {
|
|
295
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
296
|
+
mockAccess.mockReturnValue({ metadata: true, content: false });
|
|
297
|
+
|
|
298
|
+
const response = await fastify.inject({
|
|
299
|
+
method: 'GET',
|
|
300
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
301
|
+
});
|
|
302
|
+
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
303
|
+
|
|
304
|
+
expect(response.statusCode).toBe(403);
|
|
305
|
+
expect(body.error.code).toBe('FORBIDDEN');
|
|
306
|
+
expect(body.error.message).toBe('You do not have permission to access this resource');
|
|
307
|
+
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('should serve the content when only the metadata is not accessible', async () => {
|
|
311
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
312
|
+
mockAccess.mockReturnValue({ metadata: false, content: true });
|
|
313
|
+
vi.mocked(mockFileHandler.get).mockResolvedValue({ type: 'redirect', url: 'https://example.com/test.wav' });
|
|
314
|
+
|
|
315
|
+
const response = await fastify.inject({
|
|
316
|
+
method: 'GET',
|
|
317
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(response.statusCode).toBe(302);
|
|
321
|
+
expect(mockFileHandler.get).toHaveBeenCalled();
|
|
322
|
+
});
|
|
323
|
+
|
|
244
324
|
it('should return 500 when fileHandler throws error', async () => {
|
|
245
|
-
prisma.file.
|
|
325
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
246
326
|
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
247
327
|
|
|
248
328
|
const response = await fastify.inject({
|
|
@@ -263,11 +343,11 @@ describe('File Route', () => {
|
|
|
263
343
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
264
344
|
|
|
265
345
|
expect(response.statusCode).toBe(400);
|
|
266
|
-
expect(body.error).toBe('
|
|
346
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
267
347
|
});
|
|
268
348
|
|
|
269
349
|
it('should validate disposition parameter', async () => {
|
|
270
|
-
prisma.file.
|
|
350
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
271
351
|
|
|
272
352
|
const response = await fastify.inject({
|
|
273
353
|
method: 'GET',
|
|
@@ -276,11 +356,11 @@ describe('File Route', () => {
|
|
|
276
356
|
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
277
357
|
|
|
278
358
|
expect(response.statusCode).toBe(400);
|
|
279
|
-
expect(body.error).toBe('
|
|
359
|
+
expect(body.error.code).toBe('VALIDATION_ERROR');
|
|
280
360
|
});
|
|
281
361
|
|
|
282
362
|
it('should return 404 when get handler returns false', async () => {
|
|
283
|
-
prisma.file.
|
|
363
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
284
364
|
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
285
365
|
|
|
286
366
|
const response = await fastify.inject({
|
|
@@ -297,7 +377,7 @@ describe('File Route', () => {
|
|
|
297
377
|
|
|
298
378
|
describe('HEAD /file/:id', () => {
|
|
299
379
|
it('should return headers without body using head handler', async () => {
|
|
300
|
-
prisma.file.
|
|
380
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
301
381
|
|
|
302
382
|
const mockMetadata = {
|
|
303
383
|
contentType: 'audio/wav',
|
|
@@ -319,7 +399,7 @@ describe('File Route', () => {
|
|
|
319
399
|
expect(response.body).toBe(''); // No body for HEAD
|
|
320
400
|
|
|
321
401
|
expect(mockFileHandler.head).toHaveBeenCalledWith(
|
|
322
|
-
|
|
402
|
+
handlerFile,
|
|
323
403
|
expect.objectContaining({
|
|
324
404
|
request: expect.any(Object),
|
|
325
405
|
fastify: expect.any(Object),
|
|
@@ -328,7 +408,7 @@ describe('File Route', () => {
|
|
|
328
408
|
});
|
|
329
409
|
|
|
330
410
|
it('should return 404 when file not found', async () => {
|
|
331
|
-
prisma.file.
|
|
411
|
+
prisma.file.findFirst.mockResolvedValue(null);
|
|
332
412
|
|
|
333
413
|
const response = await fastify.inject({
|
|
334
414
|
method: 'HEAD',
|
|
@@ -341,8 +421,21 @@ describe('File Route', () => {
|
|
|
341
421
|
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
342
422
|
});
|
|
343
423
|
|
|
424
|
+
it('should return 403 when the content is not accessible', async () => {
|
|
425
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
426
|
+
mockAccess.mockReturnValue({ metadata: true, content: false });
|
|
427
|
+
|
|
428
|
+
const response = await fastify.inject({
|
|
429
|
+
method: 'HEAD',
|
|
430
|
+
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(response.statusCode).toBe(403);
|
|
434
|
+
expect(mockFileHandler.head).not.toHaveBeenCalled();
|
|
435
|
+
});
|
|
436
|
+
|
|
344
437
|
it('should return 500 when head handler throws error', async () => {
|
|
345
|
-
prisma.file.
|
|
438
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
346
439
|
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
347
440
|
|
|
348
441
|
const response = await fastify.inject({
|
|
@@ -356,7 +449,7 @@ describe('File Route', () => {
|
|
|
356
449
|
});
|
|
357
450
|
|
|
358
451
|
it('should return 404 when head handler returns false', async () => {
|
|
359
|
-
prisma.file.
|
|
452
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
360
453
|
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
361
454
|
|
|
362
455
|
const response = await fastify.inject({
|
|
@@ -373,7 +466,7 @@ describe('File Route', () => {
|
|
|
373
466
|
|
|
374
467
|
describe('GET /file/:id - exhaustiveness check', () => {
|
|
375
468
|
it('should handle unexpected file result type', async () => {
|
|
376
|
-
prisma.file.
|
|
469
|
+
prisma.file.findFirst.mockResolvedValue(mockFile);
|
|
377
470
|
|
|
378
471
|
// Mock an invalid result type to test exhaustiveness check
|
|
379
472
|
const invalidResult = {
|
|
@@ -390,6 +483,8 @@ describe('File Route', () => {
|
|
|
390
483
|
|
|
391
484
|
expect(response.statusCode).toBe(500);
|
|
392
485
|
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
486
|
+
expect(response.headers['content-disposition']).toBeUndefined();
|
|
487
|
+
expect(response.headers['content-type']).toContain('application/json');
|
|
393
488
|
});
|
|
394
489
|
});
|
|
395
490
|
});
|