154 lines
6 KiB
C#
154 lines
6 KiB
C#
using System.Threading;
|
|
using System;
|
|
using System.Net.Http;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore;
|
|
using Xunit;
|
|
using isnd.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Diagnostics;
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
using Microsoft.AspNetCore.Hosting.Server.Features;
|
|
using Microsoft.Extensions.Configuration;
|
|
using isnd.tests;
|
|
using NuGet.Protocol;
|
|
using NuGet.Configuration;
|
|
using System.Threading.Tasks;
|
|
using NuGet.Protocol.Core.Types;
|
|
using NuGet.Common;
|
|
using Isn.Abstract;
|
|
using isnd.Helpers;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace isnd.host.tests
|
|
{
|
|
[Collection("Web server collection")]
|
|
public class UnitTestWebHost : IClassFixture<WebServerFixture>
|
|
{
|
|
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>();
|
|
if (myDependency.Database.ProviderName?.Contains("InMemory", StringComparison.OrdinalIgnoreCase) == true)
|
|
{
|
|
myDependency.Database.EnsureCreated();
|
|
}
|
|
else
|
|
{
|
|
myDependency.Database.Migrate();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void LegacyApiKeyProtectorCanUnprotectValuesFromOlderClients()
|
|
{
|
|
const string clearValue = "legacy-api-key";
|
|
var legacyProtectedValue = new Isn.DefaultDataProtector().Protect(clearValue);
|
|
|
|
var unprotected = ApiKeyProtector.TryUnprotectKey(new ThrowingProtector(), legacyProtectedValue);
|
|
|
|
Assert.Equal(clearValue, unprotected);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApiKeyProtectorReturnsRawValueWhenInputIsNotProtected()
|
|
{
|
|
const string rawApiKey = "CfDJ8AstXUEkxpJBrmoENwy__WNPxqcOwAuxyYgiU3Mebns5yxAT3VrC5vTuWTRSGZBFB7IGUyFkUYsp2nhRy64NqeUzLgOwEcU4FPOf-yaAtPeTMeStRd60bRh2ZYyQkQ3hrhW-lwpCLwYtNOnOyX2jayuzByF-4QfHIEhg6lbWenzf";
|
|
|
|
var result = ApiKeyProtector.TryUnprotectKey(new ThrowingProtector(), rawApiKey);
|
|
|
|
Assert.Equal(rawApiKey, result);
|
|
}
|
|
|
|
[Fact]
|
|
void TestDropUser()
|
|
{
|
|
|
|
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;
|
|
if (paul!=null)
|
|
{
|
|
dbContext.Users.Remove(paul);
|
|
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 + "/" + Constants.APIPrefix + "index.json";
|
|
using var client = new HttpClient();
|
|
var response = client.GetAsync(pkgSourceUrl).GetAwaiter().GetResult();
|
|
var body = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
|
|
Assert.True(response.IsSuccessStatusCode, $"Expected {pkgSourceUrl} to be reachable but got {(int)response.StatusCode} {response.ReasonPhrase}");
|
|
Assert.False(string.IsNullOrWhiteSpace(body));
|
|
}
|
|
}
|
|
|
|
[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 + "/pkgs/index.json";
|
|
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 + "/v3.4.0//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 + "/pkgs/index.json";
|
|
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();
|
|
}
|
|
|
|
private sealed class ThrowingProtector : IDataProtector
|
|
{
|
|
public byte[] Protect(byte[] plaintext) => throw new CryptographicException("unexpected protect call");
|
|
|
|
public byte[] Unprotect(byte[] protectedData) => throw new CryptographicException("unexpected unprotect call");
|
|
|
|
public IDataProtector CreateProtector(string purpose) => this;
|
|
}
|
|
}
|
|
}
|