Skip to content

Commit

Permalink
Merge pull request #1658 from compdemocracy/1392/fix-cypress-tests
Browse files Browse the repository at this point in the history
fix cypress tests
  • Loading branch information
ballPointPenguin authored Mar 26, 2023
2 parents 764461c + 5153ba3 commit 18a0e48
Show file tree
Hide file tree
Showing 67 changed files with 1,955 additions and 5,698 deletions.
119 changes: 18 additions & 101 deletions .github/workflows/cypress-tests.yml
Original file line number Diff line number Diff line change
@@ -1,115 +1,32 @@
# Changes to workflow name require changes to badge URL in README.md
name: E2E Tests

concurrency:
group: ${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
- dev
paths-ignore:
- "**.md"
branches: [edge]
paths-ignore: ["**.md"]

pull_request:
paths-ignore:
- "**.md"
paths-ignore: ["**.md"]

jobs:
cypress-run:
# Platform recommended in cypress-io/github-action docs.
runs-on: ubuntu-20.04
env:
# Use native docker command within docker-compose
COMPOSE_DOCKER_CLI_BUILD: 1
# Use buildkit to speed up docker command
DOCKER_BUILDKIT: 1
runs-on: ubuntu-latest
steps:
# This ensures only the most recently push commit will keep running.
- name: Cancel other active runs of this workflow
uses: rokroskar/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout
uses: actions/[email protected]

# This step helps us know when to skip future steps, particularly if
# we're on a fork where upstream secrets won't be available.
- name: Ensure secrets available
- uses: actions/checkout@v3
- uses: isbang/[email protected]
env:
GOOGLE_CREDENTIALS_BASE64: ${{ secrets.GOOGLE_CREDENTIALS_BASE64 }}
run: |
if [ -n "$GOOGLE_CREDENTIALS_BASE64" ]; then
echo "DO_COMMENT_TRANSLATION=true" >> $GITHUB_ENV
fi
- name: Enable comment translation
if: env.DO_COMMENT_TRANSLATION
run: |
echo GOOGLE_CREDENTIALS_BASE64=${{ secrets.GOOGLE_CREDENTIALS_BASE64 }} >> server/docker-dev.env
echo SHOULD_USE_TRANSLATION_API=true >> server/docker-dev.env
- name: Set server configuration
run: |
# Test email transport failovers
# mailgun: unconfigured transport (will fail)
# aws-ses: unconfigured transport (will fail)
# nonexistent: nonexistent transport (will fail)
# maildev: catch-all dev email server running in container (will work)
echo EMAIL_TRANSPORT_TYPES=mailgun,aws-ses,nonexistent,maildev >> server/docker-dev.env
- name: Ensure embed test html is served
run: cp e2e/cypress/fixtures/html/embed.html client-admin/public/embed.html

- name: Serve app via docker-compose
run: docker compose up --detach

- name: "Run Cypress tests: default"
uses: cypress-io/github-action@v2
env:
CYPRESS_BASE_URL: https://localhost
with:
working-directory: ./e2e
# Ignore all tests with pattern *.secrets.spec.js
spec: "**/polis/**/!(*.secrets).spec.js"
browser: chrome

- name: "Run Cypress tests: comment translation"
if: env.DO_COMMENT_TRANSLATION
uses: cypress-io/github-action@v2
env:
CYPRESS_BASE_URL: https://localhost
compose-file: |
docker-compose.yml
docker-compose.test.yml
compose-flags: "--env-file test.env"
- uses: cypress-io/[email protected]
with:
install: false
working-directory: ./e2e
spec: "**/comment-translation.secrets.spec.js"
# Prevent cypress from discarding screenshots/videos from default test run.
config: "trashAssetsBeforeRuns=false"
browser: chrome

# NOTE: screenshots will be generated only if E2E test failed
# thus we store screenshots only on failures
- name: Save screenshot artefacts
uses: actions/[email protected]
if: failure()
with:
name: cypress-screenshots
path: e2e/cypress/screenshots

# Test run video was always captured, so this action uses "always()" condition
- name: Save video artefacts
uses: actions/[email protected]
if: always()
with:
name: cypress-videos
path: e2e/cypress/videos

- name: Check status of containers
if: failure()
run: docker compose ps

- name: 'Check logs: server'
if: failure()
run: docker compose logs server

- name: 'Check logs: nginx-proxy'
if: failure()
run: docker compose logs nginx-proxy
config: baseUrl=http://localhost
spec: cypress/e2e/!(third-party)/**/*.cy.js
working-directory: e2e
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## TTD:
# make start; make stop
# make PROD start; make PROD stop
# make TEST start; make TEST stop
# update TAG

SHELL=/bin/bash
Expand All @@ -17,6 +18,11 @@ PROD: ## Run in prod mode (e.g. `make PROD start`, etc.)
$(eval TAG = $(shell grep -e ^TAG ${ENV_FILE} | awk -F'[=]' '{gsub(/ /,"");print $$2}'))
$(eval COMPOSE_FILE_ARGS = -f docker-compose.yml)

TEST: ## Run in test mode (e.g. `make TEST start`, etc.)
$(eval ENV_FILE = test.env)
$(eval TAG = $(shell grep -e ^TAG ${ENV_FILE} | awk -F'[=]' '{gsub(/ /,"");print $$2}'))
$(eval COMPOSE_FILE_ARGS = -f docker-compose.yml -f docker-compose.test.yml)

echo_vars:
@echo ENV_FILE=${ENV_FILE}
@echo TAG=${TAG}
Expand Down Expand Up @@ -48,6 +54,15 @@ rm-ALL: rm-containers rm-volumes rm-images ## Remove Docker containers, volumes,
hash: ## Show current short hash
@echo Git hash: ${GIT_HASH}

build: echo_vars ## [Re]Build all Docker containers
docker compose ${COMPOSE_FILE_ARGS} --env-file ${ENV_FILE} build

build-no-cache: echo_vars ## Build all Docker containers without cache
docker compose ${COMPOSE_FILE_ARGS} --env-file ${ENV_FILE} build --no-cache

start-recreate: echo_vars ## Start all Docker containers with recreated environments
docker compose ${COMPOSE_FILE_ARGS} --env-file ${ENV_FILE} up --force-recreate

start-rebuild: echo_vars ## Start all Docker containers, [re]building as needed
docker compose ${COMPOSE_FILE_ARGS} --env-file ${ENV_FILE} up --build

Expand Down Expand Up @@ -87,8 +102,9 @@ rbs: start-rebuild
%:
@true

.PHONY: help pull start stop rm-containers rm-volumes rm-images rm-ALL hash start-rebuild restart-FULL-REBUILD \
e2e-install e2e-prepare e2e-run-minimal e2e-run-standalone e2e-run-secret e2e-run-subset e2e-run-all
.PHONY: help pull start stop rm-containers rm-volumes rm-images rm-ALL hash build-no-cache start-rebuild \
start-recreate restart-FULL-REBUILD e2e-install e2e-prepare e2e-run-minimal e2e-run-standalone e2e-run-secret \
e2e-run-subset e2e-run-all

help:
@echo 'Usage: make <command>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ShareAndEmbed extends React.Component {
{" class='polis'"}
{" data-conversation_id='" + match.params.conversation_id + "'>"}
{'</div>\n'}
{"<script async src='https://pol.is/embed.js'></script>"}
{"<script async src='" + Url.urlPrefix + "embed.js'></script>"}
</pre>
</div>
<p>
Expand Down
25 changes: 13 additions & 12 deletions client-admin/src/components/conversations-and-account/integrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Box, Heading, Text } from 'theme-ui'
import Url from '../../util/url'

@connect((state) => state.user)
class Integrate extends React.Component {
render() {
const userSiteId = this.props.user === null
? '__loading, try refreshing__'
: this.props.user.site_ids[0];

return (
<Box>
<Box>
Expand Down Expand Up @@ -44,18 +49,14 @@ class Integrate extends React.Component {
</ul>
<Box>
<pre>
{'<div\n'}
{" class='polis'\n"}
{" data-page_id='PAGE_ID'\n"}
{" data-site_id='"}
{this.props.user === null
? '__loading, try refreshing__'
: this.props.user.site_ids[0]}
{"'>\n"}
{'</div>\n'}
{'<script async\n'}
{" src='https://pol.is/embed.js'>\n"}
{'</script>'}
{`
<div
class="polis"
data-page_id="PAGE_ID"
data-site_id="${userSiteId}">
</div>
<script async src="${Url.urlPrefix}embed.js"></script>
`}
</pre>
</Box>
</Box>
Expand Down
12 changes: 2 additions & 10 deletions client-admin/src/util/url.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

// build may prepend 'devWithPreprod'

let urlPrefix = document.location.protocol + '//' + document.location.hostname

if (document.location.port) {
urlPrefix = urlPrefix + ':' + document.location.port
}

urlPrefix = urlPrefix + '/'
const urlPrefix = `${document.location.protocol}//${document.location.host}/`

export default {
urlPrefix: urlPrefix
urlPrefix,
}
2 changes: 2 additions & 0 deletions client-participation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

FROM docker.io/node:18-alpine

ARG EMBED_SERVICE_HOSTNAME
ARG FB_APP_ID
ARG GA_TRACKING_ID
ENV EMBED_SERVICE_HOSTNAME ${EMBED_SERVICE_HOSTNAME}
ENV FB_APP_ID ${FB_APP_ID}
ENV GA_TRACKING_ID ${GA_TRACKING_ID}

Expand Down
11 changes: 6 additions & 5 deletions client-participation/api/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var firstRun = !window.polis._hasRun;
polis._hasRun = 1;
var iframes = [];
var polisUrl = "https://<%= polisHostName %>";
var serviceUrl = window.location.protocol + "//" + "<%= embedServiceHostname %>";
var maxHeightsSeen = {};

polis.on = polis.on || {};
Expand Down Expand Up @@ -119,7 +119,7 @@
alert("Error: need data-conversation_id or data-site_id");
return;
}
var src = polisUrl+ "/" + path.join("/");
var src = serviceUrl+ "/" + path.join("/");

appendIfPresent("parent_url");
if (o.parent_url) {
Expand Down Expand Up @@ -170,6 +170,7 @@
iframe.style.backgroundColor = "white";
// iframe.style.backgroundColor = "rgb(247, 247, 247)";
iframe.id = id;
iframe.setAttribute('data-test-id', 'polis-iframe');
parent.appendChild(iframe);
iframes.push(iframe);
}
Expand Down Expand Up @@ -212,7 +213,7 @@
window.addEventListener("message", function(event) {
var data = event.data||{};
var domain = event.origin.replace(/^https?:\/\//,'');
if (!domain.match(/(^|\.)<%= polisHostName %>$/)) {
if (!domain.match(/(^|\.)<%= embedServiceHostname %>$/)) {
return;
}

Expand All @@ -232,13 +233,13 @@
}

if (data === "cookieRedirect" && cookiesEnabledAtTopLevel()) {// // temporarily redirect to polis, which will set a cookie and redirect back
window.location = polisUrl + "/api/v3/launchPrep?dest=" + encodeReturnUrl(window.location+"");
window.location = serviceUrl + "/api/v3/launchPrep?dest=" + encodeReturnUrl(window.location+"");
}
// if (data === "twitterConnectBegin") {
// // open a new window where the twitter auth screen will show.
// // that window will redirect back to a simple page that calls window.opener.twitterStatus("ok")
// var params = 'location=0,status=0,width=800,height=400';
// twitterWindow = window.open(polisUrl + "/api/v3/twitterBtn?dest=" + encodeReturnUrl(window.location+""), 'twitterWindow', params);
// twitterWindow = window.open(serviceUrl + "/api/v3/twitterBtn?dest=" + encodeReturnUrl(window.location+""), 'twitterWindow', params);
// }

if (data.name === "resize") {
Expand Down
3 changes: 3 additions & 0 deletions client-participation/js/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ preloadHelper.acceptLanguagePromise.then(function() {
else if (languageCode.match(/^ru/)) {
_.extend(strings, translations.ru);
}
else if (languageCode.match(/^hr/)) {
_.extend(strings, translations.hr);
}
});
});

Expand Down
4 changes: 2 additions & 2 deletions client-participation/js/templates/participation.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
class="HeadingA"
style="
margin-bottom: 10px;
"
"
dir="auto">
{{topic}}
{{#unless is_active}}
Expand Down Expand Up @@ -437,7 +437,7 @@
{{! ------------ BEGIN FOOTER --------------- }}
{{#unless no_footer}}

<div style="margin-top: 40px; margin-bottom: 20px;">
<div style="margin-top: 40px; margin-bottom: 20px;" data-test-footer>
<a href="https://pol.is" target="_blank" dir="auto">
{{> logo}}
</a>
Expand Down
14 changes: 3 additions & 11 deletions client-participation/js/util/url.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

// build may prepend 'devWithPreprod'

let urlPrefix = document.location.protocol + '//' + document.location.hostname

if (document.location.port) {
urlPrefix = urlPrefix + ':' + document.location.port
}

urlPrefix = urlPrefix + '/'
const urlPrefix = `${document.location.protocol}//${document.location.host}/`;

module.exports = {
urlPrefix: urlPrefix,
}
urlPrefix,
};
2 changes: 1 addition & 1 deletion client-participation/js/views/conversationTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports = Handlebones.ModelView.extend({
c.showMajorityTab = this.showMajorityTab;
c.selfUrl = null;
if (Utils.isInIframe()) {
c.selfUrl = (document.location + "").replace("embed.pol.is", "pol.is");
c.selfUrl = (document.location + "").replace("embed.", "");
}
return c;
},
Expand Down
4 changes: 2 additions & 2 deletions client-participation/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fs = require('fs')
const pkg = require('./package.json')
const TerserPlugin = require("terser-webpack-plugin")

const embedServiceHostname = process.env.EMBED_SERVICE_HOSTNAME;
const embedServiceHostname = process.env.EMBED_SERVICE_HOSTNAME || 'pol.is';
const fbAppId = process.env.FB_APP_ID;
const gaTrackingId = process.env.GA_TRACKING_ID;
const outputDirectory = 'dist'
Expand Down Expand Up @@ -115,7 +115,7 @@ module.exports = (env, options) => {
{
from: 'api/embed.js',
transform(content, absoluteFrom) {
return lodashTemplate(content.toString())({ polisHostName: embedServiceHostname })
return lodashTemplate(content.toString())({ embedServiceHostname })
}
},
{ from: 'node_modules/font-awesome/fonts/**/*', to: './fonts/[name][ext]' }
Expand Down
Loading

0 comments on commit 18a0e48

Please sign in to comment.