fix some issue and cover by test (#669)
This commit is contained in:
@@ -1,17 +1,11 @@
|
|||||||
using System;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.Infrastructure.Identity;
|
using Microsoft.eShopWeb.Infrastructure.Identity;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account;
|
namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
using System;
|
using System.Text;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Threading.Tasks;
|
using MediatR;
|
||||||
using MediatR;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.eShopWeb.Web.Features.MyOrders;
|
using Microsoft.eShopWeb.Web.Features.MyOrders;
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
using System;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
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.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace Microsoft.eShopWeb.Web.ViewModels.Manage;
|
|||||||
|
|
||||||
public class IndexViewModel
|
public class IndexViewModel
|
||||||
{
|
{
|
||||||
public string Username { get; set; }
|
public string? Username { get; set; }
|
||||||
|
|
||||||
public bool IsEmailConfirmed { get; set; }
|
public bool IsEmailConfirmed { get; set; }
|
||||||
|
|
||||||
@@ -16,5 +16,5 @@ public class IndexViewModel
|
|||||||
[Display(Name = "Phone number")]
|
[Display(Name = "Phone number")]
|
||||||
public string PhoneNumber { get; set; }
|
public string PhoneNumber { get; set; }
|
||||||
|
|
||||||
public string StatusMessage { get; set; }
|
public string? StatusMessage { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<RootNamespace>Microsoft.eShopWeb.Web</RootNamespace>
|
<RootNamespace>Microsoft.eShopWeb.Web</RootNamespace>
|
||||||
<UserSecretsId>aspnet-Web2-1FA3F72E-E7E3-4360-9E49-1CCCD7FE85F7</UserSecretsId>
|
<UserSecretsId>aspnet-Web2-1FA3F72E-E7E3-4360-9E49-1CCCD7FE85F7</UserSecretsId>
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
using System;
|
using System.Net;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Testing;
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -54,35 +49,65 @@ public class AccountControllerSignIn : IClassFixture<TestApplication>
|
|||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
string token = GetRequestVerificationToken(stringResponse);
|
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse);
|
||||||
Assert.True(token.Length > 50);
|
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]
|
[Fact]
|
||||||
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
|
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
|
||||||
{
|
{
|
||||||
var getResponse = await Client.GetAsync("/identity/account/login");
|
var getResponse = await Client.GetAsync("/identity/account/login");
|
||||||
getResponse.EnsureSuccessStatusCode();
|
getResponse.EnsureSuccessStatusCode();
|
||||||
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
|
var stringResponse1 = await getResponse.Content.ReadAsStringAsync();
|
||||||
string token = GetRequestVerificationToken(stringResponse1);
|
|
||||||
|
|
||||||
var keyValues = new List<KeyValuePair<string, string>>();
|
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"));
|
new KeyValuePair<string, string>("Email", "demouser@microsoft.com"),
|
||||||
|
new KeyValuePair<string, string>("Password", "Pass@word1"),
|
||||||
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
|
new KeyValuePair<string, string>(WebPageHelpers.TokenTag, WebPageHelpers.GetRequestVerificationToken(stringResponse1))
|
||||||
|
};
|
||||||
var formContent = new FormUrlEncodedContent(keyValues);
|
var formContent = new FormUrlEncodedContent(keyValues);
|
||||||
|
|
||||||
var postResponse = await Client.PostAsync("/identity/account/login", formContent);
|
var postResponse = await Client.PostAsync("/identity/account/login", formContent);
|
||||||
Assert.Equal(HttpStatusCode.Redirect, postResponse.StatusCode);
|
Assert.Equal(HttpStatusCode.Redirect, postResponse.StatusCode);
|
||||||
Assert.Equal(new System.Uri("/", UriKind.Relative), postResponse.Headers.Location);
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
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 Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages;
|
namespace Microsoft.eShopWeb.FunctionalTests.Web.Pages.Basket;
|
||||||
|
|
||||||
[Collection("Sequential")]
|
[Collection("Sequential")]
|
||||||
public class BasketPageCheckout : IClassFixture<TestApplication>
|
public class BasketPageCheckout : IClassFixture<TestApplication>
|
||||||
@@ -22,45 +16,32 @@ public class BasketPageCheckout : IClassFixture<TestApplication>
|
|||||||
|
|
||||||
public HttpClient Client { get; }
|
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]
|
[Fact]
|
||||||
public async Task RedirectsToLoginIfNotAuthenticated()
|
public async Task RedirectsToLoginIfNotAuthenticated()
|
||||||
{
|
{
|
||||||
// Arrange & Act
|
|
||||||
|
|
||||||
// Load Home Page
|
// Load Home Page
|
||||||
var response = await Client.GetAsync("/");
|
var response = await Client.GetAsync("/");
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
var stringResponse1 = await response.Content.ReadAsStringAsync();
|
var stringResponse1 = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
string token = GetRequestVerificationToken(stringResponse1);
|
string token = WebPageHelpers.GetRequestVerificationToken(stringResponse1);
|
||||||
|
|
||||||
// Add Item to Cart
|
// Add Item to Cart
|
||||||
var keyValues = new List<KeyValuePair<string, string>>();
|
var keyValues = new List<KeyValuePair<string, string>>
|
||||||
keyValues.Add(new KeyValuePair<string, string>("id", "2"));
|
{
|
||||||
keyValues.Add(new KeyValuePair<string, string>("name", "shirt"));
|
new KeyValuePair<string, string>("id", "2"),
|
||||||
|
new KeyValuePair<string, string>("name", "shirt"),
|
||||||
keyValues.Add(new KeyValuePair<string, string>("price", "19.49"));
|
new KeyValuePair<string, string>("price", "19.49"),
|
||||||
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
|
new KeyValuePair<string, string>("__RequestVerificationToken", token)
|
||||||
|
};
|
||||||
var formContent = new FormUrlEncodedContent(keyValues);
|
var formContent = new FormUrlEncodedContent(keyValues);
|
||||||
|
|
||||||
var postResponse = await Client.PostAsync("/basket/index", formContent);
|
var postResponse = await Client.PostAsync("/basket/index", formContent);
|
||||||
postResponse.EnsureSuccessStatusCode();
|
postResponse.EnsureSuccessStatusCode();
|
||||||
var stringResponse = await postResponse.Content.ReadAsStringAsync();
|
var stringResponse = await postResponse.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Contains(".NET Black & White Mug", stringResponse);
|
Assert.Contains(".NET Black & White Mug", stringResponse);
|
||||||
|
|
||||||
keyValues.Clear();
|
keyValues.Clear();
|
||||||
keyValues.Add(new KeyValuePair<string, string>("__RequestVerificationToken", token));
|
|
||||||
|
|
||||||
formContent = new FormUrlEncodedContent(keyValues);
|
formContent = new FormUrlEncodedContent(keyValues);
|
||||||
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);
|
var postResponse2 = await Client.PostAsync("/Basket/Checkout", formContent);
|
||||||
68
tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
Normal file
68
tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
Normal file
@@ -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 & 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
Normal file
100
tests/FunctionalTests/Web/Pages/Basket/IndexTest.cs
Normal file
@@ -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 & 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 & 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
Normal file
27
tests/FunctionalTests/Web/WebPageHelpers.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||||
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
using Microsoft.eShopWeb.PublicApi.AuthEndpoints;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PublicApiIntegrationTests.AuthEndpoints
|
namespace PublicApiIntegrationTests.AuthEndpoints
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
<RootNamespace>Microsoft.eShopWeb.UnitTests</RootNamespace>
|
<RootNamespace>Microsoft.eShopWeb.UnitTests</RootNamespace>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user