public void Add_ValidCategoryDescription_Succeeds(string username, string name, string password) { //arrange var expected = new UserModel { username = username, name = name, password = password }; var builder = new UserServiceBuilder(); var mock = builder.GetDefaultCategoryRepository(); mock.Setup(r => r.Add(It.IsAny <User>())) .Returns(new User { username = username, name = name, password = password }); mock.Setup(r => r.SaveChanges()) .Returns(1); var service = builder.WithCategoryRepository(mock.Object).Build(); //act var result = service.AddUser(expected); //assert Assert.Equal(ResponseCode.Success, result.ResponseCode); Assert.Equal(username, result.Result.username); Assert.Equal(name, result.Result.name); Assert.Equal(password, result.Result.password); }
public async Task MissingMember() { var member = new Member { Id = Guid.NewGuid(), Email = "*****@*****.**", FirstName = "UpdatedFirst", LastName = "UpdatedLast" }; var acct = GetSampleAccount(); acct.MemberId = member.Id; var args = new UserServiceBuilder(); args.DB.Add(acct); args.Logger.Setup(f => f.Error(LogStrings.LinkedMemberNotFound, acct)); args.Members.Setup(f => f.GetMember(member.Id)).Returns(Task.FromResult((Member)null)); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = Password }; args.DB.SaveChangesCount = 0; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeTrue(); ctx.AuthenticateResult.ErrorMessage.Should().Be(Strings.AccountLocked); }
public async Task User_Should_Subscribe() { var unitOfWork = new UnitOfWorkBuilder().Build(); var passwordService = A.Fake <IPasswordService>(); A.CallTo(() => passwordService.GenerateRandomPassword()).Returns(("hash", "salt")); var dateTime = DateTime.UtcNow; var userService = new UserServiceBuilder().WithUnitOfWork(unitOfWork) .WithDateTime(dateTime) .WithPasswordService(passwordService) .Build(); var user = GetUser(); await userService.RegisterMembers(new List <User> { user }); var savedUser = await unitOfWork.Users.GetFirst(x => x.Email == user.Email); savedUser.Should().NotBeNull(); savedUser.FirstName.Should().Be(user.FirstName); savedUser.LastName.Should().Be(user.LastName); savedUser.Password.Should().Be("hash"); savedUser.PasswordSalt.Should().Be("salt"); savedUser.CreationDate.Should().Be(dateTime); }
public async Task UpdateFromMember() { var member = new Member { Id = Guid.NewGuid(), Email = "*****@*****.**", FirstName = "UpdatedFirst", LastName = "UpdatedLast" }; var acct = GetSampleAccount(); acct.MemberId = member.Id; var args = new UserServiceBuilder(); args.DB.Add(acct); args.Members.Setup(f => f.GetMember(member.Id)).Returns(Task.FromResult(member)); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = Password }; args.DB.SaveChangesCount = 0; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeFalse(); args.DB.SaveChangesCount.Should().Be(1); acct.Email.Should().Be(member.Email); acct.FirstName.Should().Be(member.FirstName); acct.LastName.Should().Be(member.LastName); }
public async Task ManyAccountsWithEmail() { string email = "*****@*****.**"; var args = new UserServiceBuilder(); args.DB.AddRange(new[] { new AccountRow { Email = email, FirstName = "First", LastName = "Last", Id = Guid.NewGuid() }, new AccountRow { Email = email, FirstName = "Other First", LastName = "Other Last", Id = Guid.NewGuid() } }); args.Members.Setup(f => f.FindMembersByEmail(email)).Returns(Task.FromResult <IList <Member> >(new List <Member> { new Member { Email = email, FirstName = "First", LastName = "Last", Id = Guid.NewGuid() } })); args.Logger.Setup(f => f.Warning(LogStrings.MultipleAccountsForEmail, It.IsAny <object[]>())); var svc = args.Build(); var claimsId = new ClaimsIdentity(new[] { new Claim(Constants.ClaimTypes.ExternalProviderUserId, "abcdefg", null, "facebook") }); var result = await svc.SendExternalVerificationCode(claimsId, email); result.Should().Be(ProcessVerificationResult.EmailNotAvailable); }
public void EmailNull() { var args = new UserServiceBuilder(); var svc = args.Build(); var claimsId = new ClaimsIdentity(new[] { new Claim(Constants.ClaimTypes.ExternalProviderUserId, "abcdefg", null, "facebook") }); Func <Task <ProcessVerificationResult> > act = async() => await svc.SendExternalVerificationCode(claimsId, null); act.ShouldThrow <ArgumentNullException>(); }
public async Task InvalidUser() { var args = new UserServiceBuilder(); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = "******" }; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeTrue(); ctx.AuthenticateResult.ErrorMessage.Should().Be(Strings.UserPasswordNotCorrect); }
public async Task InvalidPassword() { var args = new UserServiceBuilder(); var acct = GetSampleAccount(); args.DB.Add(acct); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = "******" }; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeTrue(); ctx.AuthenticateResult.ErrorMessage.Should().Be(Strings.UserPasswordNotCorrect); }
public async Task NoMemberNoAccountWithEmail() { string email = "*****@*****.**"; var args = new UserServiceBuilder(); args.Members.Setup(f => f.FindMembersByEmail(email)).Returns(Task.FromResult <IList <Member> >(new List <Member>())); args.Logger.Setup(f => f.Warning(LogStrings.EmailNotFound, It.IsAny <object[]>())); var svc = args.Build(); var claimsId = new ClaimsIdentity(new[] { new Claim(Constants.ClaimTypes.ExternalProviderUserId, "abcdefg", null, "facebook") }); var result = await svc.SendExternalVerificationCode(claimsId, email); result.Should().Be(ProcessVerificationResult.EmailNotAvailable); }
public async Task Locked() { var args = new UserServiceBuilder(); var acct = GetSampleAccount(); acct.Locked = DateTime.Now; acct.LockReason = "Some reason"; args.DB.Add(acct); args.Logger.Setup(f => f.Warning(LogStrings.LockedAccountAttempt, acct)); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = Password }; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeTrue(); ctx.AuthenticateResult.ErrorMessage.Should().Be(Strings.AccountLocked); }
public async Task UserService_Should_ReSend_Email_To_Invited_Users() { var unitOfWork = new UnitOfWorkBuilder().Build(); var emailService = A.Fake <IEmailService>(); var userService = new UserServiceBuilder().WithUnitOfWork(unitOfWork) .WithEmailService(emailService) .Build(); var user = GetUser(); user.InvitationSent = true; user.Password = "******"; await unitOfWork.Users.Add(user); await userService.SendAgainEmailToUsers(); A.CallTo(() => emailService.SendAccountCreationEmail(user, "pwd")).MustHaveHappened(); }
public async Task UserService_Should_Return_ByEmail() { var unitOfWork = new UnitOfWorkBuilder().Build(); var userService = new UserServiceBuilder().WithUnitOfWork(unitOfWork) .Build(); var user = GetUser(); user.Password = "******"; user.UserId = "userId"; await unitOfWork.Users.Add(user); var savedUser = await userService.GetUserFromEmail(user.Email); savedUser.Should().NotBeNull(); savedUser.Password.Should().Be("code"); savedUser.UserId.Should().Be("userId"); }
public async Task Accepting_Invitation_Should_Be_Flagged() { var unitOfWork = new UnitOfWorkBuilder().Build(); var datetime = DateTime.UtcNow; var userService = new UserServiceBuilder() .WithDateTime(datetime) .WithUnitOfWork(unitOfWork) .Build(); var user = GetUser(); user.Password = "******"; await unitOfWork.Users.Add(user); await userService.AcceptInvitation("code"); var savedUser = await unitOfWork.Users.GetFirst(x => x.Email == user.Email); savedUser.InvitationAccepted.Should().BeTrue(); savedUser.AcceptingDate.Should().Be(datetime); }
public async Task Invited_User_Should_Be_Flagged() { var unitOfWork = new UnitOfWorkBuilder().Build(); var emailService = A.Fake <IEmailService>(); var datetime = DateTime.UtcNow; var userService = new UserServiceBuilder().WithUnitOfWork(unitOfWork) .WithEmailService(emailService) .WithDateTime(datetime) .Build(); var user = GetUser(); await unitOfWork.Users.Add(user); await userService.SendEmailToUsers(); var savedUser = await unitOfWork.Users.GetFirst(x => x.Email == user.Email); savedUser.InvitationSent.Should().BeTrue(); savedUser.SendingDate.Should().Be(datetime); }
public async Task Login() { var acct = GetSampleAccount(); var args = new UserServiceBuilder(); args.DB.Add(acct); var svc = args.Build(); var ctx = new LocalAuthenticationContext { UserName = Username, Password = Password }; await svc.AuthenticateLocalAsync(ctx); ctx.AuthenticateResult.IsError.Should().BeFalse(); ctx.AuthenticateResult.IsPartialSignIn.Should().BeFalse(); var identity = ctx.AuthenticateResult.User as ClaimsPrincipal; identity.Identity.Name.Should().Be("First Last"); identity.FindFirst(Constants.ClaimTypes.Subject).Value.Should().Be(acct.Id.ToString()); }
public async Task OneMemberNoAccountSendCode() { string email = "*****@*****.**"; var args = new UserServiceBuilder(); args.Members.Setup(f => f.FindMembersByEmail(email)).Returns(Task.FromResult <IList <Member> >(new List <Member> { new Member { Email = email, FirstName = "First", LastName = "Last", Id = Guid.NewGuid() } })); args.Logger.Setup(f => f.Information(LogStrings.SendingVerifyCode, It.IsAny <object[]>())); args.EMails.Setup(f => f.SendEmail(email, Strings.VerifyMessageSubject, It.IsAny <string>(), true)).Returns(Task.FromResult(0)); var svc = args.Build(); var claimsId = new ClaimsIdentity(new[] { new Claim(Constants.ClaimTypes.ExternalProviderUserId, "abcdefg", null, "facebook") }); var result = await svc.SendExternalVerificationCode(claimsId, email); result.Should().Be(ProcessVerificationResult.Success); }
public async Task User_Should_Not_Subscribe_With_Same_Email() { var unitOfWork = new UnitOfWorkBuilder().Build(); var passwordService = A.Fake <IPasswordService>(); A.CallTo(() => passwordService.GenerateRandomPassword()).Returns(("hash", "salt")); var userService = new UserServiceBuilder().WithUnitOfWork(unitOfWork) .WithPasswordService(passwordService) .Build(); var user = new User { Email = "*****@*****.**", FirstName = "John", LastName = "Doe" }; var user2 = new User { Email = "*****@*****.**", FirstName = "Jane", LastName = "Doe" }; var users = await unitOfWork.Users.GetAll(); users.Count().Should().Be(0); await userService.RegisterMembers(new List <User> { user }); users = await unitOfWork.Users.GetAll(); users.Count().Should().Be(1); await userService.RegisterMembers(new List <User> { user2 }); users = await unitOfWork.Users.GetAll(); users.Count().Should().Be(1); }
public static void Main(string[] args) { int a = -1; IUserService service = new UserServiceBuilder().UserService; while (true) { try { Console.WriteLine("1. Add"); Console.WriteLine("2. Remove"); Console.WriteLine("3. Search"); Console.WriteLine("4. Show all"); Console.WriteLine("5. Save"); Console.WriteLine("0. Exit"); a = int.Parse(Console.ReadLine()); Console.Clear(); if (a == 1) { User u = new User(); Console.Write("Name: "); u.FirstName = Console.ReadLine(); Console.Write("Surname: "); u.LastName = Console.ReadLine(); Console.Write("Age: "); u.Age = int.Parse(Console.ReadLine()); service.Add(u); } if (a == 2) { Console.WriteLine("ID: "); int id = int.Parse(Console.ReadLine()); service.Remove(service.SearchById(id)); } if (a == 3) { Console.WriteLine("ID: "); int id = int.Parse(Console.ReadLine()); User user = service.SearchById(id); Console.WriteLine("ID: " + user.Id); Console.WriteLine("Name: " + user.FirstName); Console.WriteLine("Surname: " + user.LastName); Console.WriteLine("Age: " + user.Age); Console.WriteLine(); } if (a == 4) { foreach (var user in service.GetUsers()) { Console.WriteLine("ID: " + user.Id); Console.WriteLine("Name: " + user.FirstName); Console.WriteLine("Surname: " + user.LastName); Console.WriteLine("Age: " + user.Age); Console.WriteLine(); } } if (a == 5) { service.Save(); Console.WriteLine("Saved!"); } if (a == 0) { break; } Console.ReadKey(); Console.Clear(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); Console.Clear(); } } }