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 void V2FeedGetUpdatesReturnsVersionsNewerThanListedVersion()
                {
                    // Arrange
                    var packageRegistrationA = new PackageRegistration { Id = "Foo" };
                    var packageRegistrationB = new PackageRegistration { Id = "Qux" };
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
                    repo.Setup(r => r.GetAll()).Returns(
                        new[]
                    {
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
                    }.AsQueryable());
                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });
                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

                    // Act
                    var result = v2Service.GetUpdates(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        "Foo|Qux",
                        "1.0.0|0.9",
                        includePrerelease: false,
                        includeAllVersions: true,
                        targetFrameworks: null,
                        versionConstraints: null)
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.Equal(3, result.Length);
                    AssertPackage(new { Id = "Foo", Version = "1.1.0" }, result[0]);
                    AssertPackage(new { Id = "Foo", Version = "1.2.0" }, result[1]);
                    AssertPackage(new { Id = "Qux", Version = "2.0" }, result[2]);
                }
        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 V2FeedSearchCountFiltersPackagesBySearchTermAndPrereleaseFlag(string searchTerm, bool includePrerelease, int expectedNumberOfPackages)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Search()/$count?searchTerm='" + searchTerm + "'&targetFramework=''&includePrerelease=false");

                    // Act
                    var result = (await v2Service.SearchCount(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        searchTerm: searchTerm,
                        targetFramework: null,
                        includePrerelease: includePrerelease))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectResult<PlainTextResult>();

                    // Assert
                    Assert.Equal(expectedNumberOfPackages.ToString(), result.Content);
                }
                public void V2FeedGetUpdatesReturnsEmptyResultsIfInputIsMalformed(string id, string version)
                {
                    // Arrange
                    var repo = Mock.Of<IEntityRepository<Package>>();
                    var configuration = Mock.Of<IGalleryConfigurationService>();
                    var v2Service = new TestableV2Feed(repo, configuration, null);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

                    // Act
                    var result = v2Service.GetUpdates(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        id,
                        version,
                        includePrerelease: false,
                        includeAllVersions: true,
                        targetFrameworks: null,
                        versionConstraints: null)
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>();

                    // Assert
                    Assert.Empty(result);
                }
                public async Task V2FeedFindPackagesByIdDoesNotReturnDeletedPackages()
                {
                    // 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/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });

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

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

                    // Assert
                    Assert.Equal(0, result.Count());
                }
                public async Task V2FeedSearchFiltersPackagesBySearchTermAndPrereleaseFlag(string searchTerm, bool includePrerelease, int expectedNumberOfPackages, string[] expectedIds, string[] expectedVersions)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Search()?searchTerm='" + searchTerm + "'&targetFramework=''&includePrerelease=false");

                    // Act
                    var result = (await v2Service.Search(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        searchTerm: searchTerm,
                        targetFramework: null,
                        includePrerelease: includePrerelease))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.Equal(expectedNumberOfPackages, result.Length);
                    for (int i = 0; i < expectedIds.Length; i++)
                    {
                        var expectedId = expectedIds[i];
                        var expectedVersion = expectedVersions[i];

                        Assert.True(result.Any(p => p.Id == expectedId && p.Version == expectedVersion), string.Format("Search results did not contain {0} {1}", expectedId, expectedVersion));
                    }
                }
                public async Task V2FeedFindPackagesByIdReturnsEmptyCollectionWhenNoPackages()
                {
                    // Arrange
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
                    repo.Setup(r => r.GetAll()).Returns(() => Enumerable.Empty<Package>().AsQueryable());

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

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

                    // Assert
                    Assert.Equal(0, result.Count());
                }
                public async Task V2FeedFindPackagesByIdDoesNotHitBackendWhenIdIsEmpty()
                {
                    // Arrange
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Loose);

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

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

                    // Act
                    var result = (await v2Service.FindPackagesById(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        ""))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>();

                    // Assert
                    repo.Verify(r => r.GetAll(), Times.Never);
                    Assert.Equal(0, result.Count());
                }
                public async Task V2FeedPackagesCollectionDoesNotContainDeletedPackages(string filter)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages?$filter=" + filter);

                    // Act
                    var result = (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.False(result.Any(p => p.Id == "Baz"));
                }
                public async Task V2FeedFindPackagesByIdReturnsUnlistedAndPrereleasePackages()
                {
                    // 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,
                                FlattenedAuthors = string.Empty,
                                Description = string.Empty,
                                Summary = string.Empty,
                                Tags = string.Empty
                            },
                        new Package
                            {
                                PackageRegistration = packageRegistration,
                                Version = "1.0.1-a",
                                IsPrerelease = true,
                                Listed = true,
                                FlattenedAuthors = string.Empty,
                                Description = string.Empty,
                                Summary = string.Empty,
                                Tags = string.Empty
                            },
                    }.AsQueryable());
                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

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

                    // Assert
                    Assert.Equal(2, result.Count());
                    Assert.Equal("Foo", result.First().Id);
                    Assert.Equal("1.0.0", result.First().Version);

                    Assert.Equal("Foo", result.Last().Id);
                    Assert.Equal("1.0.1-a", result.Last().Version);
                }
                public async Task V2FeedPackagesByIdAndVersionReturnsNotFoundWhenPackageNotFound(string expectedId, string expectedVersion)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<IGalleryConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages(Id='" + expectedId + "', Version='" + expectedVersion + "')");

                    // Act
                    (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request), expectedId, expectedVersion))
                        .ExpectResult<NotFoundResult>();
                }
                public async Task V2FeedPackagesDoesNotUseSearchHijackForFunkyQueries(string filter)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

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

                    bool called = false;
                    var searchService = new Mock<ExternalSearchService>(MockBehavior.Loose);
                    searchService.CallBase = true;
                    searchService.Setup(s => s.RawSearch(It.IsAny<SearchFilter>())).Returns
                        <IQueryable<Package>, string>((_, __) => Task.FromResult(new SearchResults(_.Count(), DateTime.UtcNow, _)))
                            .Callback(() => called = true);

                    string rawUrl = "https://localhost:8081/api/v2/Packages?$filter=" + filter + "&$top=10";

                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, rawUrl);
                    v2Service.RawUrl = rawUrl;

                    // Act
                    var result = (await v2Service.Get(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>()
                        .ToArray();

                    // Assert
                    Assert.NotNull(result);
                    Assert.False(called); // Hijack was performed and it should not have been.
                    searchService.Verify();
                }
            public void UsesCurrentRequestToDetermineSiteRoot()
            {
                // Arrange
                var config = new Mock<IGalleryConfigurationService>();
                config.Setup(s => s.GetSiteRoot(true)).Returns("https://nuget.org").Verifiable();
                var feed = new TestableV2Feed(null, config.Object, null);
                feed.Request = new HttpRequestMessage(HttpMethod.Get, "https://nuget.org");

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

                // Assert
                Assert.Equal("https://nuget.org/", actual);
                config.Verify();
            }
Пример #15
0
                public async Task V2FeedPackagesCountReturnsCorrectCount(string filter, int top, int expectedNumberOfPackages)
                {
                    // Arrange
                    var repo = FeedServiceHelpers.SetupTestPackageRepository();

                    var configuration = new Mock<ConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(It.IsAny<bool>())).Returns("https://localhost:8081/");
                    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 v2Service = new TestableV2Feed(repo.Object, configuration.Object, searchService.Object);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/api/v2/Packages/$count?$filter=" + filter + "&$top=" + top);

                    // Act
                    var result = (await v2Service.GetCount(new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request)))
                        .ExpectQueryResult<V2FeedPackage>()
                        .GetInnerResult()
                        .ExpectResult<PlainTextResult>();

                    // Assert
                    Assert.Equal(expectedNumberOfPackages.ToString(), result.Content);
                }
Пример #16
0
                public void V2FeedGetUpdatesReturnsEmptyIfVersionConstraintsContainWrongNumberOfElements(string constraintString)
                {
                    // Arrange
                    var packageRegistrationA = new PackageRegistration { Id = "Foo" };
                    var packageRegistrationB = new PackageRegistration { Id = "Qux" };
                    var repo = new Mock<IEntityRepository<Package>>(MockBehavior.Strict);
                    repo.Setup(r => r.GetAll()).Returns(
                        new[]
                    {
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.0.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.1.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0-alpha", IsPrerelease = true, Listed = true },
                        new Package { PackageRegistration = packageRegistrationA, Version = "1.2.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationB, Version = "2.0", IsPrerelease = false, Listed = true },
                        new Package { PackageRegistration = packageRegistrationB, Version = "3.0", IsPrerelease = false, Listed = true },
                    }.AsQueryable());
                    var configuration = new Mock<ConfigurationService>(MockBehavior.Strict);
                    configuration.Setup(c => c.GetSiteRoot(false)).Returns("https://localhost:8081/");
                    configuration.Setup(c => c.Features).Returns(new FeatureConfiguration() { FriendlyLicenses = true });
                    var v2Service = new TestableV2Feed(repo.Object, configuration.Object, null);
                    v2Service.Request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:8081/");

                    // Act
                    var result = v2Service.GetUpdates(
                        new ODataQueryOptions<V2FeedPackage>(new ODataQueryContext(NuGetODataV2FeedConfig.GetEdmModel(), typeof(V2FeedPackage)), v2Service.Request),
                        "Foo|Qux",
                        "1.0.0|0.9",
                        includePrerelease: false,
                        includeAllVersions: true,
                        targetFrameworks: null,
                        versionConstraints: constraintString)
                        .ExpectOkNegotiatedContentResult<IQueryable<V2FeedPackage>>();

                    // Assert
                    Assert.Equal(0, result.Count());
                }