1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-05 14:23:30 +01:00
bitwarden-server/test/Billing.Test/Controllers/FreshsalesControllerTests.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

83 lines
2.7 KiB
C#
Raw Normal View History

using Bit.Billing.Controllers;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Entities;
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
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
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
});
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
}
[RequiredEnvironmentTheory(ApiKey, TestLead), EnvironmentData(ApiKey, TestLead)]
public async Task PostWebhook_Success(string freshsalesApiKey, long leadId)
2022-08-29 22:06:55 +02: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);
organizationRepository.GetManyByUserIdAsync(user.Id)
.Returns(new List<Organization>
2022-08-29 22:06:55 +02: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);
}
}