ApiTestBase.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Microsoft.AspNetCore.Mvc.Testing;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Logging;
  5. using RackPeek.Web;
  6. using Shared.Rcl;
  7. using Xunit.Abstractions;
  8. namespace Tests.Api;
  9. public abstract class ApiTestBase : IDisposable {
  10. /// <summary>
  11. /// The config directory the server is pointed at. Writing to it before the first
  12. /// call to <see cref="CreateClient" /> seeds an inventory, because the host is
  13. /// not built until then.
  14. /// </summary>
  15. protected readonly string TempDir;
  16. protected readonly WebApplicationFactory<Program> Factory;
  17. protected readonly ITestOutputHelper Output;
  18. protected ApiTestBase(ITestOutputHelper output) {
  19. Output = output;
  20. TempDir = Path.Combine(
  21. Path.GetTempPath(),
  22. "rackpeek-tests",
  23. Guid.NewGuid().ToString());
  24. Directory.CreateDirectory(TempDir);
  25. Factory = new WebApplicationFactory<Program>()
  26. .WithWebHostBuilder(builder => {
  27. builder.UseSetting("RPK_YAML_DIR", TempDir);
  28. builder.ConfigureAppConfiguration((context, configBuilder) => {
  29. var baseConfig = new Dictionary<string, string?> {
  30. ["RPK_YAML_DIR"] = TempDir,
  31. ["RPK_API_KEY"] = "test-key-123"
  32. };
  33. ConfigureTestConfiguration(baseConfig);
  34. configBuilder.AddInMemoryCollection(baseConfig);
  35. IConfigurationRoot configuration = configBuilder.Build();
  36. CliBootstrap.RegisterInternals(
  37. new ServiceCollection(),
  38. configuration,
  39. TempDir,
  40. "test.yaml")
  41. .GetAwaiter()
  42. .GetResult();
  43. });
  44. builder.ConfigureServices(services => {
  45. services.AddLogging(logging => {
  46. logging.ClearProviders();
  47. logging.AddProvider(
  48. new XUnitLoggerProvider(Output));
  49. });
  50. ConfigureTestServices(services);
  51. });
  52. });
  53. }
  54. public void Dispose() {
  55. try {
  56. Factory.Dispose();
  57. if (Directory.Exists(TempDir))
  58. Directory.Delete(TempDir, true);
  59. }
  60. catch {
  61. // ignore cleanup issues
  62. }
  63. }
  64. /// <summary>
  65. /// Override to modify configuration per test class
  66. /// </summary>
  67. protected virtual void ConfigureTestConfiguration(
  68. IDictionary<string, string?> config) {
  69. }
  70. /// <summary>
  71. /// Override to modify services per test class
  72. /// </summary>
  73. protected virtual void ConfigureTestServices(
  74. IServiceCollection services) {
  75. }
  76. protected HttpClient CreateClient(bool withApiKey = false) {
  77. HttpClient client = Factory.CreateClient();
  78. if (withApiKey) client.DefaultRequestHeaders.Add("X-Api-Key", "test-key-123");
  79. return client;
  80. }
  81. }