From af4dec27049b6c4cb28569bc2d837fede0d11ae6 Mon Sep 17 00:00:00 2001 From: Cassie Liu Date: Mon, 8 Jul 2024 16:05:16 -0400 Subject: [PATCH 1/2] M3-8138 Add Cypress test for login redirect upon API unauthorized response --- .../general/account-login-redirect.spec.ts | 20 ++++++++++++++++ .../cypress/support/intercepts/general.ts | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 packages/manager/cypress/e2e/core/general/account-login-redirect.spec.ts diff --git a/packages/manager/cypress/e2e/core/general/account-login-redirect.spec.ts b/packages/manager/cypress/e2e/core/general/account-login-redirect.spec.ts new file mode 100644 index 00000000000..2f7048bb057 --- /dev/null +++ b/packages/manager/cypress/e2e/core/general/account-login-redirect.spec.ts @@ -0,0 +1,20 @@ +import { mockApiRequestWithError } from 'support/intercepts/general'; +import { LOGIN_ROOT } from 'src/constants'; + +describe('account login redirect', () => { + /** + * The API will return 401 with the body below for all the endpoints. + * + * { "errors": [ { "reason": "Your account must be authorized to use this endpoint" } ] } + */ + it('should redirect to the login page when the user is not authorized', () => { + const errorReason = 'Your account must be authorized to use this endpoint'; + + mockApiRequestWithError(401, errorReason); + + cy.visitWithLogin('/linodes/create'); + + cy.url().should('contain', `${LOGIN_ROOT}/login?`, { exact: false }); + cy.findByText('Please log in to continue.').should('be.visible'); + }); +}); diff --git a/packages/manager/cypress/support/intercepts/general.ts b/packages/manager/cypress/support/intercepts/general.ts index 09cd74e0167..3c53386a098 100644 --- a/packages/manager/cypress/support/intercepts/general.ts +++ b/packages/manager/cypress/support/intercepts/general.ts @@ -57,3 +57,27 @@ export const mockApiMaintenanceMode = (): Cypress.Chainable => { return cy.intercept(apiMatcher('**'), errorResponse); }; + +/** + * Intercepts all requests to Linode API v4 and mocks an error HTTP response. + * + * @param errorCode - HTTP status code to mock. + * @param errorMessage - Response error message to mock. + * + * @returns Cypress chainable. + */ +export const mockApiRequestWithError = ( + errorCode: number, + errorReason: string +): Cypress.Chainable => { + return cy.intercept('GET', apiMatcher('*'), { + statusCode: errorCode, + body: { + errors: [ + { + reason: errorReason, + }, + ], + }, + }); +}; From 0fc024a9aad180fa894f9d1f6d68191e8524eeb4 Mon Sep 17 00:00:00 2001 From: Cassie Liu Date: Thu, 18 Jul 2024 15:14:41 -0400 Subject: [PATCH 2/2] Added changeset: Add Cypress test for login redirect upon API unauthorized response --- packages/manager/.changeset/pr-10655-tests-1721330081453.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 packages/manager/.changeset/pr-10655-tests-1721330081453.md diff --git a/packages/manager/.changeset/pr-10655-tests-1721330081453.md b/packages/manager/.changeset/pr-10655-tests-1721330081453.md new file mode 100644 index 00000000000..5317b7b6029 --- /dev/null +++ b/packages/manager/.changeset/pr-10655-tests-1721330081453.md @@ -0,0 +1,5 @@ +--- +"@linode/manager": Tests +--- + +Add Cypress test for login redirect upon API unauthorized response ([#10655](https://github.com/linode/manager/pull/10655))