1
0
mirror of https://github.com/bitwarden/server synced 2025-12-30 15:14:02 +00:00
Files
server/test/Core.Test/Billing/OrganizationFeatures/OrganizationLicenses/SelfHostedGetOrganizationLicenseQueryTests.cs
Conner Turnbull 9b65e9f4cc [PM-22580] Org/User License Codeownership Move (No logic changes) (#6080)
* Moved license models to billing

* Moved LicensingService to billing

* Moved license command and queries to billing

* Moved LicenseController to billing
2025-07-11 16:41:32 -04:00

96 lines
4.0 KiB
C#

using System.Text.Json;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Billing.Models.Business;
using Bit.Core.Billing.OrganizationFeatures.OrganizationLicenses.SelfHosted;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Models.OrganizationConnectionConfigs;
using Bit.Core.Settings;
using Bit.Core.Test.Billing.AutoFixture;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Bit.Test.Common.Helpers;
using Xunit;
namespace Bit.Core.Test.Billing.OrganizationFeatures.OrganizationLicenses;
[SutProviderCustomize]
public class SelfHostedGetOrganizationLicenseQueryTests
{
private static SutProvider<SelfHostedGetOrganizationLicenseQuery> GetSutProvider(BillingSyncConfig config,
string apiResponse = null)
{
return new SutProvider<SelfHostedGetOrganizationLicenseQuery>()
.ConfigureBaseIdentityClientService($"licenses/organization/{config.CloudOrganizationId}",
HttpMethod.Get, apiResponse: apiResponse);
}
[Theory]
[BitAutoData]
[OrganizationLicenseCustomize]
public async Task GetLicenseAsync_Success(Organization organization,
OrganizationConnection<BillingSyncConfig> billingSyncConnection, BillingSyncConfig config, OrganizationLicense license)
{
var sutProvider = GetSutProvider(config, JsonSerializer.Serialize(license));
billingSyncConnection.Enabled = true;
billingSyncConnection.Config = config;
var result = await sutProvider.Sut.GetLicenseAsync(organization, billingSyncConnection);
AssertHelper.AssertPropertyEqual(result, license);
}
[Theory]
[BitAutoData]
public async Task GetLicenseAsync_WhenNotSelfHosted_Throws(Organization organization,
OrganizationConnection billingSyncConnection, BillingSyncConfig config)
{
var sutProvider = GetSutProvider(config);
sutProvider.GetDependency<IGlobalSettings>().SelfHosted = false;
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.GetLicenseAsync(organization, billingSyncConnection));
Assert.Contains("only available for self-hosted", exception.Message);
}
[Theory]
[BitAutoData]
public async Task GetLicenseAsync_WhenCloudCommunicationDisabled_Throws(Organization organization,
OrganizationConnection billingSyncConnection, BillingSyncConfig config)
{
var sutProvider = GetSutProvider(config);
sutProvider.GetDependency<IGlobalSettings>().EnableCloudCommunication = false;
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.GetLicenseAsync(organization, billingSyncConnection));
Assert.Contains("Cloud communication is disabled", exception.Message);
}
[Theory]
[BitAutoData]
public async Task GetLicenseAsync_WhenCantUseConnection_Throws(Organization organization,
OrganizationConnection<BillingSyncConfig> billingSyncConnection, BillingSyncConfig config)
{
var sutProvider = GetSutProvider(config);
billingSyncConnection.Enabled = false;
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.GetLicenseAsync(organization, billingSyncConnection));
Assert.Contains("Connection disabled", exception.Message);
}
[Theory]
[BitAutoData]
public async Task GetLicenseAsync_WhenNullResponse_Throws(Organization organization,
OrganizationConnection<BillingSyncConfig> billingSyncConnection, BillingSyncConfig config)
{
var sutProvider = GetSutProvider(config);
billingSyncConnection.Enabled = true;
billingSyncConnection.Config = config;
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
sutProvider.Sut.GetLicenseAsync(organization, billingSyncConnection));
Assert.Contains("An error has occurred. Check your internet connection and ensure the billing token is correct.",
exception.Message);
}
}