isn/test/isnd.tests/UnitTestWebHost.cs

175 lines
6.5 KiB
C#

using System.Linq;
1 year ago
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;
1 year ago
namespace isnd.host.tests
7 months ago
{
1 year ago
[Collection("Web server collection")]
public class UnitTestWebHost : IClassFixture<WebServerFixture>
{
const string apiindex = Constants.ApiVersionPrefix + "/index";
1 year ago
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()
{
7 months ago
1 year ago
using (var serviceScope = server.Host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var dbContext = services.GetRequiredService<ApplicationDbContext>();
var paul = dbContext.Users.FirstOrDefaultAsync
(u => u.Email == "paul@pschneider.fr").Result;
7 months ago
if (paul != null)
1 year ago
{
dbContext.Users.Remove(paul);
dbContext.SaveChanges();
}
}
}
[Fact]
public void NugetInstallsTest()
{
using (var serviceScope = server.Host.Services.CreateScope())
7 months ago
{
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
string pkgSourceUrl = isnSettings.ExternalUrl + apiindex;
ProcessStartInfo psi = new ProcessStartInfo("nuget");
psi.ArgumentList.Add("install");
psi.ArgumentList.Add("gitversion");
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!");
1 year ago
}
}
[Fact]
public void TestRegistrationV3Resource()
{
using (var serviceScope = server.Host.Services.CreateScope())
7 months ago
{
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"));
1 year ago
}
}
7 months ago
1 year ago
[Fact]
public void TrueTestRegistrationV3Resource()
{
using (var serviceScope = server.Host.Services.CreateScope())
7 months ago
{
1 year ago
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
7 months ago
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);
1 year ago
}
}
private Task<HttpHandlerResource> PkgSourceMessageHandler()
{
throw new NotImplementedException();
}
7 months ago
public string SPIIndexURI
{
get => server.Addresses.First() + "/v3/index";
}
7 months ago
[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(
"isn.abst",
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}");
}
}
7 months ago
[Fact]
public async Task TestFindPackageAsync()
{
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceRepository repository = Repository.Factory.GetCoreV3(SPIIndexURI);
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}");
}
}
1 year ago
}
}