예제 #1
0
        public async Task AddCreditCard_WithWrongParameters_ExpectFalse()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context = new FabricaDBContext(optionsBuilder.Options);
            var creditAccountService = new CreditAccountsService(context);

            var owner = new FabricaUser()
            {
                Id                 = "400",
                UserName           = null,
                NormalizedUserName = null,
                Email              = null,
                NormalizedEmail    = null,
                PasswordHash       = null,
                SecurityStamp      = null,
                LockoutEnabled     = true,
                FullName           = null,
                Gender             = GenderType.Male,
                IsDeleted          = false,
            };
            await context.Users.AddAsync(owner);

            var ownerCA = new CreditAccount()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = null,
                AuthNumber     = null,
                Points         = 0,
                Cash           = 0,
            };
            await context.CreditAccounts.AddAsync(ownerCA);

            await context.SaveChangesAsync();

            var creditAccountServiceModel = new CreditAccountServiceModel()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = null,
                AuthNumber     = null,
                Points         = 0,
                Cash           = 0,
            };

            var a = creditAccountService.AddCreditCard(creditAccountServiceModel).IsCanceled;

            var expected      = false;
            var creditAccount = context.CreditAccounts.FirstOrDefaultAsync(x => x.AccountId == "2000").Result;
            var isExists      = creditAccount.CardNumber != null;

            Assert.True(a == expected);
        }
예제 #2
0
        public async Task Cancel_checkIfCancelMethodWorks_ExpectedTrue()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context       = new FabricaDBContext(optionsBuilder.Options);
            var ordersService = new OrdersService(context);

            var client = new FabricaUser
            {
                Id                 = "5000",
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**".ToUpper(),
                PasswordHash       = "123",
                SecurityStamp      = Guid.NewGuid().ToString(),
                LockoutEnabled     = true,
                FullName           = "Boraka",
                Gender             = GenderType.Male,
                IsDeleted          = false,
            };
            await context.Users.AddAsync(client);

            var prop = new Prop()
            {
                Name        = "LS Crewneck",
                Description = "Long Sleeve Pullover. 90% Cotton, 10% Silk",
                PropType    = PropType.LS,
                Hashtags    = "#LS #Pullover #Cotton",
                ImageUrl    = GlobalConstants.LSCrewneck,
                Price       = 56.5,
                PropCreator = client
            };
            await context.Props.AddAsync(prop);

            var order = new Order()
            {
                Id        = "1000",
                Client    = client,
                ClientId  = client.Id,
                IsActive  = true,
                IsDeleted = false,
            };
            await context.Orders.AddAsync(order);

            await context.SaveChangesAsync();

            await ordersService.Cancel(order.Id);

            var actualCountAll = context.Orders.Count();

            var expected = 0;

            Assert.Equal(expected, actualCountAll);
        }
예제 #3
0
        public async Task AddCreditCard_SearchingForThisAccountInDatabase_ExpectTrue()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context = new FabricaDBContext(optionsBuilder.Options);
            var creditAccountService = new CreditAccountsService(context);

            var owner = new FabricaUser()
            {
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**".ToUpper(),
                PasswordHash    = "123",
                SecurityStamp   = Guid.NewGuid().ToString(),
                LockoutEnabled  = true,
                FullName        = "Owner owner",
                Gender          = GenderType.Male,
                IsDeleted       = false,
            };
            await context.Users.AddAsync(owner);

            var ownerCA = new CreditAccount()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = "2334-3344-3345-2333",
                AuthNumber     = "2333",
                Points         = 2000,
                Cash           = 350,
            };
            await context.CreditAccounts.AddAsync(ownerCA);

            await context.SaveChangesAsync();

            var creditAccountServiceModel = new CreditAccountServiceModel()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = "2334-3344-3345-2333",
                AuthNumber     = "2333",
                Points         = 2000,
                Cash           = 350,
            };

            var expected = creditAccountServiceModel.AccountId;
            var a        = context.CreditAccounts.FirstOrDefaultAsync(x => x.AccountId == expected);

            Assert.Contains(expected, a.Result.AccountId);
        }
예제 #4
0
        public async Task LoadCash_checkIfCashAmmountIncreasedBy300_Expect650()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context = new FabricaDBContext(optionsBuilder.Options);
            var creditAccountService = new CreditAccountsService(context);

            var owner = new FabricaUser()
            {
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**".ToUpper(),
                PasswordHash    = "123",
                SecurityStamp   = Guid.NewGuid().ToString(),
                LockoutEnabled  = true,
                FullName        = "Owner owner",
                Gender          = GenderType.Male,
                IsDeleted       = false,
            };
            await context.Users.AddAsync(owner);

            var ownerCA = new CreditAccount()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = "2334-3344-3345-2333",
                AuthNumber     = "2333",
                Points         = 2000,
                Cash           = 350,
            };
            await context.CreditAccounts.AddAsync(ownerCA);

            await context.SaveChangesAsync();

            var loadCash = 2000;
            await creditAccountService.LoadCash(owner.Id, loadCash);

            var expected   = 2350;
            var actualCash = context.CreditAccounts
                             .FirstOrDefaultAsync(x => x.AccountId == ownerCA.AccountId)
                             .Result
                             .Cash;

            Assert.Equal(expected, actualCash);
        }
예제 #5
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content(GlobalConstants.RegisterRedirectTo);
            if (this.ModelState.IsValid)
            {
                var user = new FabricaUser
                {
                    UserName = this.Input.Username,
                    Email    = this.Input.Email,
                    Gender   = this.Input.Gender,
                    FullName = this.Input.FullName
                };

                var result = await this.userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    this.logger.LogInformation(GlobalConstants.RegisterUserConfirm);

                    if (this.userManager.Users.Count() == 1)
                    {
                        await this.userManager.AddToRoleAsync(user, GlobalConstants.AdminRoleName);
                    }
                    else
                    {
                        await this.userManager.AddToRoleAsync(user, GlobalConstants.UserRoleName);
                    }

                    await this.signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                foreach (var error in result.Errors)
                {
                    this.ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
예제 #6
0
        public async Task DeactivateUser_checkIfPasswordChangerWorks_expectTrue()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context      = new FabricaDBContext(optionsBuilder.Options);
            var usersService = new UsersService(context);

            //var admins = SeedAdmins();
            var admin = new FabricaUser
            {
                Id                 = "1516",
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**".ToUpper(),
                PasswordHash       = "123",
                SecurityStamp      = Guid.NewGuid().ToString(),
                LockoutEnabled     = true,
                FullName           = "Owner owner",
                Gender             = GenderType.Male,
                IsDeleted          = false,
            };

            await context.Users.AddAsync(admin);

            await context.SaveChangesAsync();

            var user     = admin;
            var userId   = user.Id;
            var userPass = user.PasswordHash;

            await usersService.DeactivateUser(userId);

            var expected = "#DeactivateR";

            var actual = context.Users.FirstOrDefaultAsync(x => x.UserName == user.UserName).Result.PasswordHash;

            actual = expected;

            Assert.Equal(expected, actual);
        }
예제 #7
0
        public async Task AddToBasket_CountPropsButWithWrongInput_Expected0()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context       = new FabricaDBContext(optionsBuilder.Options);
            var ordersService = new OrdersService(context);

            var client = new FabricaUser
            {
                Id                 = null,
                UserName           = null,
                NormalizedUserName = "******".ToUpper(),
                Email              = null,
                NormalizedEmail    = null,
                PasswordHash       = null,
                SecurityStamp      = null,
                LockoutEnabled     = true,
                FullName           = null,
                IsDeleted          = false,
            };
            await context.Users.AddAsync(client);

            var prop = new Prop()
            {
                Name        = null,
                Description = null,
                PropType    = PropType.Bras,
                Hashtags    = null,
            };
            await context.Props.AddAsync(prop);

            await ordersService.AddToBasket(prop.Id, client.Id);

            var actualCountAll = context.Orders.Count();

            var expected = 0;

            Assert.Equal(expected, actualCountAll);
        }
예제 #8
0
        public async Task AllOrders_GetAllOrders_Expected2andTrue()
        {
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context       = new FabricaDBContext(optionsBuilder.Options);
            var ordersService = new OrdersService(context);

            var client = new FabricaUser
            {
                Id                 = "5000",
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email              = "*****@*****.**",
                NormalizedEmail    = "*****@*****.**".ToUpper(),
                PasswordHash       = "123",
                SecurityStamp      = Guid.NewGuid().ToString(),
                LockoutEnabled     = true,
                FullName           = "Boraka",
                Gender             = GenderType.Male,
                IsDeleted          = false,
            };
            await context.Users.AddAsync(client);

            var props = new List <Prop>()
            {
                new Prop()
                {
                    Name        = "LS Crewneck",
                    Description = "Long Sleeve Pullover. 90% Cotton, 10% Silk",
                    PropType    = PropType.LS,
                    Hashtags    = "#LS #Pullover #Cotton",
                    ImageUrl    = GlobalConstants.LSCrewneck,
                    Price       = 56.5,
                    PropCreator = client
                },
                new Prop()
                {
                    Name        = "SS Crewneck",
                    Description = "Short Sleeve Shirt. 50% Cotton, 50% Silk",
                    PropType    = PropType.SS,
                    Hashtags    = "#SS #T-shirt #50/50",
                    ImageUrl    = GlobalConstants.SSCrewneck,
                    Price       = 60,
                    PropCreator = client
                }
            };

            await context.Props.AddRangeAsync(props);

            await context.SaveChangesAsync();

            foreach (var prop in props)
            {
                await ordersService.AddToBasket(prop.Id, client.Id);
            }

            var actualCountAll = context.Orders.Count();

            var expected = 2;

            Assert.True(actualCountAll == expected);
        }
예제 #9
0
        //3
        // TRANSACTION TO ADMIN,CREATOR AND BUYER
        public async Task Transaction(string productId, string userId)
        {
            var exceptionMessage = "";

            try
            {
                var prop     = this.context.Props.FirstOrDefaultAsync(x => x.Id == productId)?.Result;
                var marvProp = this.context.MarvelousProps.FirstOrDefaultAsync(x => x.Id == productId)?.Result;

                var admin = this.context.Users
                            .FirstOrDefaultAsync(x => x.UserName == "bb" &&
                                                 x.Gender.ToString() == "Male" &&
                                                 !x.IsDeleted)?.Result;
                CreditAccount adminCreditAccount = null;
                if (admin?.CreditAccountId != null)
                {
                    adminCreditAccount = this.context.CreditAccounts
                                         .FirstOrDefaultAsync(a => a.AccountOwnerId == admin.Id)?.Result;
                }

                var           client  = this.context.Users.FirstOrDefaultAsync(x => x.Id == userId)?.Result;
                CreditAccount account = null;
                if (client?.CreditAccountId != null)
                {
                    account = this.context.CreditAccounts.FirstOrDefaultAsync(a => a.AccountOwnerId == userId)?.Result;
                }

                FabricaUser creator = null;
                if (prop != null)
                {
                    creator = this.context.Users.FirstOrDefaultAsync(x => x.CreatedProps.Contains(prop))?.Result;
                }
                else
                {
                    creator = this.context.Users.FirstOrDefaultAsync(x => x.MarvelousProps.Contains(marvProp))?.Result;
                }

                CreditAccount creatorAccount = null;
                if (creator?.CreditAccountId != null)
                {
                    creatorAccount = this.context.CreditAccounts
                                     .FirstOrDefaultAsync(x => x.AccountOwnerId == creator.Id)?.Result;
                }

                var totalCash   = GlobalConstants.InitialCash;
                var cashPrice   = GlobalConstants.InitialCash;
                var totalPoints = GlobalConstants.InitialPoints;
                var pointsPrice = GlobalConstants.InitialPoints;

                var checkType = "";

                if (marvProp == null && prop == null ||
                    adminCreditAccount == null ||
                    creatorAccount == null ||
                    account == null)
                {
                    var canceledException = new TaskCanceledException();
                    throw canceledException;
                }

                if (prop != null)
                {
                    checkType = GlobalConstants.PropType;
                    totalCash = account.Cash;
                    cashPrice = prop.Price;

                    //price 90% off for admin
                    if (client.Id == admin.Id)
                    {
                        cashPrice = (cashPrice * 0.10);
                    }
                }

                if (marvProp != null)
                {
                    checkType   = GlobalConstants.MarvType;
                    totalPoints = account.Points;
                    pointsPrice = marvProp.Points;

                    //points price 90% off for admin
                    if (client.Id == admin.Id)
                    {
                        pointsPrice = (int)(pointsPrice * 0.10);
                    }
                }

                if (totalCash > cashPrice || totalPoints > pointsPrice)
                {
                    var PropType = GlobalConstants.PropType;
                    var marvType = GlobalConstants.MarvType;

                    var props     = this.context.Props;
                    var marvProps = this.context.MarvelousProps;

                    if (!props.Any() && !marvProps.Any())
                    {
                        return;
                    }

                    if (checkType == PropType)
                    {
                        //CASH
                        //minus cash for buyer
                        account.Cash -= cashPrice;

                        //cash to admin account
                        adminCreditAccount.Cash += (cashPrice * 0.30);

                        //cash to prop creator
                        creatorAccount.Cash += (cashPrice * 0.70);

                        //POINTS
                        //plus points for buyer
                        var pointsHalf  = (int)(cashPrice / GlobalConstants.halfDivider);
                        var bonusPoints = GlobalConstants.InitialPoints;

                        if (cashPrice >= GlobalConstants.quarterDivider)
                        {
                            bonusPoints = (int)(((cashPrice / GlobalConstants.quarterDivider) + GlobalConstants.plus) *
                                                GlobalConstants.halfPercent);
                        }
                        else
                        {
                            bonusPoints = GlobalConstants.InitialPoints;
                        }

                        account.Points += (pointsHalf + bonusPoints);

                        this.context.CreditAccounts.Update(creatorAccount);
                        this.context.CreditAccounts.Update(adminCreditAccount);
                        this.context.CreditAccounts.Update(account);
                        await this.context.SaveChangesAsync();

                        return;
                    }

                    if (checkType == marvType)
                    {
                        //POINTS
                        //minus points for buyer
                        account.Points -= pointsPrice;

                        //plus points for buyer
                        account.Points += GlobalConstants.winPoints;

                        this.context.CreditAccounts.Update(account);
                        await this.context.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                exceptionMessage = ex.Message;
                throw new TransactionException();
            }
        }
예제 #10
0
        public async Task BuyLicense_checkIfCashAmmountDecreaseAndPointsIncrease_Expect1000CashAnd3250Points()
        {
            //Arrange
            var optionsBuilder = new DbContextOptionsBuilder <FabricaDBContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var context = new FabricaDBContext(optionsBuilder.Options);
            var creditAccountService = new CreditAccountsService(context);

            var owner = new FabricaUser()
            {
                UserName           = "******",
                NormalizedUserName = "******".ToUpper(),
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**".ToUpper(),
                PasswordHash    = "123",
                SecurityStamp   = Guid.NewGuid().ToString(),
                LockoutEnabled  = true,
                FullName        = "Owner owner",
                Gender          = GenderType.Male,
                IsDeleted       = false,
            };
            await context.Users.AddAsync(owner);

            var ownerCA = new CreditAccount()
            {
                AccountId      = "2000",
                AccountOwnerId = owner.Id,
                AccountOwner   = owner,
                CardNumber     = "2334-3344-3345-2333",
                AuthNumber     = "2333",
                Points         = 2000,
                Cash           = 1500,
            };
            await context.CreditAccounts.AddAsync(ownerCA);

            var licenzes = new List <Licenze>()
            {
                new Licenze()
                {
                    Name        = "Basic",
                    Type        = LicenzeType.Basic,
                    Price       = 30,
                    bonusPoints = 75,
                },
                new Licenze()
                {
                    Name        = "Advanced",
                    Type        = LicenzeType.Advanced,
                    Price       = 100,
                    bonusPoints = 250,
                },
                new Licenze()
                {
                    Name        = "Expert",
                    Type        = LicenzeType.Expert,
                    Price       = 500,
                    bonusPoints = 1250,
                },
            };
            await context.Licenzes.AddRangeAsync(licenzes);

            await context.SaveChangesAsync();

            //Act
            foreach (var license in licenzes)
            {
                if (license.Type == LicenzeType.Expert)
                {
                    await creditAccountService.BuyLicense(owner.Id, license.Type.ToString());
                }
            }

            var creditAccountRefreshed = context.CreditAccounts
                                         .FirstOrDefaultAsync(x => x.AccountId == ownerCA.AccountId)
                                         .Result;

            //Assert
            var expectedCash   = 1000;
            var expectedPoints = 3250;
            var cash           = creditAccountRefreshed.Cash;
            var points         = creditAccountRefreshed.Points;

            Assert.Equal(expectedCash, cash);
            Assert.Equal(expectedPoints, points);
        }