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.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<string>("ValidStateData", queryItems["state"]);
Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie
Assert.NotEmpty(
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri))
.Cast<Cookie>()
.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<KeyValuePair<string, string>>
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
{
new KeyValuePair<string, string>("provider", "Facebook"),
new KeyValuePair<string, string>("returnUrl", "/"),
new KeyValuePair<string, string>("__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);

View File

@ -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<string>("ValidStateData", queryItems["state"]);
Assert.Equal<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie
Assert.NotEmpty(
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri))
.Cast<Cookie>()
.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<KeyValuePair<string, string>>
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
{
new KeyValuePair<string, string>("provider", "Google"),
new KeyValuePair<string, string>("returnUrl", "/"),
new KeyValuePair<string, string>("__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");

View File

@ -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<string>("custom", queryItems["custom_redirect_uri"]);
//Check for the correlation cookie
Assert.NotEmpty(
_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri))
.Cast<Cookie>()
.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<KeyValuePair<string, string>>
foreach (var header in SetCookieHeaderValue.ParseList(response.Headers.GetValues("Set-Cookie").ToList()))
{
new KeyValuePair<string, string>("provider", "Microsoft"),
new KeyValuePair<string, string>("returnUrl", "/"),
new KeyValuePair<string, string>("__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<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>()
.Select(c => c.Name);
}