mirror of
https://github.com/bitwarden/server
synced 2025-12-16 08:13:33 +00:00
[PM-20980] Add a note to freshdesk ticket when no user is found (#5768)
This commit is contained in:
@@ -63,6 +63,12 @@ public class FreshdeskController : Controller
|
|||||||
note += $"<li>Region: {_billingSettings.FreshDesk.Region}</li>";
|
note += $"<li>Region: {_billingSettings.FreshDesk.Region}</li>";
|
||||||
var customFields = new Dictionary<string, object>();
|
var customFields = new Dictionary<string, object>();
|
||||||
var user = await _userRepository.GetByEmailAsync(ticketContactEmail);
|
var user = await _userRepository.GetByEmailAsync(ticketContactEmail);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
note += $"<li>No user found: {ticketContactEmail}</li>";
|
||||||
|
await CreateNote(ticketId, note);
|
||||||
|
}
|
||||||
|
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
var userLink = $"{_globalSettings.BaseServiceUri.Admin}/users/edit/{user.Id}";
|
var userLink = $"{_globalSettings.BaseServiceUri.Admin}/users/edit/{user.Id}";
|
||||||
@@ -121,18 +127,7 @@ public class FreshdeskController : Controller
|
|||||||
Content = JsonContent.Create(updateBody),
|
Content = JsonContent.Create(updateBody),
|
||||||
};
|
};
|
||||||
await CallFreshdeskApiAsync(updateRequest);
|
await CallFreshdeskApiAsync(updateRequest);
|
||||||
|
await CreateNote(ticketId, note);
|
||||||
var noteBody = new Dictionary<string, object>
|
|
||||||
{
|
|
||||||
{ "body", $"<ul>{note}</ul>" },
|
|
||||||
{ "private", true }
|
|
||||||
};
|
|
||||||
var noteRequest = new HttpRequestMessage(HttpMethod.Post,
|
|
||||||
string.Format("https://bitwarden.freshdesk.com/api/v2/tickets/{0}/notes", ticketId))
|
|
||||||
{
|
|
||||||
Content = JsonContent.Create(noteBody),
|
|
||||||
};
|
|
||||||
await CallFreshdeskApiAsync(noteRequest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkResult();
|
return new OkResult();
|
||||||
@@ -208,6 +203,21 @@ public class FreshdeskController : Controller
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task CreateNote(string ticketId, string note)
|
||||||
|
{
|
||||||
|
var noteBody = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "body", $"<ul>{note}</ul>" },
|
||||||
|
{ "private", true }
|
||||||
|
};
|
||||||
|
var noteRequest = new HttpRequestMessage(HttpMethod.Post,
|
||||||
|
string.Format("https://bitwarden.freshdesk.com/api/v2/tickets/{0}/notes", ticketId))
|
||||||
|
{
|
||||||
|
Content = JsonContent.Create(noteBody),
|
||||||
|
};
|
||||||
|
await CallFreshdeskApiAsync(noteRequest);
|
||||||
|
}
|
||||||
|
|
||||||
private async Task AddAnswerNoteToTicketAsync(string note, string ticketId)
|
private async Task AddAnswerNoteToTicketAsync(string note, string ticketId)
|
||||||
{
|
{
|
||||||
// if there is no content, then we don't need to add a note
|
// if there is no content, then we don't need to add a note
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
|
using NSubstitute.ReceivedExtensions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Bit.Billing.Test.Controllers;
|
namespace Bit.Billing.Test.Controllers;
|
||||||
@@ -71,6 +72,41 @@ public class FreshdeskControllerTests
|
|||||||
_ = mockHttpMessageHandler.Received(1).Send(Arg.Is<HttpRequestMessage>(m => m.Method == HttpMethod.Post && m.RequestUri.ToString().EndsWith($"{model.TicketId}/notes")), 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>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData(WebhookKey)]
|
||||||
|
public async Task PostWebhook_add_note_when_user_is_invalid(
|
||||||
|
string freshdeskWebhookKey, FreshdeskWebhookModel model,
|
||||||
|
SutProvider<FreshdeskController> sutProvider)
|
||||||
|
{
|
||||||
|
// Arrange - for an invalid user
|
||||||
|
model.TicketContactEmail = "invalid@user";
|
||||||
|
sutProvider.GetDependency<IUserRepository>().GetByEmailAsync(model.TicketContactEmail).Returns((User)null);
|
||||||
|
sutProvider.GetDependency<IOptions<BillingSettings>>().Value.FreshDesk.WebhookKey.Returns(WebhookKey);
|
||||||
|
|
||||||
|
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);
|
||||||
|
sutProvider.GetDependency<IHttpClientFactory>().CreateClient("FreshdeskApi").Returns(httpClient);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await sutProvider.Sut.PostWebhook(freshdeskWebhookKey, model);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var statusCodeResult = Assert.IsAssignableFrom<StatusCodeResult>(response);
|
||||||
|
Assert.Equal(StatusCodes.Status200OK, statusCodeResult.StatusCode);
|
||||||
|
|
||||||
|
await mockHttpMessageHandler
|
||||||
|
.Received(1).Send(
|
||||||
|
Arg.Is<HttpRequestMessage>(
|
||||||
|
m => m.Method == HttpMethod.Post
|
||||||
|
&& m.RequestUri.ToString().EndsWith($"{model.TicketId}/notes")
|
||||||
|
&& m.Content.ReadAsStringAsync().Result.Contains("No user found")),
|
||||||
|
Arg.Any<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[BitAutoData((string)null, null)]
|
[BitAutoData((string)null, null)]
|
||||||
[BitAutoData((string)null)]
|
[BitAutoData((string)null)]
|
||||||
|
|||||||
Reference in New Issue
Block a user