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

Updates cypress version and ports in api token #1502

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ secrets.json
e2e/cypress/videos/*
e2e/cypress/screenshots/*
e2e/cypress/results/output.xml
e2e/cypress/fixtures/api_token.json
node_modules
package-lock.json

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ clean:

cypress:
# Turn on local system, and open cypress in interactive mode
# If you haven't remapped localhost > ckan, you should change baseURL to "http://localhost:5000" in `e2e/cypress.json`
docker compose up -d && cd e2e && CYPRESS_USER=admin CYPRESS_USER_PASSWORD=password npx [email protected] open
# If you haven't remapped localhost > ckan, you should change baseURL to "http://localhost:5000" in `e2e/cypress.config.js`
# If you get receive "Error: Cannot find module 'cypress'", run `npm i` in the root directory to install the cypress binary
docker compose up -d && cd e2e && CYPRESS_USER=admin CYPRESS_USER_PASSWORD=password npx [email protected] open

dev:
docker build -t ghcr.io/gsa/catalog.data.gov:latest ckan/
Expand All @@ -42,8 +43,7 @@ update-dependencies:
# Test commands
# ###############################################

test: build
# docker build -t ghcr.io/gsa/catalog.data.gov:latest ckan/
test: clean build
docker compose -f docker-compose.yml -f docker-compose.test.yml up --abort-on-container-exit test

# everytime you added some new variables, you need to swap it with some test values
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# run with make test
version: '3'
services:
test:
image: cypress/included:12.9.0
image: cypress/included:13.16.1
entrypoint: []
command: /bin/bash -c "npx wait-on http://ckan:5000/dataset && cypress run"
# To run specific test, use the following syntax:
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
ckan:
image: ghcr.io/gsa/catalog.data.gov:latest
Expand Down
4 changes: 2 additions & 2 deletions e2e/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'http://ckan:5000',
specPattern: 'cypress/integration/*.cy.js'
specPattern: 'cypress/integration/*.cy.js',
experimentalRunAllSpecs: true
},
videoCompression: false,
videoUploadOnPasses: false,
screenshotOnRunFailure: false,
retries: {
runMode: 2,
Expand Down
3 changes: 1 addition & 2 deletions e2e/cypress/integration/000_harvest.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ describe('Harvest', { testIsolation: false }, () => {
* Login as cypress user and create an organization for testing harvest source creation and running the jobs
*/
cy.login();
// Make sure organization does not exist before creating
cy.delete_organization(harvestOrg);
cy.make_api_token();
// Create the organization
cy.visit('/organization');
cy.get('a[class="btn btn-primary"]').click();
Expand Down
2 changes: 1 addition & 1 deletion e2e/cypress/integration/login.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('Login', () => {
it('Invalid user login attempt', () => {
cy.visit('/dataset');
cy.get('a[href="/user/login"]').click();
cy.login('not-user', 'not-password', true);
cy.login('not-user', 'not-password');
cy.get('.flash-messages .alert').should('contain', 'Login failed. Bad username or password.');
});

Expand Down
14 changes: 8 additions & 6 deletions e2e/cypress/integration/spatial.cy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const group = {
name: 'climate',
desc: 'Climate Group'
}
describe('Spatial', { testIsolation: false }, () => {
before(() => {
cy.login();
cy.create_group(group.name, group.desc);
});

after(() => {
cy.delete_group('climate');
cy.delete_group(group.name);
cy.logout();
});

Expand Down Expand Up @@ -43,19 +48,16 @@ describe('Spatial', { testIsolation: false }, () => {
});

it('Can put a package with weird tags in an group', () => {
const group_name = 'climate';
cy.delete_group(group_name);
cy.create_group(group_name, 'Climate Group');
cy.request({
url: '/api/action/package_patch',
method: 'POST',
body: {
id: 'nefsc-2000-spring-bottom-trawl-survey-al0002-ek500',
groups: [{ name: group_name }],
groups: [{ name: group.name }],
},
}).should((response) => {
expect(response.body).to.have.property('success', true);
expect(response.body.result.groups[0]).to.have.property('name', 'climate');
expect(response.body.result.groups[0]).to.have.property('name', group.name);
});
});

Expand Down
5 changes: 4 additions & 1 deletion e2e/cypress/integration/zzz_cleanup.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ describe('Cleanup site', () => {

// Make sure DB is fully cleared
cy.wait(3000);

// Remove organization
cy.delete_organization(harvestOrg);
});

after(() => {
cy.logout();
});

it('Confirms empty site', () => {
cy.visit('/dataset');
cy.contains('No datasets found');
Expand Down
22 changes: 22 additions & 0 deletions e2e/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
111 changes: 75 additions & 36 deletions e2e/cypress/support/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,34 @@ Cypress.Commands.add('check_dataset_harvested', (retries) => {
}
});
});
Cypress.Commands.add('make_api_token', (userName = "admin") => {
// create an API token named 'cypress token'
cy.visit('/user/' + userName + '/api-tokens');

Cypress.Commands.add('login', (userName, password, loginTest) => {
cy.get('body').then($body => {
if ($body.text().includes('cypress token')) {
cy.log('cypress token exists. skipping token creation.');
} else {
cy.get('#name').type('cypress token');
cy.get('button[value="create"]').click();
// After creating, find the token in <code> tag and save it for later use
cy.get('div.alert-success code').invoke('text').then((text) => {
cy.writeFile('cypress/fixtures/api_token.json', { api_token: text });
});
cy.log('cypress token created.');
}
});
});

Cypress.Commands.add('login', (userName, password) => {
/**
* Method to fill and submit the CKAN Login form
* :PARAM userName String: user name of that will be attempting to login
* :PARAM password String: password for the user logging in
* :RETURN null:
*/
if (!loginTest) {
cy.visit('/user/login');
}
cy.visit('/user/login');

if (!userName) {
userName = Cypress.env('USER');
cy.log(userName, process.env);
Expand Down Expand Up @@ -139,7 +156,6 @@ Cypress.Commands.add('delete_group', (groupName) => {
cy.request({
url: '/api/action/group_purge',
method: 'POST',
failOnStatusCode: false,
body: {
id: groupName,
},
Expand All @@ -152,22 +168,34 @@ Cypress.Commands.add('delete_organization', (orgName) => {
* :PARAM orgName String: Name of the organization to purge from the current state
* :RETURN null:
*/
cy.request({
url: '/api/action/organization_delete',
method: 'POST',
failOnStatusCode: false,
body: {
id: orgName,
},
});
cy.request({
url: '/api/action/organization_purge',
method: 'POST',
failOnStatusCode: false,
body: {
id: orgName,
},
cy.fixture('api_token').then((data) => {
cy.request({
url: '/api/action/organization_delete',
method: 'POST',
headers: {
'Authorization': data.api_token,
'Content-Type': 'application/json'
},
body: {
id: orgName ? orgName : 'test-organization'
},
});

cy.request({
url: '/api/action/organization_purge',
method: 'POST',
headers: {
'Authorization': data.api_token,
'Content-Type': 'application/json'
},
body: {
id: orgName ? orgName : 'test-organization'
},
});

});


});

Cypress.Commands.add('delete_dataset', (datasetName) => {
Expand All @@ -176,12 +204,19 @@ Cypress.Commands.add('delete_dataset', (datasetName) => {
* :PARAM datasetName String: Name of the dataset to purge from the current state
* :RETURN null:
*/
cy.request({
url: '/api/action/dataset_purge',
method: 'POST',
body: {
id: datasetName,
},
// if no datasetName is provided, use the default test-dataset
cy.fixture('api_token').then((data) => {
cy.request({
url: '/api/action/dataset_purge',
method: 'POST',
headers: {
'Authorization': data.api_token,
'Content-Type': 'application/json'
},
body: {
id: datasetName ? datasetName : 'test-dataset'
},
});
});
});

Expand Down Expand Up @@ -257,17 +292,21 @@ Cypress.Commands.add('start_harvest_job', (harvestName) => {
});

Cypress.Commands.add('create_dataset', (ckan_dataset) => {
var options = {
method: 'POST',
url: '/api/3/action/package_create',
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json',
},
body: JSON.stringify(ckan_dataset),
};

return cy.request(options);
cy.fixture('api_token').then((token_data) => {
cy.fixture('ckan_dataset').then((dataset) => {
cy.request({
url: '/api/3/action/package_create',
method: 'POST',
headers: {
'Authorization': token_data.api_token,
'Content-Type': 'application/json'
},
body: ckan_dataset ? JSON.stringify(ckan_dataset) : dataset
});
});
});

});

// Performs an XMLHttpRequest instead of a cy.request (able to send data as FormData - multipart/form-data)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"cypress": "^13.16.1"
}
}
Loading