// PUT: api/Core.svc/Jobs(5) public async Task <IHttpActionResult> Put([FromODataUri] int key, Job job) { var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; var fn = String.Format("{0}:{1}", declaringType.Namespace, declaringType.Name); if (!ModelState.IsValid) { Debug.WriteLine("Job to be updated with id '{0}' has invalid ModelState.", key); return(BadRequest(ModelState)); } if (key != job.Id) { return(BadRequest()); } try { Debug.WriteLine(fn); var identity = CurrentUserDataProvider.GetIdentity(TenantId); var permissionId = CreatePermissionId("CanUpdate"); if (!identity.Permissions.Contains(permissionId)) { return(StatusCode(HttpStatusCode.Forbidden)); } var original = db.Jobs.Find(job.Id); if (null == original) { return(StatusCode(HttpStatusCode.NotFound)); } if (!CurrentUserDataProvider.IsEntityOfUser(identity.Username, identity.Tid, original)) { return(StatusCode(HttpStatusCode.Forbidden)); } job.Created = original.Created; job.CreatedBy = original.CreatedBy; job.Modified = DateTimeOffset.Now; job.ModifiedBy = identity.Username; job.Tid = original.Tid; db.Jobs.Attach(job); db.Entry(job).State = EntityState.Modified; db.SaveChanges(); Debug.WriteLine("Job with id '{0}' updated", key); return(Ok <Job>(job)); } catch (Exception e) { Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace)); throw; } }
public void PutCalloutDefinitionForAuthorizedUserWithUpdatePermissionUpdatesCalloutDefinition() { Mock.Arrange(() => CurrentUserDataProvider.GetIdentity(TENANT_ID)) .Returns(new Identity { Permissions = new List <String> { CALLOUT_DEFINITION_UPDATE_PERMISSION }, Username = CURRENT_USER_ID, Tid = TENANT_ID }) .MustBeCalled(); Mock.Arrange(() => CurrentUserDataProvider.IsEntityOfUser(CURRENT_USER_ID, TENANT_ID, Arg.IsAny <CalloutDefinition>())) .Returns(true) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.CalloutDefinitions.Find(1)) .IgnoreInstance() .Returns(CreateSampleCalloutDefinitionDbSet()[0]) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.CalloutDefinitions.Attach(Arg.IsAny <CalloutDefinition>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.Entry(Arg.IsAny <CalloutDefinition>())) .IgnoreInstance() .Returns(Mock.Create <DbEntityEntry <CalloutDefinition> >()) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.SaveChanges()) .IgnoreInstance() .MustBeCalled(); var actionResult = _calloutDefinitionsController.Put(1, new CalloutDefinition { Id = 1, CreatedBy = ANOTHER_USER_ID, Created = DateTimeOffset.Parse("05/01/2008"), ModifiedBy = ANOTHER_USER_ID, Modified = DateTimeOffset.Parse("05/01/2008"), Tid = ANOTHER_TENANT_ID, EntityType = SAMPLE_ENTITY_TYPE, EntityId = ENTITY_ID_1, TenantId = TENANT_ID, Parameters = "testparameters", }).Result; Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult <CalloutDefinition>)); var response = actionResult as OkNegotiatedContentResult <CalloutDefinition>; var calloutDefinition = response.Content; Assert.AreEqual(1, calloutDefinition.Id); Assert.AreEqual(CURRENT_USER_ID, calloutDefinition.CreatedBy); Assert.AreNotEqual(DateTimeOffset.Parse("05/01/2008"), calloutDefinition.Created); Assert.AreEqual(CURRENT_USER_ID, calloutDefinition.ModifiedBy); Assert.AreNotEqual(DateTimeOffset.Parse("05/01/2008"), calloutDefinition.Modified); Assert.AreEqual(TENANT_ID, calloutDefinition.Tid); Assert.AreEqual(SAMPLE_ENTITY_TYPE, calloutDefinition.EntityType); Assert.AreEqual(ENTITY_ID_1, calloutDefinition.EntityId); Assert.AreEqual(TENANT_ID, calloutDefinition.TenantId); Assert.AreEqual("testparameters", calloutDefinition.Parameters); Mock.Assert(() => CurrentUserDataProvider.GetIdentity(TENANT_ID)); Mock.Assert(() => CurrentUserDataProvider.IsEntityOfUser(CURRENT_USER_ID, TENANT_ID, Arg.IsAny <CalloutDefinition>())); Mock.Assert(_lifeCycleContext); }
public void PutJobForAuthorizedUserWithUpdatePermissionUpdatesJob() { Mock.Arrange(() => CurrentUserDataProvider.GetIdentity(TENANT_ID)) .Returns(new Identity { Permissions = new List <String> { JOB_UPDATE_PERMISSION }, Username = CURRENT_USER_ID, Tid = TENANT_ID }) .MustBeCalled(); Mock.Arrange(() => CurrentUserDataProvider.IsEntityOfUser(CURRENT_USER_ID, TENANT_ID, Arg.IsAny <Job>())) .Returns(true) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.Jobs.Find(1)) .IgnoreInstance() .Returns(CreateSampleJobDbSet()[0]) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.Jobs.Attach(Arg.IsAny <Job>())) .IgnoreInstance() .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.Entry(Arg.IsAny <Job>())) .IgnoreInstance() .Returns(Mock.Create <DbEntityEntry <Job> >()) .MustBeCalled(); Mock.Arrange(() => _lifeCycleContext.SaveChanges()) .IgnoreInstance() .MustBeCalled(); var actionResult = _jobsController.Put(1, new Job { Id = 1, Type = "Test", CreatedBy = ANOTHER_USER_ID, Created = DateTimeOffset.Parse("05/01/2008"), ModifiedBy = ANOTHER_USER_ID, Modified = DateTimeOffset.Parse("05/01/2008"), Tid = ANOTHER_TENANT_ID, State = JobStateEnum.Canceled.ToString(), Parameters = SAMPLE_PARAMETERS, Token = SAMPLE_TOKEN, TenantId = ANOTHER_TENANT_ID }).Result; Assert.IsTrue(actionResult.GetType() == typeof(OkNegotiatedContentResult <Job>)); var response = actionResult as OkNegotiatedContentResult <Job>; var job = response.Content; Assert.AreEqual(1, job.Id); Assert.AreEqual("Test", job.Type); Assert.AreEqual(CURRENT_USER_ID, job.CreatedBy); Assert.AreNotEqual(DateTimeOffset.Parse("05/01/2008"), job.Created); Assert.AreEqual(CURRENT_USER_ID, job.ModifiedBy); Assert.AreNotEqual(DateTimeOffset.Parse("05/01/2008"), job.Modified); Assert.AreEqual(TENANT_ID, job.Tid); Assert.AreEqual(JobStateEnum.Canceled.ToString(), job.State); Assert.AreEqual(SAMPLE_PARAMETERS, job.Parameters); Assert.AreEqual(SAMPLE_TOKEN, job.Token); Assert.AreEqual(ANOTHER_TENANT_ID, job.TenantId); Mock.Assert(() => CurrentUserDataProvider.GetIdentity(TENANT_ID)); Mock.Assert(() => CurrentUserDataProvider.IsEntityOfUser(CURRENT_USER_ID, TENANT_ID, Arg.IsAny <Job>())); Mock.Assert(_lifeCycleContext); }