@squiz/component-cli-lib 1.54.1-alpha.2 → 1.54.1-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ import { ComponentSetWebModelForCreate } from '@squiz/component-lib';
2
+ import { ContentApi } from '@squiz/component-web-api-lib';
3
+ export declare const COMPONENT_SET_WEB_PATH: string;
4
+ export declare const COMPONENT_NAME = "test-page-render";
5
+ export declare const COMPONENT_NAMESPACE = "unit-test-components";
6
+ export declare const COMPONENT_CONTENT_ID: string;
7
+ export declare const COMPONENT_NODE_ID: string;
8
+ export declare const componentSet: ComponentSetWebModelForCreate;
9
+ export declare const componentContentItem: {
10
+ id: string;
11
+ schemaName: string;
12
+ content: {
13
+ text: string;
14
+ };
15
+ };
16
+ export declare const pageContents: ContentApi.PageContentsCreate;
17
+ export declare const pageContentsWithVariants: ContentApi.PageContentsCreate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/component-cli-lib",
3
- "version": "1.54.1-alpha.2",
3
+ "version": "1.54.1-alpha.4",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -13,12 +13,12 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "devDependencies": {
16
- "@squiz/component-lib": "1.54.1-alpha.2",
17
- "@squiz/component-web-api-lib": "1.54.1-alpha.2",
18
- "@squiz/dx-common-lib": "1.54.1-alpha.2",
19
- "@squiz/dx-json-schema-lib": "1.54.1-alpha.2",
20
- "@squiz/dx-logger-lib": "1.54.1-alpha.2",
21
- "@squiz/virus-scanner-lib": "1.54.1-alpha.2",
16
+ "@squiz/component-lib": "1.54.1-alpha.4",
17
+ "@squiz/component-web-api-lib": "1.54.1-alpha.4",
18
+ "@squiz/dx-common-lib": "1.54.1-alpha.4",
19
+ "@squiz/dx-json-schema-lib": "1.54.1-alpha.4",
20
+ "@squiz/dx-logger-lib": "1.54.1-alpha.4",
21
+ "@squiz/virus-scanner-lib": "1.54.1-alpha.4",
22
22
  "@types/cli-color": "2.0.2",
23
23
  "@types/express": "4.17.17",
24
24
  "@types/jest": "28.1.8",
@@ -32,12 +32,12 @@
32
32
  "typescript": "4.9.4"
33
33
  },
34
34
  "dependencies": {
35
- "@squiz/render-runtime-lib": "1.54.1-alpha.2",
35
+ "@squiz/render-runtime-lib": "1.54.1-alpha.4",
36
36
  "archiver": "5.3.1",
37
37
  "axios": "1.3.2",
38
38
  "cli-color": "^2.0.2",
39
39
  "open": "^8.4.0",
40
40
  "supertest": "^6.2.3"
41
41
  },
42
- "gitHead": "d3535c8a51723dac528e17f579234885f77a8d25"
42
+ "gitHead": "6b462491372ff22c83f7d52c9410b531f4030a55"
43
43
  }
@@ -97,10 +97,6 @@ export const managementServiceRoot = axios.create({
97
97
 
98
98
  export const renderService = axios.create({
99
99
  baseURL: configObj.renderServiceUrl,
100
- headers: {
101
- Authorization: authToken,
102
- 'x-api-key': 'inter-service-api-key',
103
- },
104
100
  });
105
101
 
106
102
  export const contentService = axios.create({
@@ -112,9 +108,6 @@ export const contentService = axios.create({
112
108
 
113
109
  export const pageRenderService = axios.create({
114
110
  baseURL: configObj.pageRenderServiceUrl,
115
- headers: {
116
- Authorization: authToken,
117
- },
118
111
  });
119
112
 
120
113
  export const jobService = axios.create({
@@ -0,0 +1,150 @@
1
+ import { randomInt } from 'crypto';
2
+ import { ComponentSetWebModelForCreate } from '@squiz/component-lib';
3
+ import { ContentApi } from '@squiz/component-web-api-lib';
4
+
5
+ export const COMPONENT_SET_WEB_PATH = 'local-development-only' + randomInt(10000);
6
+ export const COMPONENT_NAME = 'test-page-render';
7
+ export const COMPONENT_NAMESPACE = 'unit-test-components';
8
+ export const COMPONENT_CONTENT_ID = 'component-content-id' + randomInt(10000);
9
+ export const COMPONENT_NODE_ID = `${COMPONENT_NAMESPACE}/${COMPONENT_NAME}/1.0.0/main`;
10
+
11
+ //The componentSet for the component in the content item
12
+ export const componentSet: ComponentSetWebModelForCreate = {
13
+ webPath: COMPONENT_SET_WEB_PATH,
14
+ displayName: 'test-set',
15
+ description: 'test-set',
16
+ headers: {},
17
+ components: {},
18
+ componentVersionRules: {
19
+ [`${COMPONENT_NAMESPACE}/${COMPONENT_NAME}`]: {
20
+ renderableVersionPattern: '1.0.0',
21
+ editableVersions: [],
22
+ excludedVersions: [],
23
+ },
24
+ },
25
+ };
26
+
27
+ // The content item for the component
28
+ export const componentContentItem = {
29
+ id: COMPONENT_CONTENT_ID,
30
+ schemaName: COMPONENT_NODE_ID,
31
+ content: {
32
+ text: 'component-input-string',
33
+ },
34
+ };
35
+
36
+ // The page content item which references the content item
37
+ export const pageContents: ContentApi.PageContentsCreate = {
38
+ name: 'page-contents-test-1',
39
+ layouts: [
40
+ {
41
+ name: 'single-column',
42
+ content: {
43
+ main: [
44
+ {
45
+ type: 'tag',
46
+ tag: 'h1',
47
+ children: [
48
+ {
49
+ type: 'text',
50
+ value: 'Hello World!',
51
+ },
52
+ ],
53
+ },
54
+ {
55
+ type: 'component',
56
+ componentSet: COMPONENT_SET_WEB_PATH,
57
+ componentId: COMPONENT_NODE_ID,
58
+ contentItem: { id: COMPONENT_CONTENT_ID },
59
+ },
60
+ {
61
+ type: 'tag',
62
+ tag: 'p',
63
+ children: [
64
+ {
65
+ type: 'text',
66
+ value: 'Bottom text',
67
+ },
68
+ ],
69
+ },
70
+ ],
71
+ },
72
+ },
73
+ ],
74
+ };
75
+
76
+ export const pageContentsWithVariants: ContentApi.PageContentsCreate = {
77
+ name: 'page-contents-test-2',
78
+ layouts: [
79
+ {
80
+ name: 'single-column',
81
+ content: {
82
+ main: [
83
+ {
84
+ type: 'variation',
85
+ default: {
86
+ node: {
87
+ type: 'tag',
88
+ tag: 'h1',
89
+ children: [
90
+ {
91
+ type: 'text',
92
+ value: 'Hello World!',
93
+ },
94
+ ],
95
+ },
96
+ },
97
+ variants: [
98
+ {
99
+ node: {
100
+ type: 'tag',
101
+ tag: 'h1',
102
+ children: [
103
+ {
104
+ type: 'text',
105
+ value: 'Hello Authenticated User with experiment B!',
106
+ },
107
+ ],
108
+ },
109
+ conditions: {
110
+ segment: { code: 'is-authenticated' },
111
+ experiment: { id: 'experiment1', variantId: 'B' },
112
+ },
113
+ },
114
+ {
115
+ node: {
116
+ type: 'tag',
117
+ tag: 'h1',
118
+ children: [
119
+ {
120
+ type: 'text',
121
+ value: 'Hello Authenticated User!',
122
+ },
123
+ ],
124
+ },
125
+ conditions: {
126
+ segment: { code: 'is-authenticated' },
127
+ },
128
+ },
129
+ {
130
+ node: {
131
+ type: 'tag',
132
+ tag: 'h1',
133
+ children: [
134
+ {
135
+ type: 'text',
136
+ value: 'Hello Unauthenticated User!',
137
+ },
138
+ ],
139
+ },
140
+ conditions: {
141
+ segment: { code: 'is-unauthenticated' },
142
+ },
143
+ },
144
+ ],
145
+ },
146
+ ],
147
+ },
148
+ },
149
+ ],
150
+ };
@@ -1,11 +1,10 @@
1
- import { PageContentsCreate } from '@squiz/component-web-api-lib/src/generated/ContentApi';
2
- import { ComponentSetWebModelForCreate } from '@squiz/component-lib';
3
1
  import path from 'path';
2
+ import { ContentApi } from '@squiz/component-web-api-lib';
4
3
  import {
5
4
  addComponentSet,
6
5
  contentService,
7
- deleteComponentSet,
8
6
  deleteComponents,
7
+ deleteComponentSet,
9
8
  deleteContentItem,
10
9
  deleteContentSchema,
11
10
  getTestComponent,
@@ -13,88 +12,40 @@ import {
13
12
  uploadTestComponent,
14
13
  } from './helper';
15
14
  import fsp from 'fs/promises';
16
- import { randomInt } from 'crypto';
17
-
18
- const COMPONENT_SET_WEB_PATH = 'local-development-only' + randomInt(10000);
19
- const COMPONENT_NAME = 'test-page-render';
20
- const COMPONENT_NAMESPACE = 'unit-test-components';
21
- const COMPONENT_CONTENT_ID = 'component-content-id' + randomInt(10000);
22
- const COMPONENT_NODE_ID = `${COMPONENT_NAMESPACE}/${COMPONENT_NAME}/1.0.0/main`;
23
-
24
- //The componentSet for the component in the content item
25
- const testComponentSet: ComponentSetWebModelForCreate = {
26
- webPath: COMPONENT_SET_WEB_PATH,
27
- displayName: 'test-set',
28
- description: 'test-set',
29
- headers: {},
30
- components: {},
31
- componentVersionRules: {
32
- [`${COMPONENT_NAMESPACE}/${COMPONENT_NAME}`]: {
33
- renderableVersionPattern: '1.0.0',
34
- editableVersions: [],
35
- excludedVersions: [],
36
- },
37
- },
38
- };
39
-
40
- // The content item for the component
41
- const componentContentItem = {
42
- id: COMPONENT_CONTENT_ID,
43
- schemaName: COMPONENT_NODE_ID,
44
- content: {
45
- text: 'component-input-string',
46
- },
47
- };
48
-
49
- // The page content item which references the content item
50
- const pageContents: PageContentsCreate = {
51
- name: 'page-contents-test-1',
52
- layouts: [
53
- {
54
- name: 'single-column',
55
- content: {
56
- main: [
57
- {
58
- type: 'tag',
59
- tag: 'h1',
60
- children: [
61
- {
62
- type: 'text',
63
- value: 'Hello World!',
64
- },
65
- ],
66
- },
67
- {
68
- type: 'component',
69
- componentSet: COMPONENT_SET_WEB_PATH,
70
- componentId: COMPONENT_NODE_ID,
71
- contentItem: { id: COMPONENT_CONTENT_ID },
72
- },
73
- {
74
- type: 'tag',
75
- tag: 'p',
76
- children: [
77
- {
78
- type: 'text',
79
- value: 'Bottom text',
80
- },
81
- ],
82
- },
83
- ],
84
- },
85
- },
86
- ],
87
- };
15
+ import { buildDevelopmentSession } from '@squiz/dxp-session-lib';
16
+ import { DxpCacheAccess, DxpSession, DxpSessionVersion } from '@squiz/dxp-session-lib';
17
+ import {
18
+ COMPONENT_CONTENT_ID,
19
+ COMPONENT_NAME,
20
+ COMPONENT_NODE_ID,
21
+ COMPONENT_SET_WEB_PATH,
22
+ componentContentItem,
23
+ componentSet,
24
+ pageContents,
25
+ pageContentsWithVariants,
26
+ } from './upload-and-render-page.integration.mocks';
88
27
 
89
28
  describe('PageRenderController- Integration', () => {
90
29
  let PAGE_CONTENT_ID: string;
91
30
  const testFilesDir = path.resolve(__dirname, 'test-files');
31
+ const pageContentIds: string[] = [];
32
+ const createPage = async (page: ContentApi.PageContentsCreate): Promise<string> => {
33
+ try {
34
+ const response = await contentService.post('page-contents', page);
35
+ pageContentIds.push(response.data.id);
36
+ return response.data.id;
37
+ } catch (error) {
38
+ console.log('error', error, JSON.stringify((error as any).response.data));
39
+ throw error;
40
+ }
41
+ };
42
+
92
43
  beforeAll(async () => {
93
44
  await fsp.rm(testFilesDir, { force: true, recursive: true });
94
45
  await fsp.mkdir(testFilesDir);
95
46
  // Component Configuration
96
47
  await uploadTestComponent('/__components__/test-page-render', testFilesDir);
97
- await addComponentSet(testComponentSet);
48
+ await addComponentSet(componentSet);
98
49
  // Post component items
99
50
  await contentService.post('content-item', componentContentItem);
100
51
  });
@@ -104,133 +55,127 @@ describe('PageRenderController- Integration', () => {
104
55
  await deleteContentSchema(COMPONENT_NODE_ID);
105
56
  await deleteComponentSet(COMPONENT_SET_WEB_PATH);
106
57
  await deleteComponents(await getTestComponent(COMPONENT_NAME));
58
+ await Promise.all(pageContentIds.map((id) => contentService.delete(`page-contents/${id}`)));
107
59
  });
108
60
 
109
61
  it('should render a page', async () => {
110
- const response = await contentService.post('page-contents', pageContents, {
111
- headers: { 'x-api-key': 'MOCK_KEY' },
112
- });
113
- PAGE_CONTENT_ID = response.data.id!;
114
- expect(response.status).toBe(201);
62
+ PAGE_CONTENT_ID = await createPage(pageContents);
115
63
 
116
- const mockJwtPayload = {
117
- segments: ['is-authenticated'],
118
- customerid: 'test-id-123',
119
- };
120
- const mockSession = Buffer.from(JSON.stringify(mockJwtPayload)).toString('base64');
64
+ const mockSession = buildDevelopmentSession({
65
+ version: DxpSessionVersion.version1,
66
+ });
121
67
  const page = await pageRenderService.get(`p/${PAGE_CONTENT_ID}`, {
122
- headers: { 'x-dxp-customer-session': mockSession },
68
+ headers: { 'x-dxp-session': mockSession },
123
69
  });
124
70
  expect(page.data).toEqual(
125
71
  `<div class="container"><h1>Hello World!</h1><esi:include src="/__dxp/au/components-render/${process.env.TENANT_ID}/r/unit-test-components/test-page-render/1.0.0/main?_componentSet=${COMPONENT_SET_WEB_PATH}&_contentItemId=${COMPONENT_CONTENT_ID}"/><p>Bottom text</p></div>`,
126
72
  );
127
73
  });
128
74
 
129
- describe('it should render', () => {
75
+ describe('should render a page with variations', () => {
130
76
  beforeAll(async () => {
131
- const pageVariant: PageContentsCreate = {
132
- name: 'page-contents-test-2',
133
- layouts: [
134
- {
135
- name: 'single-column',
136
- content: {
137
- main: [
138
- {
139
- type: 'variation',
140
- default: {
141
- node: {
142
- type: 'tag',
143
- tag: 'h1',
144
- children: [
145
- {
146
- type: 'text',
147
- value: 'Hello World!',
148
- },
149
- ],
150
- },
151
- },
152
- variants: [
153
- {
154
- node: {
155
- type: 'tag',
156
- tag: 'h1',
157
- children: [
158
- {
159
- type: 'text',
160
- value: 'Hello Authenticated User!',
161
- },
162
- ],
163
- },
164
- conditions: {
165
- segment: { code: 'is-authenticated' },
166
- },
167
- },
168
- {
169
- node: {
170
- type: 'tag',
171
- tag: 'h1',
172
- children: [
173
- {
174
- type: 'text',
175
- value: 'Hello Unauthenticated User!',
176
- },
177
- ],
178
- },
179
- conditions: {
180
- segment: { code: 'is-unauthenticated' },
181
- },
182
- },
183
- ],
184
- },
185
- ],
186
- },
187
- },
188
- ],
189
- };
190
- const response = await contentService.post('page-contents', pageVariant, {
191
- headers: { 'x-api-key': 'MOCK_KEY' },
192
- });
193
- PAGE_CONTENT_ID = response.data.id!;
77
+ PAGE_CONTENT_ID = await createPage(pageContentsWithVariants);
194
78
  });
195
79
 
196
- describe('Variations', () => {
197
- it('a valid page variation', async () => {
198
- const mockJwtPayload = {
199
- segments: ['is-authenticated'],
200
- customerid: 'test-id-123',
201
- };
202
- const mockSession = Buffer.from(JSON.stringify(mockJwtPayload)).toString('base64');
203
- try {
204
- const page = await pageRenderService.get(`p/${PAGE_CONTENT_ID}`, {
205
- headers: { 'x-dxp-customer-session': mockSession },
206
- });
207
- expect(page.data).toEqual('<div class="container"><h1>Hello Authenticated User!</h1></div>');
208
- } catch (error) {
209
- console.log(JSON.stringify(error, null));
210
- }
211
- });
212
- it('another valid page variation', async () => {
213
- const mockJwtPayload = {
214
- segments: ['is-unauthenticated'],
215
- customerid: 'test-id-123',
216
- };
217
- const mockSession = Buffer.from(JSON.stringify(mockJwtPayload)).toString('base64');
218
- const page = await pageRenderService.get(`p/${PAGE_CONTENT_ID}`, {
219
- headers: { 'x-dxp-customer-session': mockSession },
220
- });
221
- expect(page.data).toEqual('<div class="container"><h1>Hello Unauthenticated User!</h1></div>');
222
- });
223
- it('the default node if there is not a valid page variation', async () => {
224
- const mockJwtPayload = {
225
- segments: ['not-valid'],
226
- customerid: 'test-id-123',
227
- };
228
- const mockSession = Buffer.from(JSON.stringify(mockJwtPayload)).toString('base64');
80
+ it.each([
81
+ [
82
+ 'a segment variant',
83
+ {
84
+ version: DxpSessionVersion.version1,
85
+ cdp: {
86
+ anonymous: false,
87
+ customerid: 'test-id-123',
88
+ segments: ['is-authenticated'],
89
+ },
90
+ },
91
+ '<div class="container"><h1>Hello Authenticated User!</h1></div>',
92
+ {
93
+ version: DxpSessionVersion.version1,
94
+ cdp: {
95
+ segments: ['is-authenticated'],
96
+ },
97
+ optimize: {
98
+ experiments: [{ id: 'experiment1', variant: 'B' }],
99
+ },
100
+ },
101
+ ],
102
+ [
103
+ 'another segment variant',
104
+ {
105
+ version: DxpSessionVersion.version1,
106
+ cdp: {
107
+ anonymous: false,
108
+ customerid: 'test-id-123',
109
+ segments: ['is-unauthenticated'],
110
+ },
111
+ },
112
+ '<div class="container"><h1>Hello Unauthenticated User!</h1></div>',
113
+ {
114
+ version: DxpSessionVersion.version1,
115
+ cdp: {
116
+ segments: ['is-authenticated', 'is-unauthenticated'],
117
+ },
118
+ optimize: {
119
+ experiments: [],
120
+ },
121
+ },
122
+ ],
123
+ [
124
+ 'an experiment variant',
125
+ {
126
+ version: DxpSessionVersion.version1,
127
+ cdp: {
128
+ anonymous: false,
129
+ customerid: 'test-id-123',
130
+ segments: ['is-authenticated'],
131
+ },
132
+ optimize: {
133
+ experiments: [{ id: 'experiment1', variant: 'B' }],
134
+ },
135
+ },
136
+ '<div class="container"><h1>Hello Authenticated User with experiment B!</h1></div>',
137
+ {
138
+ version: DxpSessionVersion.version1,
139
+ cdp: {
140
+ segments: ['is-authenticated'],
141
+ },
142
+ optimize: {
143
+ experiments: [{ id: 'experiment1', variant: 'B' }],
144
+ },
145
+ },
146
+ ],
147
+ [
148
+ 'the default variant',
149
+ {
150
+ version: DxpSessionVersion.version1,
151
+ cdp: {
152
+ anonymous: false,
153
+ customerid: 'test-id-123',
154
+ segments: ['not-valid'],
155
+ },
156
+ },
157
+ '<div class="container"><h1>Hello World!</h1></div>',
158
+ {
159
+ version: DxpSessionVersion.version1,
160
+ cdp: {
161
+ segments: ['is-authenticated', 'is-unauthenticated'],
162
+ },
163
+ optimize: {
164
+ experiments: [],
165
+ },
166
+ },
167
+ ],
168
+ ])(
169
+ 'renders the matching variation for %s',
170
+ async (description: string, session: DxpSession, expectedBody: string, expectedCacheAccess: DxpCacheAccess) => {
229
171
  const page = await pageRenderService.get(`p/${PAGE_CONTENT_ID}`, {
230
- headers: { 'x-dxp-customer-session': mockSession },
172
+ headers: { 'x-dxp-session': buildDevelopmentSession(session) },
231
173
  });
232
- expect(page.data).toEqual('<div class="container"><h1>Hello World!</h1></div>');
233
- });
234
- });
174
+ const dxpCacheAccess = JSON.parse(Buffer.from(page.headers['x-dxp-cache-access'], 'base64').toString());
175
+
176
+ expect(page.data).toEqual(expectedBody);
177
+ expect(dxpCacheAccess).toEqual(expectedCacheAccess);
178
+ },
179
+ );
235
180
  });
236
181
  });