Skip to content

Commit

Permalink
Add Bitwarden.Server.Sdk Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justindbaur committed Jan 27, 2025
1 parent 13351c0 commit c471bce
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bitwarden-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{384C7FC2
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bitwarden.Server.Sdk.UnitTests", "extensions\Bitwarden.Server.Sdk\tests\Bitwarden.Server.Sdk.UnitTests\Bitwarden.Server.Sdk.UnitTests.csproj", "{6CF2554F-67DA-494F-A414-3974FAB96E6E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bitwarden.Server.Sdk.IntegrationTests", "extensions\Bitwarden.Server.Sdk\tests\Bitwarden.Server.Sdk.IntegrationTests\Bitwarden.Server.Sdk.IntegrationTests.csproj", "{C0601B8C-1A72-400F-8CCF-C5493221E555}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -94,6 +96,10 @@ Global
{6CF2554F-67DA-494F-A414-3974FAB96E6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CF2554F-67DA-494F-A414-3974FAB96E6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CF2554F-67DA-494F-A414-3974FAB96E6E}.Release|Any CPU.Build.0 = Release|Any CPU
{C0601B8C-1A72-400F-8CCF-C5493221E555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0601B8C-1A72-400F-8CCF-C5493221E555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0601B8C-1A72-400F-8CCF-C5493221E555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0601B8C-1A72-400F-8CCF-C5493221E555}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5EC8B943-2E9E-437D-9FFC-D18B5DB4D7D0} = {695C76EF-1102-4805-970F-7C995EE54930}
Expand All @@ -114,5 +120,6 @@ Global
{061FB6BF-6F7E-4E48-BD89-6BA826B15432} = {AEF7870E-D4E5-4ECC-88A1-BD2C5A6CC9C7}
{384C7FC2-51CB-4723-B2EC-353833AB4354} = {82253883-A5E2-4917-A690-A744C3855FAB}
{6CF2554F-67DA-494F-A414-3974FAB96E6E} = {384C7FC2-51CB-4723-B2EC-353833AB4354}
{C0601B8C-1A72-400F-8CCF-C5493221E555} = {384C7FC2-51CB-4723-B2EC-353833AB4354}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.Build" Version="17.12.6" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.12.6" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.12.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSBuild.ProjectCreation" Version="14.0.5" />
<PackageReference Include="xunit.v3" Version="1.0.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\src\sdk\**\*"
Link="Sdk\%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />

<None Include="..\..\src\content\**\*"
Link="Content\%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.Build.Utilities.ProjectCreation;

namespace Bitwarden.Server.Sdk.IntegrationTests;

public static class CustomProjectCreatorTemplates
{
private static readonly string ThisAssemblyDirectory = Path.GetDirectoryName(typeof(CustomProjectCreatorTemplates).Assembly.Location)!;

public static ProjectCreator SdkProject(this ProjectCreatorTemplates templates,
string? additional = null,
Action<ProjectCreator>? customAction = null)
{
var dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

Directory.CreateDirectory(dir);

File.WriteAllText(Path.Combine(dir, "Program.cs"), $"""
var builder = WebApplication.CreateBuilder(args);
builder.UseBitwardenSdk();
var app = builder.Build();
{additional}
app.Run();
"""
);

return ProjectCreator.Templates.SdkCsproj(
path: Path.Combine(dir, "Test.csproj"),
sdk: "Microsoft.NET.Sdk.Web",
targetFramework: "net9.0")
.Import(Path.Combine(ThisAssemblyDirectory, "sdk", "Sdk.props"))
.CustomAction(customAction)
.Import(Path.Combine(ThisAssemblyDirectory, "sdk", "Sdk.targets"));
}

public static ProjectCreator TryGetConstant(this ProjectCreator project, string constant, out bool result)
{
result = false;
project = project.TryGetPropertyValue("DefineConstants", out var constants);

if (string.IsNullOrEmpty(constants))
{
return project;
}

var allConstants = constants.Split(';');

if (!allConstants.Contains(constant))
{
return project;
}

result = true;
return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Microsoft.Build.Utilities.ProjectCreation;

namespace Bitwarden.Server.Sdk.IntegrationTests;

public class SdkTests : MSBuildTestBase
{
[Fact]
public void NoOverridingProperties_CanCompile()
{
ProjectCreator.Templates.SdkProject()
.TryBuild(restore: true, out var result, out var buildOutput)
.TryGetConstant("BIT_INCLUDE_LOGGING", out var hasLoggingConstant)
.TryGetConstant("BIT_INCLUDE_TELEMETRY", out var hasTelementryConstant)
.TryGetConstant("BIT_INCLUDE_FEATURES", out var hasFeaturesConstant);

Assert.True(result, buildOutput.GetConsoleLog());

Assert.True(hasLoggingConstant);
Assert.True(hasTelementryConstant);
Assert.True(hasFeaturesConstant);
}

[Fact]
public void LoggingTurnedOff_CanCompile()
{
ProjectCreator.Templates.SdkProject(
customAction: (project) =>
{
project.Property("BitIncludeLogging", bool.FalseString);
}
)
.TryBuild(restore: true, out var result, out var buildOutput);

Assert.True(result, buildOutput.GetConsoleLog());
}

[Fact]
public void TelemetryTurnedOff_CanCompile()
{
ProjectCreator.Templates.SdkProject(
customAction: (project) =>
{
project.Property("BitIncludeTelemetry", bool.FalseString);
}
)
.TryBuild(restore: true, out var result, out var buildOutput);

Assert.True(result, buildOutput.GetConsoleLog());
}

[Fact]
public void FeaturesTurnedOff_CanCompile()
{
ProjectCreator.Templates.SdkProject(
customAction: (project) =>
{
project.Property("BitIncludeFeatures", bool.FalseString);
}
)
.TryBuild(restore: true, out var result, out var buildOutput);

Assert.True(result, buildOutput.GetConsoleLog());
}

[Fact]
public void FeaturesTurnedOff_CanNotUseFeatureService()
{
ProjectCreator.Templates.SdkProject(
customAction: (project) =>
{
project.Property("BitIncludeFeatures", bool.FalseString);
},
additional: """
app.MapGet("/test", (Bitwarden.Server.Sdk.Features.IFeatureService featureService) => featureService.GetAll());
"""
)
.TryBuild(restore: true, out var result, out var buildOutput);

Assert.False(result, buildOutput.GetConsoleLog());

// error CS0234: The type or namespace name 'Features' does not exist in the namespace 'Bitwarden.Server.Sdk' (are you missing an assembly reference?)
Assert.Contains(buildOutput.ErrorEvents, e => e.Code == "CS0234");
}

public static TheoryData<bool, bool, bool> MatrixData
=> new MatrixTheoryData<bool, bool, bool>([true, false], [true, false], [true, false]);

// There will be some variants that disallow the use of feature Y if feature X is not also enabled.
// Use this set to exclude those known variants from being tested.
public static HashSet<(bool, bool, bool)> ExcludedVariants => [];

[Theory, MemberData(nameof(MatrixData))]
public void AllVariants_Work(bool includeLogging, bool includeTelemetry, bool includeFeatures)
{
if (ExcludedVariants.Contains((includeLogging, includeTelemetry, includeFeatures)))
{
Assert.Skip($"""
Excluded Variant Skipped:
IncludeLogging = {includeLogging}
IncludeTelemetry = {includeTelemetry}
IncludeFeatures = {includeFeatures}
""");
}

ProjectCreator.Templates.SdkProject(
customAction: (project) =>
{
project.Property("BitIncludeLogging", includeLogging.ToString());
project.Property("BitIncludeTelemetry", includeTelemetry.ToString());
project.Property("BitIncludeFeatures", includeFeatures.ToString());
}
)
.TryBuild(restore: true, out var result, out var buildOutput);

Assert.True(result, buildOutput.GetConsoleLog());
}
}

0 comments on commit c471bce

Please sign in to comment.