Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSL/HTTPS to WP-Cypress #104

Open
wants to merge 7 commits into
base: release/1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/cypress-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion lib/modules/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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`);
}
Expand All @@ -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,
},
);
Expand All @@ -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',
Expand Down
17 changes: 17 additions & 0 deletions lib/schemas/config-validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/templates/docker-compose.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
build: .
ports:
- <%= port %>:80
- <%= sslPort %>:443
<% if (volumes) { %>volumes: <% volumes.forEach((volume) => { %>
- <%= volume %> <% }); } %>
db:
Expand Down
8 changes: 8 additions & 0 deletions lib/templates/dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down Expand Up @@ -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"]
42 changes: 42 additions & 0 deletions lib/utils/__tests__/getBaseUrl.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
});
14 changes: 14 additions & 0 deletions lib/utils/getBaseUrl.js
Original file line number Diff line number Diff line change
@@ -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;