Exemplo n.º 1
0
        public void Should_Get_Order_ById_Success_Status_Code()
        {
            // Arrange
            Guid  _guid = Guid.NewGuid();
            Order order = new Order()
            {
                Id = _guid, OrderId = "testid-" + _guid, Description = "Order Description"
            };

            Mock <IOrderService> mockOrderService = new Mock <IOrderService>();

            mockOrderService.Setup(o => o.GetOrder(It.IsAny <string>()))
            .Returns(order);
            OrderApiController controller = new OrderApiController(mockOrderService.Object);

            // Act
            var result      = controller.GetOrder(_guid.ToString());
            var checkresult = result as ObjectResult;

            // Assert
            Assert.IsNotNull(checkresult);
            Assert.AreEqual(200, checkresult.StatusCode);
        }
Exemplo n.º 2
0
        public void GetOrder()
        {
            unitOfWork.Data = Utils.CreateAdminAndUser();
            //NO user
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "0");
            var result = controller.GetOrder("o", "c");

            Assert.IsInstanceOfType(result.Result, typeof(UnauthorizedResult));

            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "2");
            var permissions = new List <Permission>
            {
                new Permission {
                    id = (int)PermissionId.ViewOrderHistory
                }
            };

            cache.Set($"permissions_2", permissions, null);
            result = controller.GetOrder("o", "c1");
            Assert.IsNotNull(result);
            Assert.IsNotNull(apiClient.Parameters);
            Assert.IsTrue(apiClient.Parameters.ContainsKey("order_no"));
            Assert.IsTrue(apiClient.Parameters.ContainsKey("customer_code"));
            Assert.AreEqual(unitOfWork.Data.Users.FirstOrDefault(u => u.id == 2)?.customer_code, apiClient.Parameters["customer_code"]);

            //Branch admin
            unitOfWork.Data           = Utils.CreateAdminAndUser();
            apiClient.Data.Order      = new Order();
            unitOfWork.Data.Customers = new List <Customer>
            {
                new Customer {
                    code = "c1"
                },
                new Customer {
                    code = "c2", invoice_customer = "c3"
                },
                new Customer {
                    code = "c3"
                }
            };

            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "3");
            cache.Set($"permissions_3", permissions, null);
            unitOfWork.Data.Users.Add(new User {
                id = 3, Roles = new List <Role> {
                    new Role {
                        id = Role.BranchAdmin
                    }
                }, customer_code = "c3"
            });
            var res = controller.GetOrder("o", "c3").Result;

            Assert.IsNotNull(res);
            Assert.AreEqual("c3", apiClient.Parameters["customer_code"]);
            res = controller.GetOrder("o", "c2").Result;
            Assert.IsNotNull(res);
            Assert.AreEqual("c2", apiClient.Parameters["customer_code"]);

            res = controller.GetOrder("o", "c1").Result;
            Assert.IsNotNull(res);
            Assert.AreNotEqual("c1", apiClient.Parameters["customer_code"]);

            //permissions
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "2");
            cache.Remove("permissions_2");
            res = controller.GetOrder("o", "c2").Result;
            Assert.IsNotNull(res);
            Assert.IsInstanceOfType(res, typeof(UnauthorizedResult));
        }