isn/test/isnd.tests/UnitTestWebHost.cs

144 lines
5.6 KiB
C#
Raw Normal View History

2023-03-14 00:15:42 +00:00
using System.Threading;
2021-05-09 13:45:55 +01:00
using System;
2026-07-05 18:16:52 +01:00
using System.Net.Http;
2021-05-09 13:45:55 +01:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Xunit;
2021-08-15 19:09:01 +01:00
using isnd.Data;
2021-05-11 20:52:39 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
2021-09-08 01:55:00 +01:00
using Microsoft.Extensions.Options;
2022-05-25 09:07:36 +01:00
using System.Diagnostics;
2022-06-16 17:03:38 +01:00
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
2023-01-29 13:04:50 +00:00
using Microsoft.Extensions.Configuration;
using isnd.tests;
2023-02-06 21:43:18 +00:00
using NuGet.Protocol;
using NuGet.Configuration;
using System.Threading.Tasks;
using NuGet.Protocol.Core.Types;
2026-07-05 18:16:52 +01:00
using NuGet.Common;
2026-07-05 20:17:12 +01:00
using Isn.Abstract;
2026-07-05 21:18:37 +01:00
using isnd.Helpers;
using Microsoft.AspNetCore.DataProtection;
using System.Security.Cryptography;
2021-09-08 01:55:00 +01:00
2021-08-13 18:55:25 +01:00
namespace isnd.host.tests
2023-02-06 21:43:18 +00:00
{
2023-01-29 13:04:50 +00:00
[Collection("Web server collection")]
public class UnitTestWebHost : IClassFixture<WebServerFixture>
2021-05-09 13:45:55 +01:00
{
2023-01-29 13:04:50 +00:00
WebServerFixture server;
public UnitTestWebHost(WebServerFixture server)
{
this.server = server;
}
[Fact]
2021-07-05 21:24:37 +01:00
public void TestHaveTestDbContextAndMigrate()
2021-05-09 13:45:55 +01:00
{
2023-01-29 13:04:50 +00:00
using (var serviceScope = server.Host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var myDependency = services.GetRequiredService<ApplicationDbContext>();
2026-07-05 18:16:52 +01:00
if (myDependency.Database.ProviderName?.Contains("InMemory", StringComparison.OrdinalIgnoreCase) == true)
{
myDependency.Database.EnsureCreated();
}
else
{
myDependency.Database.Migrate();
}
2023-01-29 13:04:50 +00:00
}
}
2021-05-11 20:52:39 +01:00
2026-07-05 21:18:37 +01:00
[Fact]
public void LegacyApiKeyProtectorCanUnprotectValuesFromOlderClients()
{
const string clearValue = "legacy-api-key";
var legacyProtectedValue = new Isn.DefaultDataProtector().Protect(clearValue);
var unprotected = ApiKeyProtector.UnprotectWithFallback(new ThrowingProtector(), legacyProtectedValue);
Assert.Equal(clearValue, unprotected);
}
2023-01-29 13:04:50 +00:00
[Fact]
void TestDropUser()
{
using (var serviceScope = server.Host.Services.CreateScope())
2021-05-11 20:52:39 +01:00
{
var services = serviceScope.ServiceProvider;
2023-01-29 13:04:50 +00:00
var dbContext = services.GetRequiredService<ApplicationDbContext>();
2023-02-06 00:41:35 +00:00
var paul = dbContext.Users.FirstOrDefaultAsync(u => u.Email == "paul@pschneider.fr").Result;
2023-01-29 13:04:50 +00:00
if (paul!=null)
2023-02-06 00:41:35 +00:00
{
2023-01-29 13:04:50 +00:00
dbContext.Users.Remove(paul);
2023-02-06 00:41:35 +00:00
dbContext.SaveChanges();
}
2021-05-11 20:52:39 +01:00
}
2021-05-09 13:45:55 +01:00
}
2022-06-16 17:03:38 +01:00
2021-05-11 20:52:39 +01:00
2022-05-25 09:07:36 +01:00
[Fact]
2023-01-29 13:04:50 +00:00
public void NugetInstallsTest()
2022-04-09 19:53:26 +01:00
{
2023-02-06 21:43:18 +00:00
using (var serviceScope = server.Host.Services.CreateScope())
2026-07-05 18:16:52 +01:00
{
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
2026-07-05 20:17:12 +01:00
string pkgSourceUrl = isnSettings.ExternalUrl + "/" + Constants.APIPrefix + "index.json";
2026-07-05 18:16:52 +01:00
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));
2023-02-06 21:43:18 +00:00
}
}
[Fact]
public void TestRegistrationV3Resource()
{
using (var serviceScope = server.Host.Services.CreateScope())
{ var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
2026-07-05 18:16:52 +01:00
string pkgSourceUrl = isnSettings.ExternalUrl + "/pkgs/index.json";
2023-02-06 21:43:18 +00:00
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"));
}
}
2023-03-14 00:15:42 +00:00
[Fact]
public void TrueTestRegistrationV3Resource()
{
using (var serviceScope = server.Host.Services.CreateScope())
{
var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
2026-07-05 18:16:52 +01:00
string pkgSourceUrl = isnSettings.ExternalUrl + "/pkgs/index.json";
2023-03-14 00:15:42 +00:00
var prov = new RegistrationResourceV3Provider();
var source = new PackageSource(pkgSourceUrl);
var repo = new SourceRepository(source, new INuGetResourceProvider[]{ prov });
prov.TryCreate(repo, CancellationToken.None);
}
}
2023-02-06 21:43:18 +00:00
private Task<HttpHandlerResource> PkgSourceMessageHandler()
{
throw new NotImplementedException();
2022-04-09 19:53:26 +01:00
}
2026-07-05 21:18:37 +01:00
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;
}
2021-05-09 13:45:55 +01:00
}
}