diff --git a/registry/server/templates/routes/getRenderedTemplate.ts b/registry/server/templates/routes/getRenderedTemplate.ts index 2d55ee05b..4130fff03 100644 --- a/registry/server/templates/routes/getRenderedTemplate.ts +++ b/registry/server/templates/routes/getRenderedTemplate.ts @@ -92,7 +92,7 @@ async function getRenderedTemplate(req: Request { - if (!isTemplateValid(template)) { +async function renderTemplate(templateContent: string): Promise { + if (!isTemplateValid(templateContent)) { throw new Error('HTML template has invalid structure'); } - const includesAttributes = matchIncludesAttributes(template); - + const includesAttributes = matchIncludesAttributes(templateContent); const result: RenderTemplateResult = { - content: template, + content: templateContent, styleRefs: [], }; diff --git a/registry/tests/templates.spec.ts b/registry/tests/templates.spec.ts index 61f73ad08..ea1903312 100644 --- a/registry/tests/templates.spec.ts +++ b/registry/tests/templates.spec.ts @@ -35,6 +35,12 @@ const example = { 'fr-CA': { content: 'Canada content' }, }, }), + withInclude: Object.freeze({ + name: 'ncTestTemplateNameWithInclude', + content: + '' + + 'test content', + }), }; describe(`Tests ${example.url}`, () => { @@ -170,103 +176,6 @@ describe(`Tests ${example.url}`, () => { expect(response.body).deep.equal(example.correctLocalized); }); - it('should return a rendered template w/o authentication', async () => { - const includesHost = 'https://api.include.com'; - const scope = nock(includesHost); - - const includes = [ - { - api: { - route: '/get/include/1', - delay: 0, - response: { - status: 200, - data: ` -
- This include has all necessary attributes - and a specified link header which is a stylesheet -
- `, - headers: { - 'X-Powered-By': 'JS', - 'X-My-Awesome-Header': 'Awesome', - Link: 'https://my.awesome.server/my-awesome-stylesheet.css;rel=stylesheet;loveyou=3000,https://my.awesome.server/my-awesome-script.js;rel=script;loveyou=3000', - }, - }, - }, - attributes: { - id: 'include-id-1', - src: `${includesHost}/get/include/1`, - timeout: 1000, - }, - }, - ]; - - const template = { - name: 'This template is for a render test', - content: ` - - - - - - - - -
Something...
-
Something...
-
- - - `, - }; - - includes.forEach( - ({ - api: { - route, - delay, - response: { status, data, headers }, - }, - }) => scope.persist().get(route).delay(delay).reply(status, data, headers), - ); - - try { - await req.post(example.url).send(template).expect(200); - - const response = await reqWithAuth.get(example.url + template.name + '/rendered').expect(200); - - expect(response.body).to.eql({ - styleRefs: ['https://my.awesome.server/my-awesome-stylesheet.css'], - name: template.name, - content: ` - - - - - ${ - `\n` + - '\n' + - includes[0].api.response.data + - '\n' + - '\n' + - `` - } - - - -
Something...
-
Something...
-
- - - `, - }); - } finally { - await req.delete(example.url + template.name); - } - }); - it('should return localized version of rendered template', async () => { await withSetting( SettingKeys.I18nSupportedLocales, @@ -562,4 +471,122 @@ describe(`Tests ${example.url}`, () => { }); }); }); + + describe('Rendered', () => { + it('should return HTTP 500 in case of inability to render template', async () => { + const setupIncludeResults = (delay: number) => + nock('https://complete-random-ilc-include-test-domain.org.ote') + .get('/include.html') + .delay(delay) + .reply(200, '
test content
'); + setupIncludeResults(10); + const creationResponse = await req.post(example.url).send(example.withInclude); + try { + expect(creationResponse.status).to.eq(200, creationResponse.text); + setupIncludeResults(1000); // should be bigger than timeout in HTML + const templateResponse = await req.get(example.url + example.withInclude.name + '/rendered'); + + expect(templateResponse.statusCode).to.equal(500); + } finally { + await req.delete(example.url + example.withInclude.name); + } + }); + + it('should return a rendered template w/o authentication', async () => { + const includesHost = 'https://api.include.com'; + const scope = nock(includesHost); + + const includes = [ + { + api: { + route: '/get/include/1', + delay: 0, + response: { + status: 200, + data: ` +
+ This include has all necessary attributes + and a specified link header which is a stylesheet +
+ `, + headers: { + 'X-Powered-By': 'JS', + 'X-My-Awesome-Header': 'Awesome', + Link: 'https://my.awesome.server/my-awesome-stylesheet.css;rel=stylesheet;loveyou=3000,https://my.awesome.server/my-awesome-script.js;rel=script;loveyou=3000', + }, + }, + }, + attributes: { + id: 'include-id-1', + src: `${includesHost}/get/include/1`, + timeout: 1000, + }, + }, + ]; + + const template = { + name: 'This template is for a render test', + content: ` + + + + + + + + +
Something...
+
Something...
+
+ + + `, + }; + + includes.forEach( + ({ + api: { + route, + delay, + response: { status, data, headers }, + }, + }) => scope.persist().get(route).delay(delay).reply(status, data, headers), + ); + + try { + await req.post(example.url).send(template).expect(200); + + const response = await reqWithAuth.get(example.url + template.name + '/rendered').expect(200); + + expect(response.body).to.eql({ + styleRefs: ['https://my.awesome.server/my-awesome-stylesheet.css'], + name: template.name, + content: ` + + + + + ${ + `\n` + + '\n' + + includes[0].api.response.data + + '\n' + + '\n' + + `` + } + + + +
Something...
+
Something...
+
+ + + `, + }); + } finally { + await req.delete(example.url + template.name); + } + }); + }); });