mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 08:32:50 -05:00
Freshsales integration (#1782)
* Add FreshsalesController * Add better errors * Fix formatting issues * Add comments * Add Billing.Test to solution files * Fix unit test * Format code * Address PR feedback
This commit is contained in:
27
test/Billing.Test/Billing.Test.csproj
Normal file
27
test/Billing.Test/Billing.Test.csproj
Normal file
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="4.14.0" />
|
||||
<PackageReference Include="NSubstitute" Version="4.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Billing\Billing.csproj" />
|
||||
<ProjectReference Include="..\Common\Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
88
test/Billing.Test/Controllers/FreshsalesControllerTests.cs
Normal file
88
test/Billing.Test/Controllers/FreshsalesControllerTests.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Billing.Controllers;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Billing.Test.Controllers
|
||||
{
|
||||
public class FreshsalesControllerTests
|
||||
{
|
||||
private const string ApiKey = "TEST_FRESHSALES_APIKEY";
|
||||
private const string TestLead = "TEST_FRESHSALES_TESTLEAD";
|
||||
|
||||
private static (FreshsalesController, IUserRepository, IOrganizationRepository) CreateSut(
|
||||
string freshsalesApiKey)
|
||||
{
|
||||
var userRepository = Substitute.For<IUserRepository>();
|
||||
var organizationRepository = Substitute.For<IOrganizationRepository>();
|
||||
|
||||
var billingSettings = Options.Create(new BillingSettings
|
||||
{
|
||||
FreshsalesApiKey = freshsalesApiKey,
|
||||
});
|
||||
var globalSettings = new GlobalSettings();
|
||||
globalSettings.BaseServiceUri.Admin = "https://test.com";
|
||||
|
||||
var sut = new FreshsalesController(
|
||||
userRepository,
|
||||
organizationRepository,
|
||||
billingSettings,
|
||||
Substitute.For<ILogger<FreshsalesController>>(),
|
||||
globalSettings
|
||||
);
|
||||
|
||||
return (sut, userRepository, organizationRepository);
|
||||
}
|
||||
|
||||
[RequiredEnvironmentTheory(ApiKey, TestLead), EnvironmentData(ApiKey, TestLead)]
|
||||
public async Task PostWebhook_Success(string freshsalesApiKey, long leadId)
|
||||
{
|
||||
// This test is only for development to use:
|
||||
// `export TEST_FRESHSALES_APIKEY=[apikey]`
|
||||
// `export TEST_FRESHSALES_TESTLEAD=[lead id]`
|
||||
// `dotnet test --filter "FullyQualifiedName~FreshsalesControllerTests.PostWebhook_Success"`
|
||||
var (sut, userRepository, organizationRepository) = CreateSut(freshsalesApiKey);
|
||||
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Email = "test@email.com",
|
||||
Premium = true,
|
||||
};
|
||||
|
||||
userRepository.GetByEmailAsync(user.Email)
|
||||
.Returns(user);
|
||||
|
||||
organizationRepository.GetManyByUserIdAsync(user.Id)
|
||||
.Returns(new List<Organization>
|
||||
{
|
||||
new Organization
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Test Org",
|
||||
}
|
||||
});
|
||||
|
||||
var response = await sut.PostWebhook(freshsalesApiKey, new CustomWebhookRequestModel
|
||||
{
|
||||
LeadId = leadId,
|
||||
}, new CancellationToken(false));
|
||||
|
||||
var statusCodeResult = Assert.IsAssignableFrom<StatusCodeResult>(response);
|
||||
Assert.Equal(StatusCodes.Status204NoContent, statusCodeResult.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user