public async Task Details_ReturnsCorrectViewModel() { var solutions = new[] { new Solution { Id = 1, Title = "First Solution", Content = "First Content", Author = new User() }, new Solution { Id = 2, Title = "Second Solution", Content = "Second Content", Author = new User() }, new Solution { Id = 3, Title = "Third Solution", Content = "Third Content", Author = new User() }, }; var context = TestsDbContext.GetDbContext(); context.Solutions.AddRange(solutions); context.SaveChanges(); var controller = new SolutionsController(context, TestsAutoMapper.GetMapper()); var expectedSolution = solutions[1]; var result = await controller.Details(expectedSolution.Id); var model = (result as ViewResult).ViewData.Model as SolutionDetailsViewModel; Assert.IsNotNull(model); Assert.AreEqual(model.Id, expectedSolution.Id); Assert.AreEqual(model.Title, expectedSolution.Title); Assert.AreEqual(model.Content, expectedSolution.Content); Assert.IsInstanceOfType(model, typeof(SolutionDetailsViewModel)); }
public void OnPost_AddsSolutionToTheDatabase() { var mockUserManager = TestsUserManager.GetUserManager(); var page = new CreateModel(this.dbContext, mockUserManager.Object, TestsAutoMapper.GetMapper()); page.PageContext = new PageContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, WebConstants.AdminRole) })) } }; page.Model = new SolutionCreationBindingModel { Title = "Title", Content = "Content" }; var result = page.OnPost(); var lastRequest = this.dbContext.Solutions.LastOrDefault(); Assert.IsNotNull(lastRequest); Assert.IsTrue(lastRequest.Content == page.Model.Content && lastRequest.Content == page.Model.Content); }
public async Task Ban_ReturnsCorrectViewModel() { var users = new[] { new User { Id = "123" }, new User { Id = "321" }, new User { Id = "456" } }; var context = TestsDbContext.GetDbContext(); context.Users.AddRange(users); context.SaveChanges(); var mockUserManager = TestsUserManager.GetUserManager(); mockUserManager.Setup(um => um.FindByIdAsync(It.IsAny <string>())) .ReturnsAsync(users[0]); var controller = new UsersController(context, TestsAutoMapper.GetMapper(), mockUserManager.Object); var result = await controller.Ban(users[0].Id); var model = (result as ViewResult).ViewData.Model as UserDetailsViewModel; Assert.AreEqual(model.Id, users[0].Id); Assert.IsInstanceOfType(model, typeof(UserDetailsViewModel)); }
public async Task Index__ReturnsAllSolutions() { var solutions = new[] { new Solution { Id = 1, Title = "First Solution", Content = "First Content", Author = new User() }, new Solution { Id = 2, Title = "Second Solution", Content = "Second Content", Author = new User() }, new Solution { Id = 3, Title = "Third Solution", Content = "Third Content", Author = new User() }, }; this.dbContext.Solutions.AddRange(solutions); this.dbContext.SaveChanges(); var controller = new SolutionsController(this.dbContext, TestsAutoMapper.GetMapper()); var result = await controller.Index() as ViewResult; var model = result.Model as ICollection <SolutionListingViewModel>; var expectedCollection = solutions.Select(r => r.Id).ToArray(); var resultCollection = model.Select(rv => rv.Id).ToArray(); var expectedCount = solutions.Count(); Assert.IsNotNull(result); Assert.AreEqual(expectedCount, model.Count); CollectionAssert.AreEqual(expectedCollection, resultCollection); }
public void Index_WithoutFilter_ReturnsAllRequests() { var requests = new[] { new Request { Id = 1, AssignedTo = new User(), Requester = new User(), Status = new RequestStatus() }, new Request { Id = 2, AssignedTo = new User(), Requester = new User(), Status = new RequestStatus() }, new Request { Id = 3, AssignedTo = new User(), Requester = new User(), Status = new RequestStatus() }, }; this.dbContext.Requests.AddRange(requests); this.dbContext.SaveChanges(); var mockUserManager = TestsUserManager.GetUserManager(); var controller = new RequestsController(this.dbContext, TestsAutoMapper.GetMapper(), mockUserManager.Object, requestservice); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, WebConstants.AdminRole) })) } }; var result = controller.Index("", "", "", null, 1) as ViewResult; var model = result.Model as RequestSortingViewModel; //the default sorting order is Id => desc var expectedCollection = requests.Select(r => r.Id).OrderByDescending(r => r).ToArray(); var resultCollection = model.RequestListingViewModels.Select(rv => rv.Id).ToArray(); Assert.IsNotNull(result); CollectionAssert.AreEqual(expectedCollection, resultCollection); }
public async Task Index_ShouldReturnAllUsers_ExceptCurrent() { var users = new[] { new User { Id = "123" }, new User { Id = "321" }, new User { Id = "456" } }; var context = TestsDbContext.GetDbContext(); context.Users.AddRange(users); context.SaveChanges(); var mockUserManager = TestsUserManager.GetUserManager(); mockUserManager.Setup(um => um.GetUserAsync(null)) .ReturnsAsync(users[0]); mockUserManager.Setup(um => um.GetRolesAsync(It.IsAny <User>())) .ReturnsAsync(new List <string> { "Whatever" }); var controller = new UsersController(context, TestsAutoMapper.GetMapper(), mockUserManager.Object); var result = await controller.Index() as ViewResult; var model = result.Model as IEnumerable <UserConciseViewModel>; var expectedCollection = users.Where(u => u.Id != users[0].Id) .Select(u => u.Id).ToArray(); var resultCollection = model.Select(u => u.Id).ToArray(); Assert.IsNotNull(result); CollectionAssert.AreEqual(expectedCollection, resultCollection); }
public async Task Ban_ReturnsCorrectView() { var mockUserManager = TestsUserManager.GetUserManager(); var user = new User { Id = "1" }; var context = TestsDbContext.GetDbContext(); context.Users.Add(user); mockUserManager.Setup(um => um.FindByIdAsync(It.IsAny <string>())) .ReturnsAsync(user); var controller = new UsersController(context, TestsAutoMapper.GetMapper(), TestsUserManager.GetUserManager().Object); var result = await controller.Ban(user.Id); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Create_AddsRequestToTheDatabase() { this.dbContext.RequestStatuses.Add(new RequestStatus { Id = 1, Name = "Open" }); this.dbContext.SaveChanges(); var mockUserManager = TestsUserManager.GetUserManager(); var controller = new RequestsController(this.dbContext, TestsAutoMapper.GetMapper(), mockUserManager.Object, requestService); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, WebConstants.AdminRole) })) } }; var model = new RequestCreationBindingModel { Subject = "Subject", Description = "Description" }; var result = controller.Create(model); var lastRequest = this.dbContext.Requests.LastOrDefault(); Assert.IsNotNull(lastRequest); Assert.IsTrue(lastRequest.Subject == model.Subject && lastRequest.Description == model.Description); }
public void Index_WithOpenFilter_ReturnsAllClosedRequests_ForCurrentUser() { var openStatus = new RequestStatus { Id = 1, Name = "Open" }; var closedStatus = new RequestStatus { Id = 2, Name = "Closed" }; var users = new[] { new User { Id = "123" }, new User { Id = "321" }, new User { Id = "456" } }; var requests = new[] { new Request { Id = 1, AssignedTo = new User(), Requester = users[0], Status = openStatus }, new Request { Id = 2, AssignedTo = new User(), Requester = users[1], Status = openStatus }, new Request { Id = 3, AssignedTo = new User(), Requester = users[2], Status = closedStatus }, new Request { Id = 4, AssignedTo = new User(), Requester = users[0], Status = closedStatus }, new Request { Id = 5, AssignedTo = new User(), Requester = users[0], Status = closedStatus }, }; this.dbContext.Users.AddRange(users); this.dbContext.Requests.AddRange(requests); this.dbContext.SaveChanges(); var mockUserManager = TestsUserManager.GetUserManager(); mockUserManager.Setup(um => um.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns("123"); var controller = new RequestsController(this.dbContext, TestsAutoMapper.GetMapper(), mockUserManager.Object, requestservice); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Role, WebConstants.AdminRole) })) } }; var result = controller.Index("", "", "MyClosed", null, null) as ViewResult; var model = result.Model as RequestSortingViewModel; //the default sorting order is Id => desc var expectedCount = 2; var expectedCollection = requests .Where(r => r.Requester == users[0] && r.Status == closedStatus) .Select(r => r.Id).OrderByDescending(r => r).ToArray(); var resultCollection = model.RequestListingViewModels.Select(rv => rv.Id).ToArray(); Assert.IsNotNull(result); Assert.AreEqual(expectedCount, resultCollection.Count()); CollectionAssert.AreEqual(expectedCollection, resultCollection); }