public async Task <IHttpActionResult> Allow([FromODataUri] String token, ODataActionParameters parameters) { var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; var fn = String.Format("{0}:{1}", declaringType.Namespace, declaringType.Name); try { Debug.WriteLine(fn); var identity = CurrentUserDataProvider.GetIdentity(TenantId); var permissionId = CreatePermissionId("CanAllow"); if (!identity.Permissions.Contains(permissionId)) { return(StatusCode(HttpStatusCode.Forbidden)); } var job = _coreService.Jobs.Where(j => token == j.Token && CALLOUT_JOB_TYPE == j.Type && j.State == JobStateEnum.Running.ToString()) .SingleOrDefault(); if (null == job) { return(StatusCode(HttpStatusCode.NotFound)); } var calloutDefinition = JsonConvert.DeserializeObject <CalloutData>(job.Parameters); var lifeCycleManager = new LifeCycleManager(new DefaultAuthenticationProvider(), calloutDefinition.EntityType); lifeCycleManager.OnAllowCallback(job); return(Ok()); } catch (InvalidOperationException e) { return(BadRequest(String.Format("Allow job with token: '{0}' not possible", token))); } catch (Exception e) { Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace)); throw; } }
public void OnAllowCallbackForPostCalloutFinishesJobAndUnlocksEntity() { Job updatedJob = null; Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(2); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString(), false)); Assert.AreEqual(JobStateEnum.Finished.ToString(), updatedJob.State); Mock.Assert(_coreService); }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfChangingStateFails() { Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(UPDATED_ENTITY) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List <CalloutDefinition>(new List <CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(3); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .Throws <HttpRequestException>() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfPostCalloutFails() { Job createdJob = null; Job updatedJob = null; Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(SAMPLE_ENTITY) .MustBeCalled(); Mock.Arrange(() => HttpContext.Current.User.Identity) .Returns(_windowsIdentity) .MustBeCalled(); Mock.Arrange(() => CredentialCache.DefaultCredentials) .MustBeCalled(); Mock.Arrange(() => _windowsIdentity.Impersonate()) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List <CalloutDefinition>(new List <CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(4); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { createdJob = j; }) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny <CalloutData>())) .Throws <HttpRequestException>() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List <Job>(new List <Job> { CreateJob(SAMPLE_ENTITY_URI.ToString(), false) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny <Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY)) .IgnoreInstance() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List <StateChangeLock>(new List <StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws <InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Assert.AreEqual(JobStateEnum.Failed.ToString(), updatedJob.State); Assert.AreEqual(EXPECTED_POST_CALLOUT_DATA, createdJob.Parameters); Assert.AreEqual(SAMPLE_ENTITY_URI.ToString(), createdJob.ReferencedItemId); Assert.AreEqual(JobStateEnum.Running.ToString(), createdJob.State); Assert.AreEqual(CALLOUT_JOB_TYPE, createdJob.Type); Assert.AreEqual(TENANT_ID, createdJob.TenantId); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); Mock.Assert(() => CurrentUserDataProvider.GetCurrentUsername()); Mock.Assert(() => HttpContext.Current.User.Identity); Mock.Assert(CredentialCache.DefaultCredentials); Mock.Assert(_windowsIdentity); }
public async Task<IHttpActionResult> Allow([FromODataUri] String token, ODataActionParameters parameters) { var declaringType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; var fn = String.Format("{0}:{1}", declaringType.Namespace, declaringType.Name); try { Debug.WriteLine(fn); var identity = CurrentUserDataProvider.GetIdentity(TenantId); var permissionId = CreatePermissionId("CanAllow"); if (!identity.Permissions.Contains(permissionId)) { return StatusCode(HttpStatusCode.Forbidden); } var job = _coreService.Jobs.Where(j => token == j.Token && CALLOUT_JOB_TYPE == j.Type && j.State == JobStateEnum.Running.ToString()) .SingleOrDefault(); if (null == job) { return StatusCode(HttpStatusCode.NotFound); } var calloutDefinition = JsonConvert.DeserializeObject<CalloutData>(job.Parameters); var lifeCycleManager = new LifeCycleManager(new DefaultAuthenticationProvider(), calloutDefinition.EntityType); lifeCycleManager.OnAllowCallback(job); return Ok(); } catch (InvalidOperationException e) { return BadRequest(String.Format("Allow job with token: '{0}' not possible", token)); } catch (Exception e) { Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", e.Source, e.Message, e.StackTrace)); throw; } }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfChangingStateFails() { Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny<Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(UPDATED_ENTITY) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List<CalloutDefinition>(new List<CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(3); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .Throws<HttpRequestException>() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List<StateChangeLock>(new List<StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws<InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); }
public void OnAllowCallbackForPreCalloutRevertsTransactionAndThrowsInvalidOperationExceptionIfPostCalloutFails() { Job createdJob = null; Job updatedJob = null; Mock.Arrange(() => CurrentUserDataProvider.GetCurrentUsername()) .Returns("Administrator") .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny<Job>())) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.LoadEntity(SAMPLE_ENTITY_URI)) .IgnoreInstance() .Returns(SAMPLE_ENTITY) .MustBeCalled(); Mock.Arrange(() => HttpContext.Current.User.Identity) .Returns(_windowsIdentity) .MustBeCalled(); Mock.Arrange(() => CredentialCache.DefaultCredentials) .MustBeCalled(); Mock.Arrange(() => _windowsIdentity.Impersonate()) .MustBeCalled(); Mock.Arrange(() => _coreService.CalloutDefinitions) .IgnoreInstance() .ReturnsCollection(new List<CalloutDefinition>(new List<CalloutDefinition> { CreateCalloutDefinition(SAMPLE_ENTITY_URI.ToString(), Model.CalloutDefinition.CalloutDefinitionType.Post.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(4); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, UPDATED_ENTITY)) .IgnoreInstance() .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.AddToJobs(Arg.IsAny<Job>())) .IgnoreInstance() .DoInstead((Job j) => { createdJob = j; }) .MustBeCalled(); Mock.Arrange(() => _calloutExecutor.ExecuteCallout(CALLOUT_DEFINITION, Arg.IsAny<CalloutData>())) .Throws<HttpRequestException>() .MustBeCalled(); Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString(), false) })) .InSequence() .OccursOnce(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny<Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .InSequence() .OccursOnce(); Mock.Arrange(() => _entityController.UpdateEntity(SAMPLE_ENTITY_URI, SAMPLE_ENTITY)) .IgnoreInstance() .OccursOnce(); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List<StateChangeLock>(new List<StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager._calloutExecutor = _calloutExecutor; ThrowsAssert.Throws<InvalidOperationException>(() => lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString()))); Assert.AreEqual(JobStateEnum.Failed.ToString(), updatedJob.State); Assert.AreEqual(EXPECTED_POST_CALLOUT_DATA, createdJob.Parameters); Assert.AreEqual(SAMPLE_ENTITY_URI.ToString(), createdJob.ReferencedItemId); Assert.AreEqual(JobStateEnum.Running.ToString(), createdJob.State); Assert.AreEqual(CALLOUT_JOB_TYPE, createdJob.Type); Assert.AreEqual(TENANT_ID, createdJob.TenantId); Mock.Assert(_coreService); Mock.Assert(_calloutExecutor); Mock.Assert(_entityController); Mock.Assert(() => CurrentUserDataProvider.GetCurrentUsername()); Mock.Assert(() => HttpContext.Current.User.Identity); Mock.Assert(CredentialCache.DefaultCredentials); Mock.Assert(_windowsIdentity); }
public void OnAllowCallbackForPostCalloutFinishesJobAndUnlocksEntity() { Job updatedJob = null; Mock.Arrange(() => _coreService.Jobs) .IgnoreInstance() .ReturnsCollection(new List<Job>(new List<Job> { CreateJob(SAMPLE_ENTITY_URI.ToString()) })) .MustBeCalled(); Mock.Arrange(() => _coreService.UpdateObject(Arg.IsAny<Job>())) .IgnoreInstance() .DoInstead((Job j) => { updatedJob = j; }) .MustBeCalled(); Mock.Arrange(() => _coreService.SaveChanges()) .IgnoreInstance() .Occurs(2); var stateChangeLockToBeDeleted = CreateStateChangeLock(SAMPLE_ENTITY_URI); Mock.Arrange(() => _coreService.StateChangeLocks) .IgnoreInstance() .ReturnsCollection(new List<StateChangeLock>(new List<StateChangeLock> { stateChangeLockToBeDeleted })) .InSequence() .MustBeCalled(); Mock.Arrange(() => _coreService.DeleteObject(stateChangeLockToBeDeleted)) .IgnoreInstance() .OccursOnce(); var lifeCycleManager = new LifeCycleManager(_authenticationProvider, ENTITY_TYPE); lifeCycleManager.OnAllowCallback(CreateJob(SAMPLE_ENTITY_URI.ToString(), false)); Assert.AreEqual(JobStateEnum.Finished.ToString(), updatedJob.State); Mock.Assert(_coreService); }