|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http.Headers; |
| 3 | +using System.Security.Claims; |
| 4 | +using System.Text.Encodings.Web; |
| 5 | +using AngleSharp.Html.Dom; |
| 6 | +using Microsoft.AspNetCore.Authentication; |
| 7 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 8 | +using Microsoft.AspNetCore.TestHost; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | +using RazorPagesProject.Services; |
| 13 | +using RazorPagesProject.Tests.Helpers; |
| 14 | + |
| 15 | +namespace RazorPagesProject.Tests.IntegrationTests; |
| 16 | + |
| 17 | +[TestClass] |
| 18 | +public class AuthTests |
| 19 | +{ |
| 20 | + |
| 21 | + private static CustomWebApplicationFactory<Program> _factory; |
| 22 | + |
| 23 | + [ClassInitialize] |
| 24 | + public static void AssemblyInitialize(TestContext _) |
| 25 | + { |
| 26 | + _factory = new CustomWebApplicationFactory<Program>(); |
| 27 | + } |
| 28 | + |
| 29 | + [ClassCleanup(ClassCleanupBehavior.EndOfClass)] |
| 30 | + public static void AssemblyCleanup(TestContext _) |
| 31 | + { |
| 32 | + _factory.Dispose(); |
| 33 | + } |
| 34 | + |
| 35 | + // <snippet1> |
| 36 | + [TestMethod] |
| 37 | + public async Task Get_GithubProfilePageCanGetAGithubUser() |
| 38 | + { |
| 39 | + // Arrange |
| 40 | + void ConfigureTestServices(IServiceCollection services) => |
| 41 | + services.AddSingleton<IGithubClient>(new TestGithubClient()); |
| 42 | + var client = _factory |
| 43 | + .WithWebHostBuilder(builder => |
| 44 | + builder.ConfigureTestServices(ConfigureTestServices)) |
| 45 | + .CreateClient(); |
| 46 | + |
| 47 | + // Act |
| 48 | + var profile = await client.GetAsync("/GithubProfile"); |
| 49 | + Assert.AreEqual(HttpStatusCode.OK, profile.StatusCode); |
| 50 | + var profileHtml = await HtmlHelpers.GetDocumentAsync(profile); |
| 51 | + |
| 52 | + var profileWithUserName = await client.SendAsync( |
| 53 | + (IHtmlFormElement)profileHtml.QuerySelector("#user-profile"), |
| 54 | + new Dictionary<string, string> { ["Input_UserName"] = "user" }); |
| 55 | + |
| 56 | + // Assert |
| 57 | + Assert.AreEqual(HttpStatusCode.OK, profileWithUserName.StatusCode); |
| 58 | + var profileWithUserHtml = |
| 59 | + await HtmlHelpers.GetDocumentAsync(profileWithUserName); |
| 60 | + var userLogin = profileWithUserHtml.QuerySelector("#user-login"); |
| 61 | + Assert.AreEqual("user", userLogin.TextContent); |
| 62 | + } |
| 63 | + |
| 64 | + public class TestGithubClient : IGithubClient |
| 65 | + { |
| 66 | + public Task<GithubUser> GetUserAsync(string userName) |
| 67 | + { |
| 68 | + if (userName == "user") |
| 69 | + { |
| 70 | + return Task.FromResult( |
| 71 | + new GithubUser |
| 72 | + { |
| 73 | + Login = "user", |
| 74 | + Company = "Contoso Blockchain", |
| 75 | + Name = "John Doe" |
| 76 | + }); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + return Task.FromResult<GithubUser>(null); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + // </snippet1> |
| 85 | + |
| 86 | + // <snippet2> |
| 87 | + [TestMethod] |
| 88 | + public async Task Get_SecurePageRedirectsAnUnauthenticatedUser() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + var client = _factory.CreateClient( |
| 92 | + new WebApplicationFactoryClientOptions |
| 93 | + { |
| 94 | + AllowAutoRedirect = false |
| 95 | + }); |
| 96 | + |
| 97 | + // Act |
| 98 | + var response = await client.GetAsync("/SecurePage"); |
| 99 | + |
| 100 | + // Assert |
| 101 | + Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); |
| 102 | + StringAssert.StartsWith(response.Headers.Location.OriginalString, "http://localhost/Identity/Account/Login"); |
| 103 | + } |
| 104 | + // </snippet2> |
| 105 | + |
| 106 | + // <snippet3> |
| 107 | + [TestMethod] |
| 108 | + public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser() |
| 109 | + { |
| 110 | + // Arrange |
| 111 | + var client = _factory.WithWebHostBuilder(builder => |
| 112 | + { |
| 113 | + builder.ConfigureTestServices(services => |
| 114 | + { |
| 115 | + services.AddAuthentication(defaultScheme: "TestScheme") |
| 116 | + .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>( |
| 117 | + "TestScheme", options => { }); |
| 118 | + }); |
| 119 | + }) |
| 120 | + .CreateClient(new WebApplicationFactoryClientOptions |
| 121 | + { |
| 122 | + AllowAutoRedirect = false, |
| 123 | + }); |
| 124 | + |
| 125 | + client.DefaultRequestHeaders.Authorization = |
| 126 | + new AuthenticationHeaderValue(scheme: "TestScheme"); |
| 127 | + |
| 128 | + //Act |
| 129 | + var response = await client.GetAsync("/SecurePage"); |
| 130 | + |
| 131 | + // Assert |
| 132 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 133 | + } |
| 134 | + // </snippet3> |
| 135 | +} |
| 136 | + |
| 137 | +// <snippet4> |
| 138 | +public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions> |
| 139 | +{ |
| 140 | + public TestAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, |
| 141 | + ILoggerFactory logger, UrlEncoder encoder) |
| 142 | + : base(options, logger, encoder) |
| 143 | + { |
| 144 | + } |
| 145 | + |
| 146 | + protected override Task<AuthenticateResult> HandleAuthenticateAsync() |
| 147 | + { |
| 148 | + var claims = new[] { new Claim(ClaimTypes.Name, "Test user") }; |
| 149 | + var identity = new ClaimsIdentity(claims, "Test"); |
| 150 | + var principal = new ClaimsPrincipal(identity); |
| 151 | + var ticket = new AuthenticationTicket(principal, "TestScheme"); |
| 152 | + |
| 153 | + var result = AuthenticateResult.Success(ticket); |
| 154 | + |
| 155 | + return Task.FromResult(result); |
| 156 | + } |
| 157 | +} |
| 158 | +// </snippet4> |
0 commit comments