diff --git a/core/server/apps/amp/lib/router.js b/core/server/apps/amp/lib/router.js index b7eff69eee..b42803240b 100644 --- a/core/server/apps/amp/lib/router.js +++ b/core/server/apps/amp/lib/router.js @@ -64,7 +64,8 @@ function getPostData(req, res, next) { } // @NOTE: amp is not supported for static pages - helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {controller: 'posts', resource: 'posts'}}, res.locals) + // @TODO: AMP should make an HTTP request to the Content API {version} + helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {controller: 'postsPublic', resource: 'posts'}}, res.locals) .then((result) => { if (result && result.entry) { req.body.post = result.entry; diff --git a/core/server/services/routing/helpers/entry-lookup.js b/core/server/services/routing/helpers/entry-lookup.js index 5911cf43ac..4ccd98adf7 100644 --- a/core/server/services/routing/helpers/entry-lookup.js +++ b/core/server/services/routing/helpers/entry-lookup.js @@ -39,10 +39,12 @@ function entryLookup(postUrl, routerOptions, locals) { let options = { include: 'author,authors,tags' }; + if (config.get('enableDeveloperExperiments')) { options.context = {member: locals.member}; } - return api[routerOptions.query.controller] + + return (api[routerOptions.query.controller] || api[routerOptions.query.resource]) .read(_.extend(_.pick(params, 'slug', 'id'), options)) .then(function then(result) { const entry = result[routerOptions.query.resource][0]; diff --git a/core/test/regression/site/site_spec.js b/core/test/regression/site/site_spec.js index 9f6e1e2bbd..764032a685 100644 --- a/core/test/regression/site/site_spec.js +++ b/core/test/regression/site/site_spec.js @@ -4,6 +4,7 @@ const should = require('should'), cheerio = require('cheerio'), testUtils = require('../../utils'), configUtils = require('../../utils/configUtils'), + appsService = require('../../../server/services/apps'), settingsService = require('../../../server/services/settings'), themeService = require('../../../server/services/themes'), siteApp = require('../../../server/web/parent-app'); @@ -20,7 +21,7 @@ describe('Integration - Web - Site', function () { describe('default routes.yaml', function () { before(function () { - testUtils.integrationTesting.defaultMocks(sinon); + testUtils.integrationTesting.defaultMocks(sinon, {amp: true, apps: true}); testUtils.integrationTesting.overrideGhostConfig(configUtils); return testUtils.integrationTesting.initGhost() @@ -30,6 +31,9 @@ describe('Integration - Web - Site', function () { app = siteApp({start: true}); return testUtils.integrationTesting.urlService.waitTillFinished(); + }) + .then(() => { + return appsService.init(); }); }); @@ -64,6 +68,22 @@ describe('Integration - Web - Site', function () { }); }); + it('serve amp', function () { + const req = { + secure: true, + method: 'GET', + url: '/html-ipsum/amp/', + host: 'example.com' + }; + + return testUtils.mocks.express.invoke(app, req) + .then(function (response) { + response.statusCode.should.eql(200); + response.template.should.match(/amp\.hbs/); + response.body.should.match(/

HTML Ipsum Presents<\/h1>/); + }); + }); + it('post not found', function () { const req = { secure: true, @@ -1749,7 +1769,7 @@ describe('Integration - Web - Site', function () { describe('default routes.yaml', function () { before(function () { testUtils.integrationTesting.urlService.resetGenerators(); - testUtils.integrationTesting.defaultMocks(sinon); + testUtils.integrationTesting.defaultMocks(sinon, {amp: true, apps: true}); testUtils.integrationTesting.overrideGhostConfig(configUtils); return testUtils.integrationTesting.initGhost() @@ -1759,6 +1779,9 @@ describe('Integration - Web - Site', function () { app = siteApp({start: true}); return testUtils.integrationTesting.urlService.waitTillFinished(); + }) + .then(() => { + return appsService.init(); }); }); @@ -1793,6 +1816,22 @@ describe('Integration - Web - Site', function () { }); }); + it('serve amp', function () { + const req = { + secure: true, + method: 'GET', + url: '/html-ipsum/amp/', + host: 'example.com' + }; + + return testUtils.mocks.express.invoke(app, req) + .then(function (response) { + response.statusCode.should.eql(200); + response.template.should.match(/amp\.hbs/); + response.body.should.match(/

HTML Ipsum Presents<\/h1>/); + }); + }); + it('post not found', function () { const req = { secure: true, diff --git a/core/test/unit/apps/amp/router_spec.js b/core/test/unit/apps/amp/router_spec.js index d28e5f1fc1..96bede1de1 100644 --- a/core/test/unit/apps/amp/router_spec.js +++ b/core/test/unit/apps/amp/router_spec.js @@ -122,7 +122,7 @@ describe('Unit - apps/amp/lib/router', function () { urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); - helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}) + helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}}) .resolves({ entry: post }); @@ -139,7 +139,7 @@ describe('Unit - apps/amp/lib/router', function () { urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); - helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}).resolves({ + helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}}).resolves({ entry: post }); @@ -154,7 +154,7 @@ describe('Unit - apps/amp/lib/router', function () { urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); - helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}) + helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}}) .rejects(new common.errors.NotFoundError()); ampController.getPostData(req, res, function (err) { diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 51ea2eaaed..1fe56b6cc7 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -1052,6 +1052,14 @@ module.exports = { cacheStub.withArgs('permalinks').returns('/:slug/'); cacheStub.withArgs('labs').returns({publicAPI: true}); + if (options.amp) { + cacheStub.withArgs('amp').returns(true); + } + + if (options.apps) { + cacheStub.withArgs('active_apps').returns([]); + } + sandbox.stub(api.clients, 'read').returns(Promise.resolve({ clients: [ {slug: 'ghost-frontend', secret: 'a1bcde23cfe5', status: 'enabled'}