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, string ipAddress, 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().IpAddress.Returns(ipAddress); sutProvider.GetDependency().DeviceType.Returns(deviceType); sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); await sutProvider.Sut.LogGroupEventAsync(group, eventType, date); var expected = new List() { new EventMessage() { IpAddress = ipAddress, DeviceType = deviceType, OrganizationId = group.OrganizationId, GroupId = group.Id, Type = eventType, ActingUserId = actingUserId, ProviderId = providerId, Date = date, SystemUser = null } }; await sutProvider.GetDependency().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expected, new[] { "IdempotencyId" }))); } [Theory, BitAutoData] public async Task LogGroupEvent_WithEventSystemUser_LogsRequiredInfo(Group group, EventType eventType, EventSystemUser eventSystemUser, DateTime date, Guid actingUserId, Guid providerId, string ipAddress, 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().IpAddress.Returns(ipAddress); sutProvider.GetDependency().DeviceType.Returns(deviceType); sutProvider.GetDependency().ProviderIdForOrg(Arg.Any()).Returns(providerId); await sutProvider.Sut.LogGroupEventAsync(group, eventType, eventSystemUser, date); var eventMessage = new EventMessage() { IpAddress = ipAddress, DeviceType = deviceType, OrganizationId = group.OrganizationId, GroupId = group.Id, Type = eventType, ActingUserId = actingUserId, ProviderId = providerId, Date = date, SystemUser = eventSystemUser }; if (eventSystemUser is EventSystemUser.SCIM) { eventMessage.DeviceType = DeviceType.Server; } var expected = new List() { eventMessage }; await sutProvider.GetDependency().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expected, new[] { "IdempotencyId" }))); } [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, 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); await sutProvider.Sut.LogOrganizationUserEventAsync(orgUser, eventType, eventSystemUser, date); var expected = new List() { new EventMessage() { IpAddress = ipAddress, DeviceType = DeviceType.Server, 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" }))); } [Theory, BitAutoData] public async Task LogProviderOrganizationEventsAsync_LogsRequiredInfo(Provider provider, ICollection providerOrganizations, EventType eventType, DateTime date, Guid actingUserId, Guid providerId, string ipAddress, DeviceType deviceType, SutProvider sutProvider) { foreach (var providerOrganization in providerOrganizations) { providerOrganization.ProviderId = provider.Id; } var providerAbilities = new Dictionary() { { provider.Id, 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.LogProviderOrganizationEventsAsync(providerOrganizations.Select(po => (po, eventType, (DateTime?)date))); var expected = providerOrganizations.Select(po => new EventMessage() { DeviceType = deviceType, IpAddress = ipAddress, ProviderId = provider.Id, ProviderOrganizationId = po.Id, Type = eventType, ActingUserId = actingUserId, Date = date }).ToList(); await sutProvider.GetDependency().Received(1).CreateManyAsync(Arg.Is(AssertHelper.AssertPropertyEqual(expected, new[] { "IdempotencyId" }))); } }