public static HttpServer SetupODataServer(TestDependencyResolver dependencyResolver = null)
        {
            // Controllers
            var repo = SetupTestPackageRepository();

            var configuration = new Mock <IGalleryConfigurationService>(MockBehavior.Strict);

            configuration.Setup(c => c.GetSiteRoot(It.IsAny <bool>())).Returns("https://nuget.org/");
            configuration.Setup(c => c.Features).Returns(new FeatureConfiguration {
                FriendlyLicenses = true
            });
            configuration.Setup(c => c.Current).Returns(new AppConfiguration()
            {
                IsODataFilterEnabled = false
            });

            var searchService = new Mock <ISearchService>(MockBehavior.Strict);

            searchService.Setup(s => s.Search(It.IsAny <SearchFilter>())).Returns
            <IQueryable <Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
            searchService.Setup(s => s.ContainsAllVersions).Returns(false);

            var v1Service = new TestableV1Feed(repo.Object, configuration.Object, searchService.Object);
            var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);

            if (dependencyResolver == null)
            {
                dependencyResolver = new TestDependencyResolver();
            }
            dependencyResolver.RegisterService(typeof(ODataV1FeedController), v1Service);
            dependencyResolver.RegisterService(typeof(ODataV2FeedController), v2Service);

            // Create server
            var server = SetupServer(dependencyResolver);

            // Additional routes needed in tests
            server.Configuration.Routes.Add("v1" + RouteName.DownloadPackage, new HttpRoute("api/v1/package/{id}/{version}"));
            server.Configuration.Routes.Add("v2" + RouteName.DownloadPackage, new HttpRoute("api/v2/package/{id}/{version}"));

            // Ready to go
            return(server);
        }
        public static HttpServer SetupODataServer(TestDependencyResolver dependencyResolver = null)
        {
            // Controllers
            var repo = SetupTestPackageRepository();

            var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
            configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://nuget.org/");
            configuration.Setup(c => c.Features).Returns(new FeatureConfiguration { FriendlyLicenses = true });

            var searchService = new Mock<ISearchService>(MockBehavior.Strict);
            searchService.Setup(s => s.Search(It.IsAny<SearchFilter>())).Returns
                <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
            searchService.Setup(s => s.ContainsAllVersions).Returns(false);

            var v1Service = new TestableV1Feed(repo.Object, configuration.Object, searchService.Object);
            var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);

            if (dependencyResolver == null)
            {
                dependencyResolver = new TestDependencyResolver();
            }
            dependencyResolver.RegisterService(typeof(ODataV1FeedController), v1Service);
            dependencyResolver.RegisterService(typeof(ODataV2FeedController), v2Service);

            // Create server
            var server = SetupServer(dependencyResolver);

            // Additional routes needed in tests
            server.Configuration.Routes.Add("v1" + RouteName.DownloadPackage, new HttpRoute("api/v1/package/{id}/{version}"));
            server.Configuration.Routes.Add("v2" + RouteName.DownloadPackage, new HttpRoute("api/v2/package/{id}/{version}"));

            // Ready to go
            return server;
        }
                public async Task V1FeedSearchDoesNotReturnDeletedPackages()
                {
                    // Arrange
                    var packageRegistration = new PackageRegistration { Id = "Foo" };
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
                    repo.Setup(r => r.GetAll()).Returns(new[]
                    {
                        new Package
                            {
                                PackageRegistration = packageRegistration,
                                Version = "1.0.0",
                                IsPrerelease = false,
                                Listed = true,
                                Deleted = true
                            },
                        new Package
                            {
                                PackageRegistration = packageRegistration,
                                Version = "1.1.0",
                                IsPrerelease = false,
                                Listed = true,
                                Deleted = false
                            },
                    }.AsQueryable());
                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    var searchService = new Mock<ISearchService>(MockBehavior.Strict);
                    searchService.Setup(s => s.Search(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)));
                    searchService.Setup(s => s.ContainsAllVersions).Returns(false);
                    var v1Service = new TestableV1Feed(repo.Object, configuration.Object, searchService.Object);
                    v1Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

                    // Act
                    var result = (await v1Service.Search(
                        new ODataQueryOptions<V1FeedPackage>(new ODataQueryContext(NuGetODataV1FeedConfig.GetEdmModel(), typeof(V1FeedPackage)), v1Service.Request),
                        null,
                        null))
                        .ExpectQueryResult<V1FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V1FeedPackage>>();

                    // Assert
                    Assert.Equal(1, result.Count());
                    Assert.Equal("Foo", result.First().Id);
                    Assert.Equal("1.1.0", result.First().Version);
                    Assert.Equal("https://localhost:8081/packages/Foo/1.1.0", result.First().GalleryDetailsUrl);
                }
                public async Task V1FeedFindPackagesByIdDoesNotReturnDeletedPackages()
                {
                    // Arrange
                    var packageRegistration = new PackageRegistration { Id = "Foo" };
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
                    repo.Setup(r => r.GetAll()).Returns(new[]
                    {
                        new Package
                            {
                                PackageRegistration = packageRegistration,
                                Version = "1.0.0",
                                IsPrerelease = false,
                                Listed = false,
                                Deleted = true
                            },
                        new Package
                            {
                                PackageRegistration = packageRegistration,
                                Version = "1.0.1",
                                IsPrerelease = false,
                                Listed = true,
                                Deleted = true
                            },
                    }.AsQueryable());
                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");

                    var v1Service = new TestableV1Feed(repo.Object, configuration.Object, null);
                    v1Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

                    // Act
                    var result = (await v1Service.FindPackagesById(
                        new ODataQueryOptions<V1FeedPackage>(new ODataQueryContext(NuGetODataV1FeedConfig.GetEdmModel(), typeof(V1FeedPackage)), v1Service.Request),
                        "Foo"))
                        .ExpectQueryResult<V1FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V1FeedPackage>>();

                    // Assert
                    Assert.Equal(0, result.Count());
                }
            public void AddsTrailingSlashes(string siteRoot, string expected)
            {
                // Arrange
                var config = new Mock<IGalleryConfigurationService>();
                config.Setup(s => s.GetSiteRoot(false)).Returns(siteRoot);
                var feed = new TestableV1Feed(null, config.Object, null);
                feed.Request = new HttpRequestMessage(HttpMethod.Get, siteRoot);

                // Act
                var actual = feed.GetSiteRootForTest();

                // Assert
                Assert.Equal(expected, actual);
            }