diff --git a/.github/workflows/jest.yml b/.github/workflows/jest.yml index 632feb5..704426a 100644 --- a/.github/workflows/jest.yml +++ b/.github/workflows/jest.yml @@ -53,8 +53,9 @@ jobs: echo "HTTPS_KEY_PASSPHRASE=${{ secrets.HTTPS_KEY_PASSPHRASE }}" >> server/.env echo "NEXT_PUBLIC_SUPABASE_URL=${{ secrets.NEXT_PUBLIC_SUPABASE_URL }}" >> server/.env echo "NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY=${{ secrets.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY }}" >> server/.env + echo "COOKIE_HOST_URL=.localhost" >> server/.env - - name: Create server https keys + - name: Import server https keys run: | echo "${{ secrets.TEST_KEY }}" > server/key.pem echo "${{ secrets.TEST_CERT }}" > server/cert.pem @@ -63,7 +64,8 @@ jobs: run: | echo "VITE_PUBLIC_SUPABASE_URL=${{ secrets.NEXT_PUBLIC_SUPABASE_URL }}" >> client/.env echo "VITE_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY=${{ secrets.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY }}" >> client/.env - echo "VITE_SERVER_URL=https://placeholder.url" >> client/.env + echo "VITE_SERVER_URL=https://localhost:8000" >> client/.env + echo "VITE_COOKIE_HOST_URL=.localhost" >> client/.env - name: Run server tests run: | @@ -74,3 +76,7 @@ jobs: run: | cd client npm run test + + - name: Run integration tests + run: | + npx start-server-and-test 'npm run dev' http://localhost:5173 'npx cypress run' diff --git a/.gitignore b/.gitignore index df460d7..c5d072a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ server/node_modules .env *.pem client/coverage -server/coverage +server/coverage \ No newline at end of file diff --git a/cypress.config.js b/cypress.config.js new file mode 100644 index 0000000..5e6fbd5 --- /dev/null +++ b/cypress.config.js @@ -0,0 +1,9 @@ +module.exports = { + allowCypressEnv: false, + + e2e: { + setupNodeEvents(on, config) { + // implement node event listeners here + }, + }, +}; diff --git a/cypress/e2e/login.cy.js b/cypress/e2e/login.cy.js new file mode 100644 index 0000000..472a405 --- /dev/null +++ b/cypress/e2e/login.cy.js @@ -0,0 +1,85 @@ +describe('login spec', () => { + beforeEach(() => { + cy.visit('http://localhost:5173/') + Cypress.Cookies.debug(true) + cy.clearCookies() + }) + it('shows sign in on load', () => { + cy.get('.App div').first().should('have.class', 'landing-page') + cy.get('h2').should('have.text', 'Sign In') + }) + + it('toggles to signup', () => { + cy.get('.link-button').click() + cy.get('h2').should('have.text', 'Sign Up') + + cy.get('.link-button').click() + cy.get('h2').should('have.text', 'Sign In') + + }) + + it('render home page when signed in', () => { + cy.intercept('POST', '**/auth/v1/token*').as('loginRequest') + + cy.get('input[placeholder="Enter email"]').type('test@email.mail') + cy.get('input[placeholder="Enter password"]').type('pass10') + cy.get('button[type="submit"]').click() + + cy.wait('@loginRequest').then((interception) => { + expect(interception.response?.statusCode).to.equal(200) + }) + + cy.getCookies().should('have.length', 1) + cy.get('.App div').contains('Create a Music Moment').should('be.visible'); + }) + + it('does not render home with incorrect credentials', () => { + cy.intercept('POST', '**/auth/v1/token*').as('loginRequest') + + cy.get('input[placeholder="Enter email"]').type('test@email.mail') + cy.get('input[placeholder="Enter password"]').type('pass101') + cy.get('button[type="submit"]').click() + + cy.wait('@loginRequest').then((interception) => { + expect(interception.response?.statusCode).to.equal(400) + }) + cy.get('.App div').contains('Invalid email or password. Please try again.').should('be.visible'); + + cy.getCookies().should('have.length', 0) + }) + + it('creates new account with sign up', () => { + const testUser = { + email: `test-${Date.now()}@example.com`, + password: 'TestPassword123!' + } + cy.get('.link-button').click() + // stubbing the signup to prevent new accounts being made every time test is run + cy.intercept('POST', '**/auth/v1/signup*', { + statusCode: 200, + body: { + user: { + id: 'mock-user-id-123', + email: 'test@example.com' + }, + session: null + } + }).as('signupRequest') + + cy.get('input[placeholder="Enter email"]').type('test123@example.com') + cy.get('input[placeholder="Enter username"]').type('test') + cy.get('input[placeholder="Enter password"]').type('pass101') + cy.get('button[type="submit"]').click() + + cy.wait('@signupRequest') + + cy.on('window:alert', (alertText) => { + expect(alertText).to.contains('Sign up successful! Please check your email to verify your account.') + }) + cy.get('h2').should('have.text', 'Sign In') + + }) + + + +}) diff --git a/cypress/e2e/make_post.cy.js b/cypress/e2e/make_post.cy.js new file mode 100644 index 0000000..773776f --- /dev/null +++ b/cypress/e2e/make_post.cy.js @@ -0,0 +1,37 @@ +describe('make post spec', () => { + beforeEach(() => { + cy.visit('http://localhost:5173/', { + onBeforeLoad(win) { + cy.stub(win.navigator.geolocation, 'getCurrentPosition').callsArgWith(0, { + coords: { + latitude: 43, + longitude: -125, + }, + }) + }, + }) + cy.intercept('POST', '**/auth/v1/token*').as('loginRequest') + cy.get('input[placeholder="Enter email"]').type('test@email.mail') + cy.get('input[placeholder="Enter password"]').type('pass10') + cy.get('button[type="submit"]').click() + cy.wait('@loginRequest') + + }) + it('adds post to map', () => { + cy.intercept('POST', '/api/posts', { + statusCode: 201 + }).as('postRequest') + cy.get('.leaflet-marker-icon').its('length').then((initialCount) => { + cy.get('.post-button').click() + cy.get('.form-input').eq(0).type('Last Christmas') + cy.get('.form-input').eq(1).type('Wham') + cy.get('.form-input').eq(2).type('integration test song') + cy.get('.btn-add').click() + + cy.wait('@postRequest') + + // Marker count increased + cy.get('.leaflet-marker-icon').should('have.length', initialCount + 1) + }) + }) +}) \ No newline at end of file diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000..02e4254 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000..66ea16e --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,25 @@ +// *********************************************** +// This example commands.js 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) => { ... }) \ No newline at end of file diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js new file mode 100644 index 0000000..3eaffff --- /dev/null +++ b/cypress/support/e2e.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example support/e2e.js 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' \ No newline at end of file diff --git a/dev-docs.md b/dev-docs.md index c128ecd..0288c3e 100644 --- a/dev-docs.md +++ b/dev-docs.md @@ -46,7 +46,13 @@ soundscape/ │ ├── package.json │ ├── package-lock.json │ └── tsconfig.json +├── cypress/ +| ├── e2e/ +| ├── fixtures/ +| ├── screenshots/ +| └── support/ ├── .gitignore +├── cypress.config.js ├── package.json ├── package-lock.json ├── tsconfig.json @@ -117,6 +123,8 @@ Tests can be run individually in the `client` and `server` directories with environment file may not be setup properly, and `npm install` may be needed in order to install packages needed. +Integrations test can be run in the root directory. Make sure the application is running locally and run `npx cypress run` to run all cypress tests. An individual test file can be run with `npx cypress run --spec "cypress/e2e/path/to/file"` and run tests with a specfic browser by running `npx cypress run --browser chrome`. To use the cypress GUI to interactively run and create tests run `npx cypress open`. + ## How to Add New Tests ### Client diff --git a/package.json b/package.json index 9101e7f..1f30093 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,8 @@ "test": "./env-setup.sh && concurrently \"npm run test --prefix server\" \"npm run test --prefix client\"" }, "devDependencies": { - "concurrently": "^9.2.1" + "concurrently": "^9.2.1", + "start-server-and-test": "^2.1.5", + "cypress": "^15.10.0" } }