コード例 #1
0
        public async void Get_WhenCalled_ReturnsOkResult()
        {
            // Act
            ActionResult <IEnumerable <Booking> > okResult = await _controller.GetBookings();

            // Assert
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
コード例 #2
0
        public async void GetBookingsByUser_ValidDetails_NoContent()
        {
            try
            {
                //Arrange
                _httpContextAccessor.Setup(x => x.HttpContext.User.FindFirst(It.IsAny <string>()))
                .Returns(new Claim(ClaimTypes.NameIdentifier, "5"));

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.GetBookings();

                //Assert
                var notFoundResult = result as NotFoundResult;
                if (notFoundResult != null)
                {
                    Assert.NotNull(notFoundResult);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
コード例 #3
0
        public async void GetBookingsByBookingId_ValidDetails_NoContent()
        {
            try
            {
                //Arrange
                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.GetBookings(2);

                //Assert
                var notFoundResult = result as NotFoundResult;
                if (notFoundResult != null)
                {
                    Assert.NotNull(notFoundResult);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
コード例 #4
0
        public void Should_GetBookings_AsAdmin()
        {
            // mock UoW and repository data
            Mock <IUnitOfWork> mockUOW = new Mock <IUnitOfWork>();

            mockUOW.Setup(f => f.Bookings.Get()).Returns(new List <Booking>
            {
                new Booking {
                    Id = 1, CustomerId = 1, ShowingId = 1, Status = BookingStatus.PaymentPending, BookedDate = DateTime.Parse("01-01-2020 12:00"), BookingItems = new List <BookingItem>()
                },
                new Booking {
                    Id = 2, CustomerId = 1, ShowingId = 2, Status = BookingStatus.PaymentPending, BookedDate = DateTime.Parse("01-01-2020 12:01"), BookingItems = new List <BookingItem>()
                },
                new Booking {
                    Id = 3, CustomerId = 2, ShowingId = 3, Status = BookingStatus.PaymentPending, BookedDate = DateTime.Parse("01-01-2020 12:02"), BookingItems = new List <BookingItem>()
                },
                new Booking {
                    Id = 4, CustomerId = 3, ShowingId = 4, Status = BookingStatus.PaymentPending, BookedDate = DateTime.Parse("01-01-2020 12:03"), BookingItems = new List <BookingItem>()
                },
                new Booking {
                    Id = 5, CustomerId = 4, ShowingId = 5, Status = BookingStatus.PaymentComplete, BookedDate = DateTime.Parse("01-01-2020 12:04"), BookingItems = new List <BookingItem>()
                }
            }
                                                         .AsQueryable());
            mockUOW.Setup(f => f.Customers.Get()).Returns(new List <Customer>
            {
                new Customer {
                    Id = 1, UserId = "test"
                }
            }.AsQueryable());

            // mock UserManager
            var userStore = new Mock <IUserStore <User> >();
            Mock <UserManager <User> > mockUserManager = new Mock <UserManager <User> >(userStore.Object, null, null, null, null, null, null, null, null);

            mockUserManager.Setup(f => f.IsInRoleAsync(It.IsAny <User>(), "admin")).Returns(Task.FromResult(true));
            mockUserManager.Setup(f => f.FindByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(new User {
                Id = "test"
            }));

            BookingsController controller = new BookingsController(mockUOW.Object, mockUserManager.Object, null);
            var userClaimsIdentity        = new ClaimsPrincipal();

            userClaimsIdentity.AddIdentity(new ClaimsIdentity(new[] { new Claim("sub", "*****@*****.**") }));
            controller.ControllerContext.HttpContext = new DefaultHttpContext()
            {
                User = userClaimsIdentity
            };
            var bookings = controller.GetBookings();

            Assert.True(bookings.Count() == 5);
        }
コード例 #5
0
        public async void GetBookingsByUser_WhenCalled_ShowBookedMovies()
        {
            try
            {
                //Arrange

                _httpContextAccessor.Setup(x => x.HttpContext.User.FindFirst(It.IsAny <string>()))
                .Returns(new Claim(ClaimTypes.NameIdentifier, "1"));


                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.GetBookings();

                //Assert
                var okResult = result as OkObjectResult;
                if (okResult != null)
                {
                    Assert.NotNull(okResult);
                }

                var model = okResult.Value as IEnumerable <UserBookingReturnDto>;
                if (model.Count() > 0)
                {
                    Assert.NotNull(model);

                    var expected = 1;
                    var actual   = model?.FirstOrDefault().UserId;

                    Assert.Equal(expected: expected, actual: actual);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }
コード例 #6
0
        //[ExpectedException(typeof(Exception))]
        public void TestGetBookings()
        {
            var allBookings = new[]
            {
                new Booking()
                {
                    BookingId  = 1,
                    User       = "******",
                    Service    = "Service1",
                    BookedFrom = DateTime.Now,
                    BookedTo   = DateTime.Now.AddDays(1),
                    Deleted    = false
                },
                new Booking()
                {
                    BookingId  = 2,
                    User       = "******",
                    Service    = "Service2",
                    BookedFrom = DateTime.Now,
                    BookedTo   = DateTime.Now.AddDays(1),
                    Deleted    = false
                },
            };

            var mockBookingValidator = new Mock <IBookingValidator>();

            mockBookingValidator.Setup(x => x.GetBookings()).Returns(allBookings);

            var controller = new BookingsController(mockBookingValidator.Object);

            // Arrange
            var controllerContext = new HttpControllerContext();
            var request           = new HttpRequestMessage();

            request.Headers.Add("TODO_PAGOS_TOKEN", "1");

            // Don't forget these lines, if you do then the request will be null.
            controllerContext.Request    = request;
            controller.ControllerContext = controllerContext;

            IHttpActionResult actionResult = controller.GetBookings();
            //throw new Exception(actionResult.GetType().ToString());
            var contentResult = actionResult as OkNegotiatedContentResult <IEnumerable <Booking> >;

            Assert.IsNotNull(contentResult);
        }
コード例 #7
0
        public async void GetBookingsByBookingId_ValidDetails_ShowBookedMovie()
        {
            try
            {
                //Arrange

                //auto mapper configuration
                var mockMapper = new MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new AutoMapperProfiles());
                });
                var mapper = mockMapper.CreateMapper();

                BookingsController controller = new BookingsController(repo: mockRepo.Object, mapper: mapper);

                //Act
                var result = await controller.GetBookings(1);

                //Assert
                var okResult = result as OkObjectResult;
                if (okResult != null)
                {
                    Assert.NotNull(okResult);
                }

                var model = okResult.Value as IEnumerable <UserBookingReturnDto>;
                if (model.Count() > 0)
                {
                    Assert.NotNull(model);

                    var expected = 1;
                    var actual   = model?.FirstOrDefault().Id;

                    Assert.Equal(expected: expected, actual: actual);
                }
            }
            catch (Exception ex)
            {
                //Assert
                Assert.False(false, ex.Message);
            }
        }