@contember/engine-http 2.2.0-alpha.19 → 2.2.0-alpha.20
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/CLAUDE.md +2 -0
- package/dist/development/index.cjs +8 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +9 -1
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/MasterContainer.cjs +28 -14
- package/dist/development/src/MasterContainer.cjs.map +1 -1
- package/dist/development/src/MasterContainer.js +28 -14
- package/dist/development/src/MasterContainer.js.map +1 -1
- package/dist/development/src/config/configSchema.cjs +50 -0
- package/dist/development/src/config/configSchema.cjs.map +1 -1
- package/dist/development/src/config/configSchema.js +50 -0
- package/dist/development/src/config/configSchema.js.map +1 -1
- package/dist/development/src/config/configTemplate.cjs +8 -0
- package/dist/development/src/config/configTemplate.cjs.map +1 -1
- package/dist/development/src/config/configTemplate.js +8 -0
- package/dist/development/src/config/configTemplate.js.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/development/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/development/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/development/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/development/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/development/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/development/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/development/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/development/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/development/src/content/ContentRequestLog.cjs +97 -0
- package/dist/development/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/development/src/content/ContentRequestLog.js +97 -0
- package/dist/development/src/content/ContentRequestLog.js.map +1 -0
- package/dist/development/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/development/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/development/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/development/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/development/src/graphql/execution.cjs +18 -5
- package/dist/development/src/graphql/execution.cjs.map +1 -1
- package/dist/development/src/graphql/execution.js +19 -6
- package/dist/development/src/graphql/execution.js.map +1 -1
- package/dist/development/src/project/ProjectContainer.cjs +2 -0
- package/dist/development/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/development/src/project/ProjectContainer.js +2 -0
- package/dist/development/src/project/ProjectContainer.js.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/development/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/development/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/development/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/development/src/system/ContentQueryExecutor.js +25 -8
- package/dist/development/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/index.cjs +8 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +9 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/MasterContainer.cjs +28 -14
- package/dist/production/src/MasterContainer.cjs.map +1 -1
- package/dist/production/src/MasterContainer.js +28 -14
- package/dist/production/src/MasterContainer.js.map +1 -1
- package/dist/production/src/config/configSchema.cjs +50 -0
- package/dist/production/src/config/configSchema.cjs.map +1 -1
- package/dist/production/src/config/configSchema.js +50 -0
- package/dist/production/src/config/configSchema.js.map +1 -1
- package/dist/production/src/config/configTemplate.cjs +8 -0
- package/dist/production/src/config/configTemplate.cjs.map +1 -1
- package/dist/production/src/config/configTemplate.js +8 -0
- package/dist/production/src/config/configTemplate.js.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentApiControllerFactory.js +12 -1
- package/dist/production/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.cjs +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.cjs.map +1 -1
- package/dist/production/src/content/ContentApiSpecificCache.js +20 -5
- package/dist/production/src/content/ContentApiSpecificCache.js.map +1 -1
- package/dist/production/src/content/ContentGraphQLEngine.cjs +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.cjs.map +1 -0
- package/dist/production/src/content/ContentGraphQLEngine.js +17 -0
- package/dist/production/src/content/ContentGraphQLEngine.js.map +1 -0
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs +4 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.cjs.map +1 -1
- package/dist/production/src/content/ContentQueryHandlerFactory.js +5 -2
- package/dist/production/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/production/src/content/ContentRequestLog.cjs +97 -0
- package/dist/production/src/content/ContentRequestLog.cjs.map +1 -0
- package/dist/production/src/content/ContentRequestLog.js +97 -0
- package/dist/production/src/content/ContentRequestLog.js.map +1 -0
- package/dist/production/src/content/GraphQlSchemaFactory.cjs +121 -45
- package/dist/production/src/content/GraphQlSchemaFactory.cjs.map +1 -1
- package/dist/production/src/content/GraphQlSchemaFactory.js +122 -46
- package/dist/production/src/content/GraphQlSchemaFactory.js.map +1 -1
- package/dist/production/src/graphql/execution.cjs +18 -5
- package/dist/production/src/graphql/execution.cjs.map +1 -1
- package/dist/production/src/graphql/execution.js +19 -6
- package/dist/production/src/graphql/execution.js.map +1 -1
- package/dist/production/src/project/ProjectContainer.cjs +2 -0
- package/dist/production/src/project/ProjectContainer.cjs.map +1 -1
- package/dist/production/src/project/ProjectContainer.js +2 -0
- package/dist/production/src/project/ProjectContainer.js.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.cjs.map +1 -1
- package/dist/production/src/projectGroup/ProjectGroupContainer.js +2 -0
- package/dist/production/src/projectGroup/ProjectGroupContainer.js.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.cjs +23 -6
- package/dist/production/src/system/ContentQueryExecutor.cjs.map +1 -1
- package/dist/production/src/system/ContentQueryExecutor.js +25 -8
- package/dist/production/src/system/ContentQueryExecutor.js.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentGraphqlCompat.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts +2 -0
- package/dist/tests/cases/unit/contentRequestLog.test.d.ts.map +1 -0
- package/dist/tests/contentControllerHarness.d.ts +29 -0
- package/dist/tests/contentControllerHarness.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/MasterContainer.d.ts +10 -1
- package/dist/types/MasterContainer.d.ts.map +1 -1
- package/dist/types/config/configSchema.d.ts +10 -0
- package/dist/types/config/configSchema.d.ts.map +1 -1
- package/dist/types/config/configTemplate.d.ts.map +1 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts +3 -1
- package/dist/types/content/ContentApiControllerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentApiSpecificCache.d.ts +2 -0
- package/dist/types/content/ContentApiSpecificCache.d.ts.map +1 -1
- package/dist/types/content/ContentGraphQLEngine.d.ts +18 -0
- package/dist/types/content/ContentGraphQLEngine.d.ts.map +1 -0
- package/dist/types/content/ContentQueryHandlerFactory.d.ts +3 -2
- package/dist/types/content/ContentQueryHandlerFactory.d.ts.map +1 -1
- package/dist/types/content/ContentRequestLog.d.ts +49 -0
- package/dist/types/content/ContentRequestLog.d.ts.map +1 -0
- package/dist/types/content/GraphQlSchemaFactory.d.ts +14 -4
- package/dist/types/content/GraphQlSchemaFactory.d.ts.map +1 -1
- package/dist/types/content/index.d.ts +2 -0
- package/dist/types/content/index.d.ts.map +1 -1
- package/dist/types/graphql/execution.d.ts +21 -3
- package/dist/types/graphql/execution.d.ts.map +1 -1
- package/dist/types/system/ContentQueryExecutor.d.ts +4 -1
- package/dist/types/system/ContentQueryExecutor.d.ts.map +1 -1
- package/dist/types/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -18
- package/src/MasterContainer.ts +31 -14
- package/src/config/configSchema.ts +63 -0
- package/src/config/configTemplate.ts +8 -0
- package/src/content/ContentApiControllerFactory.ts +15 -2
- package/src/content/ContentApiSpecificCache.ts +22 -7
- package/src/content/ContentGraphQLEngine.ts +24 -0
- package/src/content/ContentQueryHandlerFactory.ts +16 -4
- package/src/content/ContentRequestLog.ts +138 -0
- package/src/content/GraphQlSchemaFactory.ts +173 -54
- package/src/content/index.ts +2 -0
- package/src/graphql/execution.ts +35 -7
- package/src/project/ProjectContainer.ts +1 -1
- package/src/system/ContentQueryExecutor.ts +43 -10
- package/src/system/SystemGraphQLHandlerFactory.ts +8 -2
- package/src/tenant/TenantGraphQLHandlerFactory.ts +2 -2
- package/src/tsconfig.json +1 -0
- package/tests/cases/unit/__snapshots__/contentGraphqlCompat.test.ts.snap +14295 -0
- package/tests/cases/unit/contentGraphqlCompat.test.ts +372 -0
- package/tests/cases/unit/contentRequestLog.test.ts +141 -0
- package/tests/cases/unit/forcePrimary.test.ts +93 -284
- package/tests/cases/unit/graphQlSchemaFactory.test.ts +227 -35
- package/tests/cases/unit/requestMemoryBudget.test.ts +96 -78
- package/tests/cases/unit/serverConfig.test.ts +133 -0
- package/tests/contentControllerHarness.ts +222 -0
- package/tests/tsconfig.json +3 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { expect, test } from 'bun:test'
|
|
2
|
-
import {
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { parse } from 'graphql'
|
|
3
|
+
import { TypeKind, validate } from '@contember/graphql-json'
|
|
4
|
+
import { GraphQlSchemaBuilderFactory, JsonContentSchema, PermissionFactory } from '@contember/engine-content-api'
|
|
3
5
|
import { Acl, Model, Schema } from '@contember/schema'
|
|
4
6
|
import { emptySchema } from '@contember/schema-utils'
|
|
5
7
|
import { ContentApiSpecificCache } from '../../../src/content/ContentApiSpecificCache.js'
|
|
8
|
+
import { ContentGraphQLEngine, ContentGraphQLEngineResolver } from '../../../src/content/ContentGraphQLEngine.js'
|
|
6
9
|
import { GraphQlSchemaFactory, GraphQLSchemaFactoryResult } from '../../../src/content/GraphQlSchemaFactory.js'
|
|
7
10
|
import { GraphQLSchemaContributor } from '../../../src/content/GraphQLSchemaContributor.js'
|
|
8
11
|
import { ProjectConfig } from '../../../src/project/config.js'
|
|
@@ -18,10 +21,11 @@ const article: Model.Entity = {
|
|
|
18
21
|
fields: {
|
|
19
22
|
id: { name: 'id', columnName: 'id', type: Model.ColumnType.Uuid, columnType: 'uuid', nullable: false },
|
|
20
23
|
title: { name: 'title', columnName: 'title', type: Model.ColumnType.String, columnType: 'text', nullable: true },
|
|
24
|
+
data: { name: 'data', columnName: 'data', type: Model.ColumnType.Json, columnType: 'jsonb', nullable: true },
|
|
21
25
|
},
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
const allFields: Acl.FieldPermissions = { id: true, title: true }
|
|
28
|
+
const allFields: Acl.FieldPermissions = { id: true, title: true, data: true }
|
|
25
29
|
|
|
26
30
|
const role = (entities: Acl.Permissions): Acl.RolePermissions => ({ variables: {}, entities })
|
|
27
31
|
|
|
@@ -45,45 +49,130 @@ const project: ProjectConfig = {
|
|
|
45
49
|
db: { host: 'localhost', port: 5432, user: 'test', password: 'test', database: 'test' },
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
const createFactory = (
|
|
49
|
-
|
|
52
|
+
const createFactory = (
|
|
53
|
+
contributors: GraphQLSchemaContributor[] = [],
|
|
54
|
+
engineResolver = new ContentGraphQLEngineResolver('graphql-json'),
|
|
55
|
+
cache = new ContentApiSpecificCache<Schema, GraphQLSchemaFactoryResult>({}),
|
|
56
|
+
) => new GraphQlSchemaFactory(cache, new GraphQlSchemaBuilderFactory(), new PermissionFactory(), contributors, engineResolver)
|
|
57
|
+
|
|
58
|
+
const create = (factory: GraphQlSchemaFactory, projectRoles: string[], forProject = project) => factory.create(schema, { projectRoles }, forProject)
|
|
59
|
+
|
|
60
|
+
const mutationFields = ({ schema }: GraphQLSchemaFactoryResult): Record<string, unknown> => {
|
|
61
|
+
if (schema.engine === 'graphql-js') {
|
|
62
|
+
return schema.schema.getMutationType()?.getFields() ?? {}
|
|
63
|
+
}
|
|
64
|
+
const json = schema.schema.schema
|
|
65
|
+
const mutation = json.m ? json.t[json.m] : undefined
|
|
66
|
+
return mutation?.[0] === TypeKind.OBJECT ? mutation[1] : {}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const jsonSchemaOf = ({ schema }: GraphQLSchemaFactoryResult): JsonContentSchema => {
|
|
70
|
+
if (schema.engine !== 'graphql-json') {
|
|
71
|
+
throw new Error(`expected a graphql-json schema, got ${schema.engine}`)
|
|
72
|
+
}
|
|
73
|
+
return schema.schema
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
test('the content API runs on graphql-js unless configured otherwise', async () => {
|
|
77
|
+
const factory = new GraphQlSchemaFactory(
|
|
50
78
|
new ContentApiSpecificCache<Schema, GraphQLSchemaFactoryResult>({}),
|
|
51
79
|
new GraphQlSchemaBuilderFactory(),
|
|
52
80
|
new PermissionFactory(),
|
|
53
|
-
|
|
81
|
+
[],
|
|
54
82
|
)
|
|
55
83
|
|
|
56
|
-
|
|
84
|
+
expect((await create(factory, ['admin'])).schema.engine).toBe('graphql-js')
|
|
85
|
+
})
|
|
57
86
|
|
|
58
|
-
|
|
59
|
-
|
|
87
|
+
// Both projects share the content schema and the roles, so this also checks that the engine is part of the cache key.
|
|
88
|
+
test('a listed project runs on graphql-json, the other projects on the default engine', async () => {
|
|
89
|
+
const factory = createFactory([], new ContentGraphQLEngineResolver('graphql-js', ['switched']))
|
|
90
|
+
const switched = await create(factory, ['admin'], { ...project, slug: 'switched' })
|
|
91
|
+
const other = await create(factory, ['admin'])
|
|
60
92
|
|
|
61
|
-
expect(
|
|
93
|
+
expect(switched.schema.engine).toBe('graphql-json')
|
|
94
|
+
expect(other.schema.engine).toBe('graphql-js')
|
|
95
|
+
expect(mutationFields(switched).createArticle).toBeDefined()
|
|
96
|
+
expect(mutationFields(other).createArticle).toBeDefined()
|
|
62
97
|
})
|
|
63
98
|
|
|
64
|
-
test('
|
|
65
|
-
const factory = createFactory()
|
|
99
|
+
test('the default engine applies to every project that is not listed', async () => {
|
|
100
|
+
const factory = createFactory([], new ContentGraphQLEngineResolver('graphql-json', ['switched']))
|
|
66
101
|
|
|
67
|
-
expect(create(factory, ['admin'
|
|
102
|
+
expect((await create(factory, ['admin'])).schema.engine).toBe('graphql-json')
|
|
103
|
+
expect((await create(factory, ['admin'], { ...project, slug: 'switched' })).schema.engine).toBe('graphql-json')
|
|
68
104
|
})
|
|
69
105
|
|
|
70
|
-
|
|
71
|
-
const factory = createFactory()
|
|
72
|
-
const admin = create(factory, ['admin'])
|
|
73
|
-
const reader = create(factory, ['reader'])
|
|
106
|
+
const engines: ContentGraphQLEngine[] = ['graphql-js', 'graphql-json']
|
|
74
107
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
108
|
+
describe.each(engines)('on %s', engine => {
|
|
109
|
+
const createEngineFactory = (contributors: GraphQLSchemaContributor[] = []) => createFactory(contributors, new ContentGraphQLEngineResolver(engine))
|
|
110
|
+
|
|
111
|
+
test('role combinations with identical permissions share one schema', async () => {
|
|
112
|
+
const factory = createEngineFactory()
|
|
113
|
+
|
|
114
|
+
expect(await create(factory, ['editor'])).toBe(await create(factory, ['admin']))
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('a role without content permissions does not split the cache', async () => {
|
|
118
|
+
const factory = createEngineFactory()
|
|
119
|
+
|
|
120
|
+
expect(await create(factory, ['admin', 'newsletter'])).toBe(await create(factory, ['admin']))
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('role combinations with different permissions get their own schema', async () => {
|
|
124
|
+
const factory = createEngineFactory()
|
|
125
|
+
const admin = await create(factory, ['admin'])
|
|
126
|
+
const reader = await create(factory, ['reader'])
|
|
127
|
+
|
|
128
|
+
expect(reader).not.toBe(admin)
|
|
129
|
+
expect(mutationFields(admin).createArticle).toBeDefined()
|
|
130
|
+
expect(mutationFields(reader).createArticle).toBeUndefined()
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('a contributor cache key separates role combinations with identical permissions', async () => {
|
|
134
|
+
const factory = createEngineFactory([{
|
|
135
|
+
getCacheKey: ({ identity }) => identity.projectRoles.includes('editor') ? 'editor' : 'other',
|
|
136
|
+
createSchema: () => undefined,
|
|
137
|
+
}])
|
|
138
|
+
|
|
139
|
+
expect(await create(factory, ['editor'])).not.toBe(await create(factory, ['admin']))
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('a role without the refresh permission cannot refresh a materialized view another role may refresh', async () => {
|
|
143
|
+
const factory = createEngineFactory()
|
|
144
|
+
|
|
145
|
+
expect(await hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
146
|
+
expect(await hasRefreshMutation(factory, ['refresher'])).toBe(true)
|
|
147
|
+
expect(await hasRefreshMutation(factory, ['reader'])).toBe(false)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('the refresh permission is inherited', async () => {
|
|
151
|
+
expect(await hasRefreshMutation(createEngineFactory(), ['inheritedRefresher'])).toBe(true)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('concurrent requests of one role combination build it once', async () => {
|
|
155
|
+
const factory = createEngineFactory()
|
|
156
|
+
const [editor, admin] = await Promise.all([create(factory, ['editor']), create(factory, ['admin'])])
|
|
157
|
+
|
|
158
|
+
expect(editor).toBe(admin)
|
|
159
|
+
})
|
|
79
160
|
|
|
80
|
-
test('a
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
161
|
+
test('a failed build is not cached', async () => {
|
|
162
|
+
let fail = true
|
|
163
|
+
const factory = createEngineFactory([{
|
|
164
|
+
createSchema: () => {
|
|
165
|
+
if (fail) {
|
|
166
|
+
throw new Error('contributor failed')
|
|
167
|
+
}
|
|
168
|
+
return undefined
|
|
169
|
+
},
|
|
170
|
+
}])
|
|
85
171
|
|
|
86
|
-
|
|
172
|
+
await expect(create(factory, ['admin'])).rejects.toThrow('contributor failed')
|
|
173
|
+
fail = false
|
|
174
|
+
expect(mutationFields(await create(factory, ['admin'])).createArticle).toBeDefined()
|
|
175
|
+
})
|
|
87
176
|
})
|
|
88
177
|
|
|
89
178
|
const stats: Model.Entity = {
|
|
@@ -114,17 +203,120 @@ const materializedViewSchema: Schema = {
|
|
|
114
203
|
},
|
|
115
204
|
}
|
|
116
205
|
|
|
117
|
-
const hasRefreshMutation = (factory: GraphQlSchemaFactory, projectRoles: string[]) =>
|
|
118
|
-
factory.create(materializedViewSchema, { projectRoles }, project)
|
|
206
|
+
const hasRefreshMutation = async (factory: GraphQlSchemaFactory, projectRoles: string[]) =>
|
|
207
|
+
mutationFields(await factory.create(materializedViewSchema, { projectRoles }, project)).refreshMaterializedView !== undefined
|
|
119
208
|
|
|
120
|
-
test('a
|
|
209
|
+
test('a Json column accepts an object literal next to the introspection schema', async () => {
|
|
210
|
+
const result = jsonSchemaOf(await create(createFactory(), ['admin']))
|
|
211
|
+
const document = parse('mutation { createArticle(data: { data: { message: "hello", tags: ["a"] } }) { ok } }')
|
|
212
|
+
expect(validate(result.schema, document, { scalars: result.scalars }).map(it => it.message)).toEqual([])
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test('role schemas of one project schema share the types their permissions do not change', async () => {
|
|
121
216
|
const factory = createFactory()
|
|
217
|
+
const jsonSchema = async (roles: string[]) => jsonSchemaOf(await create(factory, roles)).schema
|
|
218
|
+
const admin = await jsonSchema(['admin'])
|
|
219
|
+
const reader = await jsonSchema(['reader'])
|
|
122
220
|
|
|
123
|
-
expect(
|
|
124
|
-
expect(
|
|
125
|
-
expect(
|
|
221
|
+
expect(reader.t.Article).toBe(admin.t.Article)
|
|
222
|
+
expect(reader.t.ArticleWhere).toBe(admin.t.ArticleWhere)
|
|
223
|
+
expect(reader.t.Query).not.toBe(admin.t.Query)
|
|
224
|
+
expect(reader.m).toBeNull()
|
|
126
225
|
})
|
|
127
226
|
|
|
128
|
-
|
|
129
|
-
|
|
227
|
+
const collectGarbage = async () => {
|
|
228
|
+
// a WeakRef target stays alive until the current job ends
|
|
229
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
230
|
+
Bun.gc(true)
|
|
231
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The collector scans the stack conservatively, so a single pass may keep a dead object; retry a few times. */
|
|
235
|
+
const isCollected = async (ref: WeakRef<object>): Promise<boolean> => {
|
|
236
|
+
for (let attempt = 0; attempt < 10 && ref.deref() !== undefined; attempt++) {
|
|
237
|
+
await new Promise(resolve => setTimeout(resolve, 20))
|
|
238
|
+
await collectGarbage()
|
|
239
|
+
}
|
|
240
|
+
return ref.deref() === undefined
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const manyRolesSchema: Schema = {
|
|
244
|
+
...emptySchema,
|
|
245
|
+
model: {
|
|
246
|
+
entities: Object.fromEntries(Array.from({ length: 20 }, (_, i) => [`Article${i}`, { ...article, name: `Article${i}`, tableName: `article${i}` }])),
|
|
247
|
+
enums: {},
|
|
248
|
+
},
|
|
249
|
+
acl: {
|
|
250
|
+
roles: Object.fromEntries(
|
|
251
|
+
Array.from({ length: 20 }, (_, i) => [`role${i}`, role({ [`Article${i}`]: { predicates: {}, operations: { read: allFields } } })]),
|
|
252
|
+
),
|
|
253
|
+
},
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Keeps one interned type and only a weak reference to the cached schema, so the cache alone decides its lifetime. */
|
|
257
|
+
const buildAndForget = async (factory: GraphQlSchemaFactory, schema: Schema, role: string, typeName: string) => {
|
|
258
|
+
const built = jsonSchemaOf(await factory.create(schema, { projectRoles: [role] }, project))
|
|
259
|
+
return { built: new WeakRef(built), type: built.schema.t[typeName] }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
test('once every schema of a project schema is evicted, the data they shared is not retained', async () => {
|
|
263
|
+
const factory = createFactory([], undefined, new ContentApiSpecificCache<Schema, GraphQLSchemaFactoryResult>({ ttlSeconds: 0.01 }))
|
|
264
|
+
const evicted = await buildAndForget(factory, schema, 'admin', 'Article')
|
|
265
|
+
await new Promise(resolve => setTimeout(resolve, 50))
|
|
266
|
+
expect(await isCollected(evicted.built)).toBe(true)
|
|
267
|
+
|
|
268
|
+
// the interner of the evicted schema is gone, so the rebuilt schema does not reuse its types
|
|
269
|
+
expect(jsonSchemaOf(await factory.create(schema, { projectRoles: ['admin'] }, project)).schema.t.Article).not.toBe(evicted.type)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
test('once most schemas of a generation are evicted, new schemas no longer share its interned data', async () => {
|
|
273
|
+
const factory = createFactory([], undefined, new ContentApiSpecificCache<Schema, GraphQLSchemaFactoryResult>({ maxEntries: 1 }))
|
|
274
|
+
const first = await buildAndForget(factory, manyRolesSchema, 'role0', 'Article0')
|
|
275
|
+
await buildAndForget(factory, manyRolesSchema, 'role1', 'Article1')
|
|
276
|
+
expect(await isCollected(first.built)).toBe(true)
|
|
277
|
+
// 16 evictions start a new generation; the extra roles leave room for a schema the collector keeps for a while
|
|
278
|
+
for (let i = 2; i < 20; i++) {
|
|
279
|
+
await buildAndForget(factory, manyRolesSchema, `role${i}`, `Article${i}`)
|
|
280
|
+
await collectGarbage()
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
expect(jsonSchemaOf(await factory.create(manyRolesSchema, { projectRoles: ['role0'] }, project)).schema.t.Article0).not.toBe(first.type)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
const chainedEntities = (count: number): Model.Schema['entities'] =>
|
|
287
|
+
Object.fromEntries(Array.from({ length: count }, (_, i): [string, Model.Entity] => [`Node${i}`, {
|
|
288
|
+
...article,
|
|
289
|
+
name: `Node${i}`,
|
|
290
|
+
tableName: `node${i}`,
|
|
291
|
+
fields: {
|
|
292
|
+
...article.fields,
|
|
293
|
+
next: {
|
|
294
|
+
name: 'next',
|
|
295
|
+
type: Model.RelationType.ManyHasOne,
|
|
296
|
+
target: `Node${(i + 1) % count}`,
|
|
297
|
+
joiningColumn: { columnName: 'next_id', onDelete: Model.OnDelete.restrict },
|
|
298
|
+
nullable: true,
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
}]))
|
|
302
|
+
|
|
303
|
+
test('a large schema is built in slices, between which the event loop runs I/O callbacks', async () => {
|
|
304
|
+
const entities = chainedEntities(150)
|
|
305
|
+
const allOperations = {
|
|
306
|
+
read: { ...allFields, next: true },
|
|
307
|
+
create: { ...allFields, next: true },
|
|
308
|
+
update: { ...allFields, next: true },
|
|
309
|
+
delete: true,
|
|
310
|
+
}
|
|
311
|
+
const largeSchema: Schema = {
|
|
312
|
+
...emptySchema,
|
|
313
|
+
model: { entities, enums: {} },
|
|
314
|
+
acl: { roles: { admin: role(Object.fromEntries(Object.keys(entities).map(name => [name, { predicates: {}, operations: allOperations }]))) } },
|
|
315
|
+
}
|
|
316
|
+
const events: string[] = []
|
|
317
|
+
setImmediate(() => events.push('io'))
|
|
318
|
+
await createFactory().create(largeSchema, { projectRoles: ['admin'] }, project)
|
|
319
|
+
events.push('built')
|
|
320
|
+
|
|
321
|
+
expect(events).toEqual(['io', 'built'])
|
|
130
322
|
})
|
|
@@ -1,67 +1,83 @@
|
|
|
1
|
-
import { expect, test } from 'bun:test'
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
2
|
import { fetch } from 'bun'
|
|
3
3
|
import { once } from 'node:events'
|
|
4
4
|
import Koa from 'koa'
|
|
5
5
|
import { GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'
|
|
6
6
|
import { RequestMemoryBudget } from '@contember/database'
|
|
7
7
|
import { createLogger, TestLoggerHandler, withLogger } from '@contember/logger'
|
|
8
|
-
import {
|
|
8
|
+
import { buildSchema, builtInScalars } from '@contember/graphql-json'
|
|
9
|
+
import { createGraphQLJsExecutor, createGraphQLJsonExecutor, createGraphQLQueryHandler, GraphQLExecutor } from '../../../src/graphql/execution.js'
|
|
9
10
|
import { serverConfigSchema } from '../../../src/config/configSchema.js'
|
|
10
11
|
import { readConfig } from '../../../src/config/config.js'
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
type BudgetContext = { budget: RequestMemoryBudget }
|
|
14
|
+
type Engine = 'graphql-js' | 'graphql-json'
|
|
15
|
+
|
|
16
|
+
const createResolvers = (chargeInResolver: boolean) => ({
|
|
17
|
+
body: (context: BudgetContext) => {
|
|
18
|
+
if (chargeInResolver) {
|
|
19
|
+
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
return 'x'.repeat(4096)
|
|
23
|
+
},
|
|
24
|
+
swallowed: (context: BudgetContext) => {
|
|
25
|
+
try {
|
|
26
|
+
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
27
|
+
} catch {}
|
|
28
|
+
return 'partial'
|
|
29
|
+
},
|
|
30
|
+
write: (context: BudgetContext) => {
|
|
31
|
+
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
32
|
+
return 'written'
|
|
33
|
+
},
|
|
34
|
+
report: (context: BudgetContext) => {
|
|
35
|
+
try {
|
|
36
|
+
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
37
|
+
} catch {
|
|
38
|
+
return 'exhausted'
|
|
39
|
+
}
|
|
40
|
+
return 'written'
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const createExecutor = (engine: Engine, chargeInResolver: boolean): GraphQLExecutor<BudgetContext> => {
|
|
45
|
+
const resolvers = createResolvers(chargeInResolver)
|
|
46
|
+
if (engine === 'graphql-json') {
|
|
47
|
+
return createGraphQLJsonExecutor<BudgetContext>({
|
|
48
|
+
schema: buildSchema('type Query { body: String swallowed: String } type Mutation { write: String! report: String! }'),
|
|
49
|
+
resolvers: [{
|
|
50
|
+
Query: { body: (_, args, context) => resolvers.body(context), swallowed: (_, args, context) => resolvers.swallowed(context) },
|
|
51
|
+
Mutation: { write: (_, args, context) => resolvers.write(context), report: (_, args, context) => resolvers.report(context) },
|
|
52
|
+
}],
|
|
53
|
+
scalars: builtInScalars,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
return createGraphQLJsExecutor(
|
|
57
|
+
new GraphQLSchema({
|
|
58
|
+
query: new GraphQLObjectType<unknown, BudgetContext>({
|
|
18
59
|
name: 'Query',
|
|
19
60
|
fields: {
|
|
20
|
-
body: {
|
|
21
|
-
|
|
22
|
-
resolve: (_, args, context) => {
|
|
23
|
-
if (chargeInResolver) {
|
|
24
|
-
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
|
-
return 'x'.repeat(4096)
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
swallowed: {
|
|
31
|
-
type: GraphQLString,
|
|
32
|
-
resolve: (_, args, context) => {
|
|
33
|
-
try {
|
|
34
|
-
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
35
|
-
} catch {}
|
|
36
|
-
return 'partial'
|
|
37
|
-
},
|
|
38
|
-
},
|
|
61
|
+
body: { type: GraphQLString, resolve: (_, args, context) => resolvers.body(context) },
|
|
62
|
+
swallowed: { type: GraphQLString, resolve: (_, args, context) => resolvers.swallowed(context) },
|
|
39
63
|
},
|
|
40
64
|
}),
|
|
41
|
-
mutation: new GraphQLObjectType<unknown,
|
|
65
|
+
mutation: new GraphQLObjectType<unknown, BudgetContext>({
|
|
42
66
|
name: 'Mutation',
|
|
43
67
|
fields: {
|
|
44
|
-
write: {
|
|
45
|
-
|
|
46
|
-
resolve: (_, args, context) => {
|
|
47
|
-
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
48
|
-
return 'written'
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
report: {
|
|
52
|
-
type: new GraphQLNonNull(GraphQLString),
|
|
53
|
-
resolve: (_, args, context) => {
|
|
54
|
-
try {
|
|
55
|
-
context.budget.addDatabaseRow({ body: 'x'.repeat(4096) })
|
|
56
|
-
} catch {
|
|
57
|
-
return 'exhausted'
|
|
58
|
-
}
|
|
59
|
-
return 'written'
|
|
60
|
-
},
|
|
61
|
-
},
|
|
68
|
+
write: { type: new GraphQLNonNull(GraphQLString), resolve: (_, args, context) => resolvers.write(context) },
|
|
69
|
+
report: { type: new GraphQLNonNull(GraphQLString), resolve: (_, args, context) => resolvers.report(context) },
|
|
62
70
|
},
|
|
63
71
|
}),
|
|
64
72
|
}),
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function request(engine: Engine, maxBytes: number, chargeInResolver: boolean, query = '{ body }') {
|
|
77
|
+
const budget = new RequestMemoryBudget({ warnBytes: 512, maxBytes })
|
|
78
|
+
const responses: unknown[] = []
|
|
79
|
+
const handler = createGraphQLQueryHandler<BudgetContext>({
|
|
80
|
+
executor: createExecutor(engine, chargeInResolver),
|
|
65
81
|
listeners: [{ onResponse: ({ response }) => void responses.push(response) }],
|
|
66
82
|
getMemoryBudget: context => context.budget,
|
|
67
83
|
})
|
|
@@ -89,43 +105,45 @@ async function request(maxBytes: number, chargeInResolver: boolean, query = '{ b
|
|
|
89
105
|
}
|
|
90
106
|
}
|
|
91
107
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
expect.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
108
|
+
describe.each<Engine>(['graphql-js', 'graphql-json'])('on %s', engine => {
|
|
109
|
+
test('budget failure inside a nullable resolver returns one resource error with its path and no partial data', async () => {
|
|
110
|
+
const { status, body, responses } = await request(engine, 1024, true)
|
|
111
|
+
expect(status).toBe(422)
|
|
112
|
+
expect(body.data).toBeNull()
|
|
113
|
+
expect(body.errors).toEqual([
|
|
114
|
+
expect.objectContaining({ message: 'Request memory budget exceeded', path: ['body'], extensions: { code: 'RESOURCE_EXHAUSTED' } }),
|
|
115
|
+
])
|
|
116
|
+
// Response listeners (debug query log, triggered actions) see the rejected response.
|
|
117
|
+
expect(responses).toEqual([expect.objectContaining({ data: null })])
|
|
118
|
+
})
|
|
102
119
|
|
|
103
|
-
test('a query whose resolver swallowed the budget failure is still rejected as a whole', async () => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
120
|
+
test('a query whose resolver swallowed the budget failure is still rejected as a whole', async () => {
|
|
121
|
+
const { status, body } = await request(engine, 1024, true, '{ swallowed }')
|
|
122
|
+
expect(status).toBe(422)
|
|
123
|
+
expect(body).toEqual({
|
|
124
|
+
data: null,
|
|
125
|
+
errors: [{ message: 'Request memory budget exceeded', extensions: { code: 'RESOURCE_EXHAUSTED' } }],
|
|
126
|
+
})
|
|
109
127
|
})
|
|
110
|
-
})
|
|
111
128
|
|
|
112
|
-
test('a mutation that reports budget exhaustion in its own result keeps its data', async () => {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
})
|
|
129
|
+
test('a mutation that reports budget exhaustion in its own result keeps its data', async () => {
|
|
130
|
+
const { status, body } = await request(engine, 1024, true, 'mutation { report }')
|
|
131
|
+
expect({ status, body }).toEqual({ status: 200, body: { data: { report: 'exhausted' } } })
|
|
132
|
+
})
|
|
116
133
|
|
|
117
|
-
test('budget failure thrown by a mutation field is a resource error with its path, not an internal error', async () => {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})
|
|
134
|
+
test('budget failure thrown by a mutation field is a resource error with its path, not an internal error', async () => {
|
|
135
|
+
const response = await request(engine, 1024, true, 'mutation { write }')
|
|
136
|
+
expect(response.status).toBe(422)
|
|
137
|
+
expect(response.body.data).toBeNull()
|
|
138
|
+
expect(response.body.errors).toEqual([
|
|
139
|
+
expect.objectContaining({ message: 'Request memory budget exceeded', path: ['write'], extensions: { code: 'RESOURCE_EXHAUSTED' } }),
|
|
140
|
+
])
|
|
141
|
+
})
|
|
125
142
|
|
|
126
|
-
test('response of a request within its budget is preserved', async () => {
|
|
127
|
-
|
|
128
|
-
|
|
143
|
+
test('response of a request within its budget is preserved', async () => {
|
|
144
|
+
const { status, body } = await request(engine, 32768, false)
|
|
145
|
+
expect({ status, body }).toEqual({ status: 200, body: { data: { body: 'x'.repeat(4096) } } })
|
|
146
|
+
})
|
|
129
147
|
})
|
|
130
148
|
|
|
131
149
|
test('memory budget configuration rejects invalid or reversed thresholds', () => {
|
|
@@ -105,6 +105,43 @@ test('schemaCacheMaxEntries: zero is refused', () => {
|
|
|
105
105
|
expect(() => serverConfigSchema({ contentApi: { schemaCacheMaxEntries: 0 } })).toThrow()
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
+
test('graphql engine: unset when no variable is set', async () => {
|
|
109
|
+
const { serverConfig } = await readConfig()
|
|
110
|
+
expect(serverConfig.contentApi?.graphqlEngine).toBeUndefined()
|
|
111
|
+
expect(serverConfig.contentApi?.graphqlJsonProjects).toBeUndefined()
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('graphql engine: read from CONTEMBER_CONTENT_API_GRAPHQL_ENGINE and _GRAPHQL_JSON_PROJECTS', async () => {
|
|
115
|
+
process.env.CONTEMBER_CONTENT_API_GRAPHQL_ENGINE = 'graphql-json'
|
|
116
|
+
process.env.CONTEMBER_CONTENT_API_GRAPHQL_JSON_PROJECTS = 'blog, shop,'
|
|
117
|
+
try {
|
|
118
|
+
const { serverConfig } = await readConfig()
|
|
119
|
+
expect(serverConfig.contentApi?.graphqlEngine).toBe('graphql-json')
|
|
120
|
+
expect(serverConfig.contentApi?.graphqlJsonProjects).toEqual(['blog', 'shop'])
|
|
121
|
+
} finally {
|
|
122
|
+
delete process.env.CONTEMBER_CONTENT_API_GRAPHQL_ENGINE
|
|
123
|
+
delete process.env.CONTEMBER_CONTENT_API_GRAPHQL_JSON_PROJECTS
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('graphql engine: an unknown engine is refused', () => {
|
|
128
|
+
expect(() => serverConfigSchema({ contentApi: { graphqlEngine: 'graphql-other' } })).toThrow()
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('graphql engine: an empty value means unset', () => {
|
|
132
|
+
expect(serverConfigSchema({ contentApi: { graphqlEngine: '', graphqlJsonProjects: '' } }).contentApi).toEqual({})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('graphql engine: the project list accepts a list of slugs', () => {
|
|
136
|
+
expect(serverConfigSchema({ contentApi: { graphqlJsonProjects: ['blog', ' shop '] } }).contentApi?.graphqlJsonProjects).toEqual(['blog', 'shop'])
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('graphql engine: a project list that is not a list of slugs is refused', () => {
|
|
140
|
+
expect(() => serverConfigSchema({ contentApi: { graphqlJsonProjects: { blog: true } } })).toThrow()
|
|
141
|
+
expect(() => serverConfigSchema({ contentApi: { graphqlJsonProjects: [1] } })).toThrow()
|
|
142
|
+
expect(() => serverConfigSchema({ contentApi: { graphqlJsonProjects: true } })).toThrow()
|
|
143
|
+
})
|
|
144
|
+
|
|
108
145
|
test('migration lock options: read from CONTEMBER_SYSTEM_API_MIGRATION_* with defaults for the retry', async () => {
|
|
109
146
|
process.env.CONTEMBER_SYSTEM_API_MIGRATION_LOCK_TIMEOUT_MS = '2000'
|
|
110
147
|
try {
|
|
@@ -182,3 +219,99 @@ test('requestBodySize: a malformed size fails instead of disabling the limit', (
|
|
|
182
219
|
expect(() => requestBodySize(value)).toThrow()
|
|
183
220
|
}
|
|
184
221
|
})
|
|
222
|
+
|
|
223
|
+
const requestLog = (val: unknown): unknown => serverConfigSchema({ contentApi: { requestLog: val } }).contentApi?.requestLog
|
|
224
|
+
|
|
225
|
+
const inOneDay = () => new Date(Date.now() + 24 * 3600 * 1000).toISOString()
|
|
226
|
+
|
|
227
|
+
test('content request log: off when nothing is configured', async () => {
|
|
228
|
+
const { serverConfig } = await readConfig()
|
|
229
|
+
expect(serverConfig.contentApi?.requestLog).toBeUndefined()
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('content request log: off without listed projects, even with a destination', () => {
|
|
233
|
+
expect(requestLog({ file: '/var/log/requests.jsonl', maxRequests: 100, until: inOneDay() })).toBeUndefined()
|
|
234
|
+
expect(requestLog({ projects: '', file: '/var/log/requests.jsonl', maxRequests: 100, until: inOneDay() })).toBeUndefined()
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('content request log: empty variables of an unused log do not stop startup', async () => {
|
|
238
|
+
const keys = ['PROJECTS', 'FILE', 'MAX_REQUESTS', 'UNTIL'].map(it => `CONTEMBER_CONTENT_API_REQUEST_LOG_${it}`)
|
|
239
|
+
for (const key of keys) {
|
|
240
|
+
process.env[key] = ''
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
const { serverConfig } = await readConfig()
|
|
244
|
+
expect(serverConfig.contentApi?.requestLog).toBeUndefined()
|
|
245
|
+
} finally {
|
|
246
|
+
for (const key of keys) {
|
|
247
|
+
delete process.env[key]
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test('content request log: read from CONTEMBER_CONTENT_API_REQUEST_LOG_*', async () => {
|
|
253
|
+
const until = inOneDay()
|
|
254
|
+
process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_PROJECTS = 'blog, shop'
|
|
255
|
+
process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_FILE = '/var/log/requests.jsonl'
|
|
256
|
+
process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_MAX_REQUESTS = '500'
|
|
257
|
+
process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_UNTIL = until
|
|
258
|
+
try {
|
|
259
|
+
const { serverConfig } = await readConfig()
|
|
260
|
+
expect(serverConfig.contentApi?.requestLog).toEqual({
|
|
261
|
+
projects: ['blog', 'shop'],
|
|
262
|
+
file: '/var/log/requests.jsonl',
|
|
263
|
+
maxRequests: 500,
|
|
264
|
+
until,
|
|
265
|
+
})
|
|
266
|
+
} finally {
|
|
267
|
+
delete process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_PROJECTS
|
|
268
|
+
delete process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_FILE
|
|
269
|
+
delete process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_MAX_REQUESTS
|
|
270
|
+
delete process.env.CONTEMBER_CONTENT_API_REQUEST_LOG_UNTIL
|
|
271
|
+
}
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('content request log: listed projects require a destination, a cap and an end', () => {
|
|
275
|
+
const complete = { projects: ['blog'], file: '/var/log/requests.jsonl', maxRequests: 100, until: inOneDay() }
|
|
276
|
+
expect(() => requestLog({ ...complete, file: undefined })).toThrow(/file/)
|
|
277
|
+
expect(() => requestLog({ ...complete, file: '' })).toThrow(/file/)
|
|
278
|
+
expect(() => requestLog({ ...complete, maxRequests: undefined })).toThrow(/maxRequests/)
|
|
279
|
+
expect(() => requestLog({ ...complete, until: undefined })).toThrow(/until/)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('content request log: the cap is positive and bounded', () => {
|
|
283
|
+
const complete = { projects: ['blog'], file: '/var/log/requests.jsonl', until: inOneDay() }
|
|
284
|
+
expect(() => requestLog({ ...complete, maxRequests: 0 })).toThrow()
|
|
285
|
+
expect(() => requestLog({ ...complete, maxRequests: 100_001 })).toThrow(/at most 100000/)
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
test('content request log: `until` is an ISO timestamp with a time zone, at most 7 days ahead', () => {
|
|
289
|
+
const complete = { projects: ['blog'], file: '/var/log/requests.jsonl', maxRequests: 100 }
|
|
290
|
+
expect(() => requestLog({ ...complete, until: 'tomorrow' })).toThrow(/ISO timestamp/)
|
|
291
|
+
expect(() => requestLog({ ...complete, until: '2026-01-02T18:00:00' })).toThrow(/time zone/)
|
|
292
|
+
expect(() => requestLog({ ...complete, until: 'Nov 19 2026 Z' })).toThrow(/ISO timestamp/)
|
|
293
|
+
expect(() => requestLog({ ...complete, until: '2026-11-19 10:00:00Z' })).toThrow(/ISO timestamp/)
|
|
294
|
+
expect(() => requestLog({ ...complete, until: new Date(Date.now() + 8 * 24 * 3600 * 1000).toISOString() })).toThrow(/7 days/)
|
|
295
|
+
// a past end is accepted: the forgotten configuration just records nothing
|
|
296
|
+
expect(requestLog({ ...complete, until: '2020-01-01T00:00:00+02:00' })).toEqual({
|
|
297
|
+
...complete,
|
|
298
|
+
projects: ['blog'],
|
|
299
|
+
until: '2019-12-31T22:00:00.000Z',
|
|
300
|
+
})
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
test('content request log: `until` from a YAML config must be quoted', async () => {
|
|
304
|
+
const yaml = (until: string) =>
|
|
305
|
+
`server:
|
|
306
|
+
contentApi:
|
|
307
|
+
requestLog:
|
|
308
|
+
projects: [blog]
|
|
309
|
+
file: /var/log/requests.jsonl
|
|
310
|
+
maxRequests: 100
|
|
311
|
+
until: ${until}
|
|
312
|
+
`
|
|
313
|
+
const until = inOneDay()
|
|
314
|
+
const { serverConfig } = await readConfig([{ type: 'yaml', data: yaml(`'${until}'`) }])
|
|
315
|
+
expect(serverConfig.contentApi?.requestLog?.until).toBe(until)
|
|
316
|
+
await expect(readConfig([{ type: 'yaml', data: yaml(until) }])).rejects.toThrow(/quote it in YAML/)
|
|
317
|
+
})
|