mirror of
https://github.com/bitwarden/server.git
synced 2026-02-06 09:03:21 +08:00
Add some integration tests for the Server project
This commit is contained in:
23
test/Server.IntegrationTest/Server.IntegrationTest.csproj
Normal file
23
test/Server.IntegrationTest/Server.IntegrationTest.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
|
||||
<PackageReference Include="xunit.v3" Version="3.0.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\util\Server\Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
44
test/Server.IntegrationTest/Server.cs
Normal file
44
test/Server.IntegrationTest/Server.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Server.IntegrationTest;
|
||||
|
||||
public class Server : WebApplicationFactory<Program>
|
||||
{
|
||||
public string? ContentRoot { get; set; }
|
||||
public string? WebRoot { get; set; }
|
||||
public bool ServeUnknown { get; set; }
|
||||
public bool? WebVault { get; set; }
|
||||
public string? AppIdLocation { get; set; }
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
|
||||
builder.ConfigureLogging(logging =>
|
||||
{
|
||||
logging.SetMinimumLevel(LogLevel.Debug);
|
||||
});
|
||||
|
||||
var config = new Dictionary<string, string?>
|
||||
{
|
||||
{"contentRoot", ContentRoot},
|
||||
{"webRoot", WebRoot},
|
||||
{"serveUnknown", ServeUnknown.ToString().ToLowerInvariant()},
|
||||
};
|
||||
|
||||
if (WebVault.HasValue)
|
||||
{
|
||||
config["webVault"] = WebVault.Value.ToString().ToLowerInvariant();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(AppIdLocation))
|
||||
{
|
||||
config["appIdLocation"] = AppIdLocation;
|
||||
}
|
||||
|
||||
builder.UseConfiguration(new ConfigurationBuilder().AddInMemoryCollection(config).Build());
|
||||
}
|
||||
}
|
||||
102
test/Server.IntegrationTest/ServerTests.cs
Normal file
102
test/Server.IntegrationTest/ServerTests.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Bit.Server.IntegrationTest;
|
||||
|
||||
public class ServerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task AttachmentsStyleUse()
|
||||
{
|
||||
using var tempDir = new TempDir();
|
||||
|
||||
await tempDir.WriteAsync("my-file.txt", "Hello!");
|
||||
|
||||
using var server = new Server
|
||||
{
|
||||
ContentRoot = tempDir.Info.FullName,
|
||||
WebRoot = ".",
|
||||
ServeUnknown = true,
|
||||
};
|
||||
|
||||
var client = server.CreateClient();
|
||||
|
||||
var response = await client.GetAsync("/my-file.txt", TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("Hello!", await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WebVaultStyleUse()
|
||||
{
|
||||
using var tempDir = new TempDir();
|
||||
|
||||
await tempDir.WriteAsync("index.html", "<html></html>");
|
||||
await tempDir.WriteAsync(Path.Join("app", "file.js"), "AppStuff");
|
||||
await tempDir.WriteAsync(Path.Join("locales", "file.json"), "LocalesStuff");
|
||||
await tempDir.WriteAsync(Path.Join("fonts", "file.ttf"), "FontsStuff");
|
||||
await tempDir.WriteAsync(Path.Join("connectors", "file.js"), "ConnectorsStuff");
|
||||
await tempDir.WriteAsync(Path.Join("scripts", "file.js"), "ScriptsStuff");
|
||||
await tempDir.WriteAsync(Path.Join("images", "file.avif"), "ImagesStuff");
|
||||
await tempDir.WriteAsync(Path.Join("test", "file.json"), "{}");
|
||||
|
||||
using var server = new Server
|
||||
{
|
||||
ContentRoot = tempDir.Info.FullName,
|
||||
WebRoot = ".",
|
||||
ServeUnknown = false,
|
||||
WebVault = true,
|
||||
AppIdLocation = Path.Join(tempDir.Info.FullName, "test", "file.json"),
|
||||
};
|
||||
|
||||
var client = server.CreateClient();
|
||||
|
||||
// Going to root should return the default file
|
||||
var response = await client.GetAsync("", TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("<html></html>", await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken));
|
||||
// No caching on the default document
|
||||
Assert.Null(response.Headers.CacheControl?.MaxAge);
|
||||
|
||||
await ExpectMaxAgeAsync("app/file.js", TimeSpan.FromDays(14));
|
||||
await ExpectMaxAgeAsync("locales/file.json", TimeSpan.FromDays(14));
|
||||
await ExpectMaxAgeAsync("fonts/file.ttf", TimeSpan.FromDays(14));
|
||||
await ExpectMaxAgeAsync("connectors/file.js", TimeSpan.FromDays(14));
|
||||
await ExpectMaxAgeAsync("scripts/file.js", TimeSpan.FromDays(14));
|
||||
await ExpectMaxAgeAsync("images/file.avif", TimeSpan.FromDays(7));
|
||||
|
||||
response = await client.GetAsync("app-id.json", TestContext.Current.CancellationToken);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("application/json", response.Content.Headers.ContentType?.MediaType);
|
||||
|
||||
async Task ExpectMaxAgeAsync(string path, TimeSpan maxAge)
|
||||
{
|
||||
response = await client.GetAsync(path);
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.NotNull(response.Headers.CacheControl);
|
||||
Assert.Equal(maxAge, response.Headers.CacheControl.MaxAge);
|
||||
}
|
||||
}
|
||||
|
||||
private class TempDir([CallerMemberName] string test = null!) : IDisposable
|
||||
{
|
||||
public DirectoryInfo Info { get; } = Directory.CreateTempSubdirectory(test);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Info.Delete(recursive: true);
|
||||
}
|
||||
|
||||
public async Task WriteAsync(string fileName, string content)
|
||||
{
|
||||
var fullPath = Path.Join(Info.FullName, fileName);
|
||||
var directory = Path.GetDirectoryName(fullPath);
|
||||
if (directory != null)
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
await File.WriteAllTextAsync(fullPath, content, TestContext.Current.CancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user