Skip to content

Commit

Permalink
Merge pull request #243 from UTDallasEPICS/experimental/cypress-integ…
Browse files Browse the repository at this point in the history
…ration-tests

Experimental/cypress integration tests
  • Loading branch information
devAgant authored Dec 5, 2024
2 parents fc1c2ea + 1f2b161 commit 40182e8
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*
# local env files
.env*.local
.env
.env*.test

# vercel
.vercel
Expand All @@ -42,3 +43,6 @@ postgres-data
/prisma/customerQueries.js
# Package Lock
package-lock.json

#Cypress env
cypress.env.json
5 changes: 5 additions & 0 deletions cucumber.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { defineConfig } = require('@badeball/cypress-cucumber-preprocessor');

module.exports = defineConfig({
stepDefinitions: 'cypress/support/step_definitions/**/*.{js,ts}', // Include both JS and TS files
});
28 changes: 28 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from 'cypress';
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
import createEsbuildPlugin from '@badeball/cypress-cucumber-preprocessor/esbuild';

export default defineConfig({
e2e: {
baseUrl: 'http://localhost:3000',
async setupNodeEvents(on, config) {
await addCucumberPreprocessorPlugin(on, config);

on(
'file:preprocessor',
createBundler({
plugins: [createEsbuildPlugin(config)],
})
);

return config;
},
specPattern: 'cypress/e2e/features/**/*.feature', // Path for feature files
},
env: {
AUTH0_USERNAME: process.env.CYPRESS_AUTH0_USERNAME,
AUTH0_PASSWORD: process.env.CYPRESS_AUTH0_PASSWORD,
POSTGRES_PRISMA_URL: process.env.POSTGRES_PRISMA_URL,
},
});
5 changes: 5 additions & 0 deletions cypress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Gherkin Documentation
[Gherkin Documentation](https://cucumber.io/docs/gherkin/reference/)

# Cypress Wiki
[Cypress E2E Testing](https://docs.cypress.io/app/end-to-end-testing/writing-your-first-end-to-end-test)
14 changes: 14 additions & 0 deletions cypress/e2e/features/homepage.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Feature: Homepage Load

Scenario: User logs in successfully
Given I am on the homepage
When the user clicks the login button
Then the user should be redirected to the Auth0 login page
And the user logs in with valid credentials
Then the user is authenticated and redirected back to the dashboard

Scenario: User logs out successfully
Given the user is logged in
When the user clicks the logout button
Then the user should be logged out and see the login button
And the user should not be able to access the dashboard
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
27 changes: 27 additions & 0 deletions cypress/pages/authPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class AuthPage {
static enterUsername(username) {
cy.origin(
'https://dev-5qwmxmrqa1bl88h4.us.auth0.com',
{ args: { username } },
({ username }) => {
cy.get('input[name="username"]').type(username);
}
);
}

static enterPassword(password) {
cy.origin(
'https://dev-5qwmxmrqa1bl88h4.us.auth0.com',
{ args: { password } },
({ password }) => {
cy.get('input[name="password"]').type(password);
}
);
}

static submit() {
cy.origin('https://dev-5qwmxmrqa1bl88h4.us.auth0.com', () => {
cy.get('button[type="submit"]').click();
});
}
}
17 changes: 17 additions & 0 deletions cypress/pages/homePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export class HomePage {
static visit() {
cy.visit('/');
}

static verifyTitle() {
cy.title().should('include', 'WCOA Rides');
}

static clickLogin() {
cy.contains('button', 'Log In').click();
}

static clickLogout() {
cy.contains('button', 'Log Out').click();
}
}
62 changes: 62 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// <reference types="cypress" />

// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn,
// url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }

Cypress.Commands.add('login', () => {
const username = Cypress.env('AUTH0_USERNAME');
const password = Cypress.env('AUTH0_PASSWORD');

cy.session('authSession', () => {
cy.visit('/');
cy.wait(1000);
cy.contains('button', 'Log In').click();
cy.origin('https://dev-5qwmxmrqa1bl88h4.us.auth0.com', () => {
cy.url().should('include', 'auth0');
});
cy.origin(
'https://dev-5qwmxmrqa1bl88h4.us.auth0.com',
{ args: { username, password } },
({ username, password }) => {
cy.get('input[name="username"]').type(username);
cy.get('input[name="password"]').type(password);
cy.get('button[type="submit"]').click();
}
);
});
});
12 changes: 12 additions & 0 deletions cypress/support/cypress.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />

declare global {
// eslint-disable-next-line no-unused-vars
namespace Cypress {
interface Chainable {
login(): Chainable<void>;
}
}
}

export {}; // This is important to avoid issues with module augmentation
20 changes: 20 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands';

// Alternatively you can use CommonJS syntax:
// require('./commands')
55 changes: 55 additions & 0 deletions cypress/support/step_definitions/homepageSteps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Given, When, Then } from '@badeball/cypress-cucumber-preprocessor';
import HomePage from '../../pages/homePage';
import AuthPage from '../../pages/authPage';

const username = Cypress.env('AUTH0_USERNAME');
const password = Cypress.env('AUTH0_PASSWORD');

Given('I am on the homepage', () => {
HomePage.visit();
});

When('the user clicks the login button', () => {
cy.wait(1000);
HomePage.clickLogin();
});

Then('the user should be redirected to the Auth0 login page', () => {
cy.origin('https://dev-5qwmxmrqa1bl88h4.us.auth0.com', () => {
cy.url().should('include', 'auth0');
});
});

Then('the user logs in with valid credentials', () => {
AuthPage.enterUsername(username);
AuthPage.enterPassword(password);
AuthPage.submit();
});

Then('the user is authenticated and redirected back to the dashboard', () => {
cy.wait(1000);
HomePage.verifyTitle();
cy.url().should('include', 'dashboard');
});

Given('the user is logged in', () => {
cy.login();
});

When('the user clicks the logout button', () => {
HomePage.visit();
cy.wait(1000);
HomePage.clickLogout();
});

Then('the user should be logged out and see the login button', () => {
HomePage.verifyTitle();
cy.contains('button', 'Log In');
});

Then('the user should not be able to access the dashboard', () => {
cy.visit('/dashboardEmployee');
cy.origin('https://dev-5qwmxmrqa1bl88h4.us.auth0.com', () => {
cy.url().should('include', 'auth0');
});
});
32 changes: 32 additions & 0 deletions docker-compose.e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3.8'
services:
e2e_postgres:
image: postgres:latest
container_name: e2e_postgres_db
ports:
- "5433:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: e2e_test_db
volumes:
- e2e_postgres_data:/var/lib/postgresql/data
e2e_websockify:
image: kamehb/websockify
restart: unless-stopped
command: "0.0.0.0:6433 e2e_postgres:5433"
ports:
- "6433:6432"
depends_on:
- e2e_postgres
volumes:
- e2e-websockify-data:/opt/websockify/data
- e2e-websockify-config:/opt/websockify/config

volumes:
e2e_postgres_data:
driver: local
e2e-websockify-data:
driver: local
e2e-websockify-config:
driver: local
4 changes: 2 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { PrismaNeon } from '@prisma/adapter-neon';
import { Pool, neonConfig } from '@neondatabase/serverless';
import { jwtVerify, importX509 } from 'jose';

const localDatabasePort = 6432;
const localDatabasePort = process.env.WEBSOCKET_PORT || 6432;
const localDatabaseHost = 'localhost';
neonConfig.fetchEndpoint = `http://${localDatabaseHost}:${localDatabasePort}`;
neonConfig.useSecureWebSocket = false; // SET TO TRUE IN PROD
neonConfig.pipelineConnect = false;
neonConfig.wsProxy = 'localhost:6432';
neonConfig.wsProxy = `localhost:${localDatabasePort}`;
const neon = new Pool({ connectionString: process.env.POSTGRES_PRISMA_URL });
const adapter = new PrismaNeon(neon);
const client = new PrismaClient({ adapter });
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"build": "next build",
"start": "next start",
"postinstall": "prisma generate",
"lint": "eslint ."

"lint": "eslint .",
"prisma:seede2e": "node prisma/seed.e2e.js",
"dev:test": "env-cmd -f .env.test next dev",
"post-e2e": "docker compose -f docker-compose.e2e.yml down -v",
"test:e2e": "docker compose -f docker-compose.e2e.yml down -v && docker compose -f docker-compose.e2e.yml up -d e2e_postgres && cross-env node wait.mjs && env-cmd -f .env.test npx prisma db push && env-cmd -f .env.test npm run prisma:seede2e && concurrently --kill-others --success first \"npm run dev:test\" \"wait-on http://localhost:3000 && npx cypress open\" && npm run post-e2e"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,mjs}": [
Expand Down Expand Up @@ -46,6 +49,8 @@
"ws": "^8.18.0"
},
"devDependencies": {
"@badeball/cypress-cucumber-preprocessor": "^21.0.2",
"@bahmutov/cypress-esbuild-preprocessor": "^2.2.3",
"@eslint/compat": "^1.2.0",
"@types/node": "^20.11.30",
"@types/react": "^18.2.73",
Expand All @@ -54,7 +59,13 @@
"@typescript-eslint/eslint-plugin": "^8.9.0",
"@typescript-eslint/parser": "^8.9.0",
"autoprefixer": "^10.4.20",
"concurrently": "^9.1.0",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
"cross-os": "^1.5.0",
"cypress": "^13.15.1",
"env-cmd": "^10.1.0",
"esbuild": "^0.24.0",
"eslint": "^9.12.0",
"eslint-config-next": "^14.2.15",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -66,7 +77,8 @@
"prettier": "^3.3.3",
"prisma": "^5.15.0",
"tailwindcss": "^3.4.13",
"typescript": "^5.6.3"
"typescript": "^5.6.3",
"wait-on": "^8.0.1"
},
"overrides": {
"eslint": "$eslint"
Expand Down
24 changes: 24 additions & 0 deletions prisma/seed.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
await prisma.admin.create({
data: {
email: "[email protected]",
firstName: "E2E",
lastName: "TEST",
phone: "0000000000",
},
});

// Other Seed Data goes here
}

main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Loading

0 comments on commit 40182e8

Please sign in to comment.