Skip to content

Commit

Permalink
setting up database cleanup for cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MwayaB committed Apr 12, 2022
1 parent 71e6191 commit ce29c81
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"devDependencies": {
"cypress": "^9.5.3",
"husky": "^7.0.4",
"lerna": "^4.0.0"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const environment = process.env.NODE_ENV || 'development';
const promiseRouter = require('express-promise-router');
const { Model } = require('objection');
const checkJwt = require('./middleware/acl/checkJwt');
const { tableCleanup } = require('../tests/testEnvironment');
const cors = require('cors');

// initialize knex
Expand Down Expand Up @@ -58,6 +59,7 @@ const rolesRoutes = require('./routes/rolesRoute');
const organicCertifierSurveyRoutes = require('./routes/organicCertifierSurveyRoute');
const passwordResetRoutes = require('./routes/passwordResetRoute.js');
const showedSpotlightRoutes = require('./routes/showedSpotlightRoute.js');
const testDataRoute = require('./routes/testDataRoute.js');

const waterBalanceScheduler = require('./jobs/waterBalance/waterBalance');
const nitrogenBalanceScheduler = require('./jobs/nitrogenBalance/nitrogenBalance');
Expand All @@ -70,14 +72,16 @@ const documentRoute = require('./routes/documentRoute');
const taskRoute = require('./routes/taskRoute');
const productRoute = require('./routes/productRoute');
const notificationUserRoute = require('./routes/notificationUserRoute');
const { use } = require('chai');

// register API
const router = promiseRouter();

app.get('/', (req, res) => {
app.get('/', async (req, res) => {
res.sendStatus(200);
});


app.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))

Expand All @@ -99,6 +103,8 @@ app.use(bodyParser.json())
.set('json spaces', 2)
.use('/login', loginRoutes)
.use('/password_reset', passwordResetRoutes)
//test data route
.use('/testData', testDataRoute)
// ACL middleware
.use(checkJwt)

Expand Down Expand Up @@ -140,6 +146,8 @@ app.use(bodyParser.json())
.use('/task', taskRoute)
.use('/product', productRoute)
.use('/notification_user', notificationUserRoute)



// handle errors
.use((req, res, next) => {
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/cypress.env.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"auth_password": "P4ssword!",
"auth_user_id": "auth0|5d93178e8a968e0e14cd1573",
"user_farm_id": "16ce7028-e42b-11e9-9717-9bd9186e761c",
"apiUrl":"http://localhost:5001",
"REACT_APP_GOOGLE_CLIENTID": "1062873406351-nfrri5bpk5bj9gek9qidtmv2l161uj1c.apps.googleusercontent.com",
"REACT_APP_GOOGLE_CLIENT_SECRET": "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",
"GOOGLE_REFRESH_TOKEN": "1//047GYoNbAAPrRCgYIARAAGAQSNwF-L9IrixqUUiJJSkGpnRVu2YTrbHx5AsOnalIbFcy0fzdY5JwTcVAd48zvLgPSGvqK39z5hb8"
Expand Down
72 changes: 36 additions & 36 deletions packages/webapp/cypress/integration/loginForm.spec.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
describe.only('The login flow and user creation flow', () => {
before(() => {

cy.task("db:tableCleanup");

})

it('should ensure login page loads correctly', () => {

cy.visit('/');
cy.get('[data-cy=email]').should('exist');
cy.get('[data-cy=continue]').should('exist');
cy.get('[data-cy=continue]').should('be.disabled');
cy.get('[data-cy=continueGoogle]').should('exist');


//get users.json fixtures file
cy.fixture('users').as('myUserFixture');

//create test data
const email = '[email protected]';
const fullName = 'Test User';
const password = 'P@ssword123';
const farmName = 'UBC FARM';
const location = '49.250833,-123.2410777';
//type email from the first user in users.json file into email input
cy.get('@myUserFixture').then((user) => {
cy.get('[data-cy=email]').type(user[0].email).then(()=>{
//check that the continue button is enabled
cy.contains('Continue').should('exist').should('be.enabled').click();
});
});



cy.get('[data-cy=email]').type(email);
//check that the continue button is enabled
cy.contains('Continue').should('exist').and('be.enabled').click();

//check you are on the create user account page
cy.contains('Create new user account').should('exist');

//type a fullName
cy.fixture('users').as('myUserFixture');
cy.get('@myUserFixture').then((user) => {
cy.get('[data-cy=createUser-fullName]').type(user[0].name);
});
cy.get('[data-cy=createUser-fullName]').type(fullName);

//type a password
cy.get('[data-cy=createUser-password]').type('P@ssword123');
cy.get('[data-cy=createUser-password]').type(password);
//click create account
cy.contains('Create Account').should('exist').should('be.enabled').click();
cy.contains('Create Account').should('exist').and('be.enabled').click();

cy.contains('started').should('exist');
cy.pause();


// Get Started page
cy.get('[data-cy=getStarted]').should('exist');
cy.get('[data-cy=getStarted]').click();
// Step 1
cy.get('[data-cy=addFarm-continue]').should('exist');
cy.get('[data-cy=addFarm-continue]').should('be.disabled');
cy.get('[data-cy=addFarm-farmName]').should('exist');
cy.get('[data-cy=addFarm-location]').should('exist');

// Step 2

cy.get('[data-cy=addFarm-farmName]').type('UBC FARM');
cy.get('[data-cy=addFarm-location]').type('49.250833,-123.2410777');
cy.get('[data-cy=addFarm-continue]').should('not.be.disabled');
cy.get('[data-cy=addFarm-continue]').click();
cy.get('[data-cy=getStarted]').should('exist');
cy.get('[data-cy=getStarted]').click();

// Step 1
cy.get('[data-cy=addFarm-continue]').should('exist');
cy.get('[data-cy=addFarm-continue]').should('be.disabled');
cy.get('[data-cy=addFarm-farmName]').should('exist');
cy.get('[data-cy=addFarm-location]').should('exist');

// Step 2
cy.get('[data-cy=addFarm-farmName]').type(farmName);
cy.get('[data-cy=addFarm-location]').type(location);
cy.get('[data-cy=addFarm-continue]').should('not.be.disabled');
cy.get('[data-cy=addFarm-continue]').click();



});

Expand Down
12 changes: 12 additions & 0 deletions packages/webapp/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
const axios = require('axios');

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
Expand All @@ -24,6 +26,16 @@ module.exports = (on, config) => {
config.env.googleClientSecret = process.env.REACT_APP_GOOGLE_CLIENT_SECRET;

// plugins code ...
//const testDataApiEndpoint = `${config.env.apiUrl}`;


on("task", {
async "db:tableCleanup"() {
// clean up the database tables
const { data } = await axios.post('http://localhost:5001/testData/tableCleanup');
return data;
}});


return config;
};

0 comments on commit ce29c81

Please sign in to comment.