Skip to content

Commit

Permalink
Got RedirectToLoginIfNotAuth test working
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Feb 22, 2019
1 parent f5a17ec commit 765e150
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ private static void ConfigureCookieSettings(IServiceCollection services)
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Signout";
options.Cookie = new CookieBuilder
{
Expand Down Expand Up @@ -228,6 +229,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGene

app.UseMvc(routes =>
{
routes.MapRoute(
name: "identity",
template: "Identity/{controller=Account}/{action=Register}/{id?}");

routes.MapRoute(
name: "default",
template: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
Expand Down
6 changes: 4 additions & 2 deletions tests/FunctionalTests/Web/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
Expand Down Expand Up @@ -40,8 +41,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
});

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();

// Build the service provider.
var sp = services.BuildServiceProvider();
Expand Down
71 changes: 71 additions & 0 deletions tests/FunctionalTests/Web/Pages/BasketPageCheckout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb.FunctionalTests.Web.Controllers;
using Microsoft.eShopWeb.Web;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.eShopWeb.FunctionalTests.WebRazorPages
{
public class BasketPageCheckout : IClassFixture<CustomWebApplicationFactory<Startup>>
{
public BasketPageCheckout(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = true
});
}

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.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);

// 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 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);
Assert.Contains("/Identity/Account/Login", postResponse2.RequestMessage.RequestUri.ToString());
}
}
}

0 comments on commit 765e150

Please sign in to comment.