Esempio n. 1
0
        public void TestSetUserAsMainForSecondDeveloper()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));
            AccountUnitOfWork   unit    = new AccountUnitOfWork(null, manager, this.Context);

            GetTwoUsers(unit);

            TelimenaUser jim  = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();
            TelimenaUser jack = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();

            DeveloperTeam jackDev = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jack.Id);
            DeveloperTeam jimDev  = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jim.Id);

            Assert.AreEqual(jackDev.Id, jack.GetDeveloperAccountsLedByUser().Single().Id);
            Assert.AreEqual(jimDev.Id, jim.GetDeveloperAccountsLedByUser().Single().Id);

            jimDev.SetMainUser(jack);
            unit.Complete();

            Assert.IsNotNull(jimDev.AssociatedUsers.Single(x => x.Id == jim.Id));
            Assert.IsNotNull(jimDev.AssociatedUsers.Single(x => x.Id == jack.Id));

            Assert.AreEqual(1, jim.AssociatedDeveloperAccounts.Count());
            Assert.AreEqual(jack, jimDev.MainUser);

            Assert.AreEqual(0, jim.GetDeveloperAccountsLedByUser().Count());
            Assert.AreEqual(2, jack.GetDeveloperAccountsLedByUser().Count());

            Assert.IsNotNull(jack.GetDeveloperAccountsLedByUser().Single(x => x.Id == jackDev.Id));
            Assert.IsNotNull(jack.GetDeveloperAccountsLedByUser().Single(x => x.Id == jimDev.Id));
        }
Esempio n. 2
0
        private static void GetTwoUsers(AccountUnitOfWork unit, [CallerMemberName] string caller = null)
        {
            var result = unit.RegisterUserAsync(new TelimenaUser(caller + "*****@*****.**", caller + "Jim Beam"), "P@ssword", TelimenaRoles.Developer).GetAwaiter()
                         .GetResult();

            if (!result.Item1.Succeeded)
            {
                var msg = result.Item1.Errors?.FirstOrDefault();
                if (msg != null && !msg.Contains("is already taken."))
                {
                    Assert.Fail($"Failed to register user. Error: {result.Item1.Errors?.FirstOrDefault()}");
                }
            }
            unit.Complete();

            result = unit.RegisterUserAsync(new TelimenaUser(caller + "*****@*****.**", caller + "Jack Daniels"), "P@ssword", TelimenaRoles.Developer).GetAwaiter()
                     .GetResult();
            if (!result.Item1.Succeeded)
            {
                var msg = result.Item1.Errors?.FirstOrDefault();
                if (msg != null && !msg.Contains("is already taken."))
                {
                    Assert.Fail($"Failed to register user. Error: {result.Item1.Errors?.FirstOrDefault()}");
                }
            }
            unit.Complete();
        }
Esempio n. 3
0
        public void RegisterTest()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));
            AccountUnitOfWork   unit    = new AccountUnitOfWork(null, manager, this.Context);
            AccountController   sut     = new AccountController(unit, new Mock <ILog>().Object);

            RegisterViewModel model = new RegisterViewModel
            {
                Name = "Jim Beam"
                , ConfirmPassword = "******"
                , Password        = "******"
                , Email           = "*****@*****.**"
                , Role            = TelimenaRoles.Developer
            };

            ViewResult result = sut.Register(model).GetAwaiter().GetResult() as ViewResult;

            Assert.AreEqual("WaitForActivationInfo", result.ViewName);

            TelimenaUser user = unit.UserManager.FindByNameAsync(model.Email).GetAwaiter().GetResult();

            Assert.AreEqual("Jim Beam", user.DisplayName);
            Assert.IsTrue(user.RegisteredDate.Date == DateTime.UtcNow.Date);
            //Assert.IsFalse(user.IsActivated);
            Assert.IsTrue(unit.UserManager.IsInRoleAsync(user.Id, TelimenaRoles.Developer).GetAwaiter().GetResult());
            Assert.IsTrue(unit.UserManager.IsInRoleAsync(user.Id, TelimenaRoles.Viewer).GetAwaiter().GetResult());

            DeveloperTeam developerTeam = unit.DeveloperRepository.SingleOrDefault(x => x.MainUser.Id == user.Id);

            Assert.AreEqual(user.DisplayName, developerTeam.MainUser.DisplayName);
            Assert.AreEqual(user.Email, developerTeam.MainEmail);

            Assert.AreEqual(developerTeam, user.GetDeveloperAccountsLedByUser().Single());
        }
Esempio n. 4
0
 public BankRequestResult <IEnumerable <Transfer> > GetUserStatement(string accountid)
 {
     using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
     {
         return(new BankRequestResult <IEnumerable <Transfer> >(BankRequestStatus.Done,
                                                                accountUnitOfWork.TransferRepository.All(x => x.BankAccountId == accountid).ToList()));
     }
 }
Esempio n. 5
0
 public BankRequestResult <IEnumerable <BankAccount> > AccountList()
 {
     using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
     {
         return(new BankRequestResult <IEnumerable <BankAccount> >(BankRequestStatus.Done,
                                                                   accountUnitOfWork.BankAccountRepository.All().ToList()));
     }
 }
Esempio n. 6
0
 public BankRequestResult DeleteAccount(string id)
 {
     using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
     {
         accountUnitOfWork.BankAccountRepository.Delete(accountUnitOfWork.BankAccountRepository.GetByID(id));
     }
     return(new BankRequestResult());
 }
Esempio n. 7
0
 public BankRequestResult CreateAccount(BankAccount account)
 {
     using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
     {
         accountUnitOfWork.BankAccountRepository.Add(account);
     }
     return(new BankRequestResult());
 }
Esempio n. 8
0
        public BankRequestResult Transfer(string sourceUserId, string destinationUserId, decimal amount)
        {
            bool done       = false;
            int  iterations = 1;

            while (iterations < AttemptsCount && !done)
            {
                using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
                {
                    try
                    {
                        BankAccount destAccount = accountUnitOfWork.BankAccountRepository.GetByID(destinationUserId);
                        BankAccount srcAccount  = accountUnitOfWork.BankAccountRepository.GetByID(sourceUserId);

                        if (srcAccount.AccountBalance < amount)
                        {
                            done = false;
                            return(new BankRequestResult(BankRequestStatus.Error, null, "Not enouth money"));
                        }
                        srcAccount.AccountBalance  -= amount;
                        destAccount.AccountBalance += amount;

                        accountUnitOfWork.TransferRepository.Add(new Transfer()
                        {
                            Amount        = amount,
                            BankAccountId = destAccount.Id,
                            Username      = destAccount.UserName
                        });

                        accountUnitOfWork.TransferRepository.Add(new Transfer()
                        {
                            Amount        = -amount,
                            BankAccountId = srcAccount.Id,
                            Username      = srcAccount.UserName
                        });

                        accountUnitOfWork.BankAccountRepository.Update(srcAccount);
                        accountUnitOfWork.BankAccountRepository.Update(destAccount);

                        done = true;
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        done = false;
                        Thread.Sleep(1);
                    }
                }
            }

            if (done)
            {
                return(new BankRequestResult());
            }
            else
            {
                return(new BankRequestResult(BankRequestStatus.Error, null, "Request timeout"));
            }
        }
Esempio n. 9
0
        public void TestRemoveUserFromDeveloper()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));
            AccountUnitOfWork   unit    = new AccountUnitOfWork(null, manager, this.Context);

            GetTwoUsers(unit);

            TelimenaUser jim  = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();
            TelimenaUser jack = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();

            DeveloperTeam jackDev = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jack.Id);
        }
Esempio n. 10
0
        private static void GetTwoUsers(AccountUnitOfWork unit, [CallerMemberName] string caller = null)
        {
            unit.RegisterUserAsync(new TelimenaUser(caller + "*****@*****.**", caller + "_Jim Beam"), "P@ssword", TelimenaRoles.Developer).GetAwaiter()
            .GetResult();

            unit.Complete();

            unit.RegisterUserAsync(new TelimenaUser(caller + "*****@*****.**", caller + "_Jack Daniels"), "P@ssword", TelimenaRoles.Developer).GetAwaiter()
            .GetResult();

            unit.Complete();
        }
Esempio n. 11
0
        public static async Task <TelimenaUser> CreateTelimenaUser(TelimenaPortalContext context, string email, string displayName = null, [CallerMemberName] string caller = "")
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(context));

            AccountUnitOfWork unit = new AccountUnitOfWork(null, manager, context);


            TelimenaUser user = new TelimenaUser(caller + "_" + email, caller + "_" + (displayName ?? email.ToUpper()));
            await unit.RegisterUserAsync(user, "P@ssword", TelimenaRoles.Developer).ConfigureAwait(false);

            unit.Complete();
            return(user);
        }
Esempio n. 12
0
        public UserValidator(AccountUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;

            RuleFor(u => u.Email).EmailAddress();
            RuleFor(u => u.Login).NotEmpty().Length(2, 40);
            RuleFor(u => u.Login)
            .MustAsync(ValidateUserLogin)
            .When(u => u.Id == 0)
            .WithMessage(user => $"Login '{user.Login}' already exists");
            RuleFor(u => u.IsActive)
            .Must(isActive => !isActive)
            .When(u => u.DeletedAt.HasValue)
            .WithMessage("Can't remove active element");
        }
Esempio n. 13
0
        public BankRequestResult Withdraw(string userid, decimal amount)
        {
            bool done       = false;
            int  iterations = 1;

            while (iterations < AttemptsCount && !done)
            {
                iterations++;
                try
                {
                    using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
                    {
                        BankAccount account = accountUnitOfWork.BankAccountRepository.GetByID(userid);

                        if (account.AccountBalance < amount)
                        {
                            done = false;
                            return(new BankRequestResult(BankRequestStatus.Error, null, "Not enough money"));
                        }

                        account.AccountBalance -= amount;

                        accountUnitOfWork.TransferRepository.Add(new Transfer()
                        {
                            Amount        = -amount,
                            BankAccountId = account.Id,
                            Username      = account.UserName
                        });

                        accountUnitOfWork.BankAccountRepository.Update(account);
                        done = true;
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    done = false;
                    Thread.Sleep(1);
                }
            }
            if (done)
            {
                return(new BankRequestResult());
            }
            else
            {
                return(new BankRequestResult(BankRequestStatus.Error, null, "Request timeout"));
            }
        }
Esempio n. 14
0
        public void TestRemoveDeveloper()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));

            AccountUnitOfWork unit = new AccountUnitOfWork(null, manager, this.Context);

            GetTwoUsers(unit);
            TelimenaUser jack = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();
            TelimenaUser jim  = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();

            DeveloperTeam jackDev = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jack.Id);

            jackDev.AssociateUser(jim);

            unit.DeveloperRepository.Remove(jackDev);
            unit.Complete();
            Assert.IsNotNull(this.Context.Users.Single(x => x.Id == jack.Id));
            Assert.IsNotNull(this.Context.Users.Single(x => x.Id == jim.Id));
            Assert.IsNull(unit.DeveloperRepository.SingleOrDefault(x => x.Id == jackDev.Id));
        }
Esempio n. 15
0
        public void TestAddSecondUserToDeveloperTwice()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));
            AccountUnitOfWork   unit    = new AccountUnitOfWork(null, manager, this.Context);

            GetTwoUsers(unit);

            TelimenaUser jim  = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();
            TelimenaUser jack = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();


            DeveloperTeam jackDev = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jack.Id);

            jackDev.AssociateUser(jim);
            unit.Complete();
            jackDev.AssociateUser(jim);
            unit.Complete();

            Assert.AreEqual(2, jackDev.AssociatedUsers.Count);
        }
Esempio n. 16
0
        public static async Task <TelimenaUser> CreateTelimenaUser(TelimenaPortalContext context, string email, string displayName = null, [CallerMemberName] string caller = "")
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(context));

            AccountUnitOfWork unit = new AccountUnitOfWork(null, manager, context);


            TelimenaUser user   = new TelimenaUser(caller + "" + email, caller + "" + (displayName ?? email.ToUpper()));
            var          result = await unit.RegisterUserAsync(user, "P@ssword", TelimenaRoles.Developer).ConfigureAwait(false);

            if (!result.Item1.Succeeded)
            {
                var msg = result.Item1.Errors?.FirstOrDefault();
                if (msg != null && !msg.Contains("is already taken."))
                {
                    Assert.Fail($"Failed to register user {user.UserName}. Error: {result.Item1.Errors?.FirstOrDefault()}");
                }
            }
            unit.Complete();
            return(user);
        }
Esempio n. 17
0
        public void TestRemoveUser()
        {
            TelimenaUserManager manager = new TelimenaUserManager(new UserStore <TelimenaUser>(this.Context));
            AccountUnitOfWork   unit    = new AccountUnitOfWork(null, manager, this.Context);

            GetTwoUsers(unit);
            TelimenaUser jack = unit.UserManager.FindByNameAsync(Helpers.GetName("*****@*****.**")).GetAwaiter().GetResult();

            DeveloperTeam jackDev   = unit.DeveloperRepository.FirstOrDefault(x => x.MainUser.Id == jack.Id);
            var           jackDevId = jackDev.Id;

            Assert.AreEqual(jackDevId, jack.AssociatedDeveloperAccounts.Single().Id);

            unit.UserManager.DeleteAsync(jack).GetAwaiter().GetResult();
            unit.Complete();
            Assert.IsNull(unit.UserManager.FindByNameAsync(jack.UserName).GetAwaiter().GetResult());

            jackDev = unit.DeveloperRepository.FirstOrDefault(x => x.Id == jackDevId);

            Assert.IsNull(jackDev.MainUser);
            Assert.AreEqual(Helpers.GetName("*****@*****.**"), jackDev.MainEmail);
        }
        //private readonly ICreateItemStrategy<User, IUser> _createItemStrategy;

        public CreateUserDatabaseHandler(AccountUnitOfWork unitOfWork, IMediator mediator, IReadOnlyCollection <IValidator <IUser> > validators) //, ICreateItemStrategyFactory createItemStrategyFactory)
            : base(unitOfWork.Users, validators)
        {
            _mediator = mediator;
            //_createItemStrategy = createItemStrategyFactory.CreateDatabaseItemStrategy<User, IUser>();
        }
Esempio n. 19
0
 public BankRequestResult <Decimal> GetBalance(string userid)
 {
     using (AccountUnitOfWork accountUnitOfWork = this._factory.CreateAccountUnitOfWork())
         return(new BankRequestResult <Decimal>(BankRequestStatus.Done, accountUnitOfWork.BankAccountRepository.GetByID(userid).AccountBalance));
 }
 public QueryUsersDatabaseHandler(AccountUnitOfWork unitOfWork)
     : base(unitOfWork.Users)
 {
 }
Esempio n. 21
0
 public GetUserDatabaseHandler(AccountUnitOfWork unitOfWork)
 {
     _unitOfWork = unitOfWork;
 }
Esempio n. 22
0
 public EditUserDatabaseHandler(AccountUnitOfWork unitOfWork, IReadOnlyCollection <IValidator <IUser> > validators)
     : base(unitOfWork.Users, validators)
 {
 }
 public DeleteUserDatabaseHandler(AccountUnitOfWork unitOfWork, IMediator mediator, IReadOnlyCollection <IValidator <IUser> > validators)
     : base(unitOfWork.Users, validators)
 {
     _unitOfWork = unitOfWork;
     _mediator   = mediator;
 }