|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using System.Web; |
| 5 | +using System.Xml.Linq; |
| 6 | + |
| 7 | +namespace EssentialCSharp.Web.E2ETests; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Anonymous, read-only checks against a deployed site. They gate promotion to production, so they cover |
| 11 | +/// configuration that unit and integration tests cannot see (Key Vault wiring, real content, external |
| 12 | +/// login registration, base URL) while staying well inside the site's per-IP rate limits. |
| 13 | +/// </summary> |
| 14 | +[Category("Deployed")] |
| 15 | +[RequiresDeployedSite] |
| 16 | +public partial class DeployedSmokeTests |
| 17 | +{ |
| 18 | + [Test] |
| 19 | + [Arguments("health")] |
| 20 | + [Arguments("alive")] |
| 21 | + public async Task HealthEndpoint_ReturnsOk(string path) |
| 22 | + { |
| 23 | + using HttpClient client = DeployedSite.CreateClient(); |
| 24 | + |
| 25 | + using HttpResponseMessage response = await client.GetAsync(path); |
| 26 | + |
| 27 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public async Task Version_ReportsDeployedCommit() |
| 32 | + { |
| 33 | + using HttpClient client = DeployedSite.CreateClient(); |
| 34 | + |
| 35 | + using HttpResponseMessage response = await client.GetAsync("version"); |
| 36 | + |
| 37 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 38 | + await Assert.That(response).HasJsonContent(); |
| 39 | + string body = await response.Content.ReadAsStringAsync(); |
| 40 | + await Assert.That(body).IsValidJsonObject(); |
| 41 | + |
| 42 | + using JsonDocument payload = JsonDocument.Parse(body); |
| 43 | + string? commit = payload.RootElement.GetProperty("commit").GetString(); |
| 44 | + if (DeployedSite.ExpectedCommit is { } expectedCommit) |
| 45 | + { |
| 46 | + await Assert.That(commit).IsEqualTo(expectedCommit); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + await Assert.That(commit).IsNotNullOrWhiteSpace(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public async Task HomePage_RendersWithCanonicalUrlOnDeployedOrigin() |
| 56 | + { |
| 57 | + using HttpClient client = DeployedSite.CreateClient(); |
| 58 | + |
| 59 | + using HttpResponseMessage response = await client.GetAsync("home"); |
| 60 | + string body = await response.Content.ReadAsStringAsync(); |
| 61 | + |
| 62 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 63 | + await Assert.That(body).Contains("Explore Essential C#"); |
| 64 | + // SiteSettings:BaseUrl drives canonical links and the sitemap; a wrong value silently hurts SEO. |
| 65 | + await Assert.That(body).Contains($"<link rel=\"canonical\" href=\"{new Uri(DeployedSite.RequiredBaseUrl, "home")}\""); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public async Task Sitemap_ListsFullBook_AndAChapterPageRenders() |
| 70 | + { |
| 71 | + using HttpClient client = DeployedSite.CreateClient(); |
| 72 | + |
| 73 | + using HttpResponseMessage sitemapResponse = await client.GetAsync("sitemap.xml"); |
| 74 | + await Assert.That(sitemapResponse.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 75 | + |
| 76 | + XDocument sitemap = XDocument.Parse(await sitemapResponse.Content.ReadAsStringAsync()); |
| 77 | + List<Uri> locations = sitemap.Descendants() |
| 78 | + .Where(element => element.Name.LocalName == "loc") |
| 79 | + .Select(element => new Uri(element.Value.Trim())) |
| 80 | + .ToList(); |
| 81 | + |
| 82 | + // Placeholder content (used when the content package is missing) has only a handful of pages; |
| 83 | + // the real book has hundreds. This catches an image built without the book content. |
| 84 | + await Assert.That(locations.Count).IsGreaterThan(100); |
| 85 | + await Assert.That(locations.All(uri => uri.Host == DeployedSite.RequiredBaseUrl.Host)).IsTrue(); |
| 86 | + |
| 87 | + // One content page from the middle of the book (content pages share a tight per-IP rate limit). |
| 88 | + Uri chapterPage = locations[locations.Count / 2]; |
| 89 | + using HttpResponseMessage pageResponse = await client.GetAsync(chapterPage.PathAndQuery.TrimStart('/')); |
| 90 | + string pageBody = await pageResponse.Content.ReadAsStringAsync(); |
| 91 | + |
| 92 | + await Assert.That(pageResponse.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 93 | + await Assert.That(pageBody).Contains("window.TOC_DATA"); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public async Task LoginPage_RendersCaptchaAndExternalProviders() |
| 98 | + { |
| 99 | + using HttpClient client = DeployedSite.CreateClient(); |
| 100 | + |
| 101 | + using HttpResponseMessage response = await client.GetAsync("Identity/Account/Login"); |
| 102 | + string body = await response.Content.ReadAsStringAsync(); |
| 103 | + |
| 104 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 105 | + await Assert.That(CaptchaSiteKeyPattern().IsMatch(body)).IsTrue() |
| 106 | + .Because("login requires hCaptcha, which fails closed when its keys are missing"); |
| 107 | + await Assert.That(body).Contains("name=\"provider\" value=\"GitHub\""); |
| 108 | + await Assert.That(body).Contains("name=\"provider\" value=\"Microsoft\""); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Posts the external-login form without following the redirect. Verifies the provider is registered |
| 113 | + /// with a client id and the callback points back at this deployment; the OAuth round trip itself |
| 114 | + /// (secrets, consent, account linking) is out of scope for an automated smoke test. |
| 115 | + /// </summary> |
| 116 | + [Test] |
| 117 | + [Arguments("GitHub", "github.com", "/login/oauth/authorize", "signin-github")] |
| 118 | + [Arguments("Microsoft", "login.microsoftonline.com", "/common/oauth2/v2.0/authorize", "signin-microsoft")] |
| 119 | + public async Task ExternalLogin_RedirectsToProviderWithDeployedCallback( |
| 120 | + string provider, string expectedHost, string expectedPath, string expectedCallbackPath) |
| 121 | + { |
| 122 | + using HttpClient client = DeployedSite.CreateClient(); |
| 123 | + |
| 124 | + using HttpResponseMessage loginPage = await client.GetAsync("Identity/Account/Login"); |
| 125 | + string loginBody = await loginPage.Content.ReadAsStringAsync(); |
| 126 | + Match token = AntiforgeryTokenPattern().Match(loginBody); |
| 127 | + await Assert.That(token.Success).IsTrue(); |
| 128 | + |
| 129 | + using var form = new FormUrlEncodedContent(new Dictionary<string, string> |
| 130 | + { |
| 131 | + ["provider"] = provider, |
| 132 | + ["__RequestVerificationToken"] = WebUtility.HtmlDecode(token.Groups["token"].Value), |
| 133 | + }); |
| 134 | + using HttpResponseMessage response = await client.PostAsync("Identity/Account/ExternalLogin", form); |
| 135 | + |
| 136 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.Redirect); |
| 137 | + Uri location = await Assert.That(response.Headers.Location).IsNotNull(); |
| 138 | + await Assert.That(location.Host).IsEqualTo(expectedHost); |
| 139 | + await Assert.That(location.AbsolutePath).IsEqualTo(expectedPath); |
| 140 | + |
| 141 | + var query = HttpUtility.ParseQueryString(location.Query); |
| 142 | + await Assert.That(query["client_id"]).IsNotNullOrWhiteSpace(); |
| 143 | + await Assert.That(query["redirect_uri"]).IsEqualTo(new Uri(DeployedSite.RequiredBaseUrl, expectedCallbackPath).ToString()); |
| 144 | + } |
| 145 | + |
| 146 | + [GeneratedRegex("(?:data-sitekey=\"|siteKey:\\s*\")(?<key>[^\"]+)\"")] |
| 147 | + private static partial Regex CaptchaSiteKeyPattern(); |
| 148 | + |
| 149 | + [GeneratedRegex("name=\"__RequestVerificationToken\" type=\"hidden\" value=\"(?<token>[^\"]+)\"")] |
| 150 | + private static partial Regex AntiforgeryTokenPattern(); |
| 151 | +} |
0 commit comments