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; using Bit.Test.Common.Helpers; using NSubstitute; using Xunit; namespace Bit.Core.Test.Services; [SutProviderCustomize] public class EventServiceTests { public static IEnumerable InstallationIdTestCases => TestCaseHelper.GetCombinationsOfMultipleLists( new object[] { Guid.NewGuid(), null }, Enum.GetValues().Select(e => (object)e) ).Select(p => p.ToArray()); [Theory] [BitMemberAutoData(nameof(InstallationIdTestCases))] public async Task LogOrganizationEvent_ProvidesInstallationId(Guid? installationId, EventType eventType, Organization organization, SutProvider sutProvider) { organization.Enabled = true; organization.UseEvents = true; sutProvider.GetDependency().InstallationId.Returns(installationId); await sutProvider.Sut.LogOrganizationEventAsync(organization, eventType); await sutProvider.GetDependency().Received(1).CreateAsync(Arg.Is(e => e.OrganizationId == organization.Id && 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" }))); } }