Пример #1
0
        public void UpdateUserData()
        {
            unitOfWork.Data = new MockData
            {
                Users = new List <User>
                {
                    new User
                    {
                        id            = 1,
                        email         = "user",
                        customer_code = "c1",
                        Roles         = new List <Role>
                        {
                            new Role {
                                id = Role.Admin
                            }
                        }
                    },
                    new User
                    {
                        id            = 2,
                        username      = "******",
                        address       = "address",
                        customer_code = "c2",
                        Customer      = null,
                        Roles         = new List <Role>
                        {
                            new Role {
                                id = Role.User
                            }
                        }
                    }
                },
                Customers = new List <Customer>
                {
                    new Customer
                    {
                        code     = "c2",
                        address2 = "address"
                    }
                }
            };

            var data = new User
            {
                id            = 1,
                email         = "user",
                customer_code = "c2",
                Customer      = new Customer
                {
                    code     = "c2",
                    address2 = "address2"
                }
            };

            //User with no rights
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "2");
            var result = controller.UpdateData(data);

            Assert.IsInstanceOfType(result, typeof(HttpResponseMessage));
            var message = result as HttpResponseMessage;

            Assert.AreEqual(HttpStatusCode.Unauthorized, message?.StatusCode);

            //Correct user, change customer data
            data.id      = 2;
            data.address = "address2";
            result       = controller.UpdateData(data);
            Assert.IsNotInstanceOfType(result, typeof(HttpResponseMessage));
            Assert.IsTrue(unitOfWork.Saved);
            Assert.AreEqual(data.Customer.address2, unitOfWork.Data.Customers.FirstOrDefault(c => c.code == data.customer_code)?.address2);
            Assert.AreEqual(data.email, unitOfWork.Data.Users.FirstOrDefault(u => u.id == data.id)?.email);
            Assert.IsFalse(string.IsNullOrEmpty(mailHelper.Data.Subject));

            //Non existing user
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "1");
            data.id = 3;
            result  = controller.UpdateData(data);
            Assert.IsInstanceOfType(result, typeof(HttpResponseMessage));
            message = result as HttpResponseMessage;
            Assert.AreEqual(HttpStatusCode.BadRequest, message?.StatusCode);
        }