@plone/aurora 1.0.0-alpha.19 → 1.0.0-alpha.21
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/CHANGELOG.md +59 -0
- package/acceptance/tests/anonymous-401-redirect.test.ts +75 -0
- package/acceptance/tests/multilingual-root-redirect.test.ts +45 -0
- package/app/config/server.server.test.ts +13 -0
- package/app/config/server.server.ts +17 -1
- package/app/config/types.ts +18 -0
- package/app/i18next.server.ts +2 -0
- package/app/middleware.server.test.ts +744 -8
- package/app/middleware.server.ts +211 -19
- package/app/root.tsx +2 -0
- package/app/routes.ts +1 -1
- package/package.json +26 -26
- package/react-router.config.ts +18 -0
- package/vite.config.ts +1 -4
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
import { expect, describe, it, vi, afterEach } from 'vitest';
|
|
1
|
+
import { expect, describe, it, vi, afterEach, beforeEach } from 'vitest';
|
|
2
2
|
import config from '@plone/registry';
|
|
3
3
|
import { RouterContextProvider } from 'react-router';
|
|
4
4
|
import { jwtDecode } from 'jwt-decode';
|
|
5
5
|
import { getAuthFromRequest } from '@plone/react-router';
|
|
6
|
+
import PloneClient from '@plone/client';
|
|
6
7
|
import {
|
|
8
|
+
contentResourceMiddleware,
|
|
7
9
|
fetchPloneContent,
|
|
8
10
|
getAPIResourceWithAuth,
|
|
11
|
+
getContentExpand,
|
|
12
|
+
getPloneClientClass,
|
|
13
|
+
isResourcePattern,
|
|
9
14
|
installServerMiddleware,
|
|
10
15
|
linkMiddleware,
|
|
16
|
+
matchContentResource,
|
|
17
|
+
multilingualRootRedirect,
|
|
11
18
|
ploneClearAuthCookieContext,
|
|
12
19
|
PloneClientMiddleware,
|
|
13
20
|
otherResources,
|
|
21
|
+
unauthorizedLoginRedirect,
|
|
14
22
|
ploneClientContext,
|
|
15
23
|
ploneContentContext,
|
|
16
24
|
ploneSiteContext,
|
|
17
25
|
ploneUserContext,
|
|
26
|
+
routeSkipsContent,
|
|
18
27
|
} from './middleware.server';
|
|
28
|
+
import { defaultApiExpanders } from './config/server.server';
|
|
19
29
|
|
|
20
30
|
vi.mock('jwt-decode');
|
|
21
31
|
vi.mock('@plone/react-router', async (importOriginal) => {
|
|
@@ -93,6 +103,107 @@ describe('middleware', () => {
|
|
|
93
103
|
.method(),
|
|
94
104
|
).toHaveProperty('initialize');
|
|
95
105
|
});
|
|
106
|
+
|
|
107
|
+
it('applies the configuration only once per process', async () => {
|
|
108
|
+
const request = new Request('http://example.com');
|
|
109
|
+
const runMiddleware = () =>
|
|
110
|
+
installServerMiddleware(
|
|
111
|
+
{
|
|
112
|
+
request,
|
|
113
|
+
context: new RouterContextProvider(),
|
|
114
|
+
params: {},
|
|
115
|
+
pattern: '/',
|
|
116
|
+
url: new URL(request.url),
|
|
117
|
+
},
|
|
118
|
+
vi.fn(),
|
|
119
|
+
);
|
|
120
|
+
const countSlotComponents = () =>
|
|
121
|
+
Object.values(config.slots).reduce(
|
|
122
|
+
(total, slot) =>
|
|
123
|
+
total +
|
|
124
|
+
Object.values(slot.data).reduce(
|
|
125
|
+
(sum, components) => sum + components.length,
|
|
126
|
+
0,
|
|
127
|
+
),
|
|
128
|
+
0,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
await runMiddleware();
|
|
132
|
+
const routes = config.routes?.length;
|
|
133
|
+
const slotComponents = countSlotComponents();
|
|
134
|
+
|
|
135
|
+
await runMiddleware();
|
|
136
|
+
await runMiddleware();
|
|
137
|
+
|
|
138
|
+
expect(config.routes?.length).toBe(routes);
|
|
139
|
+
expect(countSlotComponents()).toBe(slotComponents);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('getPloneClientClass', () => {
|
|
144
|
+
afterEach(() => {
|
|
145
|
+
config.utilities.clientEndpoints = {};
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('returns the ploneClient utility when there are no clientEndpoints', () => {
|
|
149
|
+
config.registerUtility({
|
|
150
|
+
name: 'ploneClient',
|
|
151
|
+
type: 'client',
|
|
152
|
+
method: () => PloneClient,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
expect(getPloneClientClass()).toBe(PloneClient);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('adds the methods of every clientEndpoints utility', () => {
|
|
159
|
+
config.registerUtility({
|
|
160
|
+
name: 'ploneClient',
|
|
161
|
+
type: 'client',
|
|
162
|
+
method: () => PloneClient,
|
|
163
|
+
});
|
|
164
|
+
config.registerUtility({
|
|
165
|
+
name: 'first-addon',
|
|
166
|
+
type: 'clientEndpoints',
|
|
167
|
+
method: () => ({
|
|
168
|
+
getApiPath(this: PloneClient) {
|
|
169
|
+
return this.config.apiPath;
|
|
170
|
+
},
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
config.registerUtility({
|
|
174
|
+
name: 'second-addon',
|
|
175
|
+
type: 'clientEndpoints',
|
|
176
|
+
method: () => ({ getAnswer: () => 42 }),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const cli = getPloneClientClass().initialize({
|
|
180
|
+
apiPath: 'http://localhost:8080/Plone',
|
|
181
|
+
}) as PloneClient & Record<string, () => unknown>;
|
|
182
|
+
|
|
183
|
+
expect(cli).toBeInstanceOf(PloneClient);
|
|
184
|
+
expect(cli.getApiPath()).toBe('http://localhost:8080/Plone');
|
|
185
|
+
expect(cli.getAnswer()).toBe(42);
|
|
186
|
+
expect(cli.getContent).toBeTypeOf('function');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('extends a custom ploneClient utility', () => {
|
|
190
|
+
class CustomClient extends PloneClient {}
|
|
191
|
+
config.registerUtility({
|
|
192
|
+
name: 'ploneClient',
|
|
193
|
+
type: 'client',
|
|
194
|
+
method: () => CustomClient,
|
|
195
|
+
});
|
|
196
|
+
config.registerUtility({
|
|
197
|
+
name: 'addon',
|
|
198
|
+
type: 'clientEndpoints',
|
|
199
|
+
method: () => ({ getAnswer: () => 42 }),
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const cli = getPloneClientClass().initialize({ apiPath: '' });
|
|
203
|
+
|
|
204
|
+
expect(cli).toBeInstanceOf(CustomClient);
|
|
205
|
+
expect(cli).toHaveProperty('getAnswer');
|
|
206
|
+
});
|
|
96
207
|
});
|
|
97
208
|
|
|
98
209
|
describe('PloneClientMiddleware', () => {
|
|
@@ -460,7 +571,9 @@ describe('middleware', () => {
|
|
|
460
571
|
});
|
|
461
572
|
|
|
462
573
|
it('intercepts requests to special urls: @portrait', async () => {
|
|
463
|
-
const request = new Request('http://example.com'
|
|
574
|
+
const request = new Request('http://example.com', {
|
|
575
|
+
headers: { Accept: 'image/avif,image/webp,image/*,*/*;q=0.8' },
|
|
576
|
+
});
|
|
464
577
|
const context = new RouterContextProvider();
|
|
465
578
|
const params = { '*': '@portrait/username' };
|
|
466
579
|
const nextMock = vi.fn();
|
|
@@ -493,6 +606,11 @@ describe('middleware', () => {
|
|
|
493
606
|
'Authorization',
|
|
494
607
|
),
|
|
495
608
|
).toBeNull();
|
|
609
|
+
expect(
|
|
610
|
+
(fetchMock.mock.calls[0]?.[1] as { headers: Headers }).headers.get(
|
|
611
|
+
'Accept',
|
|
612
|
+
),
|
|
613
|
+
).toBe('application/json');
|
|
496
614
|
});
|
|
497
615
|
|
|
498
616
|
it('retries resource requests anonymously after a 401 and clears the cookie', async () => {
|
|
@@ -561,9 +679,138 @@ describe('middleware', () => {
|
|
|
561
679
|
});
|
|
562
680
|
});
|
|
563
681
|
|
|
682
|
+
describe('getContentExpand', () => {
|
|
683
|
+
beforeEach(() => {
|
|
684
|
+
config.settings.apiExpanders = [...defaultApiExpanders];
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
afterEach(() => {
|
|
688
|
+
config.settings.apiExpanders = [];
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
it('returns the core expansions', () => {
|
|
692
|
+
expect(getContentExpand('/', false)).toEqual([
|
|
693
|
+
'navroot',
|
|
694
|
+
'breadcrumbs',
|
|
695
|
+
'navigation',
|
|
696
|
+
'actions',
|
|
697
|
+
'translations',
|
|
698
|
+
]);
|
|
699
|
+
expect(getContentExpand('/', true)).toEqual([
|
|
700
|
+
'navroot',
|
|
701
|
+
'breadcrumbs',
|
|
702
|
+
'navigation',
|
|
703
|
+
'actions',
|
|
704
|
+
'translations',
|
|
705
|
+
'types',
|
|
706
|
+
]);
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
it('adds the expansions of add-ons, once each', () => {
|
|
710
|
+
config.settings.apiExpanders = [
|
|
711
|
+
...config.settings.apiExpanders,
|
|
712
|
+
{ match: '', expand: ['translations', 'navigation'] },
|
|
713
|
+
{ match: '/', expand: ['translations', 'workflow'] },
|
|
714
|
+
];
|
|
715
|
+
|
|
716
|
+
expect(getContentExpand('/news', false)).toEqual([
|
|
717
|
+
'navroot',
|
|
718
|
+
'breadcrumbs',
|
|
719
|
+
'navigation',
|
|
720
|
+
'actions',
|
|
721
|
+
'translations',
|
|
722
|
+
'workflow',
|
|
723
|
+
]);
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
it('matches expanders by path prefix', () => {
|
|
727
|
+
config.settings.apiExpanders = [
|
|
728
|
+
{ match: '/news', expand: ['news-extra'] },
|
|
729
|
+
];
|
|
730
|
+
|
|
731
|
+
expect(getContentExpand('/news', false)).toContain('news-extra');
|
|
732
|
+
expect(getContentExpand('/news/item', false)).toContain('news-extra');
|
|
733
|
+
expect(getContentExpand('/newsletter', false)).not.toContain(
|
|
734
|
+
'news-extra',
|
|
735
|
+
);
|
|
736
|
+
expect(getContentExpand('/', false)).not.toContain('news-extra');
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it('ignores Volto-shaped expanders', () => {
|
|
740
|
+
config.settings.apiExpanders = [
|
|
741
|
+
{ match: '', GET_CONTENT: ['translations'] },
|
|
742
|
+
];
|
|
743
|
+
|
|
744
|
+
expect(getContentExpand('/', false)).not.toContain('translations');
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
it('lets add-ons change the core expansions', () => {
|
|
748
|
+
config.settings.apiExpanders = [
|
|
749
|
+
{ match: '', expand: ['navroot', 'breadcrumbs', 'actions'] },
|
|
750
|
+
];
|
|
751
|
+
|
|
752
|
+
expect(getContentExpand('/', false)).toEqual([
|
|
753
|
+
'navroot',
|
|
754
|
+
'breadcrumbs',
|
|
755
|
+
'actions',
|
|
756
|
+
]);
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
it('applies authenticated expanders to signed-in users only', () => {
|
|
760
|
+
config.settings.apiExpanders = [
|
|
761
|
+
{ match: '', expand: ['my-profile'], authenticated: true },
|
|
762
|
+
];
|
|
763
|
+
|
|
764
|
+
expect(getContentExpand('/', true)).toContain('my-profile');
|
|
765
|
+
expect(getContentExpand('/', false)).not.toContain('my-profile');
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
|
|
564
769
|
describe('fetchPloneContent', () => {
|
|
770
|
+
beforeEach(() => {
|
|
771
|
+
config.settings.apiExpanders = [...defaultApiExpanders];
|
|
772
|
+
});
|
|
773
|
+
|
|
565
774
|
afterEach(() => {
|
|
566
775
|
delete config.utilities['ploneClient'];
|
|
776
|
+
config.settings.apiExpanders = [];
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
it('skips fetching for resource routes', async () => {
|
|
780
|
+
const getContentMock = vi.fn();
|
|
781
|
+
const getSiteMock = vi.fn();
|
|
782
|
+
const getUserMock = vi.fn();
|
|
783
|
+
config.settings.apiPath = 'http://example.com';
|
|
784
|
+
registerPloneClientFactory({
|
|
785
|
+
getContent: getContentMock,
|
|
786
|
+
getSite: getSiteMock,
|
|
787
|
+
getUser: getUserMock,
|
|
788
|
+
});
|
|
789
|
+
vi.mocked(getAuthFromRequest).mockResolvedValue('valid.jwt.token');
|
|
790
|
+
vi.mocked(jwtDecode).mockReturnValue({ sub: 'admin' });
|
|
791
|
+
const request = new Request(
|
|
792
|
+
'http://example.com/@objectBrowserWidget/missing',
|
|
793
|
+
);
|
|
794
|
+
const context = new RouterContextProvider();
|
|
795
|
+
|
|
796
|
+
await initializePloneClientContext(request, context);
|
|
797
|
+
|
|
798
|
+
const result = await fetchPloneContent(
|
|
799
|
+
{
|
|
800
|
+
request,
|
|
801
|
+
params: { '*': 'missing' },
|
|
802
|
+
context,
|
|
803
|
+
pattern: '@objectBrowserWidget/*',
|
|
804
|
+
url: new URL(request.url),
|
|
805
|
+
},
|
|
806
|
+
vi.fn(),
|
|
807
|
+
);
|
|
808
|
+
|
|
809
|
+
expect(result).toBeUndefined();
|
|
810
|
+
expect(getContentMock).not.toHaveBeenCalled();
|
|
811
|
+
expect(getSiteMock).not.toHaveBeenCalled();
|
|
812
|
+
expect(getUserMock).not.toHaveBeenCalled();
|
|
813
|
+
expect(() => context.get(ploneContentContext)).toThrow();
|
|
567
814
|
});
|
|
568
815
|
|
|
569
816
|
it('fetches content and site and sets them in context', async () => {
|
|
@@ -597,7 +844,13 @@ describe('middleware', () => {
|
|
|
597
844
|
|
|
598
845
|
expect(getContentMock).toHaveBeenCalledWith({
|
|
599
846
|
path: '/',
|
|
600
|
-
expand: [
|
|
847
|
+
expand: [
|
|
848
|
+
'navroot',
|
|
849
|
+
'breadcrumbs',
|
|
850
|
+
'navigation',
|
|
851
|
+
'actions',
|
|
852
|
+
'translations',
|
|
853
|
+
],
|
|
601
854
|
});
|
|
602
855
|
expect(getSiteMock).toHaveBeenCalled();
|
|
603
856
|
expect(context.get(ploneContentContext)).toEqual({
|
|
@@ -635,7 +888,13 @@ describe('middleware', () => {
|
|
|
635
888
|
|
|
636
889
|
expect(getContentMock).toHaveBeenCalledWith({
|
|
637
890
|
path: '/test-content',
|
|
638
|
-
expand: [
|
|
891
|
+
expand: [
|
|
892
|
+
'navroot',
|
|
893
|
+
'breadcrumbs',
|
|
894
|
+
'navigation',
|
|
895
|
+
'actions',
|
|
896
|
+
'translations',
|
|
897
|
+
],
|
|
639
898
|
});
|
|
640
899
|
});
|
|
641
900
|
|
|
@@ -772,7 +1031,14 @@ describe('middleware', () => {
|
|
|
772
1031
|
expect(context.get(ploneUserContext)).toEqual(mockUser.data);
|
|
773
1032
|
expect(getContentMock).toHaveBeenCalledWith({
|
|
774
1033
|
path: '/',
|
|
775
|
-
expand: [
|
|
1034
|
+
expand: [
|
|
1035
|
+
'navroot',
|
|
1036
|
+
'breadcrumbs',
|
|
1037
|
+
'navigation',
|
|
1038
|
+
'actions',
|
|
1039
|
+
'translations',
|
|
1040
|
+
'types',
|
|
1041
|
+
],
|
|
776
1042
|
});
|
|
777
1043
|
});
|
|
778
1044
|
|
|
@@ -811,7 +1077,14 @@ describe('middleware', () => {
|
|
|
811
1077
|
|
|
812
1078
|
expect(getContentMock).toHaveBeenCalledWith({
|
|
813
1079
|
path: '/',
|
|
814
|
-
expand: [
|
|
1080
|
+
expand: [
|
|
1081
|
+
'navroot',
|
|
1082
|
+
'breadcrumbs',
|
|
1083
|
+
'navigation',
|
|
1084
|
+
'actions',
|
|
1085
|
+
'translations',
|
|
1086
|
+
'types',
|
|
1087
|
+
],
|
|
815
1088
|
});
|
|
816
1089
|
expect(getSiteMock).toHaveBeenCalled();
|
|
817
1090
|
expect(getUserMock).toHaveBeenCalledWith({ id: 'testuser' });
|
|
@@ -851,7 +1124,13 @@ describe('middleware', () => {
|
|
|
851
1124
|
expect(context.get(ploneUserContext)).toBeNull();
|
|
852
1125
|
expect(getContentMock).toHaveBeenCalledWith({
|
|
853
1126
|
path: '/',
|
|
854
|
-
expand: [
|
|
1127
|
+
expand: [
|
|
1128
|
+
'navroot',
|
|
1129
|
+
'breadcrumbs',
|
|
1130
|
+
'navigation',
|
|
1131
|
+
'actions',
|
|
1132
|
+
'translations',
|
|
1133
|
+
],
|
|
855
1134
|
});
|
|
856
1135
|
});
|
|
857
1136
|
|
|
@@ -891,6 +1170,10 @@ describe('middleware', () => {
|
|
|
891
1170
|
});
|
|
892
1171
|
|
|
893
1172
|
it('retries anonymously after a 401 and clears the auth cookie context', async () => {
|
|
1173
|
+
config.settings.apiExpanders = [
|
|
1174
|
+
...config.settings.apiExpanders,
|
|
1175
|
+
{ match: '', expand: ['my-profile'], authenticated: true },
|
|
1176
|
+
];
|
|
894
1177
|
const authContent = vi
|
|
895
1178
|
.fn()
|
|
896
1179
|
.mockRejectedValueOnce({ data: undefined, status: 401 });
|
|
@@ -946,9 +1229,27 @@ describe('middleware', () => {
|
|
|
946
1229
|
nextMock,
|
|
947
1230
|
);
|
|
948
1231
|
|
|
1232
|
+
expect(authContent).toHaveBeenCalledWith({
|
|
1233
|
+
path: '/',
|
|
1234
|
+
expand: [
|
|
1235
|
+
'navroot',
|
|
1236
|
+
'breadcrumbs',
|
|
1237
|
+
'navigation',
|
|
1238
|
+
'actions',
|
|
1239
|
+
'translations',
|
|
1240
|
+
'types',
|
|
1241
|
+
'my-profile',
|
|
1242
|
+
],
|
|
1243
|
+
});
|
|
949
1244
|
expect(anonymousContent).toHaveBeenCalledWith({
|
|
950
1245
|
path: '/',
|
|
951
|
-
expand: [
|
|
1246
|
+
expand: [
|
|
1247
|
+
'navroot',
|
|
1248
|
+
'breadcrumbs',
|
|
1249
|
+
'navigation',
|
|
1250
|
+
'actions',
|
|
1251
|
+
'translations',
|
|
1252
|
+
],
|
|
952
1253
|
});
|
|
953
1254
|
expect(context.get(ploneContentContext)).toEqual({
|
|
954
1255
|
'@id': '/',
|
|
@@ -1017,6 +1318,330 @@ describe('middleware', () => {
|
|
|
1017
1318
|
),
|
|
1018
1319
|
).rejects.toMatchObject({ init: { status: 301 } });
|
|
1019
1320
|
});
|
|
1321
|
+
|
|
1322
|
+
// The multilingual root redirect: on a site whose /@site reads
|
|
1323
|
+
// features.multilingual, the site root is managed, never visited — a
|
|
1324
|
+
// request for / goes to the default language root, as Plone's language
|
|
1325
|
+
// switcher view and Volto's MultilingualRedirector send it.
|
|
1326
|
+
const multilingualSiteSetup = (site: Record<string, unknown>) => {
|
|
1327
|
+
const getContentMock = vi.fn().mockResolvedValue({
|
|
1328
|
+
data: { '@id': 'http://example.com/', title: 'Site' },
|
|
1329
|
+
});
|
|
1330
|
+
const getSiteMock = vi.fn().mockResolvedValue({ data: site });
|
|
1331
|
+
config.settings.apiPath = 'http://example.com';
|
|
1332
|
+
registerPloneClientFactory({
|
|
1333
|
+
getContent: getContentMock,
|
|
1334
|
+
getSite: getSiteMock,
|
|
1335
|
+
});
|
|
1336
|
+
};
|
|
1337
|
+
|
|
1338
|
+
// `url` is what React Router hands the middleware: the normalised path of
|
|
1339
|
+
// the request (a `.data` suffix already stripped). It defaults to the
|
|
1340
|
+
// request's own URL, as it is for a document request.
|
|
1341
|
+
const runFetchAt = async (
|
|
1342
|
+
requestPath: string,
|
|
1343
|
+
starParam: string,
|
|
1344
|
+
normalisedUrl?: URL,
|
|
1345
|
+
) => {
|
|
1346
|
+
const request = new Request(`http://example.com${requestPath}`);
|
|
1347
|
+
const context = new RouterContextProvider();
|
|
1348
|
+
await initializePloneClientContext(request, context);
|
|
1349
|
+
return fetchPloneContent(
|
|
1350
|
+
{
|
|
1351
|
+
request,
|
|
1352
|
+
params: { '*': starParam },
|
|
1353
|
+
context,
|
|
1354
|
+
pattern: requestPath,
|
|
1355
|
+
url: normalisedUrl ?? new URL(request.url),
|
|
1356
|
+
},
|
|
1357
|
+
vi.fn(),
|
|
1358
|
+
);
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
it('redirects the site root to the default language root on a multilingual site', async () => {
|
|
1362
|
+
multilingualSiteSetup({
|
|
1363
|
+
features: { multilingual: true },
|
|
1364
|
+
'plone.default_language': 'en',
|
|
1365
|
+
});
|
|
1366
|
+
const result = await runFetchAt('/', '');
|
|
1367
|
+
expect(result).toBeInstanceOf(Response);
|
|
1368
|
+
expect((result as Response).status).toBe(302);
|
|
1369
|
+
expect((result as Response).headers.get('Location')).toBe('/en');
|
|
1370
|
+
});
|
|
1371
|
+
|
|
1372
|
+
it('redirects the site root to a hyphenated default language root', async () => {
|
|
1373
|
+
multilingualSiteSetup({
|
|
1374
|
+
features: { multilingual: true },
|
|
1375
|
+
'plone.default_language': 'pt-br',
|
|
1376
|
+
});
|
|
1377
|
+
const result = await runFetchAt('/', '');
|
|
1378
|
+
expect((result as Response).headers.get('Location')).toBe('/pt-br');
|
|
1379
|
+
});
|
|
1380
|
+
|
|
1381
|
+
it('redirects a root data request too — keyed on the normalised url, not the raw request path', async () => {
|
|
1382
|
+
multilingualSiteSetup({
|
|
1383
|
+
features: { multilingual: true },
|
|
1384
|
+
'plone.default_language': 'en',
|
|
1385
|
+
});
|
|
1386
|
+
const result = await runFetchAt(
|
|
1387
|
+
'/_.data',
|
|
1388
|
+
'',
|
|
1389
|
+
new URL('http://example.com/'),
|
|
1390
|
+
);
|
|
1391
|
+
expect(result).toBeInstanceOf(Response);
|
|
1392
|
+
expect((result as Response).headers.get('Location')).toBe('/en');
|
|
1393
|
+
});
|
|
1394
|
+
|
|
1395
|
+
it('leaves the site root alone on a monolingual site', async () => {
|
|
1396
|
+
multilingualSiteSetup({
|
|
1397
|
+
features: { multilingual: false },
|
|
1398
|
+
'plone.default_language': 'en',
|
|
1399
|
+
});
|
|
1400
|
+
const request = new Request('http://example.com/');
|
|
1401
|
+
const context = new RouterContextProvider();
|
|
1402
|
+
await initializePloneClientContext(request, context);
|
|
1403
|
+
const result = await fetchPloneContent(
|
|
1404
|
+
{
|
|
1405
|
+
request,
|
|
1406
|
+
params: { '*': '' },
|
|
1407
|
+
context,
|
|
1408
|
+
pattern: '/',
|
|
1409
|
+
url: new URL(request.url),
|
|
1410
|
+
},
|
|
1411
|
+
vi.fn(),
|
|
1412
|
+
);
|
|
1413
|
+
expect(result).toBeUndefined();
|
|
1414
|
+
expect(context.get(ploneContentContext)).toEqual({
|
|
1415
|
+
'@id': '/',
|
|
1416
|
+
title: 'Site',
|
|
1417
|
+
});
|
|
1418
|
+
});
|
|
1419
|
+
|
|
1420
|
+
it('leaves a language root alone', async () => {
|
|
1421
|
+
multilingualSiteSetup({
|
|
1422
|
+
features: { multilingual: true },
|
|
1423
|
+
'plone.default_language': 'en',
|
|
1424
|
+
});
|
|
1425
|
+
expect(await runFetchAt('/en', 'en')).toBeUndefined();
|
|
1426
|
+
});
|
|
1427
|
+
|
|
1428
|
+
it('leaves the login page alone although its splat is empty', async () => {
|
|
1429
|
+
multilingualSiteSetup({
|
|
1430
|
+
features: { multilingual: true },
|
|
1431
|
+
'plone.default_language': 'en',
|
|
1432
|
+
});
|
|
1433
|
+
expect(await runFetchAt('/login', '')).toBeUndefined();
|
|
1434
|
+
});
|
|
1435
|
+
|
|
1436
|
+
const multilingualSite = {
|
|
1437
|
+
features: { multilingual: true },
|
|
1438
|
+
'plone.default_language': 'en',
|
|
1439
|
+
} as any;
|
|
1440
|
+
|
|
1441
|
+
it('multilingualRootRedirect targets the default language root from the site root only', () => {
|
|
1442
|
+
expect(multilingualRootRedirect('/', multilingualSite)).toBe('/en');
|
|
1443
|
+
expect(multilingualRootRedirect('/en', multilingualSite)).toBeUndefined();
|
|
1444
|
+
expect(
|
|
1445
|
+
multilingualRootRedirect('/login', multilingualSite),
|
|
1446
|
+
).toBeUndefined();
|
|
1447
|
+
expect(
|
|
1448
|
+
multilingualRootRedirect('/@@contents', multilingualSite),
|
|
1449
|
+
).toBeUndefined();
|
|
1450
|
+
});
|
|
1451
|
+
|
|
1452
|
+
it('multilingualRootRedirect does nothing on a monolingual site or without a default language', () => {
|
|
1453
|
+
expect(
|
|
1454
|
+
multilingualRootRedirect('/', {
|
|
1455
|
+
features: { multilingual: false },
|
|
1456
|
+
'plone.default_language': 'en',
|
|
1457
|
+
} as any),
|
|
1458
|
+
).toBeUndefined();
|
|
1459
|
+
expect(
|
|
1460
|
+
multilingualRootRedirect('/', {
|
|
1461
|
+
features: { multilingual: true },
|
|
1462
|
+
} as any),
|
|
1463
|
+
).toBeUndefined();
|
|
1464
|
+
expect(multilingualRootRedirect('/', {} as any)).toBeUndefined();
|
|
1465
|
+
});
|
|
1466
|
+
|
|
1467
|
+
it('multilingualRootRedirect refuses a default language that is not a language token', () => {
|
|
1468
|
+
expect(
|
|
1469
|
+
multilingualRootRedirect('/', {
|
|
1470
|
+
features: { multilingual: true },
|
|
1471
|
+
'plone.default_language': '/evil.example',
|
|
1472
|
+
} as any),
|
|
1473
|
+
).toBeUndefined();
|
|
1474
|
+
expect(
|
|
1475
|
+
multilingualRootRedirect('/', {
|
|
1476
|
+
features: { multilingual: true },
|
|
1477
|
+
'plone.default_language': 'zh-cn',
|
|
1478
|
+
} as any),
|
|
1479
|
+
).toBe('/zh-cn');
|
|
1480
|
+
});
|
|
1481
|
+
|
|
1482
|
+
it('redirects the site root after the anonymous retry of an expired cookie', async () => {
|
|
1483
|
+
const authContent = vi
|
|
1484
|
+
.fn()
|
|
1485
|
+
.mockRejectedValueOnce({ data: undefined, status: 401 });
|
|
1486
|
+
const authSite = vi.fn().mockResolvedValue({ data: {} });
|
|
1487
|
+
const anonymousContent = vi.fn().mockResolvedValueOnce({
|
|
1488
|
+
data: { '@id': 'http://example.com/', title: 'Site' },
|
|
1489
|
+
});
|
|
1490
|
+
const anonymousSite = vi.fn().mockResolvedValueOnce({
|
|
1491
|
+
data: {
|
|
1492
|
+
features: { multilingual: true },
|
|
1493
|
+
'plone.default_language': 'en',
|
|
1494
|
+
},
|
|
1495
|
+
});
|
|
1496
|
+
config.settings.apiPath = 'http://example.com';
|
|
1497
|
+
const initializeMock = vi
|
|
1498
|
+
.fn()
|
|
1499
|
+
.mockReturnValueOnce({
|
|
1500
|
+
getContent: authContent,
|
|
1501
|
+
getSite: authSite,
|
|
1502
|
+
getUser: vi.fn().mockResolvedValue(null),
|
|
1503
|
+
})
|
|
1504
|
+
.mockReturnValueOnce({
|
|
1505
|
+
getContent: anonymousContent,
|
|
1506
|
+
getSite: anonymousSite,
|
|
1507
|
+
getUser: vi.fn().mockResolvedValue(null),
|
|
1508
|
+
});
|
|
1509
|
+
config.registerUtility({
|
|
1510
|
+
name: 'ploneClient',
|
|
1511
|
+
type: 'client',
|
|
1512
|
+
method: () =>
|
|
1513
|
+
({
|
|
1514
|
+
prototype: {},
|
|
1515
|
+
initialize: initializeMock,
|
|
1516
|
+
}) as any,
|
|
1517
|
+
});
|
|
1518
|
+
vi.mocked(getAuthFromRequest).mockResolvedValue('expired.jwt.token');
|
|
1519
|
+
vi.mocked(jwtDecode).mockReturnValue({
|
|
1520
|
+
sub: 'testuser',
|
|
1521
|
+
exp: 9999999999,
|
|
1522
|
+
fullname: 'Test User',
|
|
1523
|
+
});
|
|
1524
|
+
const request = new Request('http://example.com/');
|
|
1525
|
+
const context = new RouterContextProvider();
|
|
1526
|
+
await initializePloneClientContext(request, context);
|
|
1527
|
+
|
|
1528
|
+
const result = await fetchPloneContent(
|
|
1529
|
+
{
|
|
1530
|
+
request,
|
|
1531
|
+
params: { '*': '' },
|
|
1532
|
+
context,
|
|
1533
|
+
pattern: '/',
|
|
1534
|
+
url: new URL(request.url),
|
|
1535
|
+
},
|
|
1536
|
+
vi.fn(),
|
|
1537
|
+
);
|
|
1538
|
+
|
|
1539
|
+
expect(result).toBeInstanceOf(Response);
|
|
1540
|
+
expect((result as Response).headers.get('Location')).toBe('/en');
|
|
1541
|
+
expect(context.get(ploneClearAuthCookieContext)).toBe(true);
|
|
1542
|
+
});
|
|
1543
|
+
|
|
1544
|
+
const anonymous401Setup = () => {
|
|
1545
|
+
const getContentMock = vi
|
|
1546
|
+
.fn()
|
|
1547
|
+
.mockRejectedValue({ data: undefined, status: 401 });
|
|
1548
|
+
const getSiteMock = vi.fn().mockResolvedValue({ data: {} });
|
|
1549
|
+
config.settings.apiPath = 'http://example.com';
|
|
1550
|
+
registerPloneClientFactory({
|
|
1551
|
+
getContent: getContentMock,
|
|
1552
|
+
getSite: getSiteMock,
|
|
1553
|
+
});
|
|
1554
|
+
};
|
|
1555
|
+
|
|
1556
|
+
const runFetchFor = async (starParam: string) => {
|
|
1557
|
+
const request = new Request(`http://example.com/${starParam}`);
|
|
1558
|
+
const context = new RouterContextProvider();
|
|
1559
|
+
await initializePloneClientContext(request, context);
|
|
1560
|
+
return fetchPloneContent(
|
|
1561
|
+
{
|
|
1562
|
+
request,
|
|
1563
|
+
params: { '*': starParam },
|
|
1564
|
+
context,
|
|
1565
|
+
pattern: `/${starParam}`,
|
|
1566
|
+
url: new URL(request.url),
|
|
1567
|
+
},
|
|
1568
|
+
vi.fn(),
|
|
1569
|
+
);
|
|
1570
|
+
};
|
|
1571
|
+
|
|
1572
|
+
it('sends an anonymous 401 to the login page with the path as return_url', async () => {
|
|
1573
|
+
anonymous401Setup();
|
|
1574
|
+
const result = await runFetchFor('en/press');
|
|
1575
|
+
expect(result).toBeInstanceOf(Response);
|
|
1576
|
+
expect((result as Response).status).toBe(302);
|
|
1577
|
+
expect((result as Response).headers.get('Location')).toBe(
|
|
1578
|
+
'/login?return_url=%2Fen%2Fpress',
|
|
1579
|
+
);
|
|
1580
|
+
});
|
|
1581
|
+
|
|
1582
|
+
it('sends an anonymous 401 on the site root to the login page', async () => {
|
|
1583
|
+
anonymous401Setup();
|
|
1584
|
+
const result = await runFetchFor('');
|
|
1585
|
+
expect((result as Response).headers.get('Location')).toBe(
|
|
1586
|
+
'/login?return_url=%2F',
|
|
1587
|
+
);
|
|
1588
|
+
});
|
|
1589
|
+
|
|
1590
|
+
it('does not redirect /login to itself when the site root is private', async () => {
|
|
1591
|
+
anonymous401Setup();
|
|
1592
|
+
const request = new Request('http://example.com/login');
|
|
1593
|
+
const context = new RouterContextProvider();
|
|
1594
|
+
await initializePloneClientContext(request, context);
|
|
1595
|
+
await expect(() =>
|
|
1596
|
+
fetchPloneContent(
|
|
1597
|
+
{
|
|
1598
|
+
request,
|
|
1599
|
+
params: {},
|
|
1600
|
+
context,
|
|
1601
|
+
pattern: '/login',
|
|
1602
|
+
url: new URL(request.url),
|
|
1603
|
+
},
|
|
1604
|
+
vi.fn(),
|
|
1605
|
+
),
|
|
1606
|
+
).rejects.toMatchObject({ init: { status: 401 } });
|
|
1607
|
+
});
|
|
1608
|
+
|
|
1609
|
+
const tokenBearing401Setup = (exp: number) => {
|
|
1610
|
+
vi.mocked(getAuthFromRequest).mockResolvedValue('some.jwt.token');
|
|
1611
|
+
vi.mocked(jwtDecode).mockReturnValue({ sub: 'salomon', exp } as any);
|
|
1612
|
+
config.settings.apiPath = 'http://example.com';
|
|
1613
|
+
registerPloneClientFactory({
|
|
1614
|
+
getContent: vi.fn().mockRejectedValue({ data: undefined, status: 401 }),
|
|
1615
|
+
getSite: vi.fn().mockResolvedValue({ data: {} }),
|
|
1616
|
+
getUser: vi.fn().mockResolvedValue(null),
|
|
1617
|
+
});
|
|
1618
|
+
};
|
|
1619
|
+
|
|
1620
|
+
it('keeps the 401 page for a live token without permission', async () => {
|
|
1621
|
+
tokenBearing401Setup(Date.now() / 1000 + 3600);
|
|
1622
|
+
await expect(() => runFetchFor('en/press')).rejects.toMatchObject({
|
|
1623
|
+
init: { status: 401 },
|
|
1624
|
+
});
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
it('sends an expired token to the login page and clears the cookie', async () => {
|
|
1628
|
+
tokenBearing401Setup(Date.now() / 1000 - 60);
|
|
1629
|
+
const result = await runFetchFor('en/press');
|
|
1630
|
+
expect(result).toBeInstanceOf(Response);
|
|
1631
|
+
expect((result as Response).headers.get('Location')).toBe(
|
|
1632
|
+
'/login?return_url=%2Fen%2Fpress',
|
|
1633
|
+
);
|
|
1634
|
+
expect((result as Response).headers.get('Set-Cookie')).toMatch(
|
|
1635
|
+
/auth_seven=;/,
|
|
1636
|
+
);
|
|
1637
|
+
});
|
|
1638
|
+
|
|
1639
|
+
it('builds the login redirect like Volto', () => {
|
|
1640
|
+
expect(unauthorizedLoginRedirect('/en/internal')).toBe(
|
|
1641
|
+
'/login?return_url=%2Fen%2Finternal',
|
|
1642
|
+
);
|
|
1643
|
+
expect(unauthorizedLoginRedirect('/', '/login')).toBeUndefined();
|
|
1644
|
+
});
|
|
1020
1645
|
});
|
|
1021
1646
|
|
|
1022
1647
|
describe('linkMiddleware', () => {
|
|
@@ -1084,5 +1709,116 @@ describe('middleware', () => {
|
|
|
1084
1709
|
|
|
1085
1710
|
expect(result).toBeUndefined();
|
|
1086
1711
|
});
|
|
1712
|
+
|
|
1713
|
+
it('does nothing for resource routes, without content', async () => {
|
|
1714
|
+
const context = new RouterContextProvider();
|
|
1715
|
+
|
|
1716
|
+
const result = await linkMiddleware(
|
|
1717
|
+
{ ...makeArgs(context), pattern: '@reset-fetcher' },
|
|
1718
|
+
vi.fn(),
|
|
1719
|
+
);
|
|
1720
|
+
|
|
1721
|
+
expect(result).toBeUndefined();
|
|
1722
|
+
});
|
|
1723
|
+
});
|
|
1724
|
+
|
|
1725
|
+
describe('isResourcePattern', () => {
|
|
1726
|
+
it('matches a segment that starts with a single @', () => {
|
|
1727
|
+
expect(isResourcePattern('@search/*')).toBe(true);
|
|
1728
|
+
expect(isResourcePattern('@@contents/@delete/*')).toBe(true);
|
|
1729
|
+
expect(isResourcePattern('@layers.css')).toBe(true);
|
|
1730
|
+
expect(isResourcePattern('@@edit/*')).toBe(false);
|
|
1731
|
+
expect(isResourcePattern('@@contents/*')).toBe(false);
|
|
1732
|
+
expect(isResourcePattern('*')).toBe(false);
|
|
1733
|
+
});
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
describe('routeSkipsContent', () => {
|
|
1737
|
+
it('skips resource routes and the health check', () => {
|
|
1738
|
+
expect(routeSkipsContent('@objectBrowserWidget/*')).toBe(true);
|
|
1739
|
+
expect(routeSkipsContent('/@objectBrowserWidget/*')).toBe(true);
|
|
1740
|
+
expect(routeSkipsContent('@@contents/@delete/*')).toBe(true);
|
|
1741
|
+
expect(routeSkipsContent('@reset-fetcher')).toBe(true);
|
|
1742
|
+
expect(routeSkipsContent('ok')).toBe(true);
|
|
1743
|
+
expect(routeSkipsContent('/ok')).toBe(true);
|
|
1744
|
+
expect(routeSkipsContent('@@edit/*')).toBe(false);
|
|
1745
|
+
expect(routeSkipsContent('*')).toBe(false);
|
|
1746
|
+
expect(routeSkipsContent('/')).toBe(false);
|
|
1747
|
+
// A content item named `ok` is matched by the content route.
|
|
1748
|
+
expect(routeSkipsContent('folder/ok')).toBe(false);
|
|
1749
|
+
});
|
|
1750
|
+
});
|
|
1751
|
+
|
|
1752
|
+
describe('contentResourceMiddleware', () => {
|
|
1753
|
+
const resource = vi.fn(async () => new Response('resource'));
|
|
1754
|
+
|
|
1755
|
+
beforeEach(() => {
|
|
1756
|
+
config.registerUtility({
|
|
1757
|
+
type: 'contentResource',
|
|
1758
|
+
name: '@blocks-schema',
|
|
1759
|
+
method: resource,
|
|
1760
|
+
});
|
|
1761
|
+
});
|
|
1762
|
+
|
|
1763
|
+
afterEach(() => {
|
|
1764
|
+
resource.mockClear();
|
|
1765
|
+
delete (config as any)._data.utilities.contentResource;
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
it('splits the content path from the resource', () => {
|
|
1769
|
+
expect(matchContentResource('/news/2026/@blocks-schema')).toEqual({
|
|
1770
|
+
method: resource,
|
|
1771
|
+
path: '/news/2026',
|
|
1772
|
+
subpath: undefined,
|
|
1773
|
+
});
|
|
1774
|
+
expect(matchContentResource('/news/@blocks-schema/teaser')).toEqual({
|
|
1775
|
+
method: resource,
|
|
1776
|
+
path: '/news',
|
|
1777
|
+
subpath: 'teaser',
|
|
1778
|
+
});
|
|
1779
|
+
});
|
|
1780
|
+
|
|
1781
|
+
it('only matches registered resources below a content path', () => {
|
|
1782
|
+
// Without a content path, it's a resource route.
|
|
1783
|
+
expect(matchContentResource('/@blocks-schema')).toBeUndefined();
|
|
1784
|
+
expect(matchContentResource('/@blocks-schema/teaser')).toBeUndefined();
|
|
1785
|
+
expect(matchContentResource('/news/@other')).toBeUndefined();
|
|
1786
|
+
expect(matchContentResource('/news/@@blocks-schema')).toBeUndefined();
|
|
1787
|
+
expect(matchContentResource('/news/blocks-schema')).toBeUndefined();
|
|
1788
|
+
});
|
|
1789
|
+
|
|
1790
|
+
it('answers with the resource', async () => {
|
|
1791
|
+
const request = new Request(
|
|
1792
|
+
'http://localhost/news/@blocks-schema/teaser',
|
|
1793
|
+
);
|
|
1794
|
+
const response = await contentResourceMiddleware(
|
|
1795
|
+
{
|
|
1796
|
+
request,
|
|
1797
|
+
params: { '*': 'news/@blocks-schema/teaser' },
|
|
1798
|
+
context: new RouterContextProvider(),
|
|
1799
|
+
} as any,
|
|
1800
|
+
vi.fn(),
|
|
1801
|
+
);
|
|
1802
|
+
|
|
1803
|
+
expect(await (response as Response).text()).toBe('resource');
|
|
1804
|
+
expect(resource).toHaveBeenCalledWith({
|
|
1805
|
+
request,
|
|
1806
|
+
path: '/news',
|
|
1807
|
+
subpath: 'teaser',
|
|
1808
|
+
});
|
|
1809
|
+
});
|
|
1810
|
+
|
|
1811
|
+
it('lets other requests through', async () => {
|
|
1812
|
+
const response = await contentResourceMiddleware(
|
|
1813
|
+
{
|
|
1814
|
+
request: new Request('http://localhost/news'),
|
|
1815
|
+
params: { '*': 'news' },
|
|
1816
|
+
context: new RouterContextProvider(),
|
|
1817
|
+
} as any,
|
|
1818
|
+
vi.fn(),
|
|
1819
|
+
);
|
|
1820
|
+
expect(response).toBeUndefined();
|
|
1821
|
+
expect(resource).not.toHaveBeenCalled();
|
|
1822
|
+
});
|
|
1087
1823
|
});
|
|
1088
1824
|
});
|