|
| 1 | +using Microsoft.Playwright; |
| 2 | +using NUnit.Framework; |
| 3 | +using FluentAssertions; |
| 4 | + |
| 5 | +namespace Tests.UI; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// End-to-end tests for the login flow. |
| 9 | +/// Tests valid login, invalid credentials, and inactive account handling. |
| 10 | +/// </summary> |
| 11 | +[TestFixture] |
| 12 | +[Category("UI")] |
| 13 | +public class LoginTests : PlaywrightFixture |
| 14 | +{ |
| 15 | + // ── Valid login flow ── |
| 16 | + |
| 17 | + [Test] |
| 18 | + public async Task Login_ValidCeoCredentials_RedirectsToDashboard() |
| 19 | + { |
| 20 | + var email = Environment.GetEnvironmentVariable("CEO_EMAIL") ?? "ceo@companyflow.com"; |
| 21 | + var password = Environment.GetEnvironmentVariable("CEO_PASSWORD") ?? "Admin@123456!"; |
| 22 | + |
| 23 | + await Page.GotoAsync($"{BaseUrl}/Account/Login"); |
| 24 | + await Page.FillAsync("input[name='Email']", email); |
| 25 | + await Page.FillAsync("input[name='Password']", password); |
| 26 | + await Page.ClickAsync("button[type='submit']"); |
| 27 | + |
| 28 | + await Page.WaitForURLAsync(url => !url.Contains("/Account/Login"), |
| 29 | + new PageWaitForURLOptions { Timeout = 10_000 }); |
| 30 | + |
| 31 | + Page.Url.Should().NotContain("/Account/Login"); |
| 32 | + } |
| 33 | + |
| 34 | + // ── Invalid credentials ── |
| 35 | + |
| 36 | + [Test] |
| 37 | + public async Task Login_InvalidPassword_ShowsErrorMessage() |
| 38 | + { |
| 39 | + await Page.GotoAsync($"{BaseUrl}/Account/Login"); |
| 40 | + await Page.FillAsync("input[name='Email']", "invalid@test.com"); |
| 41 | + await Page.FillAsync("input[name='Password']", "WrongPassword123!"); |
| 42 | + await Page.ClickAsync("button[type='submit']"); |
| 43 | + |
| 44 | + // Should remain on login page |
| 45 | + await Page.WaitForURLAsync(url => url.Contains("/Account/Login"), |
| 46 | + new PageWaitForURLOptions { Timeout = 5_000 }); |
| 47 | + |
| 48 | + var errorText = await Page.IsVisibleAsync("[class*='text-danger'], [class*='alert-danger'], .validation-summary-errors"); |
| 49 | + errorText.Should().BeTrue("an error message should be displayed for invalid credentials"); |
| 50 | + } |
| 51 | + |
| 52 | + // ── Empty form ── |
| 53 | + |
| 54 | + [Test] |
| 55 | + public async Task Login_EmptyForm_ShowsValidationErrors() |
| 56 | + { |
| 57 | + await Page.GotoAsync($"{BaseUrl}/Account/Login"); |
| 58 | + await Page.ClickAsync("button[type='submit']"); |
| 59 | + |
| 60 | + await Page.WaitForURLAsync(url => url.Contains("/Account/Login"), |
| 61 | + new PageWaitForURLOptions { Timeout = 5_000 }); |
| 62 | + |
| 63 | + var hasErrors = await Page.IsVisibleAsync("[class*='text-danger'], [data-valmsg-for]"); |
| 64 | + hasErrors.Should().BeTrue(); |
| 65 | + } |
| 66 | + |
| 67 | + // ── Logout ── |
| 68 | + |
| 69 | + [Test] |
| 70 | + public async Task Logout_AfterLogin_RedirectsToLogin() |
| 71 | + { |
| 72 | + var email = Environment.GetEnvironmentVariable("CEO_EMAIL") ?? "ceo@companyflow.com"; |
| 73 | + var password = Environment.GetEnvironmentVariable("CEO_PASSWORD") ?? "Admin@123456!"; |
| 74 | + |
| 75 | + await LoginAsAsync(Page, email, password); |
| 76 | + await LogoutAsync(Page); |
| 77 | + |
| 78 | + // After logout, should be directed away from authenticated pages |
| 79 | + await Page.WaitForLoadStateAsync(); |
| 80 | + Page.Url.Should().NotContain("/Executive"); |
| 81 | + } |
| 82 | + |
| 83 | + // ── Access denied without auth ── |
| 84 | + |
| 85 | + [Test] |
| 86 | + public async Task UnauthenticatedAccess_ProtectedPage_RedirectsToLogin() |
| 87 | + { |
| 88 | + await Page.GotoAsync($"{BaseUrl}/Department"); |
| 89 | + |
| 90 | + await Page.WaitForLoadStateAsync(); |
| 91 | + Page.Url.Should().Contain("/Account/Login"); |
| 92 | + } |
| 93 | +} |
0 commit comments