Ejemplo n.º 1
0
        public void GetUser()
        {
            var user = new User
            {
                id            = 1,
                name          = "user",
                address       = "add",
                password      = "******",
                email         = "email",
                customer_code = "cust",
                token         = "tok",
                isInternal    = false,
                lastname      = "last",
                lastLogin     = DateTime.Today,
                phone         = "phone",
                username      = "******",
                Customer      = new Customer
                {
                    code             = "c",
                    address1         = "add",
                    address2         = "add",
                    address3         = "add",
                    address4         = "add",
                    address5         = "add",
                    address6         = "add",
                    currency         = "curr",
                    analysis_codes_1 = "a",
                    county           = "c",
                    town_city        = "t",
                    name             = "n"
                },
                Roles = new List <Role>
                {
                    new Role {
                        id = Role.User, name = "User"
                    }
                },
                Permissions = new List <Permission>
                {
                    new Permission {
                        id = (int)PermissionId.ViewAccountDetails
                    }
                }
            };

            unitOfWork.Data = new MockData
            {
                Users = new List <User>
                {
                    user
                }
            };
            var data = controller.GetUser(1);

            string[] properties =
            {
                "id",        "name",  "address", "password", "email", "customer_code", "token", "isInternal", "lastname",
                "lastLogin", "phone", "username"
            };
            string[] customerProperties =
            {
                "code",
                "address1",
                "address2",
                "address3",
                "address4",
                "address5",
                "address6",
                "currency",
                "analysis_codes_1",
                "county",
                "town_city",
                "name"
            };
            foreach (var prop in properties)
            {
                var origPropInfo = user.GetType().GetProperty(prop);
                var dataPropInfo = data.GetType().GetProperty(prop);
                if (origPropInfo != null && dataPropInfo != null)
                {
                    Assert.AreEqual(origPropInfo.GetValue(user), dataPropInfo.GetValue(data));
                }
            }

            Assert.AreEqual(false, data.GetType().GetProperty("isBranchAdmin")?.GetValue(data));
            Assert.AreEqual(false, data.GetType().GetProperty("isTopAdmin")?.GetValue(data));
            Assert.IsNotNull(data.GetType().GetProperty("customer")?.GetValue(data));
            var cust = data.GetType().GetProperty("customer")?.GetValue(data);

            foreach (var prop in customerProperties)
            {
                var origPropInfo = user.Customer.GetType().GetProperty(prop);
                var dataPropInfo = cust.GetType().GetProperty(prop);
                if (origPropInfo != null && dataPropInfo != null)
                {
                    Assert.AreEqual(origPropInfo.GetValue(user.Customer), dataPropInfo.GetValue(cust));
                }
            }

            var rolesProp = data.GetType().GetProperty("roles");

            Assert.IsNotNull(rolesProp);
            var roleInterface = rolesProp.PropertyType.GetInterfaces()
                                .FirstOrDefault(i => i == typeof(IEnumerable));

            Assert.IsNotNull(roleInterface);
            Assert.IsNotNull(rolesProp.PropertyType.GenericTypeArguments);
            Assert.AreEqual(1, rolesProp.PropertyType.GenericTypeArguments.Length);
            var idProp   = rolesProp.PropertyType.GenericTypeArguments[0].GetProperty("id");
            var nameProp = rolesProp.PropertyType.GenericTypeArguments[0].GetProperty("name");

            Assert.IsNotNull(idProp);
            Assert.IsNotNull(nameProp);

            var roleData = rolesProp.GetValue(data);

            Assert.IsNotNull(roleData);
            var collection = (roleData as IEnumerable).Cast <object>().ToList();

            Assert.IsNotNull(collection);
            Assert.AreEqual(1, collection.Count);

            Assert.AreEqual(user.Roles[0].id, idProp.GetValue(collection[0]));
            Assert.AreEqual(user.Roles[0].name, nameProp.GetValue(collection[0]));

            var permProp = data.GetType().GetProperty("permissions");

            Assert.IsNotNull(permProp);
            TestCollection(permProp.GetValue(data), 1);
        }