mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
[SM-153] Add scaffolded API integration test project (#2209)
This commit is contained in:
29
test/Api.IntegrationTest/Api.IntegrationTest.csproj
Normal file
29
test/Api.IntegrationTest/Api.IntegrationTest.csproj
Normal file
@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkVersion)" />
|
||||
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorVersion)">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Api\Api.csproj" />
|
||||
<ProjectReference Include="..\IntegrationTestCommon\IntegrationTestCommon.csproj" />
|
||||
|
||||
<Content Include="..\..\src\Api\appsettings.*.json">
|
||||
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -0,0 +1,40 @@
|
||||
using System.Net.Http.Headers;
|
||||
using Bit.Api.IntegrationTest.Factories;
|
||||
using Bit.Api.Models.Response;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Api.IntegrationTest.Controllers;
|
||||
|
||||
public class AccountsControllerTest : IClassFixture<ApiApplicationFactory>
|
||||
{
|
||||
private readonly ApiApplicationFactory _factory;
|
||||
|
||||
public AccountsControllerTest(ApiApplicationFactory factory) => _factory = factory;
|
||||
|
||||
[Fact]
|
||||
public async Task GetPublicKey()
|
||||
{
|
||||
var tokens = await _factory.LoginWithNewAccount();
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
using var message = new HttpRequestMessage(HttpMethod.Get, "/accounts/profile");
|
||||
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.Token);
|
||||
var response = await client.SendAsync(message);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var content = await response.Content.ReadFromJsonAsync<ProfileResponseModel>();
|
||||
|
||||
Assert.NotEmpty(content.Id);
|
||||
Assert.Equal("integration-test@bitwarden.com", content.Email);
|
||||
Assert.Null(content.Name);
|
||||
Assert.False(content.EmailVerified);
|
||||
Assert.False(content.Premium);
|
||||
Assert.False(content.PremiumFromOrganization);
|
||||
Assert.Null(content.MasterPasswordHint);
|
||||
Assert.Equal("en-US", content.Culture);
|
||||
Assert.Null(content.Key);
|
||||
Assert.Null(content.PrivateKey);
|
||||
Assert.NotNull(content.SecurityStamp);
|
||||
}
|
||||
}
|
44
test/Api.IntegrationTest/Factories/ApiApplicationFactory.cs
Normal file
44
test/Api.IntegrationTest/Factories/ApiApplicationFactory.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using Bit.Core.Models.Api.Request.Accounts;
|
||||
using Bit.IntegrationTestCommon.Factories;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
|
||||
namespace Bit.Api.IntegrationTest.Factories
|
||||
{
|
||||
public class ApiApplicationFactory : WebApplicationFactoryBase<Startup>
|
||||
{
|
||||
private readonly IdentityApplicationFactory _identityApplicationFactory;
|
||||
|
||||
public ApiApplicationFactory()
|
||||
{
|
||||
_identityApplicationFactory = new IdentityApplicationFactory();
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.PostConfigure<IdentityServerAuthenticationOptions>(IdentityServerAuthenticationDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
options.JwtBackChannelHandler = _identityApplicationFactory.Server.CreateHandler();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper for registering and logging in to a new account
|
||||
/// </summary>
|
||||
public async Task<(string Token, string RefreshToken)> LoginWithNewAccount(string email = "integration-test@bitwarden.com", string masterPasswordHash = "master_password_hash")
|
||||
{
|
||||
await _identityApplicationFactory.RegisterAsync(new RegisterRequestModel
|
||||
{
|
||||
Email = email,
|
||||
MasterPasswordHash = masterPasswordHash,
|
||||
});
|
||||
|
||||
return await _identityApplicationFactory.TokenFromPasswordAsync(email, masterPasswordHash);
|
||||
}
|
||||
}
|
||||
}
|
3196
test/Api.IntegrationTest/packages.lock.json
Normal file
3196
test/Api.IntegrationTest/packages.lock.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user