1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

Update Slack and Webhook handlers to use new Repositories

This commit is contained in:
Brant DeBow 2025-03-31 09:33:09 -04:00
parent 3803139ddb
commit 3e161144cd
No known key found for this signature in database
GPG Key ID: 94411BB25947C72B
4 changed files with 52 additions and 67 deletions

View File

@ -13,12 +13,12 @@ public class SlackEventHandler(
{
public async Task HandleEventAsync(EventMessage eventMessage)
{
var organizationId = eventMessage.OrganizationId ?? Guid.Empty;
var organizationId = eventMessage.OrganizationId ?? new Guid("f431e04c-f2c3-473c-8cd1-b291014b0236");
var configurations = await configurationRepository.GetConfigurationsAsync(organizationId, IntegrationType.Slack, eventMessage.Type);
foreach (var configuration in configurations)
{
var config = JsonSerializer.Deserialize<SlackConfiguration>(configuration.Configuration ?? string.Empty);
var config = configuration.MergedConfiguration.Deserialize<SlackConfiguration>();
if (config is null)
{
continue;

View File

@ -20,13 +20,13 @@ public class WebhookEventHandler(
public async Task HandleEventAsync(EventMessage eventMessage)
{
var organizationId = eventMessage.OrganizationId ?? Guid.Empty;
var organizationId = eventMessage.OrganizationId ?? new Guid("f431e04c-f2c3-473c-8cd1-b291014b0236");
var configurations = await configurationRepository.GetConfigurationsAsync(organizationId,
IntegrationType.Webhook, eventMessage.Type);
foreach (var configuration in configurations)
{
var config = JsonSerializer.Deserialize<WebhookConfiguration>(configuration.Configuration ?? string.Empty);
var config = configuration.MergedConfiguration.Deserialize<WebhookConfiguration>();
if (config is null)
{
continue;

View File

@ -1,6 +1,7 @@
using Bit.Core.Enums;
using System.Text.Json;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Models.Data.Integrations;
using Bit.Core.Models.Data.Organizations;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture;
@ -22,9 +23,9 @@ public class SlackEventHandlerTests
private readonly string _token2 = "xoxb-another-test-token";
private SutProvider<SlackEventHandler> GetSutProvider(
List<IntegrationConfiguration<SlackConfiguration>> integrationConfigurations)
List<OrganizationIntegrationConfigurationDetails> integrationConfigurations)
{
_repository.GetConfigurationsAsync<SlackConfiguration>(Arg.Any<Guid>(),
_repository.GetConfigurationsAsync(Arg.Any<Guid>(),
IntegrationType.Slack, Arg.Any<EventType>())
.Returns(integrationConfigurations);
@ -34,39 +35,33 @@ public class SlackEventHandlerTests
.Create();
}
private List<IntegrationConfiguration<SlackConfiguration>> NoConfigurations()
private List<OrganizationIntegrationConfigurationDetails> NoConfigurations()
{
return [];
}
private List<IntegrationConfiguration<SlackConfiguration>> OneConfiguration()
private List<OrganizationIntegrationConfigurationDetails> OneConfiguration()
{
return
[
new IntegrationConfiguration<SlackConfiguration>
{
Configuration = new SlackConfiguration(channelId: _channelId, token: _token),
Template = "Date: #Date#, Type: #Type#, UserId: #UserId#"
}
];
var config = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config.Configuration = JsonSerializer.Serialize(new { token = _token });
config.IntegrationConfiguration = JsonSerializer.Serialize(new { channelId = _channelId });
config.Template = "Date: #Date#, Type: #Type#, UserId: #UserId#";
return [config];
}
private List<IntegrationConfiguration<SlackConfiguration>> TwoConfigurations()
private List<OrganizationIntegrationConfigurationDetails> TwoConfigurations()
{
return
[
new IntegrationConfiguration<SlackConfiguration>
{
Configuration = new SlackConfiguration(channelId: _channelId, token: _token),
Template = "Date: #Date#, Type: #Type#, UserId: #UserId#"
},
var config = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config.Configuration = JsonSerializer.Serialize(new { token = _token });
config.IntegrationConfiguration = JsonSerializer.Serialize(new { channelId = _channelId });
config.Template = "Date: #Date#, Type: #Type#, UserId: #UserId#";
var config2 = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config2.Configuration = JsonSerializer.Serialize(new { token = _token2 });
config2.IntegrationConfiguration = JsonSerializer.Serialize(new { channelId = _channelId2 });
config2.Template = "Date: #Date#, Type: #Type#, UserId: #UserId#";
new IntegrationConfiguration<SlackConfiguration>
{
Configuration = new SlackConfiguration(channelId: _channelId2, token: _token2),
Template = "Date: #Date#, Type: #Type#, UserId: #UserId#"
}
];
return [config, config2];
}
[Theory, BitAutoData]
@ -124,7 +119,6 @@ public class SlackEventHandlerTests
Assert.Equal(eventMessages.Count, received.Count());
var index = 0;
foreach (var eventMessage in eventMessages)
{
Assert.True(calls.MoveNext());
@ -133,8 +127,6 @@ public class SlackEventHandlerTests
Assert.Equal($"Date: {eventMessage.Date}, Type: {eventMessage.Type}, UserId: {eventMessage.UserId}",
arguments[1] as string);
Assert.Equal(_channelId, arguments[2] as string);
index++;
}
}
@ -146,11 +138,10 @@ public class SlackEventHandlerTests
await sutProvider.Sut.HandleManyEventsAsync(eventMessages);
var received = sutProvider.GetDependency<ISlackService>().ReceivedCalls();
var calls = received.GetEnumerator();
using var calls = received.GetEnumerator();
Assert.Equal(eventMessages.Count * 2, received.Count());
var index = 0;
foreach (var eventMessage in eventMessages)
{
Assert.True(calls.MoveNext());
@ -166,8 +157,6 @@ public class SlackEventHandlerTests
Assert.Equal($"Date: {eventMessage.Date}, Type: {eventMessage.Type}, UserId: {eventMessage.UserId}",
arguments2[1] as string);
Assert.Equal(_channelId2, arguments2[2] as string);
index++;
}
}
}

View File

@ -1,8 +1,9 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Models.Data.Integrations;
using Bit.Core.Models.Data.Organizations;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture;
@ -41,13 +42,13 @@ public class WebhookEventHandlerTests
}
private SutProvider<WebhookEventHandler> GetSutProvider(
List<IntegrationConfiguration<WebhookConfiguration>> configurations)
List<OrganizationIntegrationConfigurationDetails> configurations)
{
var clientFactory = Substitute.For<IHttpClientFactory>();
clientFactory.CreateClient(WebhookEventHandler.HttpClientName).Returns(_httpClient);
var repository = Substitute.For<IOrganizationIntegrationConfigurationRepository>();
repository.GetConfigurationsAsync<WebhookConfiguration>(Arg.Any<Guid>(),
repository.GetConfigurationsAsync(Arg.Any<Guid>(),
IntegrationType.Webhook, Arg.Any<EventType>()).Returns(configurations);
return new SutProvider<WebhookEventHandler>()
@ -56,38 +57,33 @@ public class WebhookEventHandlerTests
.Create();
}
List<IntegrationConfiguration<WebhookConfiguration>> NoConfigurations()
List<OrganizationIntegrationConfigurationDetails> NoConfigurations()
{
return new List<IntegrationConfiguration<WebhookConfiguration>>();
return [];
}
List<IntegrationConfiguration<WebhookConfiguration>> OneConfiguration()
List<OrganizationIntegrationConfigurationDetails> OneConfiguration()
{
return new List<IntegrationConfiguration<WebhookConfiguration>>
{
new IntegrationConfiguration<WebhookConfiguration>
{
Configuration = new WebhookConfiguration { Url = _webhookUrl },
Template = _template
}
};
var config = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config.Configuration = null;
config.IntegrationConfiguration = JsonSerializer.Serialize(new { url = _webhookUrl });
config.Template = _template;
return [config];
}
List<IntegrationConfiguration<WebhookConfiguration>> TwoConfigurations()
List<OrganizationIntegrationConfigurationDetails> TwoConfigurations()
{
return new List<IntegrationConfiguration<WebhookConfiguration>>
{
new IntegrationConfiguration<WebhookConfiguration>
{
Configuration = new WebhookConfiguration { Url = _webhookUrl },
Template = _template
},
new IntegrationConfiguration<WebhookConfiguration>
{
Configuration = new WebhookConfiguration { Url = _webhookUrl2 },
Template = _template
}
};
var config = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config.Configuration = null;
config.IntegrationConfiguration = JsonSerializer.Serialize(new { url = _webhookUrl });
config.Template = _template;
var config2 = Substitute.For<OrganizationIntegrationConfigurationDetails>();
config2.Configuration = null;
config2.IntegrationConfiguration = JsonSerializer.Serialize(new { url = _webhookUrl2 });
config2.Template = _template;
return [config, config2];
}
[Theory, BitAutoData]