From 1ef62a40b38be955baddb88029b4a69184bc9b25 Mon Sep 17 00:00:00 2001 From: Troy Dai Date: Tue, 9 Aug 2016 16:15:49 -0700 Subject: [PATCH] Add test for CookieAuthentication --- .../Cookies/CookieMiddlewareTests.cs | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.Authentication.Test/Cookies/CookieMiddlewareTests.cs b/test/Microsoft.AspNetCore.Authentication.Test/Cookies/CookieMiddlewareTests.cs index fa4a4502ff..a5283c80f1 100644 --- a/test/Microsoft.AspNetCore.Authentication.Test/Cookies/CookieMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.Test/Cookies/CookieMiddlewareTests.cs @@ -90,7 +90,6 @@ namespace Microsoft.AspNetCore.Authentication.Cookies Assert.True(responded.Single().StartsWith("http://example.com/Account/Login")); } - [Theory] [InlineData(true)] [InlineData(false)] @@ -1052,6 +1051,53 @@ namespace Microsoft.AspNetCore.Authentication.Cookies Assert.Equal("?ReturnUrl=%2F", location.Query); } + [Fact] + public async Task RedirectUriIsHoneredAfterSignin() + { + var options = new CookieAuthenticationOptions + { + LoginPath = "/testpath", + CookieName = "TestCookie" + }; + + var server = CreateServer(options, async context => + { + await context.Authentication.SignInAsync( + CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", CookieAuthenticationDefaults.AuthenticationScheme))), + new AuthenticationProperties { RedirectUri = "/redirect_test" }); + }); + var transaction = await SendAsync(server, "http://example.com/testpath"); + + Assert.NotEmpty(transaction.SetCookie); + Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); + Assert.Equal("/redirect_test", transaction.Response.Headers.Location.ToString()); + } + + [Fact] + public async Task EnsurePrecedenceOfRedirectUriAfterSignin() + { + var options = new CookieAuthenticationOptions + { + LoginPath = "/testpath", + ReturnUrlParameter = "return", + CookieName = "TestCookie" + }; + + var server = CreateServer(options, async context => + { + await context.Authentication.SignInAsync( + CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", CookieAuthenticationDefaults.AuthenticationScheme))), + new AuthenticationProperties { RedirectUri = "/redirect_test" }); + }); + var transaction = await SendAsync(server, "http://example.com/testpath?return=%2Fret_path_2"); + + Assert.NotEmpty(transaction.SetCookie); + Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); + Assert.Equal("/ret_path_2", transaction.Response.Headers.Location.ToString()); + } + [Fact] public async Task NestedMapWillNotAffectAccessDenied() {