-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): implement user auth flow with token strategy
- Loading branch information
Showing
4 changed files
with
189 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"TEST_USER": "testuser", | ||
"TEST_PASSWORD": "testpass", | ||
"AUTH_TOKEN": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
* | ||
* See AUTHORS.txt | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* | ||
* SPDX-License-Identifier: MPL-2.0 | ||
* | ||
* This file is part of the Antares project. | ||
*/ | ||
|
||
import "@testing-library/cypress/add-commands"; | ||
|
||
declare global { | ||
namespace Cypress { | ||
interface Chainable { | ||
/** | ||
* Full authentication flow with proper user creation and login validation | ||
* | ||
* @example cy.login() | ||
*/ | ||
login(username?: string, password?: string): Chainable<void>; | ||
} | ||
} | ||
} | ||
|
||
Cypress.Commands.add( | ||
"login", | ||
( | ||
username = Cypress.env("AUTH_USER") || "testuser", | ||
password = Cypress.env("AUTH_PASSWORD") || "testpass", | ||
) => { | ||
cy.session([username, password], () => { | ||
// 1. Verify/Create test user | ||
cy.request({ | ||
method: "POST", | ||
url: "/v1/users", | ||
body: { | ||
name: username, | ||
password: password, | ||
}, | ||
failOnStatusCode: false, | ||
}).then((userResponse) => { | ||
if (userResponse.status === 401) { | ||
throw new Error("User creation failed: Unauthorized"); | ||
} | ||
|
||
if (userResponse.status === 409) { | ||
cy.log("User already exists, proceeding with login"); | ||
} else if (userResponse.status >= 400) { | ||
throw new Error(`User creation failed: ${userResponse.body.detail}`); | ||
} | ||
}); | ||
|
||
// 2. Login with credentials | ||
cy.request({ | ||
method: "POST", | ||
url: "/v1/login", | ||
body: { | ||
name: username, | ||
password: password, | ||
}, | ||
failOnStatusCode: false, | ||
}).then((loginResponse) => { | ||
if (loginResponse.status !== 200) { | ||
throw new Error(`Login failed: ${loginResponse.body.detail}`); | ||
} | ||
|
||
// 3. Create bot token with authenticated session | ||
const cookies = loginResponse.headers["set-cookie"]; | ||
cy.request({ | ||
method: "POST", | ||
url: "/v1/bots", | ||
headers: { | ||
Cookie: Array.isArray(cookies) ? cookies.join("; ") : cookies || "", | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
name: `cypress-bot-${Date.now()}`, | ||
roles: [{ group: "cypress-tests", role: 0 }], | ||
is_author: true, | ||
}, | ||
}).then((botResponse) => { | ||
window.localStorage.setItem("authToken", botResponse.body.token); | ||
Cypress.env("AUTH_TOKEN", botResponse.body.token); | ||
}); | ||
}); | ||
|
||
// 4. Validate UI flow | ||
cy.visit("/"); | ||
cy.get('input[name="name"]').type(username); | ||
cy.get('input[name="password"]').type(password, { log: false }); | ||
cy.get('button[type="submit"]').click(); | ||
cy.url().should("include", "/studies"); | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters