diff --git a/lib/cypress-plugin/index.js b/lib/cypress-plugin/index.js index ca5593f..0d0d581 100644 --- a/lib/cypress-plugin/index.js +++ b/lib/cypress-plugin/index.js @@ -2,6 +2,7 @@ const fs = require('fs-extra'); const path = require('path'); const shell = require('shelljs'); +const getBaseUrl = require('../utils/getBaseUrl'); const watchDir = require('../utils/watch-dir'); const { copyDirSync } = require('../utils/copy'); @@ -36,9 +37,7 @@ module.exports = async (_on, config) => { watchDir(config.integrationFolder, duplicateTests); } - const baseUrl = packageConfig.port - ? `http://localhost:${packageConfig.port}` - : 'http://localhost'; + const baseUrl = getBaseUrl(packageConfig); return { ...config, diff --git a/lib/modules/createConfig.js b/lib/modules/createConfig.js index 27f354f..ce4136c 100644 --- a/lib/modules/createConfig.js +++ b/lib/modules/createConfig.js @@ -4,6 +4,8 @@ const glob = require('glob'); const { get, defaults } = require('lodash'); const Ajv = require('ajv'); +const getBaseUrl = require('../utils/getBaseUrl'); + const createDefaultSeeds = require('./createDefaultSeeds'); const getThemeAndPluginData = require('./getThemeAndPluginData'); const renderTemplate = require('./renderTemplate'); @@ -49,7 +51,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { } if (!config.url) { - config.url = config.port ? `http://localhost:${config.port}` : 'http://localhost'; + config.url = getBaseUrl(config); } let volumes = []; @@ -104,6 +106,13 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { `${packageDir}/plugin:/var/www/html/wp-content/plugins/wp-cypress`, ); + if (config.sslCertificate) { + volumes.push( + `${CWD}/${config.sslCertificate.cert}:/var/www/html/localhost.pem`, + `${CWD}/${config.sslCertificate.key}:/var/www/html/localhost.key`, + ); + } + if (config.configFile) { volumes.push(`${path.resolve(config.configFile)}:/var/www/html/wp-cypress-config.php`); } @@ -123,6 +132,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { { port: config.port || 80, dbPort: config.dbPort || 3306, + sslPort: config.sslPort || 4433, volumes: hasVolumeSupport ? volumes : false, }, ); @@ -138,6 +148,7 @@ const createConfig = async (packageDir, hasVolumeSupport = true) => { await renderTemplate(`${packageDir}/lib/templates/dockerfile.ejs`, `${packageDir}/Dockerfile`, { isWpContent: config.wpContent, versions: validVersions, + sslCertificate: config.sslCertificate, vip: config.muPlugins ? config.muPlugins.vip : false, phpVersion: config.phpVersion || 7.4, phpMemoryLimit: config.phpMemoryLimit || '128M', diff --git a/lib/schemas/config-validation.json b/lib/schemas/config-validation.json index a9079e3..db3964a 100644 --- a/lib/schemas/config-validation.json +++ b/lib/schemas/config-validation.json @@ -32,6 +32,23 @@ "type": "number", "errorMessage": "wp.port is not a number" }, + "sslCertificate": { + "type": "object", + "properties": { + "cert": { + "type": "string", + "errorMessage": "wp.sslCertificate.cert is not a string" + }, + "key": { + "type": "string", + "errorMessage": "wp.sslCertificate.key is not a string" + } + } + }, + "sslPort": { + "type": "number", + "errorMessage": "wp.sslPort is not a number" + }, "phpVersion": { "type": "number", "errorMessage": "wp.phpVersion is not a number" diff --git a/lib/templates/docker-compose.ejs b/lib/templates/docker-compose.ejs index 80eb3b5..5c6915a 100644 --- a/lib/templates/docker-compose.ejs +++ b/lib/templates/docker-compose.ejs @@ -6,6 +6,7 @@ services: build: . ports: - <%= port %>:80 + - <%= sslPort %>:443 <% if (volumes) { %>volumes: <% volumes.forEach((volume) => { %> - <%= volume %> <% }); } %> db: diff --git a/lib/templates/dockerfile.ejs b/lib/templates/dockerfile.ejs index 58c5007..a5013ed 100644 --- a/lib/templates/dockerfile.ejs +++ b/lib/templates/dockerfile.ejs @@ -6,6 +6,13 @@ RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg defau RUN a2enmod rewrite +<% if (sslCertificate) { %> + RUN a2enmod ssl + RUN sed -i 's/SSLCertificateFile.*snakeoil\.pem/SSLCertificateFile \/var\/www\/html\/localhost.pem/' /etc/apache2/sites-available/default-ssl.conf + RUN sed -i 's/SSLCertificateKeyFile.*snakeoil\.key/SSLCertificateKeyFile \/var\/www\/html\/localhost.key/' /etc/apache2/sites-available/default-ssl.conf + RUN a2ensite default-ssl +<% } %> + COPY update.sh /var/www/html ENV PHP_MEMORY_LIMIT=<%= phpMemoryLimit %> @@ -38,5 +45,6 @@ RUN curl https://github.com/Automattic/vip-go-mu-plugins-built/archive/master.zi <% } %> EXPOSE 80 +EXPOSE 443 CMD ["apache2-foreground"] diff --git a/lib/utils/__tests__/getBaseUrl.js b/lib/utils/__tests__/getBaseUrl.js new file mode 100644 index 0000000..d3a2770 --- /dev/null +++ b/lib/utils/__tests__/getBaseUrl.js @@ -0,0 +1,42 @@ +const getBaseUrl = require('../getBaseUrl'); + +describe('getBaseUrl', () => { + test('should be a function.', () => { + expect(typeof getBaseUrl).toBe('function'); + }); + test('should use default port if none provided', () => { + const config = {}; + expect(getBaseUrl(config)).toBe('http://localhost'); + }); + test('should use provided port number', () => { + const port = 8080; + const config = { + port, + }; + expect(getBaseUrl(config)).toBe(`http://localhost:${port}`); + }); + test('should use default SSL port if sslCertificate is provided but sslPort is not provided', () => { + const port = 8080; + const config = { + sslCertificate: { + cert: './localhost.pem', + key: './localhost-key.pem', + }, + port, + }; + expect(getBaseUrl(config)).toBe('https://localhost:4433'); + }); + test('should use provided SSL port number if sslCertificate and sslPort is provided', () => { + const port = 8080; + const sslPort = 3433; + const config = { + sslCertificate: { + cert: './localhost.pem', + key: './localhost-key.pem', + }, + port, + sslPort, + }; + expect(getBaseUrl(config)).toBe(`https://localhost:${sslPort}`); + }); +}); diff --git a/lib/utils/getBaseUrl.js b/lib/utils/getBaseUrl.js new file mode 100644 index 0000000..5b12659 --- /dev/null +++ b/lib/utils/getBaseUrl.js @@ -0,0 +1,14 @@ +/** + * Uses the Cypress config option to return the correct base URL. + * + * @param {object} config the Cypress config object + * @returns the base URL + */ +const getBaseUrl = (config) => { + if (config.sslCertificate) { + return config.sslPort ? `https://localhost:${config.sslPort}` : 'https://localhost:4433'; + } + return config.port ? `http://localhost:${config.port}` : 'http://localhost'; +}; + +module.exports = getBaseUrl;