diff --git a/MusicStore.sln b/MusicStore.sln index 5e79201722..c17a8f8213 100644 --- a/MusicStore.sln +++ b/MusicStore.sln @@ -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 diff --git a/test/MusicStore.Test/ManageControllerTest.cs b/test/MusicStore.Test/ManageControllerTest.cs new file mode 100644 index 0000000000..92327372c9 --- /dev/null +++ b/test/MusicStore.Test/ManageControllerTest.cs @@ -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(); + + services.AddIdentity() + .AddEntityFrameworkStores(); + + // IHttpContextAccessor is required for SignInManager, and UserManager + services.AddInstance( + 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 { new Claim(ClaimTypes.NameIdentifier, userId) }; + + var userManager = _serviceProvider.GetRequiredService>(); + 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>(); + + var httpContext = _serviceProvider.GetRequiredService().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(result); + Assert.Null(viewResult.ViewName); + + Assert.Empty(controller.ViewBag.StatusMessage); + + Assert.NotNull(viewResult.ViewData); + var model = Assert.IsType(viewResult.ViewData.Model); + Assert.True(model.TwoFactor); + Assert.Equal(phone, model.PhoneNumber); + Assert.True(model.HasPassword); + } + + private class TestHttpContext : DefaultHttpContext + { + public override Task> + AuthenticateAsync(IEnumerable authenticationTypes) + { + return + Task.FromResult(new AuthenticateContext(authenticationTypes).Results); + } + } + } +} \ No newline at end of file diff --git a/test/MusicStore.Test/MusicStore.Test.kproj b/test/MusicStore.Test/MusicStore.Test.kproj new file mode 100644 index 0000000000..907b86e201 --- /dev/null +++ b/test/MusicStore.Test/MusicStore.Test.kproj @@ -0,0 +1,20 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + beca766f-093e-4d08-9530-2bfa63c5bf93 + MusicStore + ..\..\..\artifacts\obj\$(MSBuildProjectName) + ..\..\..\artifacts\bin\$(MSBuildProjectName)\ + + + + 2.0 + + + \ No newline at end of file diff --git a/test/MusicStore.Test/project.json b/test/MusicStore.Test/project.json new file mode 100644 index 0000000000..28300d1ed5 --- /dev/null +++ b/test/MusicStore.Test/project.json @@ -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": { } + } +} \ No newline at end of file