Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-10655-tests-1721330081453.md
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -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');
});
});
24 changes: 24 additions & 0 deletions packages/manager/cypress/support/intercepts/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,27 @@ export const mockApiMaintenanceMode = (): Cypress.Chainable<null> => {

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<null> => {
return cy.intercept('GET', apiMatcher('*'), {
statusCode: errorCode,
body: {
errors: [
{
reason: errorReason,
},
],
},
});
};