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, BitAutoData] public async Task LogGroupEvent_LogsRequiredInfo(Group group, EventType eventType, DateTime date, Guid actingUserId, Guid providerId, DeviceType deviceType, SutProvider sutProvider) { var orgAbilities = new Dictionary() { { group.OrganizationId, new OrganizationAbility() { UseEvents = true, Enabled = true } } }; sutProvider.GetDependency().GetOrganizationAbilitiesAsync().Returns(orgAbilities); sutProvider.GetDependency().UserId.Returns(actingUserId); sutProvider.GetDependency().DeviceType.Returns(deviceType); sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); await sutProvider.Sut.LogGroupEventAsync(group, eventType, date); await sutProvider.GetDependency().Received(1).CreateAsync(Arg.Is(e => e.OrganizationId == group.OrganizationId && e.GroupId == group.Id && e.Type == eventType && e.ActingUserId == actingUserId && e.ProviderId == providerId && e.Date == date && e.SystemUser == null)); } [Theory, BitAutoData] public async Task LogGroupEvent_WithEventSystemUser_LogsRequiredInfo(Group group, EventType eventType, EventSystemUser eventSystemUser, DateTime date, Guid actingUserId, Guid providerId, DeviceType deviceType, SutProvider sutProvider) { var orgAbilities = new Dictionary() { { group.OrganizationId, new OrganizationAbility() { UseEvents = true, Enabled = true } } }; sutProvider.GetDependency().GetOrganizationAbilitiesAsync().Returns(orgAbilities); sutProvider.GetDependency().UserId.Returns(actingUserId); sutProvider.GetDependency().DeviceType.Returns(deviceType); sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); await sutProvider.Sut.LogGroupEventAsync(group, eventType, eventSystemUser, date); await sutProvider.GetDependency().Received(1).CreateAsync(Arg.Is(e => e.OrganizationId == group.OrganizationId && e.GroupId == group.Id && e.Type == eventType && e.ActingUserId == actingUserId && e.ProviderId == providerId && e.Date == date && e.SystemUser == eventSystemUser)); } [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 LogOrganizationUserEvent_WithEventSystemUser_LogsRequiredInfo(OrganizationUser orgUser, EventType eventType, EventSystemUser eventSystemUser, 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().ProviderIdForOrg(Arg.Any()).Returns(providerId); sutProvider.GetDependency().DeviceType.Returns(deviceType); await sutProvider.Sut.LogOrganizationUserEventAsync(orgUser, eventType, eventSystemUser, 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, SystemUser = eventSystemUser } }; 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" }))); } }