Fixed external login scenarios tests

This commit is contained in:
Kiran Challa 2017-06-28 13:17:03 -07:00
parent f28400f986
commit fc56105d83
4 changed files with 57 additions and 55 deletions

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Xunit; using Xunit;
namespace E2ETests namespace E2ETests
@ -40,33 +41,31 @@ namespace E2ETests
Assert.Equal<string>("ValidStateData", queryItems["state"]); Assert.Equal<string>("ValidStateData", queryItems["state"]);
Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]); Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie //Check for the correlation cookie
Assert.NotEmpty( // Workaround for https://github.com/dotnet/corefx/issues/21250
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues));
.Cast<Cookie>() var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList());
.Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Facebook"))); 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(); _httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) };
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
response = await DoGetAsync("Account/Login");
responseContent = await response.Content.ReadAsStringAsync();
formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("provider", "Facebook"), // Workaround for https://github.com/dotnet/corefx/issues/21250
new KeyValuePair<string, string>("returnUrl", "/"), // The path of the cookie must either match the URI or be a prefix of it due to the fact
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), // 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 //Post a message to the Facebook middleware
response = await DoGetAsync("signin-facebook?code=ValidCode&state=ValidStateData"); response = await DoGetAsync("signin-facebook?code=ValidCode&state=ValidStateData");
await ThrowIfResponseStatusNotOk(response); await ThrowIfResponseStatusNotOk(response);
responseContent = await response.Content.ReadAsStringAsync(); responseContent = await response.Content.ReadAsStringAsync();
// Correlation cookie not getting cleared after successful signin? // 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.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains("AspnetvnextTest@test.com", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("AspnetvnextTest@test.com", responseContent, StringComparison.OrdinalIgnoreCase);

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Xunit; using Xunit;
namespace E2ETests namespace E2ETests
@ -41,26 +42,22 @@ namespace E2ETests
Assert.Equal<string>("ValidStateData", queryItems["state"]); Assert.Equal<string>("ValidStateData", queryItems["state"]);
Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]); Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie //Check for the correlation cookie
Assert.NotEmpty( // Workaround for https://github.com/dotnet/corefx/issues/21250
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues));
.Cast<Cookie>() var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList());
.Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Google"))); 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(); _httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) };
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
response = await DoGetAsync("Account/Login");
responseContent = await response.Content.ReadAsStringAsync();
formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("provider", "Google"), // Workaround for https://github.com/dotnet/corefx/issues/21250
new KeyValuePair<string, string>("returnUrl", "/"), // The path of the cookie must either match the URI or be a prefix of it due to the fact
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), // 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 Google middleware //Post a message to the Google middleware
response = await DoGetAsync("signin-google?code=ValidCode&state=ValidStateData"); response = await DoGetAsync("signin-google?code=ValidCode&state=ValidStateData");
@ -68,7 +65,8 @@ namespace E2ETests
responseContent = await response.Content.ReadAsStringAsync(); responseContent = await response.Content.ReadAsStringAsync();
//Correlation cookie not getting cleared after successful signin? //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.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains("AspnetvnextTest@gmail.com", responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("AspnetvnextTest@gmail.com", responseContent, StringComparison.OrdinalIgnoreCase);
@ -80,6 +78,7 @@ namespace E2ETests
content = new FormUrlEncodedContent(formParameters.ToArray()); content = new FormUrlEncodedContent(formParameters.ToArray());
response = await DoPostAsync("Account/ExternalLoginConfirmation", content); response = await DoPostAsync("Account/ExternalLoginConfirmation", content);
await ThrowIfResponseStatusNotOk(response); await ThrowIfResponseStatusNotOk(response);
responseContent = await response.Content.ReadAsStringAsync(); responseContent = await response.Content.ReadAsStringAsync();
@ -88,6 +87,7 @@ namespace E2ETests
// Verify cookie sent // Verify cookie sent
Assert.Contains(IdentityCookieName, GetCookieNames()); Assert.Contains(IdentityCookieName, GetCookieNames());
Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames()); Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", "AspnetvnextTest@gmail.com"); _logger.LogInformation("Successfully signed in with user '{email}'", "AspnetvnextTest@gmail.com");
_logger.LogInformation("Verifying if the middleware events were fired"); _logger.LogInformation("Verifying if the middleware events were fired");

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using Xunit; using Xunit;
namespace E2ETests namespace E2ETests
@ -41,33 +42,30 @@ namespace E2ETests
Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]); Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie //Check for the correlation cookie
Assert.NotEmpty( // Workaround for https://github.com/dotnet/corefx/issues/21250
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) Assert.True(response.Headers.TryGetValues("Set-Cookie", out var setCookieValues));
.Cast<Cookie>() var setCookie = SetCookieHeaderValue.ParseList(setCookieValues.ToList());
.Where(cookie => cookie.Name.StartsWith(".AspNetCore.Correlation.Microsoft"))); 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(); _httpClientHandler = new HttpClientHandler();
_httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) }; _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) };
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
response = await DoGetAsync("Account/Login");
responseContent = await response.Content.ReadAsStringAsync();
formParameters = new List<KeyValuePair<string, string>>
{ {
new KeyValuePair<string, string>("provider", "Microsoft"), // Workaround for https://github.com/dotnet/corefx/issues/21250
new KeyValuePair<string, string>("returnUrl", "/"), // The path of the cookie must either match the URI or be a prefix of it due to the fact
new KeyValuePair<string, string>("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), // 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 MicrosoftAccount middleware //Post a message to the MicrosoftAccount middleware
response = await DoGetAsync("signin-microsoft?code=ValidCode&state=ValidStateData"); response = await DoGetAsync("signin-microsoft?code=ValidCode&state=ValidStateData");
await ThrowIfResponseStatusNotOk(response); await ThrowIfResponseStatusNotOk(response);
responseContent = await response.Content.ReadAsStringAsync(); responseContent = await response.Content.ReadAsStringAsync();
//Correlation cookie not getting cleared after successful signin? //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); Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
formParameters = new List<KeyValuePair<string, string>> formParameters = new List<KeyValuePair<string, string>>

View File

@ -527,9 +527,14 @@ namespace E2ETests
} }
} }
private IEnumerable<string> GetCookieNames() private IEnumerable<string> 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<Cookie>() .OfType<Cookie>()
.Select(c => c.Name); .Select(c => c.Name);
} }