|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System;
|
|
|
|
using Xunit;
|
|
|
|
using isnd.Data;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using isnd.tests;
|
|
|
|
using NuGet.Protocol;
|
|
|
|
using NuGet.Configuration;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NuGet.Protocol.Core.Types;
|
|
|
|
using isn.abst;
|
|
|
|
using NuGet.Common;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace isnd.host.tests
|
|
|
|
{
|
|
|
|
[Collection("Web server collection")]
|
|
|
|
public class UnitTestWebHost : IClassFixture<WebServerFixture>
|
|
|
|
{
|
|
|
|
const string apiindex = Constants.ApiVersionPrefix + "/index";
|
|
|
|
WebServerFixture server;
|
|
|
|
public UnitTestWebHost(WebServerFixture server)
|
|
|
|
{
|
|
|
|
this.server = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestHaveTestDbContextAndMigrate()
|
|
|
|
{
|
|
|
|
using (var serviceScope = server.Host.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var services = serviceScope.ServiceProvider;
|
|
|
|
var myDependency = services.GetRequiredService<ApplicationDbContext>();
|
|
|
|
myDependency.Database.Migrate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
void TestDropUser()
|
|
|
|
{
|
|
|
|
|
|
|
|
using (var serviceScope = server.Host.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var services = serviceScope.ServiceProvider;
|
|
|
|
var dbContext = services.GetRequiredService<ApplicationDbContext>();
|
|
|
|
var tester = dbContext.Users.FirstOrDefaultAsync
|
|
|
|
(u => u.Id == server.TestingUser.Id).Result;
|
|
|
|
if (tester != null)
|
|
|
|
{
|
|
|
|
dbContext.Users.Remove(tester);
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NugetInstallsTest()
|
|
|
|
{
|
|
|
|
using (var serviceScope = server.Host.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
|
|
|
|
string pkgSourceUrl = isnSettings.ExternalUrl + apiindex + ".json";
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo("nuget");
|
|
|
|
psi.ArgumentList.Add("install");
|
|
|
|
psi.ArgumentList.Add("isnd");
|
|
|
|
psi.ArgumentList.Add("-PreRelease");
|
|
|
|
psi.ArgumentList.Add("-Source");
|
|
|
|
psi.ArgumentList.Add(pkgSourceUrl);
|
|
|
|
Process p = Process.Start(psi);
|
|
|
|
p.WaitForExit();
|
|
|
|
Assert.True(p.ExitCode == 0, "nuget install failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestRegistrationV3Resource()
|
|
|
|
{
|
|
|
|
using (var serviceScope = server.Host.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
|
|
|
|
string pkgSourceUrl = isnSettings.ExternalUrl + apiindex;
|
|
|
|
NullThrottle throttle = new NullThrottle();
|
|
|
|
|
|
|
|
PackageSource packageSource = new PackageSource(pkgSourceUrl);
|
|
|
|
HttpSource client = new HttpSource(packageSource, PkgSourceMessageHandler, throttle);
|
|
|
|
NuGet.Protocol.RegistrationResourceV3 res = new NuGet.Protocol.RegistrationResourceV3(client,
|
|
|
|
new Uri(isnSettings.ExternalUrl + Constants.ApiVersionPrefix + "/registration"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TrueTestRegistrationV3Resource()
|
|
|
|
{
|
|
|
|
using (var serviceScope = server.Host.Services.CreateScope())
|
|
|
|
{
|
|
|
|
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
|
|
|
|
string pkgSourceUrl = isnSettings.ExternalUrl + apiindex;
|
|
|
|
var prov = new RegistrationResourceV3Provider();
|
|
|
|
var source = new PackageSource(pkgSourceUrl);
|
|
|
|
var repo = new SourceRepository(source, new INuGetResourceProvider[] { prov });
|
|
|
|
prov.TryCreate(repo, CancellationToken.None);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task<HttpHandlerResource> PkgSourceMessageHandler()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
public string SPIIndexURI
|
|
|
|
{
|
|
|
|
get => server.Addresses.First(a => a.StartsWith("http:")) + "/v3/index.json";
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task TestGetMetadataAsync()
|
|
|
|
{
|
|
|
|
ILogger logger = NullLogger.Instance;
|
|
|
|
CancellationToken cancellationToken = CancellationToken.None;
|
|
|
|
|
|
|
|
SourceCacheContext cache = new SourceCacheContext();
|
|
|
|
SourceRepository repository = Repository.Factory.GetCoreV3(SPIIndexURI);
|
|
|
|
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();
|
|
|
|
|
|
|
|
IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
|
|
|
|
"isnd",
|
|
|
|
includePrerelease: true,
|
|
|
|
includeUnlisted: true,
|
|
|
|
cache,
|
|
|
|
logger,
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
Assert.NotEmpty(packages);
|
|
|
|
|
|
|
|
foreach (IPackageSearchMetadata package in packages)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Version: {package.Identity.Version}");
|
|
|
|
Console.WriteLine($"Listed: {package.IsListed}");
|
|
|
|
Console.WriteLine($"Tags: {package.Tags}");
|
|
|
|
Console.WriteLine($"Description: {package.Description}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task TestFindPackageAsync()
|
|
|
|
{
|
|
|
|
ILogger logger = new TestLogger();
|
|
|
|
CancellationToken cancellationToken = CancellationToken.None;
|
|
|
|
SourceRepository repository = Repository.Factory.GetCoreV3(SPIIndexURI);
|
|
|
|
repository.PackageSource.AllowInsecureConnections=true;
|
|
|
|
|
|
|
|
PackageSearchResource resource = await repository.GetResourceAsync<PackageSearchResource>();
|
|
|
|
SearchFilter searchFilter = new SearchFilter(includePrerelease: true);
|
|
|
|
|
|
|
|
IEnumerable<IPackageSearchMetadata> results = await resource.SearchAsync(
|
|
|
|
"isn",
|
|
|
|
searchFilter,
|
|
|
|
skip: 0,
|
|
|
|
take: 20,
|
|
|
|
logger,
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
foreach (IPackageSearchMetadata result in results)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Found package {result.Identity.Id} {result.Identity.Version}");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task TestPackagePush()
|
|
|
|
{
|
|
|
|
var logger = new TestLogger();
|
|
|
|
SourceRepository repository = Repository.Factory.GetCoreV3(SPIIndexURI);
|
|
|
|
PackageUpdateResource pushRes = await repository.GetResourceAsync<PackageUpdateResource>();
|
|
|
|
SymbolPackageUpdateResourceV3 symbolPackageResource = await repository.GetResourceAsync<SymbolPackageUpdateResourceV3>();
|
|
|
|
|
|
|
|
await pushRes.Push(new List<string>{ "../../../../../src/isnd/bin/Release/isnd.1.1.4.nupkg" }, null,
|
|
|
|
5000, false, GetApiKey, GetSymbolsApiKey, false, false, symbolPackageResource, logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TestDepedency()
|
|
|
|
{
|
|
|
|
PackageDependencyGroup g = new PackageDependencyGroup
|
|
|
|
{
|
|
|
|
TargetFramework="net7.0"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetSymbolsApiKey(string apiUrl)
|
|
|
|
{
|
|
|
|
return GetApiKey(apiUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetApiKey(string apiUrl)
|
|
|
|
{
|
|
|
|
return server.ProtectedTestingApiKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class TestLogger : NuGet.Common.LoggerBase
|
|
|
|
{
|
|
|
|
public override void Log(ILogMessage message)
|
|
|
|
{
|
|
|
|
string msg = $"{message.Level}: {message.Message}";
|
|
|
|
switch (message.Level)
|
|
|
|
{
|
|
|
|
case LogLevel.Debug:
|
|
|
|
case LogLevel.Information:
|
|
|
|
case LogLevel.Verbose:
|
|
|
|
case LogLevel.Minimal:
|
|
|
|
case LogLevel.Warning:
|
|
|
|
case LogLevel.Error:
|
|
|
|
Debug.WriteLine(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task LogAsync(ILogMessage message)
|
|
|
|
{
|
|
|
|
return Task.Run(()=>Log(message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|