Adding a unit test for SignInManager and UnitManager
This commit is contained in:
parent
2e84d68973
commit
6efdbc75c7
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 14
|
||||
VisualStudioVersion = 14.0.22230.1
|
||||
VisualStudioVersion = 14.0.22617.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{44621553-AA7D-4893-8834-79582A7D8348}"
|
||||
EndProject
|
||||
|
|
@ -24,6 +24,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MusicStore", "src\MusicStor
|
|||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MusicStore.Spa", "src\MusicStore.Spa\MusicStore.Spa.kproj", "{93891170-A8D5-46FD-A291-40F90CF258C2}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MusicStore.Test", "test\MusicStore.Test\MusicStore.Test.kproj", "{BECA766F-093E-4D08-9530-2BFA63C5BF93}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -84,6 +86,18 @@ Global
|
|||
{93891170-A8D5-46FD-A291-40F90CF258C2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{93891170-A8D5-46FD-A291-40F90CF258C2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{93891170-A8D5-46FD-A291-40F90CF258C2}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -94,5 +108,6 @@ Global
|
|||
{A319ACCE-060B-4385-9534-9F2202F6180E} = {363D2681-31A6-48C9-90BB-9ACFF4A41F06}
|
||||
{3CFBED5D-2ED8-49DB-96FB-BDAA748DC5A0} = {B7B176B6-8D4D-4EF1-BBD2-DDA650C78FFF}
|
||||
{93891170-A8D5-46FD-A291-40F90CF258C2} = {B7B176B6-8D4D-4EF1-BBD2-DDA650C78FFF}
|
||||
{BECA766F-093E-4D08-9530-2BFA63C5BF93} = {363D2681-31A6-48C9-90BB-9ACFF4A41F06}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Authentication;
|
||||
using Microsoft.AspNet.Http.Core;
|
||||
using Microsoft.AspNet.Http.Core.Authentication;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using MusicStore.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace MusicStore.Controllers
|
||||
{
|
||||
public class ManageControllerTest
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public ManageControllerTest()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddEntityFramework()
|
||||
.AddInMemoryStore()
|
||||
.AddDbContext<MusicStoreContext>();
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddEntityFrameworkStores<MusicStoreContext>();
|
||||
|
||||
// IHttpContextAccessor is required for SignInManager, and UserManager
|
||||
services.AddInstance<IHttpContextAccessor>(
|
||||
new HttpContextAccessor()
|
||||
{
|
||||
HttpContext = new TestHttpContext(),
|
||||
});
|
||||
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Index_ReturnsViewBagMessagesExpected()
|
||||
{
|
||||
// Arrange
|
||||
var userId = "TestUserA";
|
||||
var phone = "abcdefg";
|
||||
var claims = new List<Claim> { new Claim(ClaimTypes.NameIdentifier, userId) };
|
||||
|
||||
var userManager = _serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var userManagerResult = await userManager.CreateAsync(
|
||||
new ApplicationUser { Id = userId, UserName = "Test", TwoFactorEnabled = true, PhoneNumber = phone },
|
||||
"Pass@word1");
|
||||
Assert.True(userManagerResult.Succeeded);
|
||||
|
||||
var signInManager = _serviceProvider.GetRequiredService<SignInManager<ApplicationUser>>();
|
||||
|
||||
var httpContext = _serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
|
||||
httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
|
||||
|
||||
var controller = new ManageController()
|
||||
{
|
||||
UserManager = userManager,
|
||||
SignInManager = signInManager,
|
||||
};
|
||||
controller.ActionContext.HttpContext = httpContext;
|
||||
|
||||
// Act
|
||||
var result = await controller.Index();
|
||||
|
||||
// Assert
|
||||
var viewResult = Assert.IsType<ViewResult>(result);
|
||||
Assert.Null(viewResult.ViewName);
|
||||
|
||||
Assert.Empty(controller.ViewBag.StatusMessage);
|
||||
|
||||
Assert.NotNull(viewResult.ViewData);
|
||||
var model = Assert.IsType<IndexViewModel>(viewResult.ViewData.Model);
|
||||
Assert.True(model.TwoFactor);
|
||||
Assert.Equal(phone, model.PhoneNumber);
|
||||
Assert.True(model.HasPassword);
|
||||
}
|
||||
|
||||
private class TestHttpContext : DefaultHttpContext
|
||||
{
|
||||
public override Task<IEnumerable<AuthenticationResult>>
|
||||
AuthenticateAsync(IEnumerable<string> authenticationTypes)
|
||||
{
|
||||
return
|
||||
Task.FromResult(new AuthenticateContext(authenticationTypes).Results);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>beca766f-093e-4d08-9530-2bfa63c5bf93</ProjectGuid>
|
||||
<RootNamespace>MusicStore</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"compilationOptions": {
|
||||
"warningsAsErrors": "true"
|
||||
},
|
||||
"dependencies": {
|
||||
"MusicStore": "",
|
||||
"Microsoft.AspNet.Mvc" : "6.0.0-*",
|
||||
"xunit.runner.kre": "1.0.0-*"
|
||||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.kre"
|
||||
},
|
||||
"frameworks": {
|
||||
"aspnet50": { }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue