public void Order_ShouldSendEmailToUser_WhenValidOrder() { mapper.Execute(); var testId = "testId"; var testUser = new User() { DeliveryCountry = "BG", DeliveryTown = "SF", DeliveryAddress = "Liulin", Email = "testEmail" }; var usersMock = new Mock <IUsersService>(); usersMock.Setup(v => v.GetById(testId)).Returns(() => testUser); var ordersMock = new Mock <IOrdersService>(); ordersMock.Setup(v => v.Add(It.IsAny <Order>())); ordersMock.Setup(v => v.IsValidOrder(It.IsAny <Order>())).Returns(true); var emailsMock = new Mock <IEmailsService>(); emailsMock.Setup(x => x.SendEmail(testUser.Email, string.Format(GlobalConstants.OrderMade, It.IsAny <string>()), It.IsAny <string>(), GlobalConstants.SMTPServer, GlobalConstants.EmailPrimary, GlobalConstants.EmailPrimaryPassword)); // moq the user var claim = new Claim("test", testId); var mockIdentity = Mock.Of <ClaimsIdentity>(ci => ci.FindFirst(It.IsAny <string>()) == claim); var controller = new OrdersController(ordersMock.Object, usersMock.Object, emailsMock.Object, null) { User = Mock.Of <IPrincipal>(ip => ip.Identity == mockIdentity) }; var orderedItemRequest = new OrderedItemRequestModel() { Manufacturer = "nordglass", Description = "Description", Price = 1 }; var model = new OrderRequestModel { DeliveryNotes = "DeliveryNotes", Price = 1, PaidPrice = 1, UserЕmail = "testEmail", UserId = testId, FullAddress = "BG; SF; Liulin", OrderedItems = new List <OrderedItemRequestModel> { orderedItemRequest } }; var result = controller.Order(model); Assert.IsInstanceOfType(result, typeof(OkResult)); emailsMock.VerifyAll(); }
public void Order_ShouldAddNewOrderWithUserInfo_WhenAuthorized() { mapper.Execute(); var testId = "testId"; var testUser = new User() { DeliveryCountry = "BG", DeliveryTown = "SF", DeliveryAddress = "Liulin" }; var usersMock = new Mock <IUsersService>(); usersMock.Setup(v => v.GetById(testId)).Returns(() => testUser); var ordersMock = new Mock <IOrdersService>(); OrderedItem orderedItem = new OrderedItem() { Manufacturer = "nordglass", Price = 1, CreatedOn = DateTime.MinValue, DeletedOn = null, Id = 0, IsDeleted = false, ModifiedOn = null, OtherCodes = null, }; var modelToAdd = new Order { UserInfo = null, UserЕmail = "testEmail", Status = DeliveryStatus.Unpaid, FullAddress = "BG; SF; Liulin", WithInstallation = false, DeliveryNotes = "DeliveryNotes", PaidPrice = 1, Price = 1, CreatedOn = DateTime.MinValue, DeletedOn = null, Id = 0, IsDeleted = false, IsInvoiceNeeded = false, ModifiedOn = null, User = testUser, UserId = testId, OrderedItems = new List <OrderedItem> { orderedItem } }; ordersMock.Setup(v => v.Add(It.IsAny <Order>())); ordersMock.Setup(v => v.IsValidOrder(It.IsAny <Order>())).Returns(true); var emailsMock = new Mock <IEmailsService>(); // moq the user var claim = new Claim("test", testId); var mockIdentity = Mock.Of <ClaimsIdentity>(ci => ci.FindFirst(It.IsAny <string>()) == claim); var controller = new OrdersController(ordersMock.Object, usersMock.Object, emailsMock.Object, null) { User = Mock.Of <IPrincipal>(ip => ip.Identity == mockIdentity) }; var orderedItemRequest = new OrderedItemRequestModel() { Manufacturer = "nordglass", Description = "Description", Price = 1 }; var model = new OrderRequestModel { DeliveryNotes = "DeliveryNotes", Price = 1, PaidPrice = 1, UserЕmail = "testEmail", UserId = testId, FullAddress = "BG; SF; Liulin", OrderedItems = new List <OrderedItemRequestModel> { orderedItemRequest } }; var result = controller.Order(model); Assert.IsInstanceOfType(result, typeof(OkResult)); ordersMock.Verify(m => m.IsValidOrder(It.Is <Order>(x => AreObjectsEqual(x, modelToAdd)))); ordersMock.Verify(m => m.Add(It.Is <Order>(x => AreObjectsEqual(x, modelToAdd)))); }
public async Task Order_ShouldReturnBadRequest_WhenOrderNotValid() { mapper.Execute(); var usersMock = new Mock <IUsersService>(); var ordersMock = new Mock <IOrdersService>(); OrderedItem orderedItem = new OrderedItem() { Manufacturer = "nordglass", Price = 1, CreatedOn = DateTime.MinValue, DeletedOn = null, Id = 0, IsDeleted = false, ModifiedOn = null, OtherCodes = null, }; var modelToAdd = new Order { UserInfo = null, UserЕmail = "testEmail", Status = DeliveryStatus.Unpaid, FullAddress = "BG; SF; Liulin", WithInstallation = false, DeliveryNotes = "DeliveryNotes", PaidPrice = 1, Price = 1, CreatedOn = DateTime.MinValue, DeletedOn = null, Id = 0, IsDeleted = false, IsInvoiceNeeded = false, ModifiedOn = null, OrderedItems = new List <OrderedItem> { orderedItem } }; ordersMock.Setup(v => v.IsValidOrder(It.IsAny <Order>())).Returns(false); var controller = new OrdersController(ordersMock.Object, usersMock.Object, null, null); var orderedItemRequest = new OrderedItemRequestModel() { Manufacturer = "nordglass", Description = "Description", Price = 1 }; var model = new OrderRequestModel { DeliveryNotes = "DeliveryNotes", Price = 1, PaidPrice = 1, UserЕmail = "testEmail", FullAddress = "BG; SF; Liulin", OrderedItems = new List <OrderedItemRequestModel> { orderedItemRequest } }; var result = await controller.Order(model); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); string responseMessage = ((BadRequestErrorMessageResult)result).Message; Assert.IsTrue(responseMessage.Contains("Error while valditing order")); ordersMock.Verify(m => m.IsValidOrder(It.Is <Order>(x => AreObjectsEqual(x, modelToAdd)))); }