public async Task Can_Update_Api_Resource() { using (var ctx = await TestDbContext.Create(nameof(Can_Update_Api_Resource))) { var entity = new ApiResource() { Address = "http://abc", HttpMethod = DotNetAccessControl.domain.enums.HttpMethods.GET, Title = "Some Title" }; ctx.ApiResources.Add(entity); await ctx.SaveChangesAsync(); var mapper = new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile())); var model = new UpdateApiResourceDto( entity.Id, "http://Test", DotNetAccessControl.domain.enums.HttpMethods.POST, "Test Title" ); var request = new UpdateApiResource(model); var response = await new UpdateApiResourceHandler(ctx, mapper.CreateMapper()).Handle(request, CancellationToken.None); var toCheck = await ctx.ApiResources.AsNoTracking().SingleAsync(x => x.Id == entity.Id); Assert.Equal(model.Title, toCheck.Title); Assert.Equal(model.Address, toCheck.Address); Assert.Equal(model.HttpMethod, toCheck.HttpMethod); } }
public async Task Update_Api_Resource_Server_validator(UpdateApiResourceDto model, bool isvalid) { using (var ctx = await TestDbContext.Create(nameof(Update_Api_Resource_Server_validator))) { ApiResource entity = new ApiResource { Address = "z", HttpMethod = HttpMethods.GET, Title = "z" }; ApiResource entity2 = new ApiResource { Address = "zx", HttpMethod = HttpMethods.GET, Title = "zx" }; entity.Id = Update_Api_Resource_validatorTestData.validGuid; entity2.Id = Update_Api_Resource_validatorTestData.validGuid2; ctx.ApiResources.Add(entity); ctx.ApiResources.Add(entity2); await ctx.SaveChangesAsync(); var vaidator = new ServerSideValidator(ctx); var validation = vaidator.Validate(new UpdateApiResource(model)); _output.WriteLine(string.Join(",\n", validation.Errors.Select(x => x.ErrorMessage))); Assert.Equal(isvalid, validation.IsValid); } }
public async Task <ActionResult> UpdateApiResource([FromBody] UpdateApiResourceDto apiResource) { var currentIdentityResource = await this._configurationManagementService.ReturnApiResourceAsync(apiResource.OriginalName); if (currentIdentityResource == null) { return(NotFound()); } await this._configurationManagementService.UpdateApiResourceAsync(apiResource); return(NoContent()); }
public async Task UpdateApiResourceAsync(UpdateApiResourceDto resource) { DateTime now = DateTime.Now; var currentIdentityResource = await this._configContext.ApiResources.Include(p => p.Properties).FirstOrDefaultAsync(a => a.Name == resource.OriginalName); currentIdentityResource.Name = resource.Name; currentIdentityResource.Updated = now; currentIdentityResource.Description = resource.Description; currentIdentityResource.DisplayName = resource.DisplayName; currentIdentityResource.Enabled = resource.Enabled; if (resource.Properties != null) { foreach (var property in resource.Properties) { IdentityServer4.EntityFramework.Entities.ApiResourceProperty foundItem = null; if (currentIdentityResource.Properties != null) { foundItem = currentIdentityResource.Properties.FirstOrDefault(a => a.Key == property.Key && a.ApiResourceId == currentIdentityResource.Id); } else { currentIdentityResource.Properties = new List <IdentityServer4.EntityFramework.Entities.ApiResourceProperty>(); } if (foundItem == null) { currentIdentityResource.Properties.Add(new IdentityServer4.EntityFramework.Entities.ApiResourceProperty { Key = property.Key, Value = property.Value, ApiResourceId = currentIdentityResource.Id }); } else { foundItem.Value = property.Value; } } ; } this._configContext.ApiResources.Update(currentIdentityResource); await this._configContext.SaveChangesAsync(); }