From fc56105d833198421fc16fb1d9e0f13eacffcddb Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Wed, 28 Jun 2017 13:17:03 -0700 Subject: [PATCH] Fixed external login scenarios tests --- .../Implementation/FacebookLoginScenarios.cs | 33 +++++++++-------- .../Implementation/GoogleLoginScenarios.cs | 36 +++++++++---------- ...MicrosoftAccountAuthenticationScenarios.cs | 34 +++++++++--------- .../Implementation/Validator.cs | 9 +++-- 4 files changed, 57 insertions(+), 55 deletions(-) diff --git a/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs index e91befd727..e831525bc6 100644 --- a/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs @@ -1,12 +1,13 @@ using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; +using Microsoft.Net.Http.Headers; using Xunit; namespace E2ETests @@ -40,33 +41,31 @@ namespace E2ETests Assert.Equal("ValidStateData", queryItems["state"]); Assert.Equal("custom", queryItems["custom_redirect_uri"]); //Check for the correlation cookie - Assert.NotEmpty( - _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) - .Cast() - .Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Facebook"))); + // Workaround for https://github.com/dotnet/corefx/issues/21250 + Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues)); + var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList()); + Assert.Contains(setCookie, c => c.Name.StartsWith(".AspNetCore.Correlation.Facebook", StringComparison.OrdinalIgnoreCase)); - //This is just to generate a correlation cookie. Previous step would generate this cookie, but we have reset the handler now. + // This is just enable the auto-redirect. _httpClientHandler = new HttpClientHandler(); _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; - - response = await DoGetAsync("Account/Login"); - responseContent = await response.Content.ReadAsStringAsync(); - formParameters = new List> + foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList())) { - new KeyValuePair("provider", "Facebook"), - new KeyValuePair("returnUrl", "/"), - new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), - }; + // Workaround for https://github.com/dotnet/corefx/issues/21250 + // The path of the cookie must either match the URI or be a prefix of it due to the fact + // that CookieContainer doesn't support the latest version of the standard for cookies. + var uri = new Uri(new Uri(_deploymentResult.ApplicationBaseUri), header.Path.ToString()); + _httpClientHandler.CookieContainer.Add(uri, new Cookie(header.Name.ToString(), header.Value.ToString())); + } - content = new FormUrlEncodedContent(formParameters.ToArray()); - response = await DoPostAsync("Account/ExternalLogin", content); //Post a message to the Facebook middleware response = await DoGetAsync("signin-facebook?code=ValidCode&state=ValidStateData"); await ThrowIfResponseStatusNotOk(response); responseContent = await response.Content.ReadAsStringAsync(); // Correlation cookie not getting cleared after successful signin? - Assert.DoesNotContain(".AspNetCore.Correlation.Facebook", GetCookieNames()); + Assert.DoesNotContain(".AspNetCore.Correlation.Facebook", GetCookieNames(_deploymentResult.ApplicationBaseUri + "signin-facebook")); + Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri); Assert.Contains("AspnetvnextTest@test.com", responseContent, StringComparison.OrdinalIgnoreCase); diff --git a/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs index 8d1d874352..bd0cd68d6e 100644 --- a/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs @@ -1,12 +1,13 @@ using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; +using Microsoft.Net.Http.Headers; using Xunit; namespace E2ETests @@ -41,26 +42,22 @@ namespace E2ETests Assert.Equal("ValidStateData", queryItems["state"]); Assert.Equal("custom", queryItems["custom_redirect_uri"]); //Check for the correlation cookie - Assert.NotEmpty( - _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) - .Cast() - .Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Google"))); + // Workaround for https://github.com/dotnet/corefx/issues/21250 + Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues)); + var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList()); + Assert.Contains(setCookie, c => c.Name.StartsWith(".AspNetCore.Correlation.Google", StringComparison.OrdinalIgnoreCase)); - //This is just to generate a correlation cookie. Previous step would generate this cookie, but we have reset the handler now. + // This is just enable the auto-redirect. _httpClientHandler = new HttpClientHandler(); _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; - - response = await DoGetAsync("Account/Login"); - responseContent = await response.Content.ReadAsStringAsync(); - formParameters = new List> + foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList())) { - new KeyValuePair("provider", "Google"), - new KeyValuePair("returnUrl", "/"), - new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), - }; - - content = new FormUrlEncodedContent(formParameters.ToArray()); - response = await DoPostAsync("Account/ExternalLogin", content); + // Workaround for https://github.com/dotnet/corefx/issues/21250 + // The path of the cookie must either match the URI or be a prefix of it due to the fact + // that CookieContainer doesn't support the latest version of the standard for cookies. + var uri = new Uri(new Uri(_deploymentResult.ApplicationBaseUri), header.Path.ToString()); + _httpClientHandler.CookieContainer.Add(uri, new Cookie(header.Name.ToString(), header.Value.ToString())); + } //Post a message to the Google middleware response = await DoGetAsync("signin-google?code=ValidCode&state=ValidStateData"); @@ -68,7 +65,8 @@ namespace E2ETests responseContent = await response.Content.ReadAsStringAsync(); //Correlation cookie not getting cleared after successful signin? - Assert.DoesNotContain(".AspNetCore.Correlation.Google", GetCookieNames()); + Assert.DoesNotContain(".AspNetCore.Correlation.Google", GetCookieNames(_deploymentResult.ApplicationBaseUri + "signin-google")); + Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri); Assert.Contains("AspnetvnextTest@gmail.com", responseContent, StringComparison.OrdinalIgnoreCase); @@ -80,6 +78,7 @@ namespace E2ETests content = new FormUrlEncodedContent(formParameters.ToArray()); response = await DoPostAsync("Account/ExternalLoginConfirmation", content); + await ThrowIfResponseStatusNotOk(response); responseContent = await response.Content.ReadAsStringAsync(); @@ -88,6 +87,7 @@ namespace E2ETests // Verify cookie sent Assert.Contains(IdentityCookieName, GetCookieNames()); Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames()); + _logger.LogInformation("Successfully signed in with user '{email}'", "AspnetvnextTest@gmail.com"); _logger.LogInformation("Verifying if the middleware events were fired"); diff --git a/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs b/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs index 5a44760478..534e54436d 100644 --- a/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs @@ -1,12 +1,13 @@ using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; +using Microsoft.Net.Http.Headers; using Xunit; namespace E2ETests @@ -41,33 +42,30 @@ namespace E2ETests Assert.Equal("custom", queryItems["custom_redirect_uri"]); //Check for the correlation cookie - Assert.NotEmpty( - _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) - .Cast() - .Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Microsoft"))); + // Workaround for https://github.com/dotnet/corefx/issues/21250 + Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues)); + var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList()); + Assert.Contains(setCookie, c => c.Name.StartsWith(".AspNetCore.Correlation.Microsoft", StringComparison.OrdinalIgnoreCase)); - //This is just to generate a correlation cookie. Previous step would generate this cookie, but we have reset the handler now. + // This is just enable the auto-redirect. _httpClientHandler = new HttpClientHandler(); _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; - - response = await DoGetAsync("Account/Login"); - responseContent = await response.Content.ReadAsStringAsync(); - formParameters = new List> + foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList())) { - new KeyValuePair("provider", "Microsoft"), - new KeyValuePair("returnUrl", "/"), - new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), - }; - - content = new FormUrlEncodedContent(formParameters.ToArray()); - response = await DoPostAsync("Account/ExternalLogin", content); + // Workaround for https://github.com/dotnet/corefx/issues/21250 + // The path of the cookie must either match the URI or be a prefix of it due to the fact + // that CookieContainer doesn't support the latest version of the standard for cookies. + var uri = new Uri(new Uri(_deploymentResult.ApplicationBaseUri), header.Path.ToString()); + _httpClientHandler.CookieContainer.Add(uri, new Cookie(header.Name.ToString(), header.Value.ToString())); + } + //Post a message to the MicrosoftAccount middleware response = await DoGetAsync("signin-microsoft?code=ValidCode&state=ValidStateData"); await ThrowIfResponseStatusNotOk(response); responseContent = await response.Content.ReadAsStringAsync(); //Correlation cookie not getting cleared after successful signin? - Assert.DoesNotContain(".AspNetCore.Correlation.Microsoft", GetCookieNames()); + Assert.DoesNotContain(".AspNetCore.Correlation.Microsoft", GetCookieNames(_deploymentResult.ApplicationBaseUri + "signin-microsoft")); Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri); formParameters = new List> diff --git a/test/MusicStore.E2ETests/Implementation/Validator.cs b/test/MusicStore.E2ETests/Implementation/Validator.cs index 7857d4bbc9..87c83a15fa 100644 --- a/test/MusicStore.E2ETests/Implementation/Validator.cs +++ b/test/MusicStore.E2ETests/Implementation/Validator.cs @@ -527,9 +527,14 @@ namespace E2ETests } } - private IEnumerable GetCookieNames() + private IEnumerable GetCookieNames(string uri = null) { - return _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) + if (uri == null) + { + uri = _deploymentResult.ApplicationBaseUri; + } + + return _httpClientHandler.CookieContainer.GetCookies(new Uri(uri)) .OfType() .Select(c => c.Name); }