From e71b82f86f1bffbbd2bb869a80b019beeb3933c0 Mon Sep 17 00:00:00 2001 From: Wennio-Oliveira <118473401+Wennio-Oliveira@users.noreply.github.com> Date: Mon, 13 Feb 2023 14:05:23 -0300 Subject: [PATCH] test: create login test cases and update PR files (#37) * test: create login test cases and update PR files * tests: fix env name for cypress variables * tests: cypress env vars attempt * tests: fix cypress env action * tests: cypress with react app env vars --------- Co-authored-by: Ricardo Campos --- .github/workflows/cypress-nightly.yml | 9 +++ .github/workflows/merge-main.yml | 3 + .github/workflows/pr-open.yml | 6 ++ cypress/e2e/smoke-test/login-page.cy.ts | 63 +++++++++++++++ cypress/e2e/smoke-test/main-page.cy.ts | 27 ------- cypress/e2e/smoke-test/user-page.cy.ts | 103 ------------------------ cypress/fixtures/login-page.json | 5 ++ cypress/fixtures/messages.json | 16 ---- cypress/fixtures/titles.json | 6 -- cypress/fixtures/user.json | 4 - cypress/global.d.ts | 8 ++ cypress/support/commands.ts | 15 ++++ package.json | 4 +- 13 files changed, 112 insertions(+), 157 deletions(-) create mode 100644 cypress/e2e/smoke-test/login-page.cy.ts delete mode 100644 cypress/e2e/smoke-test/main-page.cy.ts delete mode 100644 cypress/e2e/smoke-test/user-page.cy.ts create mode 100644 cypress/fixtures/login-page.json delete mode 100644 cypress/fixtures/messages.json delete mode 100644 cypress/fixtures/titles.json delete mode 100644 cypress/fixtures/user.json diff --git a/.github/workflows/cypress-nightly.yml b/.github/workflows/cypress-nightly.yml index 64f7e1231..0d13c9c69 100644 --- a/.github/workflows/cypress-nightly.yml +++ b/.github/workflows/cypress-nightly.yml @@ -21,6 +21,9 @@ jobs: uses: cypress-io/github-action@v4 with: browser: chrome + env: + CYPRESS_USERNAME: ${{ secrets.BCEID_USERNAME }} + CYPRESS_PASSWORD: ${{ secrets.BCEID_PASSWORD }} - name: Upload screenshots on failure uses: actions/upload-artifact@v3 @@ -48,6 +51,9 @@ jobs: - name: Cypress run run: cypress run --browser firefox + env: + CYPRESS_USERNAME: ${{ secrets.BCEID_USERNAME }} + CYPRESS_PASSWORD: ${{ secrets.BCEID_PASSWORD }} - name: Upload screenshots on failure uses: actions/upload-artifact@v3 @@ -74,6 +80,9 @@ jobs: uses: cypress-io/github-action@v4 with: browser: edge + env: + CYPRESS_USERNAME: ${{ secrets.BCEID_USERNAME }} + CYPRESS_PASSWORD: ${{ secrets.BCEID_PASSWORD }} - name: Upload screenshots on failure uses: actions/upload-artifact@v3 diff --git a/.github/workflows/merge-main.yml b/.github/workflows/merge-main.yml index 5337d84f8..42b83fd4d 100644 --- a/.github/workflows/merge-main.yml +++ b/.github/workflows/merge-main.yml @@ -239,6 +239,9 @@ jobs: uses: cypress-io/github-action@v4 with: browser: chrome + env: + CYPRESS_USERNAME: ${{ secrets.BCEID_USERNAME }} + CYPRESS_PASSWORD: ${{ secrets.BCEID_PASSWORD }} - name: Upload screenshots on failure uses: actions/upload-artifact@v3 diff --git a/.github/workflows/pr-open.yml b/.github/workflows/pr-open.yml index 5d87f5136..3508f9dc8 100644 --- a/.github/workflows/pr-open.yml +++ b/.github/workflows/pr-open.yml @@ -401,6 +401,12 @@ jobs: - name: Cypress run uses: cypress-io/github-action@v4 + env: + CYPRESS_USERNAME: ${{ secrets.BCEID_USERNAME }} + CYPRESS_PASSWORD: ${{ secrets.BCEID_PASSWORD }} + CYPRESS_REACT_APP_KC_URL: ${{ secrets.REACT_APP_KC_URL }} + CYPRESS_REACT_APP_KC_REALM: ${{ secrets.REACT_APP_KC_REALM }} + CYPRESS_REACT_APP_KC_CLIENT_ID: ${{ secrets.REACT_APP_KC_CLIENT_ID }} with: browser: chrome config: baseUrl=https://${{ env.NAME }}-${{ github.event.number }}-app.apps.silver.devops.gov.bc.ca diff --git a/cypress/e2e/smoke-test/login-page.cy.ts b/cypress/e2e/smoke-test/login-page.cy.ts new file mode 100644 index 000000000..e30e0d65e --- /dev/null +++ b/cypress/e2e/smoke-test/login-page.cy.ts @@ -0,0 +1,63 @@ +describe('Login page test', () => { + + let loginPageData: { + title: string, + subtitle: string, + description: string + }; + + beforeEach(() => { + cy.visit('/'); + + // Clear cookies and local storage + cy.clearCookies({ log: true }) + cy.clearLocalStorage({ log: true }) + + // Loading test data + cy.fixture('login-page').then((ttls) => { + loginPageData = ttls; + }); + + }); + + it('login page is displayed and loads correctly', () => { + cy.getByDataTest('landing-title').should('have.text', loginPageData.title); + cy.getByDataTest('landing-subtitle').should('have.text', loginPageData.subtitle); + cy.getByDataTest('landing-desc').should('have.text', loginPageData.description); + }); + + it('navigate to the user form page IDIR', () => { + cy.getByDataTest('landing-button__idir').click(); + cy.get('#idirLogo').should('be.visible'); + }); + + it('navigate to the user form page BCeID', () => { + cy.getByDataTest('landing-button__bceid').click(); + cy.get('#bceidLogo').should('be.visible'); + }); + + it('try to access system using a link without user connected', () => { + cy.visit('https://nrsparwebapp-test-app.apps.silver.devops.gov.bc.ca/dashboard'); + cy.getByDataTest('landing-title').should('have.text', loginPageData.title); + }); + + it.skip('log in with BCeID and validate if after timeout the user is disconnected', () => { + cy.login(); + cy.wait(1800000); //wait for 30 minutes 1800000 + cy.getByDataTest('landing-title').should('have.text', loginPageData.title); + }); + + it('log in with BCeID and validate user role', () => { + cy.login(); + cy.getByDataTest('header-button__user').click(); + cy.get('.user-data').find('p').contains('IDIR: undefined'); + }); + + it('log in with BCeID and validate user information', () => { + cy.login(); + cy.getByDataTest('header-button__user').click(); + cy.get('.user-data').find('p').contains('NRS Load Test-3'); + cy.get('.user-data').find('p').contains('nrpp_test@nrpp.compratech.com'); + }); + +}); diff --git a/cypress/e2e/smoke-test/main-page.cy.ts b/cypress/e2e/smoke-test/main-page.cy.ts deleted file mode 100644 index 9b73f6936..000000000 --- a/cypress/e2e/smoke-test/main-page.cy.ts +++ /dev/null @@ -1,27 +0,0 @@ -describe('main page test', () => { - let pageHeaders: { - main: string, - userForm: string, - userFormCard: string, - searchCard: string - }; - - beforeEach(() => { - cy.visit('/'); - - // Loading test data - cy.fixture('titles').then((ttls) => { - pageHeaders = ttls; - }); - }); - - it('main page is displayed and loads correctly', () => { - cy.getByDataTest('home-title').should('have.text', pageHeaders.main); - cy.getByDataTest('card-form__title').should('have.text', pageHeaders.userFormCard); - }); - - it('navigate to the user form page', () => { - cy.getByDataTest('card-form__button').click(); - cy.getByDataTest('form-title').should('have.text', pageHeaders.userForm); - }); -}); diff --git a/cypress/e2e/smoke-test/user-page.cy.ts b/cypress/e2e/smoke-test/user-page.cy.ts deleted file mode 100644 index d7c479080..000000000 --- a/cypress/e2e/smoke-test/user-page.cy.ts +++ /dev/null @@ -1,103 +0,0 @@ -import msg from '../../fixtures/messages.json'; - -describe('user form test', () => { - let testUser: { - firstName: string, - lastName: string - }; - - beforeEach(() => { - cy.visit('/form'); - - // Loading test data - cy.fixture('user').then((user) => { - testUser = user; - }); - }); - - it('create a valid user', () => { - // Removing the user first to avoid false negatives - cy.deleteUser(testUser.firstName, testUser.lastName); - - // Create the test user - cy.getByDataTest('input-first').type(testUser.firstName).blur(); - cy.getByDataTest('input-last').type(testUser.lastName).blur(); - cy.getByDataTest('button-submit').contains('Submit').click(); - - // Check that the user is created in the UI - cy.intercept(`${Cypress.env('apiUrl')}/api/users/find-all`).as('getUsers'); - cy.wait('@getUsers'); - cy.contains(msg.confirm.submit); - cy.contains(testUser.firstName); - cy.contains(testUser.lastName); - }); - - it('delete a user', () => { - // Creating the user first to avoid false negatives - cy.createUser(testUser.firstName, testUser.lastName); - cy.reload(); - - // Delete user and verify that is deleted in the UI - cy.contains('td', testUser.firstName).parent('tr').within(() => { - cy.get('td').eq(3).contains('button', 'Delete').click(); - }); - cy.intercept(`${Cypress.env('apiUrl')}/api/users/find-all`).as('getUsers'); - cy.wait('@getUsers'); - cy.contains(msg.confirm.delete); - cy.contains(testUser.firstName).should('not.exist'); - cy.contains(testUser.lastName).should('not.exist'); - }); - - it('empty fields after resetting form', () => { - // Fill the form inputs and reset - cy.getByDataTest('input-first').type(testUser.firstName).blur(); - cy.getByDataTest('input-last').type(testUser.lastName).blur(); - cy.getByDataTest('button-reset').contains('Reset').click(); - - // Check if all the inputs are empty - cy.getByDataTest('input-first').should('have.text', ''); - cy.getByDataTest('input-last').should('have.text', ''); - }); - - it('error with empty username', () => { - cy.getByDataTest('input-first').focus().blur(); - cy.getByDataTest('input-last').focus().blur(); - cy.getByDataTest('button-submit').contains('Submit').click(); - - cy.get('#input-first-error-msg').should('have.text', msg.input.empty); - cy.get('#input-last-error-msg').should('have.text', msg.input.empty); - cy.get('#error-banner').within(() => { - cy.get('.cds--inline-notification__subtitle').should('have.text', msg.banner.invalid); - }); - }); - - it('error at duplicate username', () => { - // Creating the user to duplicate after - cy.createUser(testUser.firstName, testUser.lastName); - cy.reload(); - - // Try to create a duplicate user - cy.getByDataTest('input-first').type(testUser.firstName).blur(); - cy.getByDataTest('input-last').type(testUser.lastName).blur(); - cy.getByDataTest('button-submit').contains('Submit').click(); - - // Check for the error message - cy.get('#error-banner').within(() => { - cy.get('.cds--inline-notification__subtitle').should('have.text', msg.banner.duplicate); - }); - }); - - it('error typing less than 2 characters', () => { - // Fill the form inputs with invalid data - cy.getByDataTest('input-first').type('a').blur(); - cy.get('#input-first-error-msg').should('have.text', msg.input.charNumberFirst); - cy.getByDataTest('input-last').type('a').blur(); - cy.get('#input-last-error-msg').should('have.text', msg.input.charNumberLast); - - // The data doesn't goe through and throws an error banner - cy.getByDataTest('button-submit').contains('Submit').click(); - cy.get('#error-banner').within(() => { - cy.get('.cds--inline-notification__subtitle').should('have.text', msg.banner.invalid); - }); - }); -}); diff --git a/cypress/fixtures/login-page.json b/cypress/fixtures/login-page.json new file mode 100644 index 000000000..f52555215 --- /dev/null +++ b/cypress/fixtures/login-page.json @@ -0,0 +1,5 @@ +{ + "title": "Welcome to SPAR", + "subtitle": "Seed Planning and Registry Application", + "description": "Register and storage your seed and meet your annual reforestation needs using SPAR" +} \ No newline at end of file diff --git a/cypress/fixtures/messages.json b/cypress/fixtures/messages.json deleted file mode 100644 index 3c365a6dd..000000000 --- a/cypress/fixtures/messages.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "confirm": { - "submit": "Submitted!", - "delete": "Deleted!" - }, - "input": { - "empty": "Please enter a valid value", - "invalid": "Please, enter your first and last name!", - "charNumberFirst": "The first name must have at least 2 characters", - "charNumberLast": "The last name must have at least 2 characters" - }, - "banner": { - "duplicate": "User already registered!", - "invalid": "Please, enter your first and last name!" - } -} \ No newline at end of file diff --git a/cypress/fixtures/titles.json b/cypress/fixtures/titles.json deleted file mode 100644 index 8e7ebbf7f..000000000 --- a/cypress/fixtures/titles.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "main": "NR Front End Testing App", - "userForm": "NR Front End Form", - "userFormCard": "Sample User Form", - "searchCard": "Sample Search" -} \ No newline at end of file diff --git a/cypress/fixtures/user.json b/cypress/fixtures/user.json deleted file mode 100644 index b0755a333..000000000 --- a/cypress/fixtures/user.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "firstName": "John", - "lastName": "Doe" -} \ No newline at end of file diff --git a/cypress/global.d.ts b/cypress/global.d.ts index c6385970e..1f469c4f1 100644 --- a/cypress/global.d.ts +++ b/cypress/global.d.ts @@ -32,5 +32,13 @@ declare namespace Cypress { * cy.createUser('Jhon', 'Doe') */ createUser(firstname: string, lastname: string): void + + /** + * Custom command to log in on app. + * + * @example + * cy.login() + */ + login(): void } } diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 4a6645e00..8e6151b60 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -28,3 +28,18 @@ Cypress.Commands.add('createUser', (firstname, lastname) => { cy.log(response.body); }); }); + +Cypress.Commands.add('login', () => { + const USERNAME = Cypress.env('USERNAME'); + const PASSWORD = Cypress.env('PASSWORD'); + + cy.getByDataTest('landing-button__bceid').click(); + cy.get('#bceidLogo').should('be.visible'); + cy.get('input[name=user]') + .clear() + .type(USERNAME, { delay: 50 }); + cy.get('input[name=password]') + .clear() + .type(PASSWORD, { delay: 50 }); + cy.get('input[name=btnSubmit]').click(); +}); \ No newline at end of file diff --git a/package.json b/package.json index 4377dd599..539c5f350 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,9 @@ "build:production": "react-scripts build", "test": "jest", "eject": "react-scripts eject", - "lint": "./node_modules/.bin/eslint" + "lint": "./node_modules/.bin/eslint", + "cy:open": "cypress open", + "cy:run" : "cypress run" }, "eslintConfig": { "extends": [