C# 权限授权筛选器上的自定义循环逻辑错误

C# 权限授权筛选器上的自定义循环逻辑错误,c#,asp.net-core,C#,Asp.net Core,我这里有误报。基本上,我只想在用户没有权限进行创建时返回一个错误,但当前即使用户有该权限,它也会返回一个错误 使用者 许可 活跃的 12 创造 真的 12 更新 假的 12 删除 真的 最后,我将逻辑放在单元测试中,让它工作起来。有时候,最好的做法是将代码从主问题中取出,在单元测试中逐行处理 这项工作现在正如预期的那样,但我觉得可以做得更好 public void Setup() { permissionsList = new List<AppWarehouseUserPermm

我这里有误报。基本上,我只想在用户没有权限进行
创建
时返回一个错误,但当前即使用户有该权限,它也会返回一个错误

使用者 许可 活跃的 12 创造 真的 12 更新 假的 12 删除 真的
最后,我将逻辑放在单元测试中,让它工作起来。有时候,最好的做法是将代码从主问题中取出,在单元测试中逐行处理

这项工作现在正如预期的那样,但我觉得可以做得更好

 public void Setup()
 {
   permissionsList = new List<AppWarehouseUserPermmissions>(){
    new AppWarehouseUserPermmissions {
        Name="Create",
        Action="Create",
        Controller="StockItems",            
        isAcitve=true            
        //populate other properties  
    }, new AppWarehouseUserPermmissions {
        Name="Update",
        Action="Update",
        Controller="StockItems",
        isAcitve=false            
        //populate other properties  
    },new AppWarehouseUserPermmissions {
        Name="Delete",
        Action="Delete",
        Controller="StockItems",
        isAcitve=true            
        //populate other properties  
    },
       new AppWarehouseUserPermmissions
      {
          Name = "Read",
          Action = "Read",
          Controller = "Test",
          isAcitve = true
          //populate other properties  
      }
     };
     };
  }

[TestCase("Create,Update", "StockItems", 1)]
[TestCase("Delete,Update", "StockItems", 1)]
[TestCase("Read,Update", "StockItems", 2)]
[TestCase("Update", "StockItems", 1)]

public void Vlaidate_Permissions_Returns_Corrrect_Errors(string permission,string controllerName,int expected)
{
        bool isAuthorised = false;
        List<CutomError> _customError = new List<CutomError>();
        string[] permissionValues = permission.Split(",");
        Dictionary<string, bool> dict = new Dictionary<string, bool>();

        foreach (var item in permissionValues)
        {
            var permissions = permissionsList.Any(w => w.Name == item && w.Controller == controllerName && w.isAcitve == true);
            dict.Add(item, permissions);
        }
        foreach (var item in dict)
        {
            if (item.Key == "Create" || item.Key == "Read" || item.Key == "Update" || item.Key == "Delete" && item.Value == true)
            {
                isAuthorised = true;

            }
           
        }
        foreach (var item in dict.Where(w => w.Value == false))
        {
            _customError.Add(new CutomError() { Message = $"Uwas has no {item.Key} permissions", ErrorCode = item.Key });
        }
        
        Assert.AreEqual(expected, _customError.Count);
    }
}
公共作废设置()
{
permissionsList=新列表(){
新AppWarehouseUserPermmissions{
Name=“创建”,
Action=“创建”,
Controller=“StockItems”,
Isacitive=true
//填充其他属性
},新AppWarehouseUserPermmissions{
Name=“更新”,
Action=“更新”,
Controller=“StockItems”,
Isacitive=假
//填充其他属性
},新AppWarehouseUserPermmissions{
Name=“删除”,
Action=“删除”,
Controller=“StockItems”,
Isacitive=true
//填充其他属性
},
新AppWarehouseUserPermmissions
{
Name=“Read”,
Action=“Read”,
Controller=“Test”,
Isacitive=true
//填充其他属性
}
};
};
}
[TestCase(“创建、更新”、“库存项目”,1)]
[TestCase(“删除、更新”、“库存项目”,1)]
[TestCase(“读取、更新”、“库存项”,2)]
[测试用例(“更新”、“库存项目”,1)]
public void Vlaidate_Permissions_返回_correct_错误(字符串权限、字符串控制器名称、预期整数)
{
bool isauthorized=false;
列表_customError=新列表();
字符串[]permissionValues=权限.Split(“,”);
Dictionary dict=新字典();
foreach(permissionValues中的var项)
{
var permissions=permissionsList.Any(w=>w.Name==item&&w.Controller==controllerName&&w.isacitive==true);
dict.Add(项目、权限);
}
foreach(dict中的var项目)
{
如果(item.Key==“创建”| | item.Key==“读取”| | item.Key==“更新”| | item.Key==“删除”&&item.Value==true)
{
Isauthorized=正确;
}
}
foreach(dict.Where中的var项(w=>w.Value==false))
{
_添加(新的CutomError(){Message=$“Uwas没有{item.Key}权限”,ErrorCode=item.Key});
}
Assert.AreEqual(应为_customError.Count);
}
}