|
| 1 | +using System.Globalization; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using PythonScriptRunner.Panel.E2E.Tests.Infrastructure; |
| 4 | + |
| 5 | +namespace PythonScriptRunner.Panel.E2E.Tests; |
| 6 | + |
| 7 | +/// <summary>Spec 008: fixed login, expired session, roles and theme in the real panel.</summary> |
| 8 | +public sealed partial class AccessTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python, BrowserFixture browser) |
| 9 | + : PanelTest(postgres, broker, python, browser) |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public async Task Spec008_AC17_FixedLogin_GoesToEntrar_RejectsAWrongPassword_ReturnsToTheRoute_AndLogsOut() |
| 13 | + { |
| 14 | + await StartAsync(); |
| 15 | + var page = await OpenAsync(); |
| 16 | + |
| 17 | + await GoToAsync(page, "execucoes"); |
| 18 | + await Expect(page).ToHaveURLAsync(App.UrlOf("entrar?volta=/execucoes")); |
| 19 | + var form = page.Locator("form"); |
| 20 | + await form.FillLoginAsync(PanelUsers.Executor, "senha-errada"); |
| 21 | + await Expect(page.GetByText("Usuário ou senha inválidos.")).ToBeVisibleAsync(); |
| 22 | + await form.FillLoginAsync(null, PanelUsers.Password); |
| 23 | + |
| 24 | + await Expect(page).ToHaveURLAsync(App.UrlOf("execucoes")); |
| 25 | + var menu = page.Locator(".v-navigation-drawer"); |
| 26 | + await Expect(MenuItem(menu, "Execuções")).ToBeVisibleAsync(); |
| 27 | + await Expect(MenuItem(menu, "Agendamentos")).ToBeVisibleAsync(); |
| 28 | + await Expect(MenuItem(menu, "Auditoria")).ToHaveCountAsync(0); |
| 29 | + await Expect(MenuItem(menu, "Administração")).ToHaveCountAsync(0); |
| 30 | + |
| 31 | + await page.GetByTestId("user-menu").ClickAsync(); |
| 32 | + await page.GetByTestId("logout").ClickAsync(); |
| 33 | + await Expect(page).ToHaveURLAsync(new Regex("/psr/entrar")); |
| 34 | + (await page.APIRequest.GetAsync(App.UrlOf("api/me"))).Status.ShouldBe(401); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public async Task Spec008_AC18_TheEleventhAttemptInAMinute_ShowsTheCountdown_AndTheLoginWorksWhenItEnds() |
| 39 | + { |
| 40 | + await StartAsync(); |
| 41 | + var page = await OpenAsync(); |
| 42 | + await GoToAsync(page, "entrar"); |
| 43 | + var form = page.Locator("form"); |
| 44 | + var submit = form.GetByRole(AriaRole.Button, new LocatorGetByRoleOptions { Name = "Entrar", Exact = true }); |
| 45 | + |
| 46 | + for (var i = 1; i <= 10; i++) |
| 47 | + { |
| 48 | + // Unknown users: the lockout of a user (5 failures) is not what this test is about. |
| 49 | + await form.FillLoginAsync($"ninguem{i}", "senha-errada"); |
| 50 | + await Expect(form.GetByLabel("Senha")).ToHaveValueAsync(string.Empty); |
| 51 | + } |
| 52 | + |
| 53 | + await form.FillLoginAsync(PanelUsers.Author, PanelUsers.Password); |
| 54 | + var countdown = page.GetByTestId("login-throttled"); |
| 55 | + await Expect(countdown).ToContainTextAsync(CountdownText()); |
| 56 | + await Expect(submit).ToBeDisabledAsync(); |
| 57 | + var seconds = int.Parse(CountdownText().Match(await countdown.InnerTextAsync()).Groups[1].Value, CultureInfo.InvariantCulture); |
| 58 | + seconds.ShouldBeInRange(1, 60); |
| 59 | + await Expect(countdown).Not.ToContainTextAsync($"em {seconds} s"); |
| 60 | + |
| 61 | + await Expect(countdown).ToBeHiddenAsync(new LocatorAssertionsToBeHiddenOptions { Timeout = 75_000 }); |
| 62 | + await Expect(submit).ToBeEnabledAsync(); |
| 63 | + await submit.ClickAsync(); |
| 64 | + await Expect(page).ToHaveURLAsync(App.Url.ToString()); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task Spec008_AC19_ExpiredSession_AsksTheSameUserInADialog_AndFinishesTheSave() |
| 69 | + { |
| 70 | + await StartAsync(); |
| 71 | + await App.SaveStageDraftAsync("eco", PanelScripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]); |
| 72 | + var page = await OpenAsync(PanelUsers.Author); |
| 73 | + await GoToAsync(page, "etapas/eco/editar"); |
| 74 | + await page.Locator("[data-testid=field-builder][data-kind=inputs]").GetByTestId("add-field").ClickAsync(); |
| 75 | + await Expect(page.GetByTestId("dirty")).ToBeVisibleAsync(); |
| 76 | + |
| 77 | + // The session ends (the cookie is gone, as when it expires): the save answers 401. |
| 78 | + await page.Context.ClearCookiesAsync(); |
| 79 | + await page.GetByTestId("save").ClickAsync(); |
| 80 | + |
| 81 | + var dialog = page.Locator(".v-dialog").Filter(new LocatorFilterOptions { HasText = "Sua sessão expirou" }); |
| 82 | + await Expect(dialog).ToBeVisibleAsync(); |
| 83 | + await Expect(dialog.GetByLabel("Usuário")).ToHaveValueAsync(PanelUsers.Author); |
| 84 | + await Expect(dialog.GetByLabel("Usuário")).Not.ToBeEditableAsync(); |
| 85 | + await dialog.FillLoginAsync(null, PanelUsers.Password); |
| 86 | + |
| 87 | + await Expect(dialog).ToBeHiddenAsync(); |
| 88 | + await Expect(page.GetByTestId("dirty")).ToBeHiddenAsync(); |
| 89 | + var saved = await App.Host.Admin.GetStageDraftAsync("eco", Ct); |
| 90 | + saved!.Data.Inputs.Select(f => f.Name).ShouldBe(["texto", "campo_2"]); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public async Task Spec008_AC10_Reader_SeesNoActionButtons_AndEditRoutesShowNoPermission() |
| 95 | + { |
| 96 | + await StartAsync(s => s.Worker = false); |
| 97 | + await App.ImportFixtureAsync("calculo-progressao"); |
| 98 | + var run = await App.Host.Queue.EnqueuePipelineAsync("calculo-progressao", PanelScripts.Calculo, cancellationToken: Ct); |
| 99 | + var executor = await OpenAsync(PanelUsers.Executor); |
| 100 | + var reader = await OpenAsync(PanelUsers.Reader); |
| 101 | + |
| 102 | + // What the reader must not see is there for the executor (no worker: the run stays queued and cancellable). |
| 103 | + await GoToAsync(executor, $"execucoes/{run.RunId}"); |
| 104 | + await Expect(executor.GetByTestId("cancel-run")).ToBeVisibleAsync(); |
| 105 | + await GoToAsync(executor, "trilhas/calculo-progressao"); |
| 106 | + await Expect(executor.GetByTestId("run-pipeline")).ToBeVisibleAsync(); |
| 107 | + |
| 108 | + await GoToAsync(reader, $"execucoes/{run.RunId}"); |
| 109 | + await Expect(reader.GetByTestId("run-status")).ToBeVisibleAsync(); |
| 110 | + await Expect(reader.GetByTestId("cancel-run")).ToHaveCountAsync(0); |
| 111 | + await GoToAsync(reader, "trilhas/calculo-progressao"); |
| 112 | + await Expect(reader.GetByTestId("versions-table")).ToBeVisibleAsync(); |
| 113 | + await ExpectNoneAsync(reader, "run-pipeline", "edit-pipeline", "delete-pipeline"); |
| 114 | + await GoToAsync(reader, "etapas"); |
| 115 | + await Expect(reader.Locator("[data-key=progressao-termos]")).ToBeVisibleAsync(); |
| 116 | + await ExpectNoneAsync(reader, "create", "import-zip"); |
| 117 | + await GoToAsync(reader, "etapas/progressao-termos"); |
| 118 | + await Expect(reader.GetByTestId("versions-table")).ToBeVisibleAsync(); |
| 119 | + await ExpectNoneAsync(reader, "edit-stage", "delete-stage"); |
| 120 | + await GoToAsync(reader, "agendamentos"); |
| 121 | + await Expect(reader.GetByTestId("schedules-table")).ToBeVisibleAsync(); |
| 122 | + await ExpectNoneAsync(reader, "create-schedule"); |
| 123 | + |
| 124 | + foreach (var path in new[] { "etapas/progressao-termos/editar", "trilhas/calculo-progressao/editar", "auditoria", "administracao" }) |
| 125 | + { |
| 126 | + await GoToAsync(reader, path); |
| 127 | + await Expect(reader.GetByRole(AriaRole.Heading, new PageGetByRoleOptions { Name = "Sem permissão" })).ToBeVisibleAsync(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public async Task Spec008_AC12_FollowsTheDarkSystemTheme_AndKeepsTheManualChoiceAfterReload() |
| 133 | + { |
| 134 | + await StartAsync(); |
| 135 | + var page = await OpenAsync(PanelUsers.Reader, o => o.ColorScheme = ColorScheme.Dark); |
| 136 | + await GoToAsync(page, string.Empty); |
| 137 | + var app = page.Locator(".v-application"); |
| 138 | + await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--dark\b")); |
| 139 | + |
| 140 | + await page.GetByTestId("theme-menu").ClickAsync(); |
| 141 | + await page.GetByTestId("theme-light").ClickAsync(); |
| 142 | + await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--light\b")); |
| 143 | + await page.ReloadAsync(); |
| 144 | + |
| 145 | + await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--light\b")); |
| 146 | + } |
| 147 | + |
| 148 | + private static ILocator MenuItem(ILocator menu, string text) => |
| 149 | + menu.GetByText(text, new LocatorGetByTextOptions { Exact = true }); |
| 150 | + |
| 151 | + private static async Task ExpectNoneAsync(IPage page, params string[] testIds) |
| 152 | + { |
| 153 | + foreach (var id in testIds) |
| 154 | + { |
| 155 | + await Expect(page.GetByTestId(id)).ToHaveCountAsync(0); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + [GeneratedRegex(@"Tente de novo em (\d+) s")] |
| 160 | + private static partial Regex CountdownText(); |
| 161 | +} |
0 commit comments