|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 11 | +using Microsoft.Extensions.Configuration; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using NUnit.Framework; |
| 14 | +using UnityNuGet; |
| 15 | +using UnityNuGet.Npm; |
| 16 | + |
| 17 | +namespace UnityNuGet.Server.Tests |
| 18 | +{ |
| 19 | + public class AllowServingWithMissingDependenciesTests |
| 20 | + { |
| 21 | + private readonly AllowServingWithMissingDependenciesWebApplicationFactory _webApplicationFactory; |
| 22 | + |
| 23 | + public AllowServingWithMissingDependenciesTests() |
| 24 | + { |
| 25 | + _webApplicationFactory = new AllowServingWithMissingDependenciesWebApplicationFactory(); |
| 26 | + } |
| 27 | + |
| 28 | + [OneTimeTearDown] |
| 29 | + public void OneTimeTearDown() |
| 30 | + { |
| 31 | + _webApplicationFactory.Dispose(); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public async Task GetAll_Succeeds_WhenBuildHasErrors() |
| 36 | + { |
| 37 | + using HttpClient httpClient = _webApplicationFactory.CreateDefaultClient(); |
| 38 | + |
| 39 | + await WaitForInitialization(_webApplicationFactory.Services, TimeSpan.FromMinutes(2)); |
| 40 | + |
| 41 | + RegistryCacheReport registryCacheReport = _webApplicationFactory.Services.GetRequiredService<RegistryCacheReport>(); |
| 42 | + Assert.That(registryCacheReport.ErrorMessages.Any(), Is.True, "Expected build errors but none were reported."); |
| 43 | + |
| 44 | + HttpResponseMessage response = await httpClient.GetAsync("/-/all"); |
| 45 | + |
| 46 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 47 | + |
| 48 | + string responseContent = await response.Content.ReadAsStringAsync(); |
| 49 | + NpmPackageListAllResponse npmPackageListAllResponse = JsonSerializer.Deserialize( |
| 50 | + responseContent, |
| 51 | + UnityNuGetJsonSerializerContext.Default.NpmPackageListAllResponse)!; |
| 52 | + |
| 53 | + Assert.That(npmPackageListAllResponse, Is.Not.Null); |
| 54 | + } |
| 55 | + |
| 56 | + private static async Task WaitForInitialization(IServiceProvider serviceProvider, TimeSpan timeout) |
| 57 | + { |
| 58 | + RegistryCacheSingleton registryCacheSingleton = serviceProvider.GetRequiredService<RegistryCacheSingleton>(); |
| 59 | + |
| 60 | + DateTime deadline = DateTime.UtcNow.Add(timeout); |
| 61 | + |
| 62 | + while (registryCacheSingleton.Instance == null) |
| 63 | + { |
| 64 | + if (DateTime.UtcNow >= deadline) |
| 65 | + { |
| 66 | + Assert.Fail("Timed out waiting for RegistryCache initialization."); |
| 67 | + } |
| 68 | + |
| 69 | + await Task.Delay(50); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + internal class AllowServingWithMissingDependenciesWebApplicationFactory : WebApplicationFactory<Program> |
| 75 | + { |
| 76 | + private readonly string _rootPersistentFolder; |
| 77 | + private readonly string _registryFilePath; |
| 78 | + |
| 79 | + public AllowServingWithMissingDependenciesWebApplicationFactory() |
| 80 | + { |
| 81 | + _rootPersistentFolder = Path.Combine(Path.GetTempPath(), "UnityNuGet.Server.Tests", Guid.NewGuid().ToString("N")); |
| 82 | + Directory.CreateDirectory(_rootPersistentFolder); |
| 83 | + |
| 84 | + _registryFilePath = Path.Combine(AppContext.BaseDirectory, "registry.test.missing-dep.json"); |
| 85 | + } |
| 86 | + |
| 87 | + protected override void ConfigureWebHost(IWebHostBuilder builder) |
| 88 | + { |
| 89 | + base.ConfigureWebHost(builder); |
| 90 | + |
| 91 | + builder.ConfigureAppConfiguration(configBuilder => |
| 92 | + { |
| 93 | + configBuilder.AddInMemoryCollection(new Dictionary<string, string?> |
| 94 | + { |
| 95 | + { WebHostDefaults.ServerUrlsKey, "http://localhost" }, |
| 96 | + { "Registry:RegistryFilePath", _registryFilePath }, |
| 97 | + { "Registry:RootPersistentFolder", _rootPersistentFolder }, |
| 98 | + { "Registry:AllowServingWithMissingDependencies", "true" }, |
| 99 | + { "Registry:Filter", "Serilog.Sinks.File" } |
| 100 | + }); |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments