Skip to content

Commit

Permalink
fix some issue and cover by test (dotnet-architecture#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelcedric authored Jan 25, 2022
1 parent 6847cda commit 87ae6c6
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 81 deletions.
8 changes: 1 addition & 7 deletions src/Web/Areas/Identity/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
5 changes: 1 addition & 4 deletions src/Web/Controllers/ManageController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/Web/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Pages/Basket/BasketItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public class BasketItemViewModel

[Range(0, int.MaxValue, ErrorMessage = "Quantity must be bigger than 0")]
public int Quantity { get; set; }

public string? PictureUrl { get; set; }
}
8 changes: 1 addition & 7 deletions src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Web/ViewModels/Manage/IndexViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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 changes: 1 addition & 1 deletion src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
67 changes: 46 additions & 21 deletions tests/FunctionalTests/Web/Controllers/AccountControllerSignIn.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -54,35 +49,65 @@ public async Task ReturnsFormWithRequestVerificationToken()
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);

}
}
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -22,45 +16,32 @@ public BasketPageCheckout(TestApplication factory)

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);
Expand Down
68 changes: 68 additions & 0 deletions tests/FunctionalTests/Web/Pages/Basket/CheckoutTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 87ae6c6

Please sign in to comment.