public void Setup() { _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <RoleProfile>(); })); _dbFaker = new SecurityDbFaker(); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.DeleteUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(l => l.UserId == id)))) .Returns <string>(id => { return(Task.FromResult(true)); }); //not exists _mock.Setup(x => x.DeleteUserRights(It.Is <string>(id => _dbFaker.UserRights.All(l => l.UserId != id)))) .Returns <string>(id => { throw new SecurityDbException("not exists", ExceptionType.NotFound, EntityType.Role, new[] { id.ToString() }); }); _securityData = new UserRightsService(_mock.Object, _mapper, new UserRightsValidator(), new AccessRequestValidator()); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <FeatureProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.DeleteFeature(It.Is <int>(id => _dbFaker.Features.Any(l => l.Id == id)))) .Returns <int>(id => { return(Task.FromResult(true)); }); //not exists _mock.Setup(x => x.DeleteFeature(It.Is <int>(id => _dbFaker.Features.All(l => l.Id != id)))) .Returns <int>(id => { throw new SecurityDbException("not exists", ExceptionType.NotFound, EntityType.Feature, new[] { id.ToString() }); }); _securityData = new FeatureService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <UserRightsProfile>(); })); _mock = new Mock <ISecurityData>(); _mock.Setup(x => x.GetUserRights(It.IsAny <string>())) .Returns((string userId) => Task.FromResult( _dbFaker.UserRights .Single(l => l.UserId == userId))); _mock.Setup(x => x.GetUserRights(It.Is <string>(s => _dbFaker.UserRights.All(l => l.UserId != s)))) .Returns((string userId) => throw new SecurityDbException("User rights was not found", ExceptionType.NotFound, EntityType.UserRights, new[] { userId })); _securityData = new UserRightsService(_mock.Object, _mapper, new UserRightsValidator(), new AccessRequestValidator()); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <FeatureProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.AddFeature(It.Is <string>(l => !_dbFaker.Features.Any(k => k.Name.Equals(l))))) .Returns(() => Task.FromResult( _dbFaker.Features.Max(l => l.Id) + 1)); //existed name _mock.Setup(x => x.AddFeature(It.Is <string>(l => _dbFaker.Features.Any(k => k.Name.Equals(l))))) .Returns <string>(name => { throw new SecurityDbException("existed name", ExceptionType.NameExists, EntityType.Feature, new [] { name }); }); //error _mock.Setup(x => x.AddFeature(It.Is <string>(l => l.Equals("Error!")))) .Returns <string>(name => { throw new Exception(); }); _securityData = new FeatureService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <AccessFunctionProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.AddFunction(It.Is <string>(name => !string.IsNullOrEmpty(name) && !_dbFaker.Functions.Any(l => l.Name.Equals(name))), It.Is <int>(featureId => _dbFaker.Features.Any(l => l.Id == featureId)))) .Returns(() => Task.FromResult(_dbFaker.Functions.Max(l => l.Id) + 1)); //name existed _mock.Setup(x => x.AddFunction(It.Is <string>(name => !string.IsNullOrEmpty(name) && _dbFaker.Functions.Any(l => l.Name.Equals(name))), It.Is <int>(featureId => _dbFaker.Features.Any(l => l.Id == featureId)))) .Returns <string, int>((name, id) => { throw new SecurityDbException("name existed", ExceptionType.NameExists, EntityType.Function, new[] { name }); }); //feature not exists _mock.Setup(x => x.AddFunction(It.Is <string>(name => !string.IsNullOrEmpty(name) && !_dbFaker.Functions.Any(l => l.Name.Equals(name))), It.Is <int>(featureId => _dbFaker.Features.All(l => l.Id != featureId)))) .Returns <string, int>((name, id) => { throw new SecurityDbException("feature not exists", ExceptionType.NotFound, EntityType.Feature, new[] { id.ToString() }); }); //feature not exists _mock.Setup(x => x.AddFunction(It.Is <string>(name => name.Equals("Error!")), It.IsAny <int>())) .Returns <string, int>((name, id) => { throw new Exception(); }); _securityData = new FunctionService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <RoleProfile>(); })); _mock = new Mock <ISecurityData>(); _mock.Setup(x => x.GetRoles(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())) .Returns((int i, int p, string m) => Task.FromResult(new RolesGetResult { Roles = _dbFaker.Roles.Where(l => string.IsNullOrEmpty(m) || l.Name.Contains(m)) .OrderBy(l => l.Name).Skip((p - 1) * i).Take(i) })); _securityData = new RoleService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <AccessFunctionProfile>(); cfg.AddProfile <FeatureProfile>(); cfg.AddProfile <RoleProfile>(); cfg.AddProfile <UserRightsProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.AddRights(It.Is <string[]>(right => !right.Any(l => _dbFaker.AccessRights.Any(k => k.Name == l))))) .Returns <string[]>(names => { return(Task.FromResult(names.Select((name, i) => new AccessRightDb { Name = name, Id = _dbFaker.AccessRights.Max(l => l.Id) + i + 1 }).ToArray())); }); //existed name _mock.Setup(x => x.AddRights(It.Is <string[]>(right => right.Any(l => _dbFaker.AccessRights.Any(k => k.Name == l))))) .Returns <string[]>(names => { throw new SecurityDbException("existed name", ExceptionType.NameExists, EntityType.Right, names); }); //unknown error _mock.Setup(x => x.AddRights(It.Is <string[]>(right => right.Any(l => l.Equals("Error!"))))) .Returns <string[]>(names => { throw new Exception(); }); _securityData = new RightsService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <FeatureProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.EditFunctionRights(It.Is <int>(id => _dbFaker.Functions.Any(k => k.Id.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <int, int[]>((id, ids) => { return(Task.FromResult(true)); }); //not existed function _mock.Setup(x => x.EditFunctionRights(It.Is <int>(id => !_dbFaker.Functions.Any(k => k.Id.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <int, int[]>((id, ids) => { throw new SecurityDbException("not existed function", ExceptionType.NotFound, EntityType.Function, new[] { id.ToString() }); }); //not existed rights _mock.Setup(x => x.EditFunctionRights(It.Is <int>(id => _dbFaker.Functions.Any(k => k.Id.Equals(id))), It.Is <int[]>(ids => !ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <int, int[]>((id, ids) => { throw new SecurityDbException("not existed rights", ExceptionType.NotFound, EntityType.Right, ids.Select(l => l.ToString()).ToArray()); }); _securityData = new FunctionService(_mock.Object, _mapper); }
public void Setup() { _dbFaker = new SecurityDbFaker(); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.AddProfile <AccessRightProfile>(); cfg.AddProfile <FeatureProfile>(); })); _mock = new Mock <ISecurityData>(); //success _mock.Setup(x => x.EditUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { return(Task.FromResult(true)); }); //not existed userRights _mock.Setup(x => x.EditUserRights(It.Is <string>(id => !_dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { throw new SecurityDbException("", ExceptionType.NotFound, EntityType.UserRights, new [] { id }); }); //not existed role _mock.Setup(x => x.EditUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => !ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { throw new SecurityDbException("", ExceptionType.NotFound, EntityType.Role, rIds.Select(l => l.ToString()).ToArray()); }); //not existed rights 1 _mock.Setup(x => x.EditUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => !ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { throw new SecurityDbException("", ExceptionType.NotFound, EntityType.Right, aIds.Select(l => l.ToString()).ToArray()); }); //not existed rights 2 _mock.Setup(x => x.EditUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => !ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { throw new SecurityDbException("", ExceptionType.NotFound, EntityType.Right, dIds.Select(l => l.ToString()).ToArray()); }); //not existed function _mock.Setup(x => x.EditUserRights(It.Is <string>(id => _dbFaker.UserRights.Any(k => k.UserId.Equals(id))), It.Is <int[]>(ids => ids.All(l => _dbFaker.Roles.Any(k => k.Id == l))), It.Is <int[]>(ids => !ids.All(l => _dbFaker.Functions.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))), It.Is <int[]>(ids => ids.All(l => _dbFaker.AccessRights.Any(k => k.Id == l))))) .Returns <string, int[], int[], int[], int[]>((id, rIds, fIds, aIds, dIds) => { throw new SecurityDbException("", ExceptionType.NotFound, EntityType.Function, fIds.Select(l => l.ToString()).ToArray()); }); _securityData = new UserRightsService(_mock.Object, _mapper, new UserRightsValidator(), new AccessRequestValidator()); }