protected ValidationContext CreateValidationContext(string packageResource = null) { // Add the package if (packageResource != null) { var packageId = PackageIdentity.Id.ToLowerInvariant(); var packageVersion = PackageIdentity.Version.ToNormalizedString().ToLowerInvariant(); var relativeUrl = $"/packages/{packageId}/{packageVersion}/{packageId}.{packageVersion}.nupkg"; var bytes = File.ReadAllBytes(packageResource); _mockServer.SetAction( relativeUrl, request => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) })); } var httpClient = new CollectorHttpClient(_mockServer); // Mock V2 feed response for the package's Created/LastEdited timestamps. These timestamps must match // the mocked catalog entry's timestamps. var timestamp = PackageTimestampMetadata.CreateForExistingPackage(created: PackageCreationTime, lastEdited: PackageCreationTime); var timestampMetadataResource = new Mock <IPackageTimestampMetadataResource>(); timestampMetadataResource.Setup(t => t.GetAsync(It.IsAny <ValidationContext>())) .ReturnsAsync(timestamp); return(ValidationContextStub.Create( PackageIdentity, _catalogEntries, client: httpClient, timestampMetadataResource: timestampMetadataResource.Object)); }
static TestableValidator() { var sourceRepository = new Mock <SourceRepository>(); var metadataResource = new Mock <IPackageTimestampMetadataResource>(); metadataResource.Setup(x => x.GetAsync(It.IsAny <ValidationContext>())) .ReturnsAsync(PackageTimestampMetadata.CreateForExistingPackage(DateTime.Now, DateTime.Now)); sourceRepository.Setup(x => x.GetResource <IPackageTimestampMetadataResource>()) .Returns(metadataResource.Object); var feedToSource = new Mock <IDictionary <FeedType, SourceRepository> >(); feedToSource.Setup(x => x[It.IsAny <FeedType>()]).Returns(sourceRepository.Object); _validatorConfiguration = new ValidatorConfiguration(packageBaseAddress: "a", requireRepositorySignature: true); _logger = Mock.Of <ILogger <Validator> >(); }
public void Last_PicksLatest(bool exists, DateTime?created, DateTime?lastEdited, DateTime?deleted) { // Act PackageTimestampMetadata package; if (exists) { package = PackageTimestampMetadata.CreateForExistingPackage(created.Value, lastEdited.Value); } else { package = PackageTimestampMetadata.CreateForMissingPackage(deleted); } // Assert Assert.True(created == null || package.Last >= created); Assert.True(lastEdited == null || package.Last >= lastEdited); Assert.True(deleted == null || package.Last >= deleted); }
protected ValidationContext CreateValidationContext() { var timestamp = PackageTimestampMetadata.CreateForExistingPackage(created: PackageCreationTime, lastEdited: PackageCreationTime); var timestampMetadataResource = new Mock <IPackageTimestampMetadataResource>(); timestampMetadataResource.Setup(t => t.GetAsync(It.IsAny <ValidationContext>())).ReturnsAsync(timestamp); var v2Resource = new Mock <IPackageRegistrationMetadataResource>(); v2Resource .Setup(x => x.GetIndexAsync( It.IsAny <PackageIdentity>(), It.IsAny <NuGet.Common.ILogger>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => DatabaseIndex); return(ValidationContextStub.Create( PackageIdentity, CatalogEntries, clientHandler: MockServer, timestampMetadataResource: timestampMetadataResource.Object, v2Resource: v2Resource.Object)); }