コード例 #1
0
        public void GetRolesModel()
        {
            unitOfWork.Data       = Utils.CreateAdminAndUser();
            unitOfWork.Data.Roles = new List <Role>
            {
                new Role {
                    id = Role.User, name = "User", Permissions = new List <Permission>()
                },
                new Role {
                    id = Role.BranchAdmin, name = "Branch", Permissions = new List <Permission>()
                },
            };
            unitOfWork.Data.Permissions = new List <Permission>
            {
                new Permission {
                    id = 1, name = "1"
                },
                new Permission {
                    id = 2, name = "2"
                }
            };
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "2");
            var result = controller.GetRolesModel();

            Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
            controller.Request.Headers.Authorization = new AuthenticationHeaderValue("jwt", "1");
            result = controller.GetRolesModel();
            Assert.IsNotInstanceOfType(result, typeof(UnauthorizedResult));
            var rolesProp       = result.GetType().GetProperty("roles");
            var permissionsProp = result.GetType().GetProperty("permissions");

            Assert.IsNotNull(rolesProp);
            Assert.IsNotNull(permissionsProp);
            var rolesData = rolesProp.GetValue(result);

            TestCollection(rolesData, 2);
            TestCollection(permissionsProp.GetValue(result), 2);
            var rolesCollection = (rolesData as IEnumerable).Cast <object>().ToList();

            permissionsProp = rolesCollection[0].GetType().GetProperty("permissions");
            Assert.IsNotNull(permissionsProp);
        }