public void ExpelManagerFromBranch_Should_ReturnBadRequest_When_ManagerIsAssignedToOtherBranch() { // Arrange var workerAtBranch = new WorkerAtBranchDto { WorkerId = 5, BranchId = 1 }; // Act var badRequestResult = _sut.ExpelManagerFromBranch(workerAtBranch) as BadRequestObjectResult; // Assert Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(workerAtBranch.BranchId))); var workerIdErrorValues = error[nameof(workerAtBranch.BranchId)] as string[]; Assert.IsNotNull(workerIdErrorValues); Assert.IsTrue(workerIdErrorValues.Single() == $"Manager with id {workerAtBranch.WorkerId} is currently not assigned to branch with id {_secondBranch.Id}."); }
public void ExpelManagerFromBranch_Should_ReturnBadRequest_When_BranchNotExist() { // Arrange var workerAtBranch = new WorkerAtBranchDto { WorkerId = 5, BranchId = 999 }; // Act var badRequestResult = _sut.ExpelManagerFromBranch(workerAtBranch) as BadRequestObjectResult; // Assert Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(workerAtBranch.BranchId))); var workerIdErrorValues = error[nameof(workerAtBranch.BranchId)] as string[]; Assert.IsNotNull(workerIdErrorValues); Assert.IsTrue(workerIdErrorValues.Single() == $"Branch with id {workerAtBranch.BranchId} doesn't exist."); }
public void ExpelManagerFromBranch_Should_ReturnBadRequest_When_ModelStateIsInvalid() { // Arrange var workerAtBranch = new WorkerAtBranchDto(); _sut.ModelState.AddModelError(nameof(workerAtBranch.WorkerId), $"The {nameof(workerAtBranch.WorkerId)} field is required."); _sut.ModelState.AddModelError(nameof(workerAtBranch.BranchId), $"The {nameof(workerAtBranch.BranchId)} field is required."); // Act var badRequestResult = _sut.ExpelManagerFromBranch(workerAtBranch) as BadRequestObjectResult; // Assert Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(workerAtBranch.WorkerId))); Assert.IsTrue(error.ContainsKey(nameof(workerAtBranch.BranchId))); var workerIdErrorValues = error[nameof(workerAtBranch.WorkerId)] as string[]; Assert.IsNotNull(workerIdErrorValues); Assert.IsTrue(workerIdErrorValues.Single() == $"The {nameof(workerAtBranch.WorkerId)} field is required."); var branchIdErrorValues = error[nameof(workerAtBranch.BranchId)] as string[]; Assert.IsNotNull(branchIdErrorValues); Assert.IsTrue(branchIdErrorValues.Single() == $"The {nameof(workerAtBranch.BranchId)} field is required."); }
public ActionResult ExpelManagerFromBranch([FromBody] WorkerAtBranchDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var manager = _context.Managers.SingleOrDefault(e => e.Id == model.WorkerId); if (manager == null) { ModelState.AddModelError(nameof(model.WorkerId), $"Manager with id {model.WorkerId} doesn't exist."); return(BadRequest(ModelState)); } var branch = _context.Branches.SingleOrDefault(b => b.Id == model.BranchId); if (branch == null) { ModelState.AddModelError(nameof(model.BranchId), $"Branch with id {model.BranchId} doesn't exist."); return(BadRequest(ModelState)); } if (manager.WorkAtId == null) { ModelState.AddModelError(nameof(model.BranchId), $"Manager with id {model.WorkerId} is currently not assigned to any branch."); return(BadRequest(ModelState)); } if (manager.WorkAtId != model.BranchId) { ModelState.AddModelError(nameof(model.BranchId), $"Manager with id {model.WorkerId} is currently not assigned to branch with id {manager.WorkAtId}."); return(BadRequest(ModelState)); } manager.WorkAtId = null; var managerAtBranchFromDb = _context.ManagerAtBranchHistory.Where(e => e.ManagerId == model.WorkerId) .ToList().LastOrDefault(); if (managerAtBranchFromDb != null) { managerAtBranchFromDb.ExpelDate = DateTime.UtcNow; managerAtBranchFromDb.ExpelledById = int.Parse(User.Claims.Single(c => c.Type == CustomClaimTypes.UserId).Value); } _context.SaveChanges(); return(Ok()); }
public void ExpelManagerFromBranch_Should_SetWorkAtIdPropertyToNull_And_FillManagerAtBranchHistoryRecord_And_ReturnOkObjectResult_When_ModelStateIsValid() { // Arrange var workerAtBranch = new WorkerAtBranchDto { WorkerId = 5, BranchId = 2 }; var currentUser = new ApplicationUser { Id = 5 }; var claims = new List <Claim> { new Claim(CustomClaimTypes.UserId, currentUser.Id.ToString()) }; var identity = new ClaimsIdentity(claims); var claimsPrincipal = new ClaimsPrincipal(identity); var context = new ControllerContext { HttpContext = new DefaultHttpContext { User = claimsPrincipal } }; _sut.ControllerContext = context; // Act var okResult = _sut.ExpelManagerFromBranch(workerAtBranch) as OkResult; // Assert Assert.IsNotNull(okResult); var managerFromDb = _context.Managers.SingleOrDefault(m => m.Id == workerAtBranch.WorkerId); Assert.IsNotNull(managerFromDb); Assert.AreEqual(null, managerFromDb.WorkAtId); var managerAtBranchFromDb = _context.ManagerAtBranchHistory.Where(m => m.ManagerId == workerAtBranch.WorkerId).ToList().LastOrDefault(); Assert.IsNotNull(managerAtBranchFromDb); Assert.IsNotNull(managerAtBranchFromDb.ExpelDate); Assert.AreEqual(_managerAtBranchHistory.AssignDate, managerAtBranchFromDb.AssignDate); Assert.AreEqual(_managerAtBranchHistory.AssignedById, managerAtBranchFromDb.AssignedById); Assert.AreEqual(currentUser.Id, managerAtBranchFromDb.ExpelledById); Assert.AreEqual(workerAtBranch.BranchId, managerAtBranchFromDb.BranchId); Assert.AreEqual(workerAtBranch.WorkerId, managerAtBranchFromDb.ManagerId); }
public ActionResult AssignTellerToBranch([FromBody] WorkerAtBranchDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var teller = _context.Tellers.SingleOrDefault(t => t.Id == model.WorkerId); if (teller == null) { ModelState.AddModelError(nameof(model.WorkerId), $"Teller with id {model.WorkerId} doesn't exist."); return(BadRequest(ModelState)); } var branch = _context.Branches.SingleOrDefault(b => b.Id == model.BranchId); if (branch == null) { ModelState.AddModelError(nameof(model.BranchId), $"Branch with id {model.BranchId} doesn't exist."); return(BadRequest(ModelState)); } if (teller.WorkAtId != null) { ModelState.AddModelError(nameof(model.BranchId), $"Teller with id {model.WorkerId} is currently assigned to branch with id {teller.WorkAtId}."); return(BadRequest(ModelState)); } teller.WorkAtId = branch.Id; var tellerAtBranch = new TellerAtBranchHistory { AssignDate = DateTime.UtcNow, BranchId = branch.Id, TellerId = teller.Id, AssignedById = int.Parse(User.Claims.Single(c => c.Type == CustomClaimTypes.UserId).Value) }; _context.TellerAtBranchHistory.Add(tellerAtBranch); _context.SaveChanges(); return(Ok()); }