public static void UserPermissionMappingsCreating( ILogger logger, ulong userId, IEnumerable <int> permissionIds, PermissionMappingType mappingType) => _userPermissionMappingsCreating.Invoke( logger, userId, permissionIds, mappingType);
public static void UserPermissionMappingCreating( ILogger logger, ulong userId, int permissionId, PermissionMappingType type, long actionId) => _userPermissionMappingCreating.Invoke( logger, userId, permissionId, type, actionId);
public async Task <IReadOnlyList <long> > CreatePermissionMappingsAsync( ulong userId, IEnumerable <int> permissionIds, PermissionMappingType type, long actionId, CancellationToken cancellationToken) { UsersLogMessages.UserPermissionMappingsCreating(_logger, userId, permissionIds, type, actionId); var entities = permissionIds .Do(permissionId => UsersLogMessages.UserPermissionMappingCreating(_logger, userId, permissionId, type, actionId)) .Select(permissionId => new UserPermissionMappingEntity( id: default,
public async Task CreatePermissionMappingsAsync_Always_AddsNewEntities( ulong userId, IReadOnlyList <int> permissionIds, PermissionMappingType type, long actionId, IReadOnlyList <long> mappingIds) { using var testContext = TestContext.CreateReadOnly(); testContext.MockContext .Setup(x => x.AddRangeAsync(It.IsAny <IEnumerable <object> >(), It.IsAny <CancellationToken>())) .Callback <IEnumerable <object>, CancellationToken>((x, y) => { if (x?.Any() == true) { foreach ((var entity, var i) in x.Select((entity, i) => (entity, i))) { if (!(entity is null) && (entity is UserPermissionMappingEntity mapping) && (i < mappingIds.Count)) { mapping.Id = mappingIds[i]; } } } }); var uut = testContext.BuildUut(); var result = await uut.CreatePermissionMappingsAsync( userId, permissionIds, type, actionId, testContext.CancellationToken); testContext.MockContext .ShouldHaveReceived(x => x.AddRangeAsync(It.IsNotNull <IEnumerable <object> >(), testContext.CancellationToken)); testContext.MockContext .ShouldHaveReceived(x => x.SaveChangesAsync(testContext.CancellationToken)); var entities = testContext.MockContext .Invocations .Where(x => x.Method.Name == nameof(YastahDbContext.AddRangeAsync)) .Select(x => (IEnumerable <object>)x.Arguments[0]) .First() .ToArray(); entities.Length.ShouldBe(mappingIds.Count); foreach (var entity in entities) { entity.ShouldBeOfType <UserPermissionMappingEntity>(); var mapping = (UserPermissionMappingEntity)entity; mapping.UserId.ShouldBe(userId); mapping.IsDenied.ShouldBe(type == PermissionMappingType.Denied); mapping.CreationId.ShouldBe(actionId); mapping.DeletionId.ShouldBeNull(); } var mappings = entities.Cast <UserPermissionMappingEntity>(); mappings.Select(x => x.PermissionId).ShouldBeSetEqualTo(permissionIds); result.ShouldBeSetEqualTo(mappingIds); }