Browse Source

fix some issue and cover by test (#669)

main
Cédric Michel 4 years ago
committed by GitHub
parent
commit
87ae6c618c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/Web/Areas/Identity/Pages/Account/Login.cshtml.cs
  2. 5
      src/Web/Controllers/ManageController.cs
  3. 3
      src/Web/Controllers/OrderController.cs
  4. 8
      src/Web/Pages/Basket/Checkout.cshtml.cs
  5. 4
      src/Web/ViewModels/Manage/IndexViewModel.cs
  6. 2
      src/Web/Web.csproj
  7. 67
      tests/FunctionalTests/Web/Controllers/AccountControllerSignIn.cs
  8. 39
      tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs
  9. 68
      tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
  10. 100
      tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs
  11. 27
      tests/FunctionalTests/Web/WebPageHelpers.cs
  12. 9
      tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs
  13. 3
      tests/UnitTests/UnitTests.csproj

8
src/Web/Areas/Identity/Pages/Account/Login.cshtml.cs

@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.Extensions.Logging;
namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account;

5
src/Web/Controllers/ManageController.cs

@ -1,8 +1,5 @@
using System;
using System.Linq;
using System.Text;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

3
src/Web/Controllers/OrderController.cs

@ -1,5 +1,4 @@
using System.Threading.Tasks;
using MediatR;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.Web.Features.MyOrders;

8
src/Web/Pages/Basket/Checkout.cshtml.cs

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

4
src/Web/ViewModels/Manage/IndexViewModel.cs

@ -4,7 +4,7 @@ namespace Microsoft.eShopWeb.Web.ViewModels.Manage;
public class IndexViewModel
{
public string Username { get; set; }
public string? Username { get; set; }
public bool IsEmailConfirmed { get; set; }
@ -16,5 +16,5 @@ public class IndexViewModel
[Display(Name = "Phone number")]
public string PhoneNumber { get; set; }
public string StatusMessage { get; set; }
public string? StatusMessage { get; set; }
}

2
src/Web/Web.csproj

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Microsoft.eShopWeb.Web</RootNamespace>
<UserSecretsId>aspnet-Web2-1FA3F72E-E7E3-4360-9E49-1CCCD7FE85F7</UserSecretsId>

67
tests/FunctionalTests/Web/Controllers/AccountControllerSignIn.cs

@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
@ -54,35 +49,65 @@ public class AccountControllerSignIn : IClassFixture<TestApplication>
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse);
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse);
Assert.True(token.Length > 50);
}
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
[Fact]
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
{
var getResponse = await Client.GetAsync("/identity/account/login");
getResponse.EnsureSuccessStatusCode();
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse1);
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("Email", "demouser@microsoft.com"));
keyValues.Add(new KeyValuePair<string, string>("Password", "Pass@word1"));
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", "demouser@microsoft.com"),
new KeyValuePair<string, string>("Password", "Pass@word1"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse1))
};
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/identity/account/login", formContent);
Assert.Equal(HttpStatusCode.Redirect, postResponse.StatusCode);
Assert.Equal(new System.Uri("/", UriKind.Relative), postResponse.Headers.Location);
}
[Fact]
public async Task UpdatePhoneNumberProfile()
{
//Login
var getResponse = await Client.GetAsync("/identity/account/login");
getResponse.EnsureSuccessStatusCode();
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", "demouser@microsoft.com"),
new KeyValuePair<string, string>("Password", "Pass@word1"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse1))
};
var formContent = new FormUrlEncodedContent(keyValues);
await Client.PostAsync("/identity/account/login", formContent);
//Profile page
var profileResponse = await Client.GetAsync("/manage/my-account");
profileResponse.EnsureSuccessStatusCode();
var stringProfileResponse = await profileResponse.Content.ReadAsStringAsync();
//Update phone number
var updateProfileValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Email", "demouser@microsoft.com"),
new KeyValuePair<string, string>("PhoneNumber", "03656565"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringProfileResponse))
};
var updateProfileContent = new FormUrlEncodedContent(updateProfileValues);
var postProfileResponse = await Client.PostAsync("/manage/my-account", updateProfileContent);
Assert.Equal(HttpStatusCode.Redirect, postProfileResponse.StatusCode);
var profileResponse2 = await Client.GetAsync("/manage/my-account");
var stringProfileResponse2 = await profileResponse2.Content.ReadAsStringAsync();
Assert.Contains("03656565", stringProfileResponse2);
}
}

39
tests/FunctionalTests/Web/Pages/BasketPageCheckout.cs → tests/FunctionalTests/Web/Pages/Basket/BasketPageCheckout.cs

@ -1,13 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb.FunctionalTests.Web;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Pages.Basket;
[Collection("Sequential")]
public class BasketPageCheckout : IClassFixture<TestApplication>
@ -22,45 +16,32 @@ public class BasketPageCheckout : IClassFixture<TestApplication>
public HttpClient Client { get; }
private string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
[Fact]
public async Task RedirectsToLoginIfNotAuthenticated()
{
// Arrange & Act
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse1 = await response.Content.ReadAsStringAsync();
string token = GetRequestVerificationToken(stringResponse1);
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse1);
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>();
keyValues.Add(new KeyValuePair<string, string>("id", "2"));
keyValues.Add(new KeyValuePair<string, string>("name", "shirt"));
keyValues.Add(new KeyValuePair<string, string>("price", "19.49"));
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("id", "2"),
new KeyValuePair<string, string>("name", "shirt"),
new KeyValuePair<string, string>("price", "19.49"),
new KeyValuePair<string, string>("__RequestVerificationToken", token)
};
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringResponse = await postResponse.Content.ReadAsStringAsync();
// Assert
Assert.Contains(".NET Black &amp; White Mug", stringResponse);
keyValues.Clear();
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
formContent = new FormUrlEncodedContent(keyValues);
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);

68
tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs

@ -0,0 +1,68 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Pages.Basket;
[Collection("Sequential")]
public class CheckoutTest : IClassFixture<TestApplication>
{
public CheckoutTest(TestApplication factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = true
});
}
public HttpClient Client { get; }
[Fact]
public async Task SucessfullyPay()
{
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("id", "2"),
new KeyValuePair<string, string>("name", "shirt"),
new KeyValuePair<string, string>("price", "19.49"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse))
};
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringPostResponse = await postResponse.Content.ReadAsStringAsync();
Assert.Contains(".NET Black &amp; White Mug", stringPostResponse);
//Load login page
var loginResponse = await Client.GetAsync("/Identity/Account/Login");
var longinKeyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("email", "demouser@microsoft.com"),
new KeyValuePair<string, string>("password", "Pass@word1"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(await loginResponse.Content.ReadAsStringAsync()))
};
var loginFormContent = new FormUrlEncodedContent(longinKeyValues);
var loginPostResponse = await Client.PostAsync("/Identity/Account/Login?ReturnUrl=%2FBasket%2FCheckout", loginFormContent);
var loginStringResponse = await loginPostResponse.Content.ReadAsStringAsync();
//Basket checkout (Pay now)
var checkOutKeyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Items[0].Id", "2"),
new KeyValuePair<string, string>("Items[0].Quantity", "1"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(loginStringResponse))
};
var checkOutContent = new FormUrlEncodedContent(checkOutKeyValues);
var checkOutResponse = await Client.PostAsync("/basket/checkout", checkOutContent);
var stringCheckOutResponse = await checkOutResponse.Content.ReadAsStringAsync();
Assert.Contains("/Basket/Success", checkOutResponse.RequestMessage.RequestUri.ToString());
Assert.Contains("Thanks for your Order!", stringCheckOutResponse);
}
}

100
tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs

@ -0,0 +1,100 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Pages.Basket;
[Collection("Sequential")]
public class IndexTest : IClassFixture<TestApplication>
{
public IndexTest(TestApplication factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = true
});
}
public HttpClient Client { get; }
[Fact]
public async Task OnPostUpdateTo50Successfully()
{
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse1 = await response.Content.ReadAsStringAsync();
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse1);
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("id", "2"),
new KeyValuePair<string, string>("name", "shirt"),
new KeyValuePair<string, string>("price", "19.49"),
new KeyValuePair<string, string>("__RequestVerificationToken", token)
};
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringResponse = await postResponse.Content.ReadAsStringAsync();
Assert.Contains(".NET Black &amp; White Mug", stringResponse);
//Update
var updateKeyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Items[0].Id", WebPageHelpers.GetId(stringResponse)),
new KeyValuePair<string, string>("Items[0].Quantity", "50"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse))
};
var updateContent = new FormUrlEncodedContent(updateKeyValues);
var updateResponse = await Client.PostAsync("/basket/update", updateContent);
var stringUpdateResponse = await updateResponse.Content.ReadAsStringAsync();
Assert.Contains("/basket/update", updateResponse.RequestMessage.RequestUri.ToString());
Assert.Contains("974.50", stringUpdateResponse);
}
[Fact]
public async Task OnPostUpdateTo0EmptyBasket()
{
// Load Home Page
var response = await Client.GetAsync("/");
response.EnsureSuccessStatusCode();
var stringResponse1 = await response.Content.ReadAsStringAsync();
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse1);
// Add Item to Cart
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("id", "2"),
new KeyValuePair<string, string>("name", "shirt"),
new KeyValuePair<string, string>("price", "19.49"),
new KeyValuePair<string, string>("__RequestVerificationToken", token)
};
var formContent = new FormUrlEncodedContent(keyValues);
var postResponse = await Client.PostAsync("/basket/index", formContent);
postResponse.EnsureSuccessStatusCode();
var stringResponse = await postResponse.Content.ReadAsStringAsync();
Assert.Contains(".NET Black &amp; White Mug", stringResponse);
//Update
var updateKeyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Items[0].Id", WebPageHelpers.GetId(stringResponse)),
new KeyValuePair<string, string>("Items[0].Quantity", "0"),
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse))
};
var updateContent = new FormUrlEncodedContent(updateKeyValues);
var updateResponse = await Client.PostAsync("/basket/update", updateContent);
var stringUpdateResponse = await updateResponse.Content.ReadAsStringAsync();
Assert.Contains("/basket/update", updateResponse.RequestMessage.RequestUri.ToString());
Assert.Contains("Basket is empty", stringUpdateResponse);
}
}

27
tests/FunctionalTests/Web/WebPageHelpers.cs

@ -0,0 +1,27 @@
using System.Text.RegularExpressions;
namespace Microsoft.eShopWeb.FunctionalTests.Web;
public static class WebPageHelpers
{
public static string TokenTag = "__RequestVerificationToken";
public static string GetRequestVerificationToken(string input)
{
string regexpression = @"name=""__RequestVerificationToken"" type=""hidden"" value=""([-A-Za-z0-9+=/\\_]+?)""";
return RegexSearch(regexpression, input);
}
public static string GetId(string input)
{
string regexpression = @"name=""Items\[0\].Id"" value=""(\d)""";
return RegexSearch(regexpression, input);
}
private static string RegexSearch(string regexpression, string input)
{
var regex = new Regex(regexpression);
var match = regex.Match(input);
return match.Groups.Values.LastOrDefault().Value;
}
}

9
tests/PublicApiIntegrationTests/AuthEndpoints/AuthenticateEndpointTest.cs

@ -1,12 +1,11 @@
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.eShopWeb;
using Microsoft.eShopWeb.ApplicationCore.Constants;
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace PublicApiIntegrationTests.AuthEndpoints
{

3
tests/UnitTests/UnitTests.csproj

@ -2,8 +2,11 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>Microsoft.eShopWeb.UnitTests</RootNamespace>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>

Loading…
Cancel
Save