public LoginServices(IServiceBaseParameter <AspNetUsers> serviceBaseParameter, ITokenBusiness tokenBusiness, IIdentityUnitOfWork <AspNetRoles> roleUnitOfWork, IIdentityUnitOfWork <AspNetUsersRoles> userRolesUnitOfWork) { _tokenBusiness = tokenBusiness; _serviceBaseParameter = serviceBaseParameter; _roleUnitOfWork = roleUnitOfWork; _userRolesUnitOfWork = userRolesUnitOfWork; }
public async Task CreateUserAsyncMethod_WithFirstUser_ShouldBeReturnedSuccessfulIdentityOperation() { UserDTO user = new UserDTO { Email = "*****@*****.**", FName = "Josh", LName = "Collins", Password = "******", Role = "Worker", UserName = "******" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(); applicationUsers.Insert(0, new ApplicationUser()); var expected = new IdentityOperation(true, "Sign up is success", ""); mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); mock.Setup(x => x.Users.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).Returns(Task.FromResult(new IdentityResult())); mock.Setup(x => x.Users.GetUsersInRoleAsync("Admin")).Returns(Task.FromResult(applicationUsers)); mock.Setup(x => x.People.Create(It.IsAny <Person>())); var actual = await service.CreateUserAsync(user); Assert.AreSame(expected.Message, actual.Message); Assert.AreSame(expected.Property, actual.Property); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task CreateUserAsyncMethod_UserAlreadyExists_ShouldBeReturnedFailedIdentityOperation() { UserDTO user = new UserDTO { Email = "*****@*****.**", FName = "Josh", LName = "Collins", Password = "******", Role = "Worker", UserName = "******" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(1); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); var actual = await service.CreateUserAsync(user); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
private static void DIManagerDemo() { Console.WriteLine("\nUnityManager Demo\n"); try { IDIManager diManager = DIHelper.GetService <IDIManager>(); Console.WriteLine(diManager.ToString()); IIdentityUnitOfWork unitOfWork = diManager.GetService <IIdentityUnitOfWork>(); Console.WriteLine(unitOfWork.ToString()); IIdentityGenericApplication <User> application = diManager.GetService <IIdentityGenericApplication <User> >(); Console.WriteLine(application.ToString()); ZOperationResult operationResult = new ZOperationResult(); User user = application.Get(operationResult, x => x.UserName.ToLower() == "administrator"); Console.WriteLine(user.UserName); } catch (Exception exception) { WriteException(exception); } }
public async Task UpdateUserRoleAsyncMethod_ManagerWithTeamToWorker_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Worker"; IList <string> roles = new List <string>(); roles.Add("Manager"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync(new Person { TeamId = 1 }); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task CreateUserAsyncMethod_CannotCreateUser_ShouldBeReturnedFailedIdentityOperation() { UserDTO user = new UserDTO { Email = "*****@*****.**", FName = "Josh", LName = "Collins", Password = "******", Role = "Worker", UserName = "******" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(); applicationUsers.Insert(0, new ApplicationUser()); mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); mock.Setup(x => x.Users.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).Returns(Task.FromResult(new IdentityResult())); mock.Setup(x => x.Users.GetUsersInRoleAsync("Admin")).Returns(Task.FromResult(applicationUsers)); // If we already have duplicate of username in the database, for example. mock.Setup(x => x.People.Create(It.IsAny <Person>())).Throws(new ArgumentException()); var actual = await service.CreateUserAsync(user); Assert.AreEqual(false, actual.Succeeded); }
public async Task GetUserByIdAsyncMethod_UserInTeam_ShouldBeReturnedUser() { var expected = new UserDTO { Role = "Manager", TeamName = "TestTeam1" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(new List <string>() { "Manager" }); mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync(new Person() { TeamId = 1 }); mock.Setup(x => x.Teams.GetByIdAsync(It.IsAny <int>())).ReturnsAsync(new Team() { TeamName = "TestTeam1" }); var actual = await service.GetUserByIdAsync(It.IsAny <string>()); Assert.AreSame(expected.Role, actual.Role); Assert.AreSame(expected.TeamName, actual.TeamName); }
public MenuServices(IServiceBaseParameter <Menus> businessBaseParameter, IIdentityUnitOfWork <AspNetRoles> roleUnitOfWork, IIdentityUnitOfWork <MenuRoles> menuRoleUnitOfWork, IIdentityUnitOfWork <AspNetUsersRoles> userRoleUnitOfWork) : base(businessBaseParameter) { _userRoleUnitOfWork = userRoleUnitOfWork; _roleUnitOfWork = roleUnitOfWork; _menuRoleUnitOfWork = menuRoleUnitOfWork; _menuIdList = new List <string>(); _menuIdSelectedList = new List <string>(); }
public UserService(IIdentityUnitOfWork uow, IMapper mapper, IJwtFactory jwtFactory, JWTIssuerOptions jwtOptions, IHttpContextAccessor httpContextAccessor) { db = uow; this.mapper = mapper; this.jwtFactory = jwtFactory; this.jwtOptions = jwtOptions; this.httpContextAccessor = httpContextAccessor; }
private static void PersistenceIdentityData <TEntity>(IIdentityUnitOfWork unitOfWork) where TEntity : ZDataBase { IGenericRepository <TEntity> repository = unitOfWork.GetRepository <TEntity>(); TEntity entity = repository.Query().FirstOrDefault(); Console.WriteLine(typeof(TEntity).Name + ": " + repository.CountAll()); }
public RoleService(IIdentityUnitOfWork unitOfWork) { _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.CreateMap <ApplicationRole, RoleDto>(); })); }
public AccountService(IIdentityUnitOfWork uow) { Database = uow; Database.UserManager.EmailService = new EmailService(); var provider = new DpapiDataProtectionProvider("VariousTests"); Database.UserManager.UserTokenProvider = new DataProtectorTokenProvider <AppUser>(provider.Create("EmailConfirmation")); }
/// <summary> /// Create units and mappers for work. /// </summary> /// <param name="mapperDTO">Mapper for converting database entities to DTO entities</param> /// <param name="rentUnit">Rent unit of work</param> /// <param name="identityUnit">Udentity unit of work</param> /// <param name="identityMapper">Mapper for converting identity entities to BLL classes</param> /// <param name="logService">Service for logging</param> public Service(IRentMapperDTO mapperDTO, IRentUnitOfWork rentUnit, IIdentityUnitOfWork identityUnit, IIdentityMapperDTO identityMapper, ILogService logService) { RentMapperDTO = mapperDTO; RentUnitOfWork = rentUnit; IdentityMapperDTO = identityMapper; IdentityUnitOfWork = identityUnit; LogService = logService; }
/// <summary> /// Dependency Injection to database repositories. /// </summary> /// <param name="unitOfWork"> Point to context of dataBase </param> public UserService(IIdentityUnitOfWork unitOfWork) { Database = unitOfWork; // Using Factory Method. MapperCreator creator = new IdentityCreator(); IWrappedMapper wrappedMapper = creator.FactoryMethod(); mapper = wrappedMapper.CreateMapping(); }
public async Task GetUserByIdAsyncMethod_NameNotExist_ShouldBeThrownUserNotFoundException() { Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); mock.Setup(x => x.Users.FindByNameAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); await service.GetUserByIdAsync(It.IsAny <string>()); }
public TokenService(IIdentityUnitOfWork unitOfWork, IConfiguration configuration) { Database = unitOfWork; Configuration = configuration; // Using Factory Method. MapperCreator creator = new IdentityCreator(); IWrappedMapper wrappedMapper = creator.FactoryMethod(); mapper = wrappedMapper.CreateMapping(); }
public AuthorizationApp( IIdentityUnitOfWork unitOfWork, IUserRepository userRepository, IRoleRepository roleRepository, IPermissionRepository permissionRepository) { _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); _userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository)); _roleRepository = roleRepository ?? throw new ArgumentNullException(nameof(roleRepository)); _permissionRepository = permissionRepository ?? throw new ArgumentNullException(nameof(permissionRepository)); }
public UserService(IIdentityUnitOfWork identityUnitOfWork, IUnitOfWork unitOfWork) { _identityUnitOfWork = identityUnitOfWork ?? throw new ArgumentNullException(nameof(identityUnitOfWork)); _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.CreateMap <ApplicationUser, UserDto>(). ForMember(x => x.RolesId, opt => opt.MapFrom(x => x.Roles.Select(u => u.RoleId))); cfg.CreateMap <ApplicationRole, RoleDto>(). ForMember(x => x.Name, opt => opt.MapFrom(x => x.Name)); })); }
public PhotoService(IUnitOfWork unitOfWork, IIdentityUnitOfWork identityUnitOfWork) { _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); _identityUnitOfWork = identityUnitOfWork ?? throw new ArgumentNullException(nameof(identityUnitOfWork)); _mapper = new Mapper(new MapperConfiguration(cfg => { cfg.CreateMap <Photo, PhotoDto>() .ForMember(x => x.ClientProfileDtoId, opt => opt.MapFrom(x => x.ClientProfileId)) .ForMember(x => x.Data, opt => opt.MapFrom(x => Convert.ToBase64String(x.Data))); cfg.CreateMap <Like, LikeDto>() .ForMember(x => x.PhotoDtoId, opt => opt.MapFrom(x => x.PhotoId)); })); }
public LotService(IAuctionUnitOfWork database, IIdentityUnitOfWork identityDb) { if (database == null) { throw new ArgumentNullException("database"); } if (identityDb == null) { throw new ArgumentNullException("identityDb"); } IdentityDb = identityDb; Database = database; }
private static void PersistenceIdentityDemo() { Console.WriteLine("\nPersistence Identity Demo\n"); IIdentityUnitOfWork unitOfWork = DIHelper.DIManager.GetService <IIdentityUnitOfWork>(); Console.WriteLine(unitOfWork.GetType().FullName + " with " + unitOfWork.DBMS.ToString() + "\n"); PersistenceIdentityData <Role>(unitOfWork); PersistenceIdentityData <UserClaim>(unitOfWork); PersistenceIdentityData <UserLogin>(unitOfWork); PersistenceIdentityData <UserRole>(unitOfWork); PersistenceIdentityData <User>(unitOfWork); }
public async Task UpdateUserRoleAsyncMethod_UserIdNotExists_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Worker"; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task UpdateUserRoleAsyncMethod_WithoutChanges_ShouldBeReturnedSuccessfulIdentityOperation() { string userId = "1a", rolename = "Worker"; IList <string> roles = new List <string>(); roles.Add("Worker"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(true, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public MappingIdentityProfile(IIdentityUnitOfWork uow) { Config = new MapperConfiguration(cfg => { cfg.CreateMap <UserProfile, UserBLL>() .ForMember(dto => dto.Email, m => m.MapFrom(cp => cp.ApplicationUser.Email)) .ForMember(dto => dto.Password, m => m.MapFrom(cp => cp.ApplicationUser.PasswordHash)) .ForMember(dto => dto.UserName, m => m.MapFrom(cp => cp.ApplicationUser.UserName)) .ForMember(dto => dto.Birthday, m => m.MapFrom(cp => cp.Birthday.Date)) //.ForMember(dto => dto.Avatar, m => m.MapFrom(cp => uow.UserRepository //.Find(p=>p.Photos.Where(p0 => p0.IsAvatar == true) //.Select(p1=>p1.PhotoAddress) //.FirstOrDefault()))) .ForMember(dto => dto.Role, m => m.MapFrom( cp => uow.RoleManager.FindById( cp.ApplicationUser.Roles.First(p2 => p2.UserId == cp.Id).RoleId).Name)); }); }
public async Task GetUserByIdAsyncMethod_OnlyAdminExists_ShouldBeReturnedUser() { var expected = new UserDTO { Role = "Admin" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(new List <string>() { "Admin" }); mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync((Person)null); var actual = await service.GetUserByIdAsync(It.IsAny <string>()); Assert.AreSame(expected.Role, actual.Role); }
public async Task UpdateUserRoleAsyncMethod_FromAdmin_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Manager"; IList <string> roles = new List <string>(); roles.Add("Admin"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "Only the one administrator have to be in database", "userRole"); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreSame(expected.Message, actual.Message); Assert.AreSame(expected.Property, actual.Property); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public EmployeeCreatedHandler(IIdentityUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; }
public IdentityUserRepository(IIdentityUnitOfWork unitOfWork) : base(unitOfWork) { }
/// <summary>Starts unit of work.</summary> /// <param name="other"> The other. </param> /// <returns>An IIdentityUnitOfWork.</returns> public IIdentityUnitOfWork StartUnitOfWork(IIdentityUnitOfWork other) { throw new NotImplementedException(); }
public UserService(IIdentityUnitOfWork uow) { database = uow; }