Beispiel #1
0
        public void UserVoucherShould_MakeTheVoucherUsed()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_UseVoucher")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            dbContext.Users.Add(user);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)).
            Returns(user.Id);

            var random   = new Random();
            var discount = random.Next(1, 5);

            var voucher = new Voucher
            {
                ApplicationUserId = user.Id,
                Discount          = discount,
                VoucherCode       = Guid.NewGuid().ToString(),
                Status            = VoucherStatus.Active
            };

            dbContext.Vouchers.Add(voucher);
            dbContext.SaveChanges();

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            vouchersService.UseVoucher(voucher.VoucherCode);

            var expected = VoucherStatus.Used;
            var result   = dbContext.Vouchers.FirstOrDefault(x => x.Id == voucher.Id).Status;

            Assert.Equal(expected, result);
        }
Beispiel #2
0
        public void GetAllShould_ReturAllVouchers()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_AllVouchers")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user1 = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            var user2 = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin2",
                LastName  = "LastAdmin2"
            };

            dbContext.Users.Add(user1);
            dbContext.Users.Add(user2);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(It.IsAny <string>())).
            Returns((string res) => res);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            vouchersService.CreateForUser(user1.Email).GetAwaiter().GetResult();
            vouchersService.CreateForUser(user2.Email).GetAwaiter().GetResult();
            vouchersService.CreateForUser(user1.Email).GetAwaiter().GetResult();

            var expected = new List <string> {
                user1.Email, user2.Email, user1.Email
            };
            var result = dbContext.Vouchers.Select(x => x.ApplicationUserId);

            Assert.Equal(expected, result);
        }
Beispiel #3
0
        public void GetDiscountForCodeShould_ReturnRightDiscountPercent()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_DiscountValid")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            dbContext.Users.Add(user);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(It.IsAny <string>())).
            Returns((string res) => res);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            var random   = new Random();
            var discount = random.Next(1, 5);

            var voucher = new Voucher
            {
                ApplicationUserId = user.Id,
                Discount          = discount,
                VoucherCode       = Guid.NewGuid().ToString(),
                Status            = VoucherStatus.Active
            };

            dbContext.Vouchers.Add(voucher);
            dbContext.SaveChanges();

            var expected = discount;
            var result   = vouchersService.GetDiscountForCode(voucher.VoucherCode).GetAwaiter().GetResult();

            Assert.Equal(expected, result);
        }
Beispiel #4
0
        public void GetAllActiveForUserShould_ReturnOnlyActiveVouchersForUser()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_UserActiveVouchers")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            dbContext.Users.Add(user);

            var usersServiceMock = new Mock <IUsersService>();

            usersServiceMock.Setup(p => p.GetUserIdByEmail(user.Email)).
            Returns(user.Id);

            var vouchersService = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            for (int i = 0; i < 5; i++)
            {
                vouchersService.CreateForUser(user.Email);
            }

            var allVoucherId = dbContext.Vouchers.Where(x => x.ApplicationUserId == user.Id).ToList();

            allVoucherId[0].Status = VoucherStatus.Used;

            var expected = allVoucherId.Skip(1).Select(x => x.Id);
            var result   = vouchersService.GetAllActiveForUser(user.Email).Select(x => x.Id);

            Assert.Equal(expected, result);
        }
Beispiel #5
0
        public void GetAllVouchersShould_ReturnAllVouchers()
        {
            var options = new DbContextOptionsBuilder <CarRentalDbContext>()
                          .UseInMemoryDatabase(databaseName: "CarRental_Database_GetAllVouchers")
                          .Options;
            var dbContext = new CarRentalDbContext(options);

            var user = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin",
                LastName  = "LastAdmin"
            };

            var user2 = new ApplicationUser()
            {
                Id        = Guid.NewGuid().ToString(),
                Email     = "*****@*****.**",
                FirstName = "Admin2",
                LastName  = "LastAdmin2"
            };

            dbContext.Users.Add(user);
            dbContext.Users.Add(user2);

            var usersServiceMock = new Mock <IUsersService>();
            var vouchersService  = new VouchersService(dbContext, usersServiceMock.Object, this.mapper);

            vouchersService.CreateForUser(user.Email);
            vouchersService.CreateForUser(user2.Email);

            var expected = dbContext.Vouchers.Count();
            var result   = vouchersService.GetAllVouchers().Count;

            Assert.Equal(expected, result);
        }