diff --git a/test/MusicStore.E2ETests/Common/Extensions.cs b/test/MusicStore.E2ETests/Common/Extensions.cs deleted file mode 100644 index 8bac4b6585..0000000000 --- a/test/MusicStore.E2ETests/Common/Extensions.cs +++ /dev/null @@ -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; - } - } -} diff --git a/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs index 76640c1d0d..e91befd727 100644 --- a/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/FacebookLoginScenarios.cs @@ -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"); diff --git a/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs index facd839dce..8d1d874352 100644 --- a/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/GoogleLoginScenarios.cs @@ -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"); diff --git a/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs b/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs index 01ecdd2295..5a44760478 100644 --- a/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/MicrosoftAccountAuthenticationScenarios.cs @@ -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> @@ -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"); diff --git a/test/MusicStore.E2ETests/Implementation/OpenIdConnectLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/OpenIdConnectLoginScenarios.cs index c05c86e25f..12515a35ad 100644 --- a/test/MusicStore.E2ETests/Implementation/OpenIdConnectLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/OpenIdConnectLoginScenarios.cs @@ -38,7 +38,7 @@ namespace E2ETests Assert.Equal("code id_token", queryItems["response_type"]); Assert.Equal("openid profile", queryItems["scope"]); Assert.Equal("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( "https://login.windows.net/4afbc689-805b-48cf-a24c-d4aa3248a248/oauth2/logout", response.Headers.Location.AbsoluteUri.Replace(response.Headers.Location.Query, string.Empty)); diff --git a/test/MusicStore.E2ETests/Implementation/TwitterLoginScenarios.cs b/test/MusicStore.E2ETests/Implementation/TwitterLoginScenarios.cs index 0def05c28c..7c70b4787e 100644 --- a/test/MusicStore.E2ETests/Implementation/TwitterLoginScenarios.cs +++ b/test/MusicStore.E2ETests/Implementation/TwitterLoginScenarios.cs @@ -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"); diff --git a/test/MusicStore.E2ETests/Implementation/Validator.cs b/test/MusicStore.E2ETests/Implementation/Validator.cs index 1dd4c44f80..7857d4bbc9 100644 --- a/test/MusicStore.E2ETests/Implementation/Validator.cs +++ b/test/MusicStore.E2ETests/Implementation/Validator.cs @@ -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("
  • The password and confirmation password do not match.
  • ", 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("
    • Invalid login attempt.
    • ", 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 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 GetCookieNames() + { + return _httpClientHandler.CookieContainer.GetCookies(new Uri(_deploymentResult.ApplicationBaseUri)) + .OfType() + .Select(c => c.Name); + } } } \ No newline at end of file