From d2ed03fba34dcc2d750d3bf58ec731235dda3fb4 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:15:02 +0000 Subject: [PATCH 01/10] fix(embed): fix oEmbed handling The name of the oEmbed property in unfurl changed and wasn't updated with the 3.0.0 release fixes #35 --- package-lock.json | 2 +- package.json | 3 ++- src/embed.js | 28 +++++++++++++++------------- test/embed.test.js | 20 +++++++++++--------- test/postdeploy.test.js | 9 +++++---- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index f864dcad..91bf4719 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@adobe/helix-embed", - "version": "0.0.1", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f308e92b..52255a8a 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@semantic-release/npm": "^5.1.13", "@semantic-release/release-notes-generator": "^7.2.1", "ajv": "^6.10.0", + "chai": "^4.2.0", "chai-http": "^4.3.0", "codecov": "^3.3.0", "eslint": "^5.16.0", @@ -69,4 +70,4 @@ "wsk": { "name": "helix-services/embed@1.0.0" } -} \ No newline at end of file +} diff --git a/src/embed.js b/src/embed.js index 71d3e7b3..b51eb3ff 100644 --- a/src/embed.js +++ b/src/embed.js @@ -19,25 +19,25 @@ matchers.push(spark); matchers.push(unsplash); function toHTML({ - oembed = {}, open_graph = {}, twitter_card = {}, other = {}, + oEmbed = {}, open_graph = {}, twitter_card = {}, other = {}, title: otherTitle, description: otherDescription, classname, }, fallbackURL) { // there is a provider preference, let's go with it. - if (oembed.html) { + if (oEmbed.html) { return `
- ${oembed.html} + ${oEmbed.html}
`; } // gather information from different providers - const url = oembed.url || twitter_card.url || open_graph.url || other.canonical || fallbackURL; + const url = oEmbed.url || twitter_card.url || open_graph.url || other.canonical || fallbackURL; const title = twitter_card.title || open_graph.title || other.title || otherTitle; const description = twitter_card.description || open_graph.description || otherDescription; const icon = url && other.appleTouchIcon ? URI.resolve(url, other.appleTouchIcon) : null; const twitterImage = twitter_card.images ? twitter_card.images[0].url : null; const ogImage = open_graph.images ? open_graph.images[0].url : null; - const oembedImage = oembed.url !== url ? oembed.url : null; - const image = oembed.thumbnail_url || twitterImage || ogImage || oembedImage; + const oembedImage = oEmbed.url !== url ? oEmbed.url : null; + const image = oEmbed.thumbnail_url || twitterImage || ogImage || oembedImage; const classnames = ['embed', classname]; let html = []; @@ -101,13 +101,15 @@ function embed(url, params) { }; } - return unfurl(url, opts).then(enrich(params)).then(metadata => ({ - headers: { - 'Content-Type': 'text/html', - 'Cache-Control': `max-age=${metadata.oembed && metadata.oembed.cacheAge ? metadata.oembed.cacheAge : '3600'}`, - }, - body: toHTML(metadata, url), - })).catch(error => ({ + return unfurl(url, opts).then(enrich(params)).then((metadata) => { + return { + headers: { + 'Content-Type': 'text/html', + 'Cache-Control': `max-age=${metadata.oembed && metadata.oembed.cacheAge ? metadata.oembed.cacheAge : '3600'}`, + }, + body: toHTML(metadata, url), + }; + }).catch(error => ({ headers: { 'Content-Type': 'text/html', 'Cache-Control': 'max-age=3600', diff --git a/test/embed.test.js b/test/embed.test.js index db8276d3..a50ac5cd 100644 --- a/test/embed.test.js +++ b/test/embed.test.js @@ -25,10 +25,19 @@ function assertContains(actual, patterns) { patterns.map(expected => assert.ok(new RegExp(expected).test(actual), `${actual} does not match ${expected}`)); } +describe('Standalone Tests', () => { + // this test fails when recorded with Polly + it('Supports OEmbed for Youtube', async () => { + const { headers, body } = await embed('https://www.youtube.com/watch?v=ccYpEv4APec'); + assert.equal(headers['Content-Type'], 'text/html'); + assertContains(body, ['https://www.youtube.com/', 'iframe', 'oembed']); + }); +}); + describe('Embed Tests', () => { setupPolly({ - recordFailedRequests: true, - recordIfMissing: false, + recordFailedRequests: false, + recordIfMissing: true, logging: false, adapters: [NodeHttpAdapter], persister: FSPersister, @@ -73,13 +82,6 @@ describe('Embed Tests', () => { assertContains(body, ['embed-has-image']); }); - - it('Supports OEmbed Providers', async () => { - const { headers, body } = await embed('https://www.nytimes.com/2018/11/05/us/politics/house-senate-elections-georgia-trump.html'); - assert.equal(headers['Content-Type'], 'text/html'); - assertContains(body, ['https://www.nytimes.com/']); - }); - it('Supports Adobe Spark', async () => { const { headers, body } = await embed('https://spark.adobe.com/post/z4eHLkF8nZII1/'); assert.equal(headers['Content-Type'], 'text/html'); diff --git a/test/postdeploy.test.js b/test/postdeploy.test.js index 373cd6c8..5f7114a4 100644 --- a/test/postdeploy.test.js +++ b/test/postdeploy.test.js @@ -1,5 +1,5 @@ /* - * Copyright 2019 Adobe. All rights reserved. + * Copyright 2018 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 @@ -33,11 +33,12 @@ describe('Running Post-Deployment Integration Tests', () => { it('Service is reachable', async () => { await chai .request('https://adobeioruntime.net/') - .get(getbaseurl() + "/https://www.youtube.com/watch?v=TTCVn4EByfI") + .get(`${getbaseurl()}/https://www.youtube.com/watch?v=TTCVn4EByfI`) .then((response) => { expect(response).to.have.status(200); - // eslint-disable-next-line no-console expect(response.text).to.contain('youtube.com'); + expect(response.text).to.contain('iframe'); + expect(response.text).to.contain('oembed'); }).catch((e) => { throw e; }); @@ -46,7 +47,7 @@ describe('Running Post-Deployment Integration Tests', () => { it('Service reports status', async () => { await chai .request('https://adobeioruntime.net/') - .get(getbaseurl() + "") + .get(`${getbaseurl()}`) .then((response) => { expect(response).to.have.status(200); expect(response).to.have.header('Content-Type', 'application/xml; charset=UTF-8'); From c4801aad2df3a519bb3da8befa56e7350bc6b241 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:23:05 +0000 Subject: [PATCH 02/10] fix(embed): fix unsplash and spark embedding (broken by last commit) --- src/spark.js | 2 +- src/unsplash.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spark.js b/src/spark.js index 5ac7c132..df34f092 100644 --- a/src/spark.js +++ b/src/spark.js @@ -40,7 +40,7 @@ function decorator(metadata) { const enriched = Object.assign({}, metadata); const alt = metadata.title.replace(/\n/g, ''); const src = metadata.open_graph.images[0].url; - enriched.oembed = { + enriched.oEmbed = { html: `${alt}`, }; return Object.assign(enriched); diff --git a/src/unsplash.js b/src/unsplash.js index a394b927..6c804dd0 100644 --- a/src/unsplash.js +++ b/src/unsplash.js @@ -47,7 +47,7 @@ async function decorator(metadata, options) { user, urls, description, width, } = await meta(src, options.UNSPLASH_AUTH); - enriched.oembed = { + enriched.oEmbed = { html: `${description}

Photo by ${user.name} on Unsplash

`, From b1cdfe9bf1d490caf11c993bee53bd6f4f4eebd5 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:27:08 +0000 Subject: [PATCH 03/10] ci(auth): set unspash token for each deployment --- .circleci/config.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3b2771a4..57a31967 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -68,6 +68,10 @@ jobs: name: Semantic Release command: npm run semantic-release + - run: + name: Set Unsplash Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@`node -e "console.log(require('./package.json').version)"` -p UNSPLASH_AUTH $UNSPLASH_AUTH + - run: name: Post-Deployment Integration Test command: npm run test-postdeploy @@ -107,7 +111,11 @@ jobs: - run: name: Branch Deployment command: npx wsk-builder -v --entryFile index.js --deploy --test --name helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM - + + - run: + name: Set Unsplash Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@`node -e "console.log(require('./package.json').version)"` -p UNSPLASH_AUTH $UNSPLASH_AUTH + - run: name: Post-Deployment Integration Test command: npm run test-postdeploy From 9ca3847396ab902b35c5c337add5162844d94ba1 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:37:53 +0000 Subject: [PATCH 04/10] ci(deploy): set unspash token for CI deployments --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 57a31967..aab00ce8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -114,8 +114,8 @@ jobs: - run: name: Set Unsplash Token - command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@`node -e "console.log(require('./package.json').version)"` -p UNSPLASH_AUTH $UNSPLASH_AUTH - + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p UNSPLASH_AUTH $UNSPLASH_AUTH + - run: name: Post-Deployment Integration Test command: npm run test-postdeploy From 5949e455e2564f496daa03016931fb65b9fdfd0b Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:45:11 +0000 Subject: [PATCH 05/10] ci(deploy): set epsagon token immediately --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index aab00ce8..fca22fca 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -115,6 +115,10 @@ jobs: - run: name: Set Unsplash Token command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p UNSPLASH_AUTH $UNSPLASH_AUTH + + - run: + name: Set Epsagon Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN - run: name: Post-Deployment Integration Test @@ -122,10 +126,6 @@ jobs: - store_test_results: path: junit - - - run: - name: Set Epsagon Token - command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN - run: name: Link Sequence (CI) From c9bd5e6f54e6243228f8a8186d1cf6a95d4092f1 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 13:58:32 +0000 Subject: [PATCH 06/10] ci(deploy): add raw-http annotation --- .circleci/config.yml | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index fca22fca..e93ca215 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -110,7 +110,7 @@ jobs: - run: name: Branch Deployment - command: npx wsk-builder -v --entryFile index.js --deploy --test --name helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM + command: npx wsk-builder -v --raw-http --entryFile index.js --deploy --test --name helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM - run: name: Set Unsplash Token diff --git a/package.json b/package.json index 52255a8a..a764dfa4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "deploy-iframely": "npm run zip && wsk action update embed embed.zip --kind nodejs:10 --web raw --web-secure false -p api http://iframe.ly/api/oembed -p api_key insert-here", "deploy-embedly": "npm run zip && wsk action update embed embed.zip --kind nodejs:10 --web raw --web-secure false -p api https://api.embedly.com/1/oembed -p key insert-here", "build": "wsk-builder -v --entryFile index.js", - "deploy": "wsk-builder -v --deploy --test --entryFile index.js" + "deploy": "wsk-builder -v --raw-http --deploy --test --entryFile index.js" }, "repository": { "type": "git", From 5b5c2a206340f03ab5195642b4b2cfa7c2d7f3f7 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 14:03:36 +0000 Subject: [PATCH 07/10] test(deploy): add post-deploy tests for spark and unsplash --- test/postdeploy.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/postdeploy.test.js b/test/postdeploy.test.js index 5f7114a4..f9367bc6 100644 --- a/test/postdeploy.test.js +++ b/test/postdeploy.test.js @@ -30,7 +30,7 @@ function getbaseurl() { } describe('Running Post-Deployment Integration Tests', () => { - it('Service is reachable', async () => { + it('Youtube OEmbed', async () => { await chai .request('https://adobeioruntime.net/') .get(`${getbaseurl()}/https://www.youtube.com/watch?v=TTCVn4EByfI`) @@ -44,6 +44,30 @@ describe('Running Post-Deployment Integration Tests', () => { }); }); + it('Spark srcset', async () => { + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://spark.adobe.com/post/z4eHLkF8nZII1/`) + .then((response) => { + expect(response).to.have.status(200); + expect(response.text).to.contain('srcset'); + }).catch((e) => { + throw e; + }); + }); + + it('Unsplash srcset', async () => { + await chai + .request('https://adobeioruntime.net/') + .get(`${getbaseurl()}/https://unsplash.com/photos/0lD9SSMC6jo`) + .then((response) => { + expect(response).to.have.status(200); + expect(response.text).to.contain('srcset'); + }).catch((e) => { + throw e; + }); + }); + it('Service reports status', async () => { await chai .request('https://adobeioruntime.net/') From b979dd2ee7dbf48b6f592c05f89bcfba50bbddec Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 14:06:12 +0000 Subject: [PATCH 08/10] build(deploy): defer setting of EPSAGON_TOKEN --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index e93ca215..d8a2caf5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -115,14 +115,14 @@ jobs: - run: name: Set Unsplash Token command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p UNSPLASH_AUTH $UNSPLASH_AUTH - - - run: - name: Set Epsagon Token - command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN - run: name: Post-Deployment Integration Test command: npm run test-postdeploy + + - run: + name: Set Epsagon Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN - store_test_results: path: junit From 6cd469a33d91c5fdbe721ffdbcb2078183ba3206 Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 14:08:49 +0000 Subject: [PATCH 09/10] test(embed): relax Unsplash post-deploy test --- test/postdeploy.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/postdeploy.test.js b/test/postdeploy.test.js index f9367bc6..6600560b 100644 --- a/test/postdeploy.test.js +++ b/test/postdeploy.test.js @@ -62,7 +62,7 @@ describe('Running Post-Deployment Integration Tests', () => { .get(`${getbaseurl()}/https://unsplash.com/photos/0lD9SSMC6jo`) .then((response) => { expect(response).to.have.status(200); - expect(response.text).to.contain('srcset'); + expect(response.text).to.contain('Unsplash'); }).catch((e) => { throw e; }); From 15e3486e32357478edc8651e610c95fbef895d9c Mon Sep 17 00:00:00 2001 From: Lars Trieloff Date: Thu, 27 Jun 2019 14:15:09 +0000 Subject: [PATCH 10/10] ci(deploy): set unsplash token and epsagon token together Parameter setting in OpenWhisk is not additive, so we have to set all parameters for the second invocation. However, this should enable Unsplash API calls once again --- .circleci/config.yml | 8 ++++---- test/postdeploy.test.js | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d8a2caf5..4f586b8d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -80,8 +80,8 @@ jobs: path: junit - run: - name: Set Epsagon Token - command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@`node -e "console.log(require('./package.json').version)"` -p EPSAGON_TOKEN $EPSAGON_TOKEN + name: Set Epsagon Token and Unspash Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@`node -e "console.log(require('./package.json').version)"` -p EPSAGON_TOKEN $EPSAGON_TOKEN -p UNSPLASH_AUTH $UNSPLASH_AUTH - run: name: Link Sequence (Latest) @@ -121,8 +121,8 @@ jobs: command: npm run test-postdeploy - run: - name: Set Epsagon Token - command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN + name: Set Epsagon Token and Unsplash Token + command: wsk action update helix-services/`node -e "console.log(require('./package.json').name.replace('@adobe/helix-', ''))"`@ci$CIRCLE_BUILD_NUM -p EPSAGON_TOKEN $EPSAGON_TOKEN -p UNSPLASH_AUTH $UNSPLASH_AUTH - store_test_results: path: junit diff --git a/test/postdeploy.test.js b/test/postdeploy.test.js index 6600560b..ff2113d8 100644 --- a/test/postdeploy.test.js +++ b/test/postdeploy.test.js @@ -63,6 +63,7 @@ describe('Running Post-Deployment Integration Tests', () => { .then((response) => { expect(response).to.have.status(200); expect(response.text).to.contain('Unsplash'); + expect(response.text).to.contain('srcset'); }).catch((e) => { throw e; });