Skip to content

Commit

Permalink
Add EntityMappingRecordManager ref #11 closes#22
Browse files Browse the repository at this point in the history
  • Loading branch information
saturn72 committed Oct 23, 2020
1 parent f768ca7 commit 0a701b1
Show file tree
Hide file tree
Showing 30 changed files with 800 additions and 555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task GetPermittedIds_HasUserPermissions_ReturnsEmpty(UserPermission
{
var pm = new Mock<IPermissionManager>();
pm.Setup(p => p.GetUserPermissions(It.IsAny<string>())).ReturnsAsync(userPermissions);
var res = await PermissionManagerExtensions.GetPermittedIds(pm.Object, "uId", "ek", "pk");
var res = await PermissionManagerExtensions.GetPermittedEntitiesIds(pm.Object, "uId", "ek", "pk");
res.ShouldBeEmpty();
}
public static IEnumerable<object[]> GetPermittedIds_HasUserPermissions_ReturnsEmpty_DATA => new[]
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task GetPermittedIds_NotMatch_AllConditions_ReturnsEmpty()
};
var pm = new Mock<IPermissionManager>();
pm.Setup(p => p.GetUserPermissions(It.IsAny<string>())).ReturnsAsync(up);
var res = await PermissionManagerExtensions.GetPermittedIds(pm.Object, "some-user-id", "ek", "pk");
var res = await PermissionManagerExtensions.GetPermittedEntitiesIds(pm.Object, "some-user-id", "ek", "pk");
res.ShouldBeEmpty();
}
[Fact]
Expand All @@ -72,7 +72,7 @@ public async Task GetPermittedIds_ReturnsEntityIds()
};
var pm = new Mock<IPermissionManager>();
pm.Setup(p => p.GetUserPermissions(It.IsAny<string>())).ReturnsAsync(up);
var res = await PermissionManagerExtensions.GetPermittedIds(pm.Object, "some-user-id", "ek", "pk");
var res = await PermissionManagerExtensions.GetPermittedEntitiesIds(pm.Object, "some-user-id", "ek", "pk");
res.Count().ShouldBe(2);
res.Contains(eId1);
res.Contains(eId2);
Expand Down
2 changes: 1 addition & 1 deletion src/AnyService.Core/DomainEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface IDbModel<TId>
{
TId Id { get; set; }
}
public sealed class EntityMapping : IEntity
public sealed class EntityMappingRecord : IEntity
{
public string Id { get; set; }
public string ParentEntityName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static async Task<bool> UserHasPermissionOnEntity(this IPermissionManager
&& p.EntityKey.Equals(entityKey, StringComparison.InvariantCultureIgnoreCase));
return !hasPermission?.Excluded ?? false;
}
public static async Task<IEnumerable<string>> GetPermittedIds(this IPermissionManager manager, string userId, string entityKey, string permissionKey)
public static async Task<IEnumerable<string>> GetPermittedEntitiesIds(this IPermissionManager manager, string userId, string entityKey, string permissionKey)
{
var ups = await manager.GetUserPermissions(userId);
return ups?.EntityPermissions?
Expand Down
2 changes: 1 addition & 1 deletion src/AnyService.SampleApp/SampleAppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public SampleAppDbContext(DbContextOptions<SampleAppDbContext> options) : base(o

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityMapping>(b => b.Property(u => u.Id).ValueGeneratedOnAdd());
modelBuilder.Entity<EntityMappingRecord>(b => b.Property(u => u.Id).ValueGeneratedOnAdd());
modelBuilder.Entity<UserPermissions>(b => b.Property(u => u.Id).ValueGeneratedOnAdd());
modelBuilder.Entity<EntityPermission>(b =>
{
Expand Down
74 changes: 57 additions & 17 deletions src/AnyService.Tests/Controllers/GenericControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AnyService.Controllers;
using AnyService.Models;
using AnyService.Services;
using AnyService.Services.EntityMapping;
using AnyService.Services.ServiceResponseMappers;
using AnyService.Tests.Services.ServiceResponseMappers;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -24,7 +27,8 @@ public class GenericControllerTests : MappingTest
[InlineData(nameof(GenericController<MyClass, MyClass>.GetAll), "GET", null)]
[InlineData(nameof(GenericController<MyClass, MyClass>.GetById), "GET", "{id}")]
[InlineData(nameof(GenericController<MyClass, MyClass>.Put), "PUT", "{id}")]
[InlineData(nameof(GenericController<MyClass, MyClass>.UpdateEntityMappings), "PUT", "__map/{id}")]

[InlineData(nameof(EntityMappingRecordController.UpdateEntityMappings), "PUT", "__map/{id}")]
public void ValidateVerbs(string methodName, string expHttpVerb, string expTemplate)
{
var type = typeof(GenericController<,>);
Expand Down Expand Up @@ -104,29 +108,65 @@ public async Task Create_ReturnsServiceResponse()
js.Value.ShouldBe(expData);
}
#endregion
}
public class EntityMappingControllerTests
{
public class MyClass : IEntity
{
public string Id { get; set; }
}
#region UpdateEntityMappings
[Fact]
public async Task UpdateEntityMappings_InvalidModelState()
public async Task UpdateEntityMappings_InvalidModelState_ReturnsBadRequest()
{
var wc = new WorkContext
var ecrs = new EntityConfigRecord[] { };
var log = new Mock<ILogger<EntityMappingRecordController>>();
var ctrl = new EntityMappingRecordController(null, ecrs, null, log.Object);
ctrl.ModelState.AddModelError("k", "err");
var m = new EntityMappingRequestModel { ParentEntityKey = "exists", ChildEntityKey = "exists" };
var res = await ctrl.UpdateEntityMappings("id", m);
res.ShouldBeOfType<BadRequestResult>();
}
[Theory]
[MemberData(nameof(UpdateEntityMappings_InvalidRequest_ReturnsBadRequest_DATA))]
public async Task UpdateEntityMappings_InvalidRequest_ReturnsBadRequest(EntityMappingRequestModel m)
{
var ecrs = new[]
{
CurrentEndpointSettings = new EndpointSettings
{
MapToType = typeof(MyClass),
MapToPaginationType = typeof(Pagination<MyClass>),
EntityConfigRecord = new EntityConfigRecord
{
Type = typeof(MyClass),
Name = typeof(MyClass).Name,
}
}
new EntityConfigRecord{Name = "exists", ExternalName="exist"}
};
var log = new Mock<ILogger<GenericController<MyClass, MyClass>>>();
var ctrl = new GenericController<MyClass, MyClass>(null, null, null, wc, log.Object);
ctrl.ModelState.AddModelError("k", "err");
var res = await ctrl.UpdateEntityMappings("id", null);
var log = new Mock<ILogger<EntityMappingRecordController>>();
var ctrl = new EntityMappingRecordController(null, ecrs, null, log.Object);
var res = await ctrl.UpdateEntityMappings("id", m);
res.ShouldBeOfType<BadRequestResult>();
}
public static IEnumerable<object[]> UpdateEntityMappings_InvalidRequest_ReturnsBadRequest_DATA => new[]
{
new object[]{new EntityMappingRequestModel { ParentEntityKey = "not-exists", ChildEntityKey = "Exists" } },
new object[]{new EntityMappingRequestModel { ParentEntityKey = "exists", ChildEntityKey = "not-exists"} },
};
[Fact]
public async Task UpdateEntityMappings_ReturnsserviceResponse()
{
var ecrs = new[]
{
new EntityConfigRecord{Name = "e1", ExternalName="e1"},
new EntityConfigRecord{Name = "e2", ExternalName="e2"},
};
var mgr = new Mock<IEntityMappingRecordManager>();
mgr.Setup(m => m.UpdateMapping(It.IsAny<EntityMappingRequest>())).ReturnsAsync(new ServiceResponse<EntityMappingRequest> { Result = ServiceResult.Accepted });

var srm = new Mock<IServiceResponseMapper>();

var log = new Mock<ILogger<EntityMappingRecordController>>();
var ctrl = new EntityMappingRecordController(mgr.Object, ecrs, srm.Object, log.Object);

var model = new EntityMappingRequestModel { ParentEntityKey = "e1", ChildEntityKey = "e2" };
await ctrl.UpdateEntityMappings("id", model);

srm.Verify(s => s.MapServiceResponse(It.Is<ServiceResponse<EntityMappingRequest>>(sr => sr.Result == ServiceResult.Accepted)), Times.Once);
}
#endregion

}
}
3 changes: 1 addition & 2 deletions src/AnyService.Tests/EntityConfigRecordExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoMapper.Configuration.Annotations;
using Shouldly;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Loading

0 comments on commit 0a701b1

Please sign in to comment.