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

Cypress tests: preset the admin API key to a static value #3358

Merged
merged 6 commits into from
Jan 28, 2019
Merged
Changes from 1 commit
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
67 changes: 41 additions & 26 deletions cypress/cypress.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
/* eslint-disable import/no-extraneous-dependencies, no-console */
const atob = require('atob');
const { execSync } = require('child_process');
const { post } = require('request').defaults({ jar: true });
const { seedData } = require('./seed-data');
const atob = require("atob");
const { execSync } = require("child_process");
const { post } = require("request").defaults({ jar: true });
const { seedData } = require("./seed-data");

const baseUrl = process.env.CYPRESS_baseUrl || 'http://localhost:5000';
const baseUrl = process.env.CYPRESS_baseUrl || "http://localhost:5000";

function seedDatabase(seedValues) {
const request = seedValues.shift();
const data = request.type === 'form' ? { formData: request.data } : { json: request.data };
const data =
request.type === "form"
? { formData: request.data }
: { json: request.data };

post(baseUrl + request.route, data, (err, response) => {
const result = response ? response.statusCode : err;
console.log('POST ' + request.route + ' - ' + result);
console.log("POST " + request.route + " - " + result);
if (seedValues.length) {
seedDatabase(seedValues);
}
});

// Make sure the admin user has the same API key on every execution
execSync(
"docker-compose -p cypress run postgres psql -h postgres -c \"update users set api_key = 'secret' where email ='admin@redash.io';\""
);
Copy link
Member Author

@arikfr arikfr Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual change. The rest is Prettier doing its thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! That was one of the solutions I thought about (the other one would require me to use Cypress Stubs).

My idea is to create a few Cypress tests in #3354, probably one for regenerating the API Key. If needed in there, I'll revisit this.

BTW, I thought the pattern was to use singlequoted 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I thought the pattern was to use singlequoted 😅

🤔 you're correct. Something went wrong there. I'll fix.

}

function startServer() {
console.log('Starting the server...');
console.log("Starting the server...");

execSync('docker-compose -p cypress build --build-arg skip_ds_deps=true', { stdio: 'inherit' });
execSync('docker-compose -p cypress up -d', { stdio: 'inherit' });
execSync('docker-compose -p cypress run server create_db', { stdio: 'inherit' });
execSync("docker-compose -p cypress build --build-arg skip_ds_deps=true", {
stdio: "inherit"
});
execSync("docker-compose -p cypress up -d", { stdio: "inherit" });
execSync("docker-compose -p cypress run server create_db", {
stdio: "inherit"
});
}

function stopServer() {
console.log('Stopping the server...');
execSync('docker-compose -p cypress down', { stdio: 'inherit' });
console.log("Stopping the server...");
execSync("docker-compose -p cypress down", { stdio: "inherit" });
}

function runCypressCI() {
if (process.env.PERCY_TOKEN_ENCODED) {
process.env.PERCY_TOKEN = atob(`${process.env.PERCY_TOKEN_ENCODED}`);
}
execSync('docker-compose run cypress ./node_modules/.bin/percy exec -- ./node_modules/.bin/cypress run --browser chrome', { stdio: 'inherit' });
execSync(
"docker-compose run cypress ./node_modules/.bin/percy exec -- ./node_modules/.bin/cypress run --browser chrome",
{ stdio: "inherit" }
);
}

const command = process.argv[2] || 'all';
const command = process.argv[2] || "all";

switch (command) {
case 'start':
case "start":
startServer();
break;
case 'db-seed':
case "db-seed":
seedDatabase(seedData);
break;
case 'run':
execSync('cypress run --browser chrome', { stdio: 'inherit' });
case "run":
execSync("cypress run --browser chrome", { stdio: "inherit" });
break;
case 'open':
execSync('cypress open', { stdio: 'inherit' });
case "open":
execSync("cypress open", { stdio: "inherit" });
break;
case 'run-ci':
case "run-ci":
runCypressCI();
break;
case 'stop':
case "stop":
stopServer();
break;
case 'all':
case "all":
startServer();
seedDatabase(seedData);
execSync('cypress run --browser chrome', { stdio: 'inherit' });
execSync("cypress run --browser chrome", { stdio: "inherit" });
stopServer();
break;
default:
console.log('Usage: npm run cypress [start|db-seed|open|run|stop]');
console.log("Usage: npm run cypress [start|db-seed|open|run|stop]");
break;
}