yavsc/src/Yavsc.Org.Tests/Mandatory/BatchTests.cs

105 lines
3.9 KiB
C#
Raw Normal View History

2025-07-16 00:47:50 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Yavsc.Models;
2018-09-07 16:19:30 +02:00
using Yavsc.Server.Models.IT.SourceCode;
2025-07-13 18:13:04 +01:00
using Microsoft.EntityFrameworkCore;
2026-06-15 00:33:38 +01:00
2026-03-09 02:07:09 +00:00
using Yavsc.Server.Models.IT;
2026-06-19 19:59:15 +01:00
namespace Yavsc.Org.Tests
{
2025-07-16 00:47:50 +01:00
[Collection("Yavsc Server")]
2021-01-04 00:11:27 +00:00
[Trait("regression", "oui")]
public class BaseTestContext : IClassFixture<WebServerFixture>, IDisposable
{
2025-07-13 18:13:04 +01:00
public readonly WebServerFixture _serverFixture;
private readonly ITestOutputHelper _output;
2019-01-05 13:28:17 +00:00
2025-07-13 18:13:04 +01:00
public BaseTestContext(ITestOutputHelper output, WebServerFixture fixture)
{
2025-07-13 18:13:04 +01:00
this._serverFixture = fixture;
this._output = output;
2018-08-01 06:12:42 +02:00
}
2019-01-05 13:28:17 +00:00
public HttpClient CreateHttpClient()
{
return new HttpClient(new BypassSslValidationHandler())
{
BaseAddress = new Uri(this._serverFixture.HttpsAuthority ?? throw new InvalidOperationException("Missing HttpsAuthority"))
};
}
/// <summary>
/// Issue a GET against <paramref name="relativePath"/> on the
/// in-memory test server. Returns the raw HttpResponseMessage
/// without following redirects — the test asserts on the first
/// hop, not the eventual page.
/// </summary>
protected static async Task<HttpResponseMessage> GetRaw(
HttpClient client, string relativePath)
{
Assert.NotNull(client);
var request = new HttpRequestMessage(HttpMethod.Get, relativePath);
return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
}
/// <summary>
/// Smoke assertion: a GET on <paramref name="relativePath"/>
/// returns 2xx (page served) or 3xx (redirect to login) or
/// 401/403 (anonymous rejected by [Authorize]). Anything else
/// — 404 (route missing), 5xx (server crash), connection
/// refused (host not started) — fails the test.
/// </summary>
protected static async Task AssertResponds(
HttpClient client, string relativePath)
{
var response = await GetRaw(client, relativePath);
var status = (int)response.StatusCode;
Assert.True(
status >= 200 && status < 400 || status == 401 || status == 403,
$"GET {relativePath} returned {status} {response.StatusCode}, " +
"expected 2xx/3xx (page or redirect) or 401/403 (auth required).");
}
2026-03-09 02:07:09 +00:00
// FIXME write a scenario from an empty database [Fact]
2018-07-25 02:12:13 +02:00
public void GitClone()
{
2025-07-15 15:42:30 +01:00
using var scope = _serverFixture.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
2026-03-09 02:07:09 +00:00
Assert.NotNull(dbContext.Project);
Project yavsc = new Project
{
Name = "Yavsc"
};
dbContext.Project.Add(yavsc);
dbContext.SaveChanges();
var firstProject = dbContext.Project.Include(p => p.Repository).FirstOrDefault(
p => p.Name == "Yavsc"
);
Assert.NotNull(firstProject);
2025-07-13 18:13:04 +01:00
var di = new DirectoryInfo(_serverFixture.SiteSettings.GitRepository);
2019-01-05 13:28:17 +00:00
if (!di.Exists) di.Create();
2018-07-25 02:12:13 +02:00
2025-07-13 18:13:04 +01:00
var clone = new GitClone(_serverFixture.SiteSettings.GitRepository);
2018-07-25 02:12:13 +02:00
clone.Launch(firstProject);
2019-05-24 20:14:38 +01:00
gitRepo = di.FullName;
}
string gitRepo = null;
2025-07-13 18:13:04 +01:00
private IConfigurationRoot configurationRoot;
[Fact]
2025-07-13 18:13:04 +01:00
public void HaveConfigurationRoot()
{
var builder = new ConfigurationBuilder();
configurationRoot = builder.Build();
}
2019-05-24 20:14:38 +01:00
public void Dispose()
{
if (gitRepo != null)
2019-05-24 20:14:38 +01:00
{
Directory.Delete(Path.Combine(gitRepo, "yavsc"), true);
2019-05-24 20:14:38 +01:00
}
}
}
}