From 6d929f7b5d01ce8ac987ceb4df7cb2edb2b2e3cc Mon Sep 17 00:00:00 2001 From: Abdulrhmn Ghanem Date: Wed, 22 Feb 2023 14:46:00 +0200 Subject: [PATCH] e2e: remove authRedirect.spec --- e2e/cypress/integration/authRedirects.spec.js | 187 ------------------ e2e/cypress/integration/multiProject.spec.js | 18 +- e2e/cypress/support/commands.js | 27 ++- 3 files changed, 28 insertions(+), 204 deletions(-) delete mode 100644 e2e/cypress/integration/authRedirects.spec.js diff --git a/e2e/cypress/integration/authRedirects.spec.js b/e2e/cypress/integration/authRedirects.spec.js deleted file mode 100644 index 2efe14041a..0000000000 --- a/e2e/cypress/integration/authRedirects.spec.js +++ /dev/null @@ -1,187 +0,0 @@ -import { getFakeUser } from '../support/getFakeUser' - -describe('Authentication redirects', () => { - const { username, email, password } = getFakeUser() - - before(() => { - /* - * The purpose of this isn't actually visiting the homepage. - * Sometimes, the frontend has a slow startup time which results in a random failure. - */ - cy.visit('/') - cy.createUser(username, email, password) - }) - - it("should redirect unauthenticated users to '/login' when accessing require sign in page (server)", () => { - // Clear the cookies to make sure the user isn't authenticated - cy.clearCookies() - // `/project/new` is marked as require sign in. - cy.forceVisit('/projects/new') - cy.get('[data-cy=login-grid]').should('be.visible') - }) - - it("should redirect unauthenticated users to '/login' when accessing require sign in page (client)", () => { - // Clear the cookies to make sure the user isn't authenticated - cy.clearCookies() - cy.forceVisit('/') - // navigate to `/projects/new`with client-side interactions. - cy.get('[data-cy=add-project]').click({ timeout: 10000 }) - // `/project/new` is marked as require sign in. - cy.get('[data-cy=login-grid]').should('be.visible') - }) - - /* - ! There is no (client) version of this because there is no user interaction which will lead to redirecting to `/login` - ! when the user is already logged in. - */ - it('should redirect authenticated users to homepage when accessing reqSignOut page (server)', () => { - // Sign in - cy.visit('/login') - cy.signIn(username, password) - cy.get('[data-cy=user-menu]') - - // `/login` is marked as `reqSignOut`. - cy.visit('/login') - cy.get('[data-cy=cards-grid]').should('be.visible') - }) -}) - -describe('Redirect after login', () => { - const { username, email, password } = getFakeUser() - - before(() => { - cy.visit('/') - cy.createUser(username, email, password) - }) - - beforeEach(() => { - // deauthenticate the user and reload the page to update the CSRF token - cy.clearCookies() - cy.reload() - }) - - it('should redirect to homepage if there is no redirect query', () => { - cy.visit('/login') - // sign the user in. - cy.signIn(username, password) - - // After a successful login the user is redirect to the homepage. - cy.url().should('eq', `${Cypress.config().baseUrl}${Cypress.env('home_path')}`) - }) - - it('should redirect to correct page if there is a redirect query', () => { - const pageClickFrom = 'projects/new' - - cy.visit(pageClickFrom) - - // sign the user in. - cy.signIn(username, password) - // redirect to the page in the redirect query parameter - cy.url().should('eq', `http://kitspace.test:3000/${pageClickFrom}`) - }) - - it("should redirect to the `pathname` if it isn't `login`", () => { - const requireSignInPage = '/settings' - - cy.visit(requireSignInPage) - - cy.get('[data-cy=login-grid]').should('be.visible') - cy.signIn(username, password) - cy.url().should('eq', `http://kitspace.test:3000${requireSignInPage}`) - - cy.get('h1').contains('Settings') - }) -}) - -describe('Redirect after sign-up', () => { - it('should redirect to homepage if there is no redirect query', () => { - // sign the user up - const { username, email, password } = getFakeUser() - - cy.visit('/login') - cy.signUp(username, email, password) - - // After a successful sign up the user is redirect to the homepage. - cy.url().should('eq', `${Cypress.config().baseUrl}${Cypress.env('home_path')}`) - }) - - it('should redirect to correct page if there is a redirect query', () => { - const pageClickFrom = 'projects/new' - cy.visit(pageClickFrom) - // sign the user up - const { username, email, password } = getFakeUser() - - cy.signUp(username, email, password) - - // redirect to the page in the redirect query parameter - cy.url().should('eq', `http://kitspace.test:3000/${pageClickFrom}`) - }) - - it("should redirect to the `pathname` if it isn't `login`", () => { - const requireSignInPage = '/settings' - - cy.visit(requireSignInPage) - - cy.get('[data-cy=login-grid]').should('be.visible') - - // sign the user up - const { username, email, password } = getFakeUser() - cy.signUp(username, email, password) - - cy.url().should('eq', `http://kitspace.test:3000${requireSignInPage}`) - cy.get('h1').contains('Settings') - }) -}) - -describe('Redirect after logout', () => { - it('should redirect to login page', () => { - const { username, email, password } = getFakeUser() - cy.createUser(username, email, password) - - // Press the logout button - cy.get('img[alt=avatar]').trigger('mousemove').click() - cy.get('#logout').trigger('mousemove').click() - - // should redirect to `/login` - cy.url().should('eq', 'http://kitspace.test:3000/login') - }) -}) - -describe('"Add Project" behavior', () => { - const { username, email, password } = getFakeUser() - - before(() => { - cy.visit('/') - cy.createUser(username, email, password) - }) - - beforeEach(() => { - // deauthenticate the user and reload the page to update the CSRF token - cy.clearCookies() - cy.reload() - }) - - it('should redirect unauthenticated user to /login and then back to /projects/new', () => { - // The user is unauthenticated - cy.window().its('session.user').should('not.ok') - - // Clicking `Add Project` redirects to the login page. - // and adds redirect query to `/projects/new/` - cy.get('#add_project').click() - - const { username, email, password } = getFakeUser() - cy.signUp(username, email, password) - cy.url().should('eq', 'http://kitspace.test:3000/projects/new') - }) - - it('should redirect authenticated user to /projects/new', () => { - // sign the user in. - cy.visit('/login') - cy.signIn(username, password) - - // Clicking `Add Project` redirects to new project page. - cy.visit('/') - cy.get('#add_project').click() - cy.url().should('eq', 'http://kitspace.test:3000/projects/new') - }) -}) diff --git a/e2e/cypress/integration/multiProject.spec.js b/e2e/cypress/integration/multiProject.spec.js index d0bb052e7b..c9e11cb359 100644 --- a/e2e/cypress/integration/multiProject.spec.js +++ b/e2e/cypress/integration/multiProject.spec.js @@ -38,7 +38,7 @@ describe('Render project cards', () => { }) }) - it.only('should display card thumbnail', () => { + it('should display card thumbnail', () => { const user = getFakeUser() // Migrate the multiproject repo @@ -53,6 +53,11 @@ describe('Render project cards', () => { // Migrate the normal repo cy.importRepo(syncedRepoUrl, normalRepoName, user) + cy.forceVisit(`/${user.username}/${normalRepoName}`) + + // Wait for the repo to finish processing, by checking the visibility of info-bar. + cy.get('[data-cy=info-bar]', { timeout: 100_000 }).should('be.visible') + cy.visit(`/${user.username}`) // There should be 3 cards = 2 form multiprojects + 1 normal project cy.get('[data-cy=project-card]', { timeout: 60_000 }).should( @@ -133,10 +138,7 @@ describe('Multi project page', () => { // Migrate the multiproject repo cy.importRepo(syncedRepoUrlMultiProjects, multiProjectsRepoName, user) - cy.url({ timeout: 60_000 }).should( - 'contain', - `${user.username}/${multiProjectsRepoName}`, - ) + cy.forceVisit(`/${user.username}/${multiProjectsRepoName}`) // Wait for the repo to finish processing, by checking the visibility sub projects cards. cy.get('[data-cy=project-card]', { timeout: 60_000 }).as('projectCards') @@ -152,7 +154,6 @@ describe('Multi project page', () => { // Different page elements should be visible. const pageComponents = [ - 'sync-msg', 'info-bar', 'board-showcase', 'board-showcase-top', @@ -175,10 +176,7 @@ describe('Multi project page', () => { // Migrate the multiproject repo cy.importRepo(syncedRepoUrlMultiProjects, multiProjectsRepoName, user) - cy.url({ timeout: 10_000 }).should( - 'contain', - `${user.username}/${multiProjectsRepoName}`, - ) + cy.forceVisit(`/${user.username}/${multiProjectsRepoName}`) // Wait for the repo to finish processing, by checking the visibility sub projects cards. cy.get('[data-cy=project-card]', { timeout: 120_000 }).should( 'have.length', diff --git a/e2e/cypress/support/commands.js b/e2e/cypress/support/commands.js index 1be4c4043e..d4435d633b 100644 --- a/e2e/cypress/support/commands.js +++ b/e2e/cypress/support/commands.js @@ -112,18 +112,31 @@ Cypress.Commands.add('createGiteaUser', user => { const url = 'http://gitea.kitspace.test:3000/api/v1/admin/users' + // check if the user already exists cy.request({ - url, - method: 'POST', + url: `http://gitea.kitspace.test:3000/api/v1/users/${user.username}`, + method: 'GET', headers, - body: JSON.stringify(user), failOnStatusCode: false, }).then(response => { - if (!response.status === 201) { - throw new Error('Failed to create user') + if (response.status === 200) { + return response.body } - - return response.body + // if the user doesn't exist, create a new user and return the user object. + return cy + .request({ + url, + method: 'POST', + headers, + body: JSON.stringify(user), + failOnStatusCode: false, + }) + .then(response => { + if (response.status !== 201) { + throw new Error('Failed to create user') + } + return response.body + }) }) })