Beispiel #1
0
        public async Task Returns_OkResult_With_Public_Records(int totalItems)
        {
            // Arrange
            var mockUserManager = SharedFunctions.InitialiseMockUserManager();
            var mockObsRepo     = new Mock <IObservationQueryService>();

            mockObsRepo.SetupSequence(obs => obs.GetPagedObservationsFeedAsync(It.IsAny <Expression <Func <Observation, bool> > >(), It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(FeedTestHelpers.GetModel(totalItems));

            var controller = new ObservationFeedController(_logger.Object, mockUserManager.Object, mockObsRepo.Object);

            controller.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = SharedFunctions.GetTestClaimsPrincipal(string.Empty)
                }
            };

            // Act
            var result = await controller.GetObservationsFeedAsync(It.IsAny <int>(), ObservationFeedFilter.Public);

            // Assert
            var objectResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(StatusCodes.Status200OK, objectResult.StatusCode);
            var actual = Assert.IsType <ObservationFeedPagedDto>(objectResult.Value);

            Assert.Equal(totalItems, actual.TotalItems);
            Assert.IsType <ObservationFeedDto>(actual.Items.FirstOrDefault());
            Assert.Equal(ObservationFeedFilter.Public, actual.ReturnFilter);
        }
        public async Task Returns_OkResult_With_Public_Records_When_No_User_Or_Network_Records()
        {
            var options = this.CreateUniqueClassOptions <ApplicationDbContext>();

            using (var context = new ApplicationDbContext(options))
            {
                //You have to create the database
                context.Database.EnsureClean();
                context.Database.EnsureCreated();

                context.Users.Add(SharedFunctions.CreateUser("testUser1"));
                context.Users.Add(SharedFunctions.CreateUser("testUser2"));

                context.SaveChanges();

                context.Users.Count().ShouldEqual(2);

                // Arrange
                int pageIndex          = 1;
                var userManager        = SharedFunctions.InitialiseUserManager(context);
                var requestingUsername = "******";
                var mockObsRepo        = new Mock <IObservationQueryService>();
                mockObsRepo.SetupSequence(obs => obs.GetPagedObservationsFeedAsync(It.IsAny <Expression <Func <Observation, bool> > >(), It.IsAny <int>(), It.IsAny <int>()))
                .ReturnsAsync(FeedTestHelpers.GetModel(0))     // no User records
                .ReturnsAsync(FeedTestHelpers.GetModel(0))     // no Network records
                .ReturnsAsync(FeedTestHelpers.GetModel(1));    // one Public record

                var controller = new ObservationFeedController(_logger.Object, userManager, mockObsRepo.Object);

                controller.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = SharedFunctions.GetTestClaimsPrincipal(requestingUsername)
                    }
                };

                // Act
                var result = await controller.GetObservationsFeedAsync(pageIndex, ObservationFeedFilter.Own);

                // Assert
                var objectResult = Assert.IsType <OkObjectResult>(result);
                Assert.Equal(StatusCodes.Status200OK, objectResult.StatusCode);
                var actual = Assert.IsType <ObservationFeedPagedDto>(objectResult.Value);
                Assert.Equal(1, actual.TotalItems);
                Assert.IsType <ObservationFeedDto>(actual.Items.FirstOrDefault());
                Assert.Equal(ObservationFeedFilter.Public, actual.ReturnFilter);
            }
        }