public async Task TestCopySetsTheCreatorToCurrectUserWhenNotAdmin() { // Arrange Order savedResult = null; MockDbContext.Setup(a => a.Add(It.IsAny <Order>())).Callback <Order>(r => savedResult = r); var copiedOrder = CreateValidEntities.Order(7); copiedOrder.Creator = CreateValidEntities.User(5, true); OrderData[1].CreatorId = "xxx"; OrderData[1].Creator = CreateValidEntities.User(5); MockOrderService.Setup(a => a.DuplicateOrder(OrderData[1], false)).ReturnsAsync(copiedOrder); // Act var controllerResult = await Controller.Copy(SpecificGuid.GetGuid(2)); // Assert MockOrderService.Verify(a => a.DuplicateOrder(OrderData[1], false), Times.Once); MockDbContext.Verify(a => a.Add(It.IsAny <Order>()), Times.Once); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); savedResult.ShouldNotBeNull(); savedResult.CreatorId.ShouldBe("Creator1"); savedResult.Creator.Id.ShouldBe("Creator1"); savedResult.ShareIdentifier.ShouldNotBe(SpecificGuid.GetGuid(2)); savedResult.SavedTestDetails.ShouldBeNull(); var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Edit"); redirectResult.ControllerName.ShouldBeNull(); redirectResult.RouteValues["id"].ShouldBe(savedResult.Id); }
public async Task TestConfirmationPostWhenPaymentOther2() { // Arrange OrderData[1].CreatorId = "Creator1"; OrderData[1].Status = OrderStatusCodes.Created; OrderData[1].PaymentType = PaymentTypeCodes.Other; Controller.ErrorMessage = null; var orderDetails = CreateValidEntities.OrderDetails(2); orderDetails.OtherPaymentInfo.PaymentType = "SomethingElse"; orderDetails.ClientInfo.ClientId = null; OrderData[1].SaveDetails(orderDetails); // Act var controllerResult = await Controller.Confirmation(2, true); // Assert Controller.ErrorMessage.ShouldBeNull(); OrderData[1].GetOrderDetails().Payment.AccountName.ShouldBeNull(); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); MockFinancialService.Verify(a => a.GetAccountName(It.IsAny <string>()), Times.Never); MockOrderMessagingService.Verify(a => a.EnqueueBillingMessage(OrderData[1], It.IsAny <string>()), Times.Never); MockOrderService.Verify(a => a.UpdateTestsAndPrices(OrderData[1]), Times.Once); MockOrderService.Verify(a => a.UpdateAdditionalInfo(OrderData[1]), Times.Once); MockLabworksService.Verify(a => a.GetClientDetails(It.IsAny <string>()), Times.Never); MockOrderMessagingService.Verify(a => a.EnqueueCreatedMessage(OrderData[1]), Times.Once); var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Confirmed"); redirectResult.RouteValues["id"].ShouldBe(2); redirectResult.ControllerName.ShouldBeNull(); }
public async Task TestSaveWhenEditAndSuccess() { // Arrange OrderData[1].Status = OrderStatusCodes.Created; OrderData[1].CreatorId = "Creator1"; var model = new OrderSaveModel(); model.OrderId = 2; OrderData[1].SavedTestDetails.ShouldBeNull(); // Act var controllerResult = await Controller.Save(model); // Assert var result = Assert.IsType <JsonResult>(controllerResult); dynamic data = JObject.FromObject(result.Value); ((bool)data.success).ShouldBe(true); ((int)data.id).ShouldBe(2); MockOrderService.Verify(a => a.PopulateTestItemModel(It.IsAny <bool>()), Times.Once); MockOrderService.Verify(a => a.PopulateOrder(model, OrderData[1]), Times.Once); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); OrderData[1].SavedTestDetails.ShouldNotBeNull(); OrderData[1].GetTestDetails().Count.ShouldBe(10); }
public async Task TestConfirmationPostWhenUcdAccountNotVerified2() { // Arrange OrderData[1].CreatorId = "Creator1"; OrderData[1].Status = OrderStatusCodes.Created; OrderData[1].PaymentType = PaymentTypeCodes.UcDavisAccount; Controller.ErrorMessage = null; var orderDetails = CreateValidEntities.OrderDetails(2); orderDetails.Payment.Account = "3-1234567"; orderDetails.Payment.AccountName = "WHAT!"; OrderData[1].SaveDetails(orderDetails); MockFinancialService.Setup(a => a.GetAccountName(It.IsAny <string>())).ReturnsAsync(string.Empty); // Act var controllerResult = await Controller.Confirmation(2, true); // Assert Controller.ErrorMessage.ShouldBe("Unable to verify UC Account number. Please edit your order and re-enter the UC account number. Then try again."); OrderData[1].GetOrderDetails().Payment.AccountName.ShouldBeEmpty(); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); MockFinancialService.Verify(a => a.GetAccountName("3-1234567"), Times.Once); var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Confirmation"); redirectResult.RouteValues["id"].ShouldBe(2); redirectResult.ControllerName.ShouldBeNull(); }
public async Task TestSaveWhenCreateAndSuccess() { // Arrange var model = new OrderSaveModel(); model.OrderId = null; Order savedResult = null; MockDbContext.Setup(a => a.Add(It.IsAny <Order>())).Callback <Order>(r => savedResult = r); // Act var controllerResult = await Controller.Save(model); // Assert var result = Assert.IsType <JsonResult>(controllerResult); dynamic data = JObject.FromObject(result.Value); ((bool)data.success).ShouldBe(true); ((int)data.id).ShouldBe(0); //Because it would be set in the DB MockOrderService.Verify(a => a.PopulateTestItemModel(It.IsAny <bool>()), Times.Once); MockOrderService.Verify(a => a.PopulateOrder(model, It.IsAny <Order>()), Times.Once); MockDbContext.Verify(a => a.Add(It.IsAny <Order>()), Times.Once); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); savedResult.CreatorId.ShouldBe("Creator1"); savedResult.Creator.ShouldNotBeNull(); savedResult.Creator.Id.ShouldBe("Creator1"); savedResult.Status.ShouldBe(OrderStatusCodes.Created); savedResult.ShareIdentifier.ShouldNotBeNull(); }
public async Task TestConfirmationPostWhenUcdAccountVerified() { // Arrange OrderData[1].CreatorId = "Creator1"; OrderData[1].Status = OrderStatusCodes.Created; OrderData[1].PaymentType = PaymentTypeCodes.UcDavisAccount; Controller.ErrorMessage = null; var orderDetails = CreateValidEntities.OrderDetails(2); orderDetails.Payment.Account = "3-1234567"; orderDetails.Payment.AccountName = "WHAT!"; orderDetails.ClientInfo.ClientId = null; OrderData[1].SaveDetails(orderDetails); MockFinancialService.Setup(a => a.GetAccountName(It.IsAny <string>())).ReturnsAsync("My Fake Account"); // Act var controllerResult = await Controller.Confirmation(2, true); // Assert Controller.ErrorMessage.ShouldBeNull(); OrderData[1].GetOrderDetails().Payment.AccountName.ShouldBe("My Fake Account"); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); MockFinancialService.Verify(a => a.GetAccountName("3-1234567"), Times.Once); MockOrderService.Verify(a => a.UpdateAdditionalInfo(OrderData[1]), Times.Once); MockOrderService.Verify(a => a.UpdateTestsAndPrices(OrderData[1]), Times.Once); var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Confirmed"); redirectResult.RouteValues["id"].ShouldBe(2); redirectResult.ControllerName.ShouldBeNull(); }
public void TestCopySetsTheCreatorToCurrectUserWhenAdminIsTrueButNotAdminRole() { // Arrange Order savedResult = null; MockDbContext.Setup(a => a.Add(It.IsAny <Order>())).Callback <Order>(r => savedResult = r); var copiedOrder = CreateValidEntities.Order(7); copiedOrder.Creator = CreateValidEntities.User(5, true); OrderData[1].CreatorId = "xxx"; OrderData[1].Creator = CreateValidEntities.User(5); MockOrderService.Setup(a => a.DuplicateOrder(OrderData[1], true)).ReturnsAsync(copiedOrder); // Act var ex = Assert.ThrowsAsync <Exception>(async() => await Controller.Copy(SpecificGuid.GetGuid(2), true)); // Assert ex.Result.Message.ShouldBe("Permissions Missing"); MockOrderService.Verify(a => a.DuplicateOrder(OrderData[1], true), Times.Once); MockDbContext.Verify(a => a.Add(It.IsAny <Order>()), Times.Never); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task Given_AValidTestDtoObjectWithoutChanges_When_UpdateIsInvoked_Then_SaveChangesAsyncMethodShouldNotBeInvoked() { var testObjectWithoutUpdates = TestDtoObjects.First(); await DbContextBase.Update(testObjectWithoutUpdates); MockDbContext.Verify(m => m.SaveChangesAsync(), Times.Never); }
public async Task TestConfirmationPostReturnsNotFound() { // Arrange // Act var controllerResult = await Controller.Confirmation(99, true); // Assert Assert.IsType <NotFoundResult>(controllerResult); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestDeleteReturnsNotFound() { // Arrange // Act var controllerResult = await Controller.Delete(99); // Assert Assert.IsType <NotFoundResult>(controllerResult); MockDbContext.Verify(a => a.Remove(It.IsAny <Order>()), Times.Never); }
public async Task TestConfirmationPostReturnsNotFoundWhenYouDoNotHaveAccess() { // Arrange OrderData[1].CreatorId = "XXX"; Controller.ErrorMessage = null; // Act var controllerResult = await Controller.Confirmation(2, true); // Assert Assert.IsType <NotFoundResult>(controllerResult); Controller.ErrorMessage.ShouldBe("You don't have access to this order."); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestDeleteReturnsNotFoundWhenYouDoNotHaveAccess() { // Arrange OrderData[1].CreatorId = "XXX"; Controller.ErrorMessage = null; // Act var controllerResult = await Controller.Delete(2); // Assert Assert.IsType <NotFoundResult>(controllerResult); Controller.ErrorMessage.ShouldBe("You don't have access to this order."); MockDbContext.Verify(a => a.Remove(It.IsAny <Order>()), Times.Never); }
public async Task TestCopySetsTheCreatorToCorrectUserWhenAdminIsTrue() { MockClaimsPrincipal.Setup(a => a.IsInRole(RoleCodes.Admin)).Returns(true); Order savedResult = null; MockDbContext.Setup(a => a.Add(It.IsAny <Order>())).Callback <Order>(r => savedResult = r); var copiedOrder = CreateValidEntities.Order(7, true); var testItemModel = new List <TestItemModel>(); for (int i = 0; i < 5; i++) { testItemModel.Add(CreateValidEntities.TestItemModel(1)); } copiedOrder.SaveTestDetails(testItemModel); OrderData[1].SaveTestDetails(testItemModel); copiedOrder.Creator = CreateValidEntities.User(5, true); OrderData[1].CreatorId = "xxx"; OrderData[1].Creator = CreateValidEntities.User(5); MockOrderService.Setup(a => a.DuplicateOrder(OrderData[1], true)).ReturnsAsync(copiedOrder); // Act var controllerResult = await Controller.Copy(SpecificGuid.GetGuid(2), true); // Assert MockOrderService.Verify(a => a.DuplicateOrder(OrderData[1], true), Times.Once); MockDbContext.Verify(a => a.Add(It.IsAny <Order>()), Times.Once); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); savedResult.ShouldNotBeNull(); savedResult.CreatorId.ShouldBe("xxx"); savedResult.Creator.Id.ShouldBe("5"); savedResult.ShareIdentifier.ShouldNotBe(SpecificGuid.GetGuid(2)); savedResult.Status.ShouldBe(OrderStatusCodes.Confirmed); savedResult.GetOrderDetails().LabComments.ShouldContain("Duplicated from 2"); savedResult.GetTestDetails().Count.ShouldBe(5); var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("AddRequestNumber"); redirectResult.ControllerName.ShouldBe("Lab"); redirectResult.RouteValues["id"].ShouldBe(savedResult.Id); MockClaimsPrincipal.Verify(a => a.IsInRole(RoleCodes.Admin), Times.Once); }
public async Task TestConfirmationPostRedirectsWhenAlreadyConfirmed() { // Arrange OrderData[1].CreatorId = "Creator1"; OrderData[1].Status = OrderStatusCodes.Confirmed; Controller.ErrorMessage = null; // Act var controllerResult = await Controller.Confirmation(2, true); // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Index"); redirectResult.ControllerName.ShouldBeNull(); Controller.ErrorMessage.ShouldBe("Already confirmed"); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestSaveReturnsJsonWhenModelStateInvalid() { // Arrange Controller.ModelState.AddModelError("Fake", "FakeError"); // Act var controllerResult = await Controller.Save(null); // Assert var result = Assert.IsType <JsonResult>(controllerResult); dynamic data = JObject.FromObject(result.Value); ((string)data.message).ShouldBe("There were problems with your order. Unable to save. Errors: FakeError"); ((bool)data.success).ShouldBe(false); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestEditUserPostRedirectsWhenUserNotFound(string value) { // Arrange // Act var controllerResult = await Controller.EditUser(value, CreateValidEntities.User(7)); // Assert var redrectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redrectResult.ActionName.ShouldBe("ListClients"); redrectResult.ControllerName.ShouldBeNull(); Controller.ErrorMessage.ShouldBe("User Not Found."); MockDbContext.Verify(a => a.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Never); MockDbContext.Verify(a => a.Update(It.IsAny <User>()), Times.Never); }
public async Task TestDeleteRedirectsAndDeletes() { // Arrange OrderData[1].CreatorId = "Creator1"; OrderData[1].Status = OrderStatusCodes.Created; Controller.Message = null; // Act var controllerResult = await Controller.Delete(2); // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Index"); redirectResult.ControllerName.ShouldBeNull(); Controller.Message.ShouldBe("Order deleted"); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Once); MockDbContext.Verify(a => a.Remove(OrderData[1]), Times.Once); }
public async Task Given_AValidTestDtoObjectWithChanges_When_UpdateIsInvoked_Then_SaveChangesAsyncMethodShouldOnlyBeInvokedOnce() { var testObjectWithUpdates = new DateRangeEffectiveDtoObject { Id = StaticTestValues.ValidId1, Property = StaticTestValues.ValidProperty2, VirtualProperty = StaticTestValues.ValidVirtualProperty2, EffectiveStartDate = StaticTestValues.EffectiveStartDateTimeOffset2, EffectiveEndDate = StaticTestValues.EffectiveEndDateTimeOffset2, CreatedByUserId = StaticTestValues.CreatedByUserId2, CreatedOn = StaticTestValues.CreatedOnDateTimeOffset2, LastUpdatedByUserId = StaticTestValues.LastUpdatedByUserId2, LastUpdatedOn = StaticTestValues.LastUpdatedOnDateTimeOffset2 }; await DbContextBase.Update(testObjectWithUpdates); MockDbContext.Verify(m => m.SaveChangesAsync(), Times.Once); }
[InlineData(PaymentTypeCodes.UcDavisAccount)] //Probably will not happen public async Task TestConfirmPaymentPostWhenSuccess(string paymentType) { // Arrange OrderData[1].Paid = false; OrderData[1].PaymentType = paymentType; Controller.ErrorMessage = null; var op = CreateValidEntities.OtherPaymentInfo(5); op.PaymentType = "Changed"; Order savedOrder = null; string savedSubject = null; MockOrderMessageService.Setup(a => a.EnqueueBillingMessage(It.IsAny <Order>(), It.IsAny <string>())) .Callback <Order, string>((or, sub) => { savedOrder = or; savedSubject = sub; }).Returns(Task.CompletedTask); savedOrder.ShouldBeNull(); savedSubject.ShouldBeNull(); // Act var controllerResult = await Controller.ConfirmPayment(OrderData[1].ShareIdentifier, op); // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ActionName.ShouldBe("Link"); redirectResult.ControllerName.ShouldBeNull(); redirectResult.RouteValues["id"].ShouldBe(OrderData[1].ShareIdentifier); MockOrderMessageService.Verify(a => a.EnqueueBillingMessage(It.IsAny <Order>(), It.IsAny <string>()), Times.Once); //TODO: Examine passed Parameters savedOrder.ShouldNotBeNull(); savedOrder.Id.ShouldBe(OrderData[1].Id); savedSubject.ShouldBe("Anlab Work Order Billing Info"); MockDbContext.Verify(a => a.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once); OrderData[1].Paid.ShouldBe(true); OrderData[1].Status.ShouldBe(OrderStatusCodes.Complete); }
public async Task TestSaveChecksIfItIsYourOrder() { // Arrange OrderData[1].CreatorId = "XXX"; var model = new OrderSaveModel(); model.OrderId = 2; // Act var controllerResult = await Controller.Save(model); // Assert var result = Assert.IsType <JsonResult>(controllerResult); dynamic data = JObject.FromObject(result.Value); ((string)data.message).ShouldBe("This is not your order."); ((bool)data.success).ShouldBe(false); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestSaveChecksIfYourOrderIsInCreatedStatus() { // Arrange OrderData[1].Status = OrderStatusCodes.Confirmed; OrderData[1].CreatorId = "Creator1"; var model = new OrderSaveModel(); model.OrderId = 2; // Act var controllerResult = await Controller.Save(model); // Assert var result = Assert.IsType <JsonResult>(controllerResult); dynamic data = JObject.FromObject(result.Value); ((string)data.message).ShouldBe("This has been confirmed and may not be updated."); ((bool)data.success).ShouldBe(false); MockDbContext.Verify(a => a.SaveChangesAsync(new CancellationToken()), Times.Never); }
public async Task TestEditUserPostSavesExpectedFields() { // Arrange var model = CreateValidEntities.User(7, true); model.Id = UserData[1].Id; // Act var controllerResult = await Controller.EditUser(UserData[1].Id, model); // Assert var redirectResult = Assert.IsType <RedirectToActionResult>(controllerResult); redirectResult.ShouldNotBeNull(); redirectResult.ActionName.ShouldBe("ListClients"); redirectResult.ControllerName.ShouldBeNull(); Controller.ErrorMessage.ShouldBeNull(); Controller.Message.ShouldBe("User Updated."); MockDbContext.Verify(a => a.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once); MockDbContext.Verify(a => a.Update(UserData[1]), Times.Once); //Fields UserData[1].FirstName.ShouldBe("FirstName7"); UserData[1].LastName.ShouldBe("LastName7"); UserData[1].Name.ShouldBe("FirstName7 LastName7"); UserData[1].Phone.ShouldBe("Phone7"); UserData[1].Account.ShouldBe("ACCOUNT7"); UserData[1].ClientId.ShouldBe("CLIENTID7"); UserData[1].CompanyName.ShouldBe("CompanyName7"); UserData[1].BillingContactName.ShouldBe("BillingContactName7"); UserData[1].BillingContactAddress.ShouldBe("BillingContactAddress7"); UserData[1].BillingContactEmail.ShouldBe("*****@*****.**"); UserData[1].BillingContactPhone.ShouldBe("BillingContactPhone7"); UserData[1].NormalizedUserName.ShouldBe("NormalizedUserName2"); //Unchanged }
public async Task TestUnlockUpdatesInvoiceWhenHasNotBeenPaid() { // Arrange InvoiceData[0].Status = Invoice.StatusCodes.Sent; InvoiceData[0].Sent = true; InvoiceData[0].SentAt = DateTime.UtcNow; InvoiceData[0].LinkId = "FakeLink"; // Act var controllerResult = await Controller.Unlock(1); // Assert MockDbContext.Verify(a => a.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once); InvoiceData[0].Status.ShouldBe(Invoice.StatusCodes.Draft); InvoiceData[0].Sent.ShouldBeFalse(); InvoiceData[0].SentAt.ShouldBeNull(); InvoiceData[0].LinkId.ShouldBeNull(); InvoiceData[0].History.ShouldNotBeNull(); InvoiceData[0].History.Count.ShouldBe(1); InvoiceData[0].History[0].Actor.ShouldBe("FirstName2 LastName2"); InvoiceData[0].History[0].Type.ShouldBe(HistoryActionTypes.InvoiceUnlocked.TypeCode); InvoiceData[0].History[0].ActionDateTime.ShouldNotBe(default);
public async Task TestEditUserPostReturnsViewIfModelStateInvalid() { // Arrange var model = CreateValidEntities.User(7); model.Id = UserData[1].Id; Controller.ModelState.AddModelError("Fake", "Fake Error"); // Act var controllerResult = await Controller.EditUser(UserData[1].Id, model); // Assert var viewResult = Assert.IsType <ViewResult>(controllerResult); var modelResult = Assert.IsType <User>(viewResult.Model); modelResult.ShouldNotBeNull(); modelResult.Id.ShouldBe(UserData[1].Id); Controller.ErrorMessage.ShouldBe("The user had invalid data."); MockDbContext.Verify(a => a.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Never); MockDbContext.Verify(a => a.Update(It.IsAny <User>()), Times.Never); }
public async Task Given_AValidTestDtoObjectId_When_DeleteIsInvoked_Then_SaveChangesAsyncMethodShouldOnlyBeInvokedOnce() { await DbContextBase.Delete(StaticTestValues.ValidId1); MockDbContext.Verify(m => m.SaveChangesAsync(), Times.Once()); }
public async Task Given_ABusinessComponent_When_UpdateIsInvoked_Then_SaveChangesAsyncMethodShouldBeInvokedOnlyOnce() { await BusinessComponentsDal.Update(new BusinessComponentDto { Id = TestData.FirstValidBusinessComponentId }); MockDbContext.Verify(m => m.SaveChangesAsync(), Times.Once()); }
public async Task Given_ATestDtoObject_When_InsertIsInvoked_Then_SaveChangesAsyncMethodShouldBeInvokedOnlyOnce() { await DbContextBase.Create(new DateRangeEffectiveDtoObject()); MockDbContext.Verify(m => m.SaveChangesAsync(), Times.Once()); }