Adding functional tests (#197)

* Working on login tests

* Testing login with an integration test

Working on login functional test.

* Got functional login test working
This commit is contained in:
Steve Smith
2019-01-25 13:13:01 -05:00
committed by GitHub
parent be06e777a6
commit 1b610dd7a4
7 changed files with 157 additions and 13 deletions

View File

@@ -0,0 +1,60 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.eShopWeb.Web;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
{
public class AccountControllerSignIn : IClassFixture<CustomWebApplicationFactory<Startup>>
{
public AccountControllerSignIn(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
}
public HttpClient Client { get; }
[Fact]
public async Task ReturnsSignInScreenOnGet()
{
var response = await Client.GetAsync("/account/sign-in");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
Assert.Contains("demouser@microsoft.com", stringResponse);
}
// TODO: Finish this test.
[Fact]
public async Task ReturnsSuccessfulSignInOnPostWithValidCredentials()
{
//var response = await Client.GetAsync("/account/sign-in");
//response.EnsureSuccessStatusCode();
//var stringResponse = await response.Content.ReadAsStringAsync();
// TODO: Get the token from a Get call
// Ref: https://buildmeasurelearn.wordpress.com/2016/11/23/handling-asp-net-mvcs-anti-forgery-tokens-when-load-testing-with-jmeter/
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", "CfDJ8Obhlq65OzlDkoBvsSX0tgyXhgITd4pD1OocDNYfbIeOkBMVLl3SmcZjyHLFqAlfvNOcWnV73G520010NOL1VaHRODGXZxTNjkIOjOi36YW3Fs5Bb9K9baf0hLFrmFI4P1w-64FURukDzaWRGl0Tzw0"));
var formContent = new FormUrlEncodedContent(keyValues);
var response = await Client.PostAsync("/account/sign-in", formContent);
//response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal(new System.Uri("/", UriKind.Relative), response.Headers.Location);
}
}
}

View File

@@ -26,7 +26,7 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
var redirectLocation = response.Headers.Location.OriginalString;
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Contains("Account/Login", redirectLocation);
Assert.Contains("Account/Signin", redirectLocation);
}
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
@@ -17,8 +18,10 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
{
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
// Create a new service provider.
var serviceProvider = new ServiceCollection()
var provider = services
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
@@ -27,15 +30,19 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
services.AddDbContext<CatalogContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(serviceProvider);
options.UseInternalServiceProvider(provider);
});
services.AddDbContext<AppIdentityDbContext>(options =>
{
options.UseInMemoryDatabase("Identity");
options.UseInternalServiceProvider(serviceProvider);
options.UseInternalServiceProvider(provider);
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
// Build the service provider.
var sp = services.BuildServiceProvider();
@@ -57,6 +64,11 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
{
// Seed the database with test data.
CatalogContextSeed.SeedAsync(db, loggerFactory).Wait();
// seed sample user data
var userManager = scopedServices.GetRequiredService<UserManager<ApplicationUser>>();
AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
}
catch (Exception ex)
{