Use better asserts for cookies

This commit is contained in:
Pranav K 2017-06-19 17:51:08 -07:00
parent 5bcba9677e
commit be46b5a922
7 changed files with 34 additions and 46 deletions

View File

@ -1,18 +0,0 @@
namespace System.Net
{
public static class Extensions
{
public static Cookie GetCookieWithName(this CookieCollection cookieCollection, string cookieName)
{
foreach (Cookie cookie in cookieCollection)
{
if (cookie.Name == cookieName)
{
return cookie;
}
}
return null;
}
}
}

View File

@ -65,8 +65,8 @@ namespace E2ETests
await ThrowIfResponseStatusNotOk(response);
responseContent = await response.Content.ReadAsStringAsync();
//Correlation cookie not getting cleared after successful signin?
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(".AspNetCore.Correlation.Facebook"));
// Correlation cookie not getting cleared after successful signin?
Assert.DoesNotContain(".AspNetCore.Correlation.Facebook", GetCookieNames());
Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains("AspnetvnextTest@test.com", responseContent, StringComparison.OrdinalIgnoreCase);
@ -83,9 +83,9 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", "AspnetvnextTest@test.com"), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(ExternalLoginCookieName));
// Verify cookie sent
Assert.Contains(IdentityCookieName, GetCookieNames());
Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", "AspnetvnextTest@test.com");
_logger.LogInformation("Verifying if the middleware events were fired");

View File

@ -68,7 +68,7 @@ namespace E2ETests
responseContent = await response.Content.ReadAsStringAsync();
//Correlation cookie not getting cleared after successful signin?
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(".AspNetCore.Correlation.Google"));
Assert.DoesNotContain(".AspNetCore.Correlation.Google", GetCookieNames());
Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
Assert.Contains("AspnetvnextTest@gmail.com", responseContent, StringComparison.OrdinalIgnoreCase);
@ -85,9 +85,9 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", "AspnetvnextTest@gmail.com"), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(ExternalLoginCookieName));
// 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

@ -67,7 +67,7 @@ namespace E2ETests
responseContent = await response.Content.ReadAsStringAsync();
//Correlation cookie not getting cleared after successful signin?
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(".AspNetCore.Correlation.Microsoft"));
Assert.DoesNotContain(".AspNetCore.Correlation.Microsoft", GetCookieNames());
Assert.Equal(_deploymentResult.ApplicationBaseUri + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri);
formParameters = new List<KeyValuePair<string, string>>
@ -83,9 +83,9 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", "microsoft@test.com"), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(ExternalLoginCookieName));
// Verify cookie sent
Assert.Contains(IdentityCookieName, GetCookieNames());
Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", "microsoft@test.com");
_logger.LogInformation("Verifying if the middleware events were fired");

View File

@ -38,7 +38,7 @@ namespace E2ETests
Assert.Equal<string>("code id_token", queryItems["response_type"]);
Assert.Equal<string>("openid profile", queryItems["scope"]);
Assert.Equal<string>("ValidStateData", queryItems["state"]);
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(".AspNetCore.OpenIdConnect.Nonce.protectedString"));
Assert.Contains(".AspNetCore.OpenIdConnect.Nonce.protectedString", GetCookieNames());
// This is just enable the auto-redirect.
_httpClientHandler = new HttpClientHandler();
@ -75,9 +75,9 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", "User3@aspnettest.onmicrosoft.com"), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(ExternalLoginCookieName));
// Verify cookie sent
Assert.Contains(IdentityCookieName, GetCookieNames());
Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", "User3@aspnettest.onmicrosoft.com");
_logger.LogInformation("Verifying if the middleware events were fired");
@ -104,7 +104,7 @@ namespace E2ETests
_httpClient = new HttpClient(handler) { BaseAddress = new Uri(_deploymentResult.ApplicationBaseUri) };
response = await DoPostAsync("Account/LogOff", content);
Assert.Null(handler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.DoesNotContain(IdentityCookieName, GetCookieNames());
Assert.Equal<string>(
"https://login.windows.net/4afbc689-805b-48cf-a24c-d4aa3248a248/oauth2/logout",
response.Headers.Location.AbsoluteUri.Replace(response.Headers.Location.Query, string.Empty));

View File

@ -79,9 +79,9 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", "twitter@test.com"), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(ExternalLoginCookieName));
// Verify cookie sent
Assert.Contains(IdentityCookieName, GetCookieNames());
Assert.DoesNotContain(ExternalLoginCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", "twitter@test.com");
_logger.LogInformation("Verifying if the middleware events were fired");

View File

@ -223,7 +223,7 @@ namespace E2ETests
response = await DoPostAsync("Account/Register", content);
await ThrowIfResponseStatusNotOk(response);
responseContent = await response.Content.ReadAsStringAsync();
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.DoesNotContain(IdentityCookieName, GetCookieNames());
Assert.Contains("<div class=\"text-danger validation-summary-errors\" data-valmsg-summary=\"true\"><ul><li>The password and confirmation password do not match.</li>", responseContent, StringComparison.OrdinalIgnoreCase);
_logger.LogInformation("Server side model validator rejected the user '{email}''s registration as passwords do not match.", generatedEmail);
}
@ -307,7 +307,7 @@ namespace E2ETests
Assert.Contains("www.github.com/aspnet/MusicStore", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("/Images/home-showcase.png", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie cleared on logout
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.DoesNotContain(IdentityCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed out of '{email}''s session", email);
}
@ -329,7 +329,7 @@ namespace E2ETests
responseContent = await response.Content.ReadAsStringAsync();
Assert.Contains("<div class=\"text-danger validation-summary-errors\" data-valmsg-summary=\"true\"><ul><li>Invalid login attempt.</li>", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie not sent
Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.DoesNotContain(IdentityCookieName, GetCookieNames());
_logger.LogInformation("Identity successfully prevented an invalid user login.");
}
@ -352,7 +352,7 @@ namespace E2ETests
Assert.Contains(string.Format("Hello {0}!", email), responseContent, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase);
//Verify cookie sent
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Contains(IdentityCookieName, GetCookieNames());
_logger.LogInformation("Successfully signed in with user '{email}'", email);
}
@ -373,7 +373,7 @@ namespace E2ETests
response = await DoPostAsync("Manage/ChangePassword", content);
responseContent = await response.Content.ReadAsStringAsync();
Assert.Contains("Your password has been changed.", responseContent, StringComparison.OrdinalIgnoreCase);
Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)).GetCookieWithName(IdentityCookieName));
Assert.Contains(IdentityCookieName, GetCookieNames());
_logger.LogInformation("Successfully changed the password for user '{email}'", email);
}
@ -410,8 +410,7 @@ namespace E2ETests
_httpClient.DefaultRequestHeaders.Add("Origin", "http://notpermitteddomain.com");
var response = await DoGetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName));
await ThrowIfResponseStatusNotOk(response);
IEnumerable<string> values;
Assert.False(response.Headers.TryGetValues("Access-Control-Allow-Origin", out values));
Assert.False(response.Headers.TryGetValues("Access-Control-Allow-Origin", out var values));
_httpClient.DefaultRequestHeaders.Remove("Origin");
_httpClient.DefaultRequestHeaders.Add("Origin", "http://example.com");
@ -527,5 +526,12 @@ namespace E2ETests
throw new Exception(string.Format("Received the above response with status code : {0}", response.StatusCode));
}
}
private IEnumerable<string> GetCookieNames()
{
return _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri))
.OfType<Cookie>()
.Select(c => c.Name);
}
}
}