public async Task Put_Should_Update_Ressource_Server() { RessourceServer toUpdateRessourceServer = null; using (var context = new DaOAuthContext(_dbContextOptions)) { toUpdateRessourceServer = context.RessourceServers.Where(c => c.Id.Equals(_validRessourceServer.Id)).FirstOrDefault(); } Assert.IsNotNull(toUpdateRessourceServer); var updateRessourceServerDto = new UpdateRessourceServerDto() { Id = _validRessourceServer.Id, Description = "new description", IsValid = true, Name = "new name", Scopes = new List <UpdateRessourceServerScopesDto>() { new UpdateRessourceServerScopesDto() { IdScope = _scope1.Id, IsReadWrite = false, NiceWording = "new nice wording" }, new UpdateRessourceServerScopesDto() { IdScope = null, IsReadWrite = true, NiceWording = "new scope" } } }; var httpResponseMessage = await _client.PutAsJsonAsync("ressourcesServers", updateRessourceServerDto); Assert.IsTrue(httpResponseMessage.IsSuccessStatusCode); RessourceServer updatedRessourceServer = null; IList <Scope> updatedScopes = null; using (var context = new DaOAuthContext(_dbContextOptions)) { updatedRessourceServer = context.RessourceServers.Where(c => c.Id.Equals(_validRessourceServer.Id)).FirstOrDefault(); updatedScopes = context.Scopes.Where(s => s.RessourceServerId.Equals(_validRessourceServer.Id)).ToList(); } Assert.IsNotNull(updatedRessourceServer); Assert.IsNotNull(updatedScopes); Assert.AreEqual(updatedRessourceServer.Id, updateRessourceServerDto.Id); Assert.AreEqual(updatedRessourceServer.Description, updateRessourceServerDto.Description); Assert.AreEqual(updatedRessourceServer.IsValid, updateRessourceServerDto.IsValid); Assert.AreEqual(updatedRessourceServer.Name, updateRessourceServerDto.Name); Assert.AreEqual(updatedScopes.Count(), updateRessourceServerDto.Scopes.Count()); Assert.IsTrue(updatedScopes.Count() > 0); foreach (var scope in updatedScopes) { var myScope = updateRessourceServerDto.Scopes.SingleOrDefault(s => s.IdScope.Equals(scope.Id)); if (myScope == null) { myScope = updateRessourceServerDto.Scopes.SingleOrDefault(s => !s.IdScope.HasValue); } Assert.IsNotNull(myScope); if (myScope.IdScope.HasValue) { Assert.AreEqual(scope.Id, myScope.IdScope); } else { Assert.IsTrue(scope.Id > 0); } Assert.AreEqual(scope.NiceWording, myScope.NiceWording); if (scope.Wording.StartsWith("RW_")) { Assert.IsTrue(myScope.IsReadWrite); } else { Assert.IsFalse(myScope.IsReadWrite); } } }
public IActionResult Put(UpdateRessourceServerDto toUpdate) { toUpdate.UserName = User.Identity.Name; _service.Update(toUpdate); return(Ok()); }
public RessourceServerDto Update(UpdateRessourceServerDto toUpdate) { Logger.LogInformation(String.Format("Try to update ressource server for user {0}", toUpdate != null ? toUpdate.UserName : String.Empty)); Validate(toUpdate); using (var context = RepositoriesFactory.CreateContext()) { var userRepo = RepositoriesFactory.GetUserRepository(context); var rsRepo = RepositoriesFactory.GetRessourceServerRepository(context); var scopeRepo = RepositoriesFactory.GetScopeRepository(context); var clientScopeRepo = RepositoriesFactory.GetClientScopeRepository(context); var myUser = userRepo.GetByUserName(toUpdate.UserName); if (myUser == null || !myUser.IsValid) { throw new DaOAuthServiceException("UpdateRessourceServerInvalidUserName"); } if (myUser.UsersRoles.FirstOrDefault(r => r.RoleId.Equals((int)ERole.ADMIN)) == null) { throw new DaOAuthServiceException("UpdateRessourceServerNonAdminUserName"); } var myRs = rsRepo.GetById(toUpdate.Id); if (myRs == null) { throw new DaOAuthServiceException("UpdateRessourceServerRessourceServerNotFound"); } myRs.IsValid = toUpdate.IsValid; myRs.Name = toUpdate.Name; myRs.Description = toUpdate.Description; rsRepo.Update(myRs); if (toUpdate.Scopes == null) { toUpdate.Scopes = new List <UpdateRessourceServerScopesDto>(); } if (myRs.Scopes == null) { myRs.Scopes = new List <Scope>(); } IList <int> newScopesTempIds = new List <int>(); foreach (var toUpdateScope in toUpdate.Scopes) { if (toUpdateScope.IdScope.HasValue && myRs.Scopes.Select(s => s.Id).Contains(toUpdateScope.IdScope.Value)) { var myScope = myRs.Scopes.Where(s => s.Id.Equals(toUpdateScope.IdScope.Value)).First(); myScope.NiceWording = toUpdateScope.NiceWording; myScope.Wording = toUpdateScope.NiceWording.ToScopeWording(toUpdateScope.IsReadWrite); scopeRepo.Update(myScope); } else if (!toUpdateScope.IdScope.HasValue) { var toAdd = new Scope() { NiceWording = toUpdateScope.NiceWording, Wording = toUpdateScope.NiceWording.ToScopeWording(toUpdateScope.IsReadWrite), RessourceServerId = myRs.Id }; scopeRepo.Add(toAdd); newScopesTempIds.Add(toAdd.Id); } } foreach (var toDeleteScope in myRs.Scopes.Where(s => s.Id > 0 && !newScopesTempIds.Contains(s.Id))) // only existings scopes { if (!toUpdate.Scopes.Where(s => s.IdScope.HasValue).Select(s => s.IdScope.Value).Contains(toDeleteScope.Id)) { foreach (var cs in clientScopeRepo.GetAllByScopeId(toDeleteScope.Id).ToList()) { clientScopeRepo.Delete(cs); } scopeRepo.Delete(toDeleteScope); } } context.Commit(); return(myRs.ToDto()); } }