diff --git a/Sevomin.Tests/Controllers/FakeIdentity.cs b/Sevomin.Tests/Controllers/FakeIdentity.cs new file mode 100644 index 0000000..d534b7e --- /dev/null +++ b/Sevomin.Tests/Controllers/FakeIdentity.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Principal; +using System.Text; +using System.Threading.Tasks; + +namespace Sevomin.Tests.Controllers +{ + public class FakeIdentity : IIdentity + { + public string AuthenticationType + { + get { throw new NotImplementedException(); } + } + + public bool IsAuthenticated + { + get { return true; } + } + + public string Name + { + get { return "test"; } + } + } +} diff --git a/Sevomin.Tests/Controllers/Job/NewJob.cs b/Sevomin.Tests/Controllers/Job/NewJob.cs index 4b4f250..7300356 100644 --- a/Sevomin.Tests/Controllers/Job/NewJob.cs +++ b/Sevomin.Tests/Controllers/Job/NewJob.cs @@ -6,20 +6,38 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; +using Rhino.Mocks; +using Sevomin.Models.Helpers; +using System.Web; +using System.Security.Principal; namespace Sevomin.Tests.Controllers.Job { [TestClass] public class NewJob { + HttpContextBase fakeContext = MockRepository.GenerateStub(); + public JobController GetCUT() { - JobController cut = new JobController(); - return cut; + FakeJobRepository.Init(); + fakeContext = MockRepository.GenerateStub(); + fakeContext.User = MockRepository.GenerateStub(); + fakeContext.User.Stub(x => x.Identity).Return(new FakeIdentity()); + + JobController cut = new JobController(FakeJobRepository.Instance(), + MockRepository.GenerateMock(FakeDbContext.Instance()), fakeContext); + return cut; + } + + [TestMethod] + public void MyTestMethod() + { + } - + [TestMethod] - public async void Returns_View_NewJob_JobController() + public async Task Returns_View_NewJob_JobController() { // Arrange JobController cut = GetCUT(); @@ -31,7 +49,7 @@ namespace Sevomin.Tests.Controllers.Job } [TestMethod] - public async void Returns_EmptyJobModel_NewJob_JobController() + public async Task Returns_EmptyJobModel_NewJob_JobController() { // Arrange JobController cut = GetCUT(); diff --git a/Sevomin.Tests/FakeDbContext.cs b/Sevomin.Tests/FakeDbContext.cs new file mode 100644 index 0000000..4b2bf51 --- /dev/null +++ b/Sevomin.Tests/FakeDbContext.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.Identity.EntityFramework; +using Rhino.Mocks; +using Sevomin.Models; + +namespace Sevomin.Tests +{ + public class FakeDbContext + { + private static IdentityDbContext _dbcontext; + + static FakeDbContext() + { + Init(); + } + + internal static void Init() + { + + _dbcontext = MockRepository.GenerateStub>("Test"); + } + + internal static IdentityDbContext Instance() + { + return _dbcontext; + } + } + + +} diff --git a/Sevomin.Tests/FakeJobRepository.cs b/Sevomin.Tests/FakeJobRepository.cs index 369f0e1..c6e8d64 100644 --- a/Sevomin.Tests/FakeJobRepository.cs +++ b/Sevomin.Tests/FakeJobRepository.cs @@ -21,21 +21,18 @@ namespace Sevomin.Tests internal static void Init() { - //_Job = (new List() { - // new Job{ id = 1, Name = "A", TaskDefinitions = new List()}, - // new Job{ id = 2, Name = "B", TaskDefinitions = new List() } - //}).AsQueryable(); - //_JobRepository = MockRepository.GenerateStub(); - - //_JobRepository.Stub(x => x.Find(Arg.Is.Anything)) - // .WhenCalled(x => x.ReturnValue = _Job.FirstOrDefault(y => y.id == (long)x.Arguments[0])) - // .Return(new Job()); - - //_JobRepository.Stub(x => x.ListAll()).Return(_Job); - //_JobRepository.Stub(x => x.FindByUsername("new-mission")) - // .Return(null); - //_JobRepository.Stub(x => x.FindByUsername(Arg.Is.Anything)) - // .Return(new Job() { id = 1, Username = "test", Name = "A", TaskDefinitions = new List() }); + _Job = (new List() { + new Job{ Id = 1, AvalinId = "a", Avalin = new Avalin(){Id = "a", UserName="test"}, JobParameters = new List(){new JobParameter{Id = 4}}}, + new Job{ Id = 3, AvalinId = "b", Avalin = new Avalin(){Id = "b", UserName="B"}, JobParameters = new List(){new JobParameter{Id = 4}}} + }).AsQueryable(); + + _JobRepository = MockRepository.GenerateStub(); + + _JobRepository.Stub(x => x.Find(Arg.Is.Anything)) + .WhenCalled(x => x.ReturnValue = _Job.FirstOrDefault(y => y.Id == (long)x.Arguments[0])) + .Return(new Job()); + + _JobRepository.Stub(x => x.ListAll()).Return(_Job); } internal static IJobRepository Instance() diff --git a/Sevomin.Tests/Sevomin.Tests.csproj b/Sevomin.Tests/Sevomin.Tests.csproj index 9e2730d..4666cbf 100644 --- a/Sevomin.Tests/Sevomin.Tests.csproj +++ b/Sevomin.Tests/Sevomin.Tests.csproj @@ -52,6 +52,7 @@ + False ..\packages\Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll @@ -74,7 +75,9 @@ + + diff --git a/Sevomin.WebFrontend.Controllers/JobController.cs b/Sevomin.WebFrontend.Controllers/JobController.cs index 68829e3..4bd5cf7 100644 --- a/Sevomin.WebFrontend.Controllers/JobController.cs +++ b/Sevomin.WebFrontend.Controllers/JobController.cs @@ -9,6 +9,7 @@ using Sevomin.Models.Repositories; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Sevomin.Models.Helpers; +using System.Web; namespace Sevomin.WebFrontend.Controllers { @@ -16,16 +17,25 @@ namespace Sevomin.WebFrontend.Controllers { IJobRepository _JobRepository; public SevominUserManager UserManager { get; private set; } + private HttpContextBase _httpContext; public JobController() - : this(JobRepository.Current, new SevominUserManager(new UserStore(SevominDbContext.Current))) + : this(JobRepository.Current, + new SevominUserManager(new UserStore(SevominDbContext.Current)), null) { } - public JobController(IJobRepository jobRepository, SevominUserManager userManager) + protected override void Initialize(System.Web.Routing.RequestContext requestContext) + { + base.Initialize(requestContext); + _httpContext = requestContext.HttpContext; + } + + public JobController(IJobRepository jobRepository, SevominUserManager userManager, HttpContextBase httpContext) { _JobRepository = jobRepository; UserManager = userManager; + _httpContext = httpContext; } public async Task NewJob()