2022-06-30 01:46:41 +02:00
|
|
|
|
using Bit.Billing.Controllers;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2021-12-22 19:27:52 +01:00
|
|
|
|
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;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-12-22 19:27:52 +01:00
|
|
|
|
public class FreshsalesControllerTests
|
|
|
|
|
{
|
|
|
|
|
private const string ApiKey = "TEST_FRESHSALES_APIKEY";
|
|
|
|
|
private const string TestLead = "TEST_FRESHSALES_TESTLEAD";
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-12-22 19:27:52 +01:00
|
|
|
|
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,
|
2022-08-29 22:06:55 +02:00
|
|
|
|
});
|
2021-12-22 19:27:52 +01:00
|
|
|
|
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);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-12-22 19:27:52 +01:00
|
|
|
|
|
|
|
|
|
[RequiredEnvironmentTheory(ApiKey, TestLead), EnvironmentData(ApiKey, TestLead)]
|
|
|
|
|
public async Task PostWebhook_Success(string freshsalesApiKey, long leadId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-12-22 19:27:52 +01:00
|
|
|
|
// 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);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2021-12-22 19:27:52 +01:00
|
|
|
|
organizationRepository.GetManyByUserIdAsync(user.Id)
|
|
|
|
|
.Returns(new List<Organization>
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-12-22 19:27:52 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|