2022-07-15 18:10:56 +02:00
|
|
|
|
using Bit.Billing.Controllers;
|
|
|
|
|
using Bit.Billing.Models;
|
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
namespace Bit.Billing.Test.Controllers
|
2022-07-15 18:10:56 +02:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[ControllerCustomize(typeof(FreshdeskController))]
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class FreshdeskControllerTests
|
2022-07-15 18:10:56 +02:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
private const string ApiKey = "TESTFRESHDESKAPIKEY";
|
|
|
|
|
private const string WebhookKey = "TESTKEY";
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData((string)null, null)]
|
|
|
|
|
[BitAutoData((string)null)]
|
|
|
|
|
[BitAutoData(WebhookKey, null)]
|
|
|
|
|
public async Task PostWebhook_NullRequiredParameters_BadRequest(string freshdeskWebhookKey, FreshdeskWebhookModel model,
|
|
|
|
|
BillingSettings billingSettings, SutProvider<FreshdeskController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOptions<BillingSettings>>().Value.FreshdeskWebhookKey.Returns(billingSettings.FreshdeskWebhookKey);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var response = await sutProvider.Sut.PostWebhook(freshdeskWebhookKey, model);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var statusCodeResult = Assert.IsAssignableFrom<StatusCodeResult>(response);
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, statusCodeResult.StatusCode);
|
|
|
|
|
}
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task PostWebhook_Success(User user, FreshdeskWebhookModel model,
|
|
|
|
|
List<Organization> organizations, SutProvider<FreshdeskController> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
model.TicketContactEmail = user.Email;
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
sutProvider.GetDependency<IUserRepository>().GetByEmailAsync(user.Email).Returns(user);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetManyByUserIdAsync(user.Id).Returns(organizations);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var mockHttpMessageHandler = Substitute.ForPartsOf<MockHttpMessageHandler>();
|
|
|
|
|
var mockResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
|
|
|
|
|
mockHttpMessageHandler.Send(Arg.Any<HttpRequestMessage>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(mockResponse);
|
|
|
|
|
var httpClient = new HttpClient(mockHttpMessageHandler);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
sutProvider.GetDependency<IHttpClientFactory>().CreateClient("FreshdeskApi").Returns(httpClient);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
sutProvider.GetDependency<IOptions<BillingSettings>>().Value.FreshdeskWebhookKey.Returns(WebhookKey);
|
|
|
|
|
sutProvider.GetDependency<IOptions<BillingSettings>>().Value.FreshdeskApiKey.Returns(ApiKey);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var response = await sutProvider.Sut.PostWebhook(WebhookKey, model);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var statusCodeResult = Assert.IsAssignableFrom<StatusCodeResult>(response);
|
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, statusCodeResult.StatusCode);
|
2022-07-15 18:10:56 +02:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
_ = mockHttpMessageHandler.Received(1).Send(Arg.Is<HttpRequestMessage>(m => m.Method == HttpMethod.Put && m.RequestUri.ToString().EndsWith(model.TicketId)), Arg.Any<CancellationToken>());
|
|
|
|
|
_ = mockHttpMessageHandler.Received(1).Send(Arg.Is<HttpRequestMessage>(m => m.Method == HttpMethod.Post && m.RequestUri.ToString().EndsWith($"{model.TicketId}/notes")), Arg.Any<CancellationToken>());
|
2022-07-15 18:10:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
public class MockHttpMessageHandler : HttpMessageHandler
|
2022-07-15 18:10:56 +02:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Send(request, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Task<HttpResponseMessage> Send(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2022-07-15 18:10:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|