test: add role-based navigation tests verifying correct nav sections per user role
This commit is contained in:
@@ -49,11 +49,17 @@ public class PlaywrightFixture : IAsyncLifetime
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new page and log in with the test user.
|
||||
/// Create a new page and log in with the default multi-role test user.
|
||||
/// </summary>
|
||||
public Task<IPage> NewAuthenticatedPageAsync() =>
|
||||
NewAuthenticatedPageAsync(TestUsername, TestPassword);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new page and log in with specific credentials.
|
||||
/// Uses JavaScript fetch() to POST to /auth/login from within the browser,
|
||||
/// which sets the auth cookie in the browser context. Then navigates to the dashboard.
|
||||
/// </summary>
|
||||
public async Task<IPage> NewAuthenticatedPageAsync()
|
||||
public async Task<IPage> NewAuthenticatedPageAsync(string username, string password)
|
||||
{
|
||||
var page = await NewPageAsync();
|
||||
|
||||
@@ -63,24 +69,21 @@ public class PlaywrightFixture : IAsyncLifetime
|
||||
|
||||
// POST to /auth/login via fetch() inside the browser.
|
||||
// This sets the auth cookie in the browser context automatically.
|
||||
// Use redirect: 'follow' so the browser follows the 302 and the cookie is stored.
|
||||
var finalUrl = await page.EvaluateAsync<string>(@"
|
||||
async () => {
|
||||
async ([u, p]) => {
|
||||
const resp = await fetch('/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: 'username=' + encodeURIComponent('" + TestUsername + @"')
|
||||
+ '&password=' + encodeURIComponent('" + TestPassword + @"'),
|
||||
body: 'username=' + encodeURIComponent(u) + '&password=' + encodeURIComponent(p),
|
||||
redirect: 'follow'
|
||||
});
|
||||
return resp.url;
|
||||
}
|
||||
");
|
||||
", new object[] { username, password });
|
||||
|
||||
// The fetch followed the redirect. If it ended on /login, auth failed.
|
||||
if (finalUrl.Contains("/login"))
|
||||
{
|
||||
throw new InvalidOperationException($"Login failed — redirected back to login: {finalUrl}");
|
||||
throw new InvalidOperationException($"Login failed for '{username}' — redirected back to login: {finalUrl}");
|
||||
}
|
||||
|
||||
// Navigate to the dashboard — cookie authenticates us
|
||||
|
||||
Reference in New Issue
Block a user