diff --git a/src/Core/Services/Implementations/EventService.cs b/src/Core/Services/Implementations/EventService.cs index 39b2fdfd5..3a35555d1 100644 --- a/src/Core/Services/Implementations/EventService.cs +++ b/src/Core/Services/Implementations/EventService.cs @@ -210,7 +210,7 @@ namespace Bit.Core.Services continue; } - eventMessages.Add(new EventMessage + eventMessages.Add(new EventMessage(_currentContext) { OrganizationId = organizationUser.OrganizationId, UserId = organizationUser.UserId, @@ -259,7 +259,7 @@ namespace Bit.Core.Services { continue; } - eventMessages.Add(new EventMessage + eventMessages.Add(new EventMessage(_currentContext) { ProviderId = providerUser.ProviderId, UserId = providerUser.UserId, diff --git a/test/Common/Helpers/AssertHelper.cs b/test/Common/Helpers/AssertHelper.cs index 75ea84b11..7239cb6db 100644 --- a/test/Common/Helpers/AssertHelper.cs +++ b/test/Common/Helpers/AssertHelper.cs @@ -64,6 +64,28 @@ namespace Bit.Test.Common.Helpers public static Expression> AssertPropertyEqual(T expected, params string[] excludedPropertyStrings) => (T actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual); + private static Predicate> AssertPropertyEqualPredicate(IEnumerable expected, params string[] excludedPropertyStrings) => (actual) => + { + // IEnumerable.Zip doesn't account for different lengths, we need to check this ourselves + if (actual.Count() != expected.Count()) + { + throw new Exception(string.Concat($"Actual IEnumerable does not have the expected length.\n", + $"Expected: {expected.Count()}\n", + $"Actual: {actual.Count()}")); + } + + var elements = expected.Zip(actual); + foreach (var (expectedEl, actualEl) in elements) + { + AssertPropertyEqual(expectedEl, actualEl, excludedPropertyStrings); + } + + return true; + }; + + public static Expression>> AssertPropertyEqual(IEnumerable expected, params string[] excludedPropertyStrings) => + (actual) => AssertPropertyEqualPredicate(expected, excludedPropertyStrings)(actual); + private static Predicate AssertEqualExpectedPredicate(T expected) => (actual) => { Assert.Equal(expected, actual); diff --git a/test/Core.Test/Services/EventServiceTests.cs b/test/Core.Test/Services/EventServiceTests.cs index d8e09d4f0..988d84e13 100644 --- a/test/Core.Test/Services/EventServiceTests.cs +++ b/test/Core.Test/Services/EventServiceTests.cs @@ -1,7 +1,9 @@ using Bit.Core.Context; using Bit.Core.Entities; +using Bit.Core.Entities.Provider; using Bit.Core.Enums; using Bit.Core.Models.Data; +using Bit.Core.Models.Data.Organizations; using Bit.Core.Services; using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture.Attributes; @@ -36,5 +38,72 @@ namespace Bit.Core.Test.Services e.Type == eventType && e.InstallationId == installationId)); } + + [Theory, BitAutoData] + public async Task LogOrganizationUserEvent_LogsRequiredInfo(OrganizationUser orgUser, EventType eventType, DateTime date, + Guid actingUserId, Guid providerId, string ipAddress, DeviceType deviceType, SutProvider sutProvider) + { + var orgAbilities = new Dictionary() + { + {orgUser.OrganizationId, new OrganizationAbility() { UseEvents = true, Enabled = true } } + }; + sutProvider.GetDependency().GetOrganizationAbilitiesAsync().Returns(orgAbilities); + sutProvider.GetDependency().UserId.Returns(actingUserId); + sutProvider.GetDependency().IpAddress.Returns(ipAddress); + sutProvider.GetDependency().DeviceType.Returns(deviceType); + sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); + + await sutProvider.Sut.LogOrganizationUserEventAsync(orgUser, eventType, date); + + var expected = new List() { + new EventMessage() + { + IpAddress = ipAddress, + DeviceType = deviceType, + OrganizationId = orgUser.OrganizationId, + UserId = orgUser.UserId, + OrganizationUserId = orgUser.Id, + ProviderId = providerId, + Type = eventType, + ActingUserId = actingUserId, + Date = date + } + }; + + await sutProvider.GetDependency().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expected, new[] { "IdempotencyId" }))); + } + + [Theory, BitAutoData] + public async Task LogProviderUserEvent_LogsRequiredInfo(ProviderUser providerUser, EventType eventType, DateTime date, + Guid actingUserId, Guid providerId, string ipAddress, DeviceType deviceType, SutProvider sutProvider) + { + var providerAbilities = new Dictionary() + { + {providerUser.ProviderId, new ProviderAbility() { UseEvents = true, Enabled = true } } + }; + sutProvider.GetDependency().GetProviderAbilitiesAsync().Returns(providerAbilities); + sutProvider.GetDependency().UserId.Returns(actingUserId); + sutProvider.GetDependency().IpAddress.Returns(ipAddress); + sutProvider.GetDependency().DeviceType.Returns(deviceType); + sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); + + await sutProvider.Sut.LogProviderUserEventAsync(providerUser, eventType, date); + + var expected = new List() { + new EventMessage() + { + IpAddress = ipAddress, + DeviceType = deviceType, + ProviderId = providerUser.ProviderId, + UserId = providerUser.UserId, + ProviderUserId = providerUser.Id, + Type = eventType, + ActingUserId = actingUserId, + Date = date + } + }; + + await sutProvider.GetDependency().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expected, new[] { "IdempotencyId" }))); + } } }