public AuthControllerTests() { _context = Helpers.GetContext("test"); var option = new JwtIssuerOptions { Issuer = "webApi", Audience = "http://localhost:5000/" }; var optionsMock = new Mock <IOptions <JwtIssuerOptions> >(); optionsMock.Setup(ap => ap.Value).Returns(option); _userManager = new Mock <FakeUserManager>(); var userRepository = new EntityUserRepository(_context, _userManager.Object); var userService = new UserService(userRepository, null); var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); var mapper = Helpers.GetMapper(); var userViewModelService = new UserApiViewModelService(userService, mapper, tenantProviderMock.Object); _authLocalizerMock = new Mock <IStringLocalizer <AuthController> >(); _controller = new AuthController(optionsMock.Object, _userManager.Object, userService, userViewModelService, _authLocalizerMock.Object); }
public PollJobServiceTests() { _context = Helpers.GetContext("test"); _tenantsDbContext = Helpers.GetTenantContext(); IPollRepository pollRepository = new EntityPollRepository(_context, _tenantsDbContext); IUserRepository userRepository = new EntityUserRepository(_context, null); IUserService userService = new UserService(userRepository, null); IAsyncRepository <Vote> voteRepository = new EfRepository <Vote>(_context); var voteService = new VoteService(voteRepository, userService); ITenantRepository tenantRepository = new EntityTenantRepository(_tenantsDbContext); IAsyncRepository <Setting> settingRepository = new EfRepository <Setting>(_context); ISettingService settingService = new SettingService(settingRepository); ITenantService tenantService = new TenantService(tenantRepository, settingService, null); var policyService = new PolicyService(new EfRepository <Policy>(_context)); var emailSenderMock = new Mock <IEmailSender>(); emailSenderMock.Setup(serv => serv.SendEmailAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); var genericServiceMock = new Mock <IGenericService>(); genericServiceMock.Setup(serv => serv.GetBaseUrl(It.IsAny <string>())) .Returns(Task.FromResult("decidehub.com")); IPollService pollService = new PollService(pollRepository, settingService, userService, tenantService, voteService, emailSenderMock.Object, genericServiceMock.Object); _pollJobService = new PollJobService(pollService, userService, voteService, settingService, pollRepository, tenantService, policyService); }
public ApiSettingControllerTests() { var context = Helpers.GetContext("test"); var currentUser = new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = true, Id = 1.ToString(), IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 30, LanguagePreference = "tr" } }; context.Users.Add(currentUser); context.Roles.Add(new ApplicationRole { Id = 1.ToString(), Name = "Admin" }); context.UserRoles.Add(new IdentityUserRole <string> { RoleId = 1.ToString(), UserId = 1.ToString() }); context.SaveChanges(); IAsyncRepository <Setting> settingRepository = new EfRepository <Setting>(context); ISettingService settingService = new SettingService(settingRepository); var mapper = Helpers.GetMapper(); var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); var userManager = new Mock <FakeUserManager>(); var userRepository = new EntityUserRepository(context, userManager.Object); var userService = new UserService(userRepository, null); var pollLocalizerMock = new Mock <IStringLocalizer <ApiSettingController> >(); const string key = "AdminUserError"; var localizedString = new LocalizedString(key, key); pollLocalizerMock.Setup(_ => _[key]).Returns(localizedString); _controller = new ApiSettingController(settingService, tenantProviderMock.Object, mapper, userService, pollLocalizerMock.Object); var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.PrimarySid, "1") })); _controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = user } }; }
public async Task Should_Check_User_Role() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); Assert.True(await userRepository.UserInRole(1.ToString(), "Admin")); Assert.False(await userRepository.UserInRole(1.ToString(), "Test")); }
public async Task Should_Get_Active_User_Count() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var count = await userRepository.GetActiveUserCount(); Assert.Equal(1, count); }
public async Task Should_Return_All_Users_Count_TenantIndependent() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var count = await userRepository.GetAllUserCount(); Assert.Equal(3, count); }
public async Task Should_Get_Tenant_Admins() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); var admin = await userRepository.ListAdmins(); Assert.Equal(new [] { "1" }, admin.Select(r => r.Id).ToArray()); }
public async Task Should_GetUser_By_Id() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); var user = await userRepository.GetUser(1.ToString()); Assert.NotNull(user); }
public async Task Should_Get_SpecifiedCount_Users() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var users = await userRepository.GetAllUsers(2); Assert.Equal(2, users.Count()); }
public async Task Should_Get_TenantUsers() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var result = await userRepository.GetUsers(); Assert.Equal(2, result.Count); }
public VoteServiceTests() { _context = Helpers.GetContext("test"); IAsyncRepository <Vote> voteRepository = new EfRepository <Vote>(_context); var userRepository = new EntityUserRepository(_context, null); var userService = new UserService(userRepository, null); _voteService = new VoteService(voteRepository, userService); }
public async Task Should_GetDifferentTenantUser_ById() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); var user = await userRepository.GetUser(3.ToString(), true); Assert.NotNull(user); Assert.NotEqual("test", user.TenantId); }
public async Task Should_GetVoters() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var voters = await userRepository.GetVoters(); Assert.Equal(1, voters.Count); Assert.True(voters.Count(x => x.UserDetail.AuthorityPercent > 0) == 1); }
public async Task Should_Get_UserRoles() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); var result = await userRepository.GetUserRoles(1.ToString()); Assert.Equal(1, result.Count); Assert.Contains(result, x => x.Id == 1.ToString() && x.Name == "Admin"); }
public async Task Should_DeleteUser() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); await userRepository.DeleteUser(2.ToString()); var user = await userRepository.GetUser(2.ToString()); Assert.Null(user); }
public async Task Should_Get_User_ByEmail() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); const string email = "*****@*****.**"; var user = await userRepository.GetUserByEmail(email, null); Assert.NotNull(user); Assert.Equal(email, user.Email); }
public ApiPollControllerTests() { _context = Helpers.GetContext("test"); _currentUser = new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = true, Id = 1.ToString(), IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 30, LanguagePreference = "tr" } }; _context.Users.Add(_currentUser); _context.SaveChanges(); var tenantsDbContext = Helpers.GetTenantContext(); IPollRepository pollRepository = new EntityPollRepository(_context, tenantsDbContext); IAsyncRepository <Setting> settingRepository = new EfRepository <Setting>(_context); ISettingService settingService = new SettingService(settingRepository); IUserRepository userRepository = new EntityUserRepository(_context, null); IUserService userService = new UserService(userRepository, null); ITenantRepository tenantRepository = new EntityTenantRepository(tenantsDbContext); IAsyncRepository <Vote> voteRepository = new EfRepository <Vote>(_context); var voteService = new VoteService(voteRepository, userService); ITenantService tenantService = new TenantService(tenantRepository, settingService, null); IPollService pollService = new PollService(pollRepository, settingService, userService, tenantService, voteService, null, null); var mapper = Helpers.GetMapper(); var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); _pollLocalizerMock = new Mock <IStringLocalizer <ApiPollController> >(); IPollApiViewModelService pollApiViewModelService = new PollApiViewModelService(tenantProviderMock.Object, pollService, mapper, voteService, userService, settingService); var genericServiceMock = new Mock <IGenericService>(); genericServiceMock.Setup(serv => serv.GetBaseUrl(null)).Returns(Task.FromResult("decidehub.com")); _controller = new ApiPollController(pollService, mapper, userService, pollApiViewModelService, _pollLocalizerMock.Object, voteService, genericServiceMock.Object, tenantProviderMock.Object); var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.PrimarySid, "1") })); _controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = user } }; }
public async Task Should_Get_User_By_IdAndTenant() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var user = await userRepository.GetUserByIdAndTenant(1.ToString(), "test"); var user2 = await userRepository.GetUserByIdAndTenant(3.ToString(), "test"); Assert.NotNull(user); Assert.Null(user2); }
public async Task Should_Set_GeneratedPassToken() { var token = Guid.NewGuid().ToString(); var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); await userRepository.SetGeneratedPassToken(1.ToString(), token, "test"); var user = context.Users.FirstOrDefault(x => x.Id == "1"); Assert.Equal(token, user?.GeneratePassToken); }
public async Task Should_Return_TenantVoters_Count() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var testCount = await userRepository.GetVoterCount("test"); var test2Count = await userRepository.GetVoterCount("test2"); Assert.Equal(1, testCount); Assert.Equal(0, test2Count); }
public async Task Should_Assign_Role_To_User() { var context = Helpers.GetContextAndUserRoleTestData(); var userRepository = new EntityUserRepository(context, null); var userRole = new IdentityUserRole <string> { RoleId = 3.ToString(), UserId = 1.ToString() }; await userRepository.AssignRoleToUser(userRole); Assert.NotNull(context.UserRoles.SingleOrDefault(x => x.RoleId == userRole.RoleId && x.UserId == userRole.UserId)); }
public async Task Should_Check_User_Can_Vote() { var poll = new PolicyChangePoll { Name = "PolicyChangePoll test", Active = true, CreateTime = new DateTime(2018, 6, 2), Deadline = DateTime.UtcNow.AddHours(VotingDuration + 24), QuestionBody = "PolicyChangePoll test" }; _context.Polls.Add(poll); _context.SaveChanges(); _context.Users.Add(new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", TenantId = "test", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = false, Id = 1.ToString(), IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 1, LanguagePreference = "tr" } }); _context.Users.Add(new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", CreatedAt = new DateTime(2018, 5, 2), SecurityStamp = new Guid().ToString(), EmailConfirmed = false, Id = 2.ToString(), IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 0, LanguagePreference = "tr" } }); _context.SaveChanges(); var userRepository = new EntityUserRepository(_context, null); var userService = new UserService(userRepository, null); var pollService = new PollService(_pollRepository, _settingService, userService, null, null, null, null); var user1Result = await pollService.UserCanVote(poll.Id, 1.ToString()); var user2Result = await pollService.UserCanVote(poll.Id, 2.ToString()); Assert.False(user1Result); Assert.True(user2Result); }
public async Task Should_Get_User_Not_Voted_Polls() { var userRepository = new EntityUserRepository(_context, null); var userService = new UserService(userRepository, null); var pollService = new PollService(_pollRepository, _settingService, userService, null, null, null, null); var poll = new PolicyChangePoll { Name = "PolicyChangePoll test", Active = true, CreateTime = new DateTime(2018, 6, 2), Deadline = DateTime.UtcNow.AddHours(VotingDuration + 24), QuestionBody = "PolicyChangePoll test" }; var poll2 = new PolicyChangePoll { Name = "test", Active = true, CreateTime = new DateTime(2018, 7, 3), Deadline = DateTime.UtcNow.AddHours(VotingDuration), QuestionBody = "test 123" }; _context.Polls.Add(poll); _context.Polls.Add(poll2); _context.SaveChanges(); _context.Users.Add(new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", TenantId = "test", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = false, Id = 1.ToString(), IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 1, LanguagePreference = "tr" } }); _context.SaveChanges(); _context.Votes.Add(new Vote { PollId = poll.Id, Value = 1, VoterId = 1.ToString() }); _context.SaveChanges(); var result = await pollService.GetUserNotVotedPolls(1.ToString()); Assert.Equal(1, result.Count); }
public async Task Should_Set_User_Language() { var context = Helpers.GetContextAndUserTestData(); var userManagerMock = new Mock <FakeUserManager>(); userManagerMock.Setup(x => x.UpdateAsync(It.IsAny <ApplicationUser>())) .ReturnsAsync(IdentityResult.Success); var userRepository = new EntityUserRepository(context, userManagerMock.Object); await userRepository.SetUserLangPreference(1.ToString(), "en"); var user = context.UserDetails.FirstOrDefault(x => x.UserId == "1"); Assert.Equal("en", user?.LanguagePreference); }
public async Task Should_Update_UserDetail() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var userDetail = context.UserDetails.FirstOrDefault(x => x.UserId == "1"); Assert.NotNull(userDetail); userDetail.AuthorityPercent = 50; userDetail.InitialAuthorityPercent = 64; await userRepository.UpdateUserDetails(userDetail); var user = context.UserDetails.FirstOrDefault(x => x.UserId == "1"); Assert.Equal(userDetail.AuthorityPercent, user?.AuthorityPercent ?? 0); Assert.Equal(userDetail.InitialAuthorityPercent, user?.InitialAuthorityPercent ?? 0); }
public async Task Should_Correctly_Update_Authority_Percents() { var context = Helpers.GetContextAndUserTestData(); var userRepository = new EntityUserRepository(context, null); var scores = new Dictionary <string, decimal> { { 1.ToString(), 80 }, { 2.ToString(), 40 } }; await userRepository.UpdateAuthorityPercents(scores, "test"); var user1 = context.UserDetails.FirstOrDefault(x => x.UserId == "1"); var user2 = context.UserDetails.FirstOrDefault(x => x.UserId == "2"); Assert.Equal(66.7M, Math.Round(user1?.AuthorityPercent ?? 0, 1)); Assert.Equal(33.3M, Math.Round(user2?.AuthorityPercent ?? 0, 1)); }
public UserApiViewModelServiceTests() { _context = Helpers.GetContext("test"); var list = new List <ApplicationUser> { new ApplicationUser { Id = 1.ToString(), Email = "*****@*****.**", FirstName = "test3", LastName = "test3", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = true, IsDeleted = false, UserDetail = new UserDetail { AuthorityPercent = 0, LanguagePreference = "tr" } } }; _userManager = new Mock <FakeUserManager>(); _userManager.Setup(x => x.Users) .Returns(list.AsQueryable()); _userManager.Setup(x => x.CreateAsync(It.IsAny <ApplicationUser>())). Callback((ApplicationUser usr) => list.Add(usr)) .ReturnsAsync(IdentityResult.Success); _userManager.Setup(x => x.UpdateAsync(It.IsAny <ApplicationUser>())) .Callback((ApplicationUser usr) => list[list.FindIndex(x => x.Id == usr.Id)] = usr) .ReturnsAsync(IdentityResult.Success); var userRepository = new EntityUserRepository(_context, _userManager.Object); var userService = new UserService(userRepository, null); var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); var mapper = Helpers.GetMapper(); _userApiViewModelService = new UserApiViewModelService(userService, mapper, tenantProviderMock.Object); }
public UserAccount User_Validate(string username, string password) { try { IUserRepository userrep = new EntityUserRepository(); User user = userrep.ValidateUser(username, password); if (user == null) { return(null); } IAccountRepository acctrep = new EntityAccountRepository(); Account acct = acctrep.GetAccount(user.AccountID); if (acct == null || !acct.IsActive) { return(null); } UserAccount useracct = new UserAccount(); useracct.UserID = user.UserID; useracct.Username = user.Username; useracct.FirstName = user.FirstName; useracct.LastName = user.LastName; useracct.EmailAddress = user.EmailAddress; useracct.IsAdmin = user.IsAdmin; useracct.UserIsActive = user.IsActive; useracct.AccountID = acct.AccountID; useracct.AccountName = acct.AccountName; useracct.AccountDescription = acct.AccountDescription; useracct.FTPServer = acct.FTPServer; useracct.FTPUsername = acct.FTPUsername; useracct.FTPPassword = acct.FTPPassword; useracct.AccountIsActive = acct.IsActive; return(useracct); } catch { return(null); } }
public PollApiViewModelServiceTests() { var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); _context = Helpers.GetContext("test"); var tenantsDbContext = Helpers.GetTenantContext(); ITenantRepository tenantRepository = new EntityTenantRepository(tenantsDbContext); IAsyncRepository <Setting> settingRepository = new EfRepository <Setting>(_context); IPollRepository pollRepository = new EntityPollRepository(_context, tenantsDbContext); ISettingService settingService = new SettingService(settingRepository); ITenantService tenantService = new TenantService(tenantRepository, settingService, null); IUserRepository userRepository = new EntityUserRepository(_context, null); IUserService userService = new UserService(userRepository, null); IAsyncRepository <Vote> voteRepository = new EfRepository <Vote>(_context); var voteService = new VoteService(voteRepository, userService); IPollService pollService = new PollService(pollRepository, settingService, userService, tenantService, voteService, null, null); var mapper = Helpers.GetMapper(); _pollApiViewModelService = new PollApiViewModelService(tenantProviderMock.Object, pollService, mapper, voteService, userService, settingService); }
public ApiAccountControllerTests() { _context = Helpers.GetContext("test"); var tenantsDbContext = Helpers.GetTenantContext(); _userManager = new Mock <FakeUserManager>(); ITenantRepository tenantRepository = new EntityTenantRepository(tenantsDbContext); IAsyncRepository <Setting> settingRepository = new EfRepository <Setting>(_context); ISettingService settingService = new SettingService(settingRepository); IAsyncRepository <ApplicationRole> roleRepository = new EfRepository <ApplicationRole>(_context); var roleService = new RoleService(roleRepository); ITenantService tenantService = new TenantService(tenantRepository, settingService, roleService); var userList = new List <ApplicationUser>(); _userManager.Setup(x => x.Users).Returns(userList.AsQueryable()); _userManager.Setup(x => x.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())) .Callback( (ApplicationUser usr, string pass) => { usr.Id = "2"; userList.Add(usr); }) .ReturnsAsync(IdentityResult.Success); var userRepository = new EntityUserRepository(_context, _userManager.Object); var userService = new UserService(userRepository, null); var emailSender = new Mock <IEmailSender>(); var configMock = new Mock <IConfiguration>(); _accountLocalizerMock = new Mock <IStringLocalizer <ApiAccountController> >(); var tenantProviderMock = new Mock <ITenantProvider>(); tenantProviderMock.Setup(serv => serv.GetTenantId()).Returns("test"); _controller = new ApiAccountController(tenantService, userService, _userManager.Object, emailSender.Object, configMock.Object, _accountLocalizerMock.Object, tenantProviderMock.Object); var currentUser = new ApplicationUser { Email = "*****@*****.**", FirstName = "test", LastName = "Test", CreatedAt = DateTime.UtcNow, SecurityStamp = new Guid().ToString(), EmailConfirmed = true, Id = "1", IsDeleted = false, UserDetail = new UserDetail { InitialAuthorityPercent = 0, AuthorityPercent = 30, LanguagePreference = "tr" } }; _context.Users.Add(currentUser); _context.SaveChanges(); tenantsDbContext.Tenants.Add(new Tenant { Id = "test", HostName = "test.decidehub.com", Lang = "tr" }); tenantsDbContext.SaveChanges(); }
public User ValidateLogin(string username, string password) { IUserRepository userrep = new EntityUserRepository(); return userrep.ValidateUser(username, password); }
public UserService(IValidationDictionary validationDictionary) { _validationDictionary = validationDictionary; _repository = new EntityUserRepository(); }
private string ValidateInput(User user, string confirmpassword, bool isEdit, bool passwordchanged) { if (user.AccountID == 0) return "Account ID is not valid."; if (String.IsNullOrEmpty(user.Username)) return "Username is required."; if (user.Username.Length < 6) return "Username must be at least 6 characters."; if (!isEdit) { IUserRepository urep = new EntityUserRepository(); User usercheck = urep.GetUserByUsername(user.Username); if (usercheck != null) return "This username already exists."; } if (String.IsNullOrEmpty(user.Password)) return "Password is required."; if (!isEdit || (isEdit && passwordchanged)) { if (user.Password != confirmpassword) return "Passwords do not match."; if (user.Password.Length < 6) return "Password must be at least 6 characters."; } if (String.IsNullOrEmpty(user.FirstName) || String.IsNullOrEmpty(user.LastName)) return "First Name and Last Name are required."; if (String.IsNullOrEmpty(user.EmailAddress)) return "Email address is required."; Regex regex = new Regex(@"^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$"); if (!regex.IsMatch(user.EmailAddress)) return "Email address is invalid."; return String.Empty; }