|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FluentAssertions; |
| 5 | +using JsonApiDotNetCore.Configuration; |
| 6 | +using JsonApiDotNetCore.Serialization.Objects; |
| 7 | +using JsonApiDotNetCoreExampleTests.Startups; |
| 8 | +using TestBuildingBlocks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace JsonApiDotNetCoreExampleTests.IntegrationTests.InputValidation.RequestBody |
| 12 | +{ |
| 13 | + public sealed class WorkflowTests : IClassFixture<ExampleIntegrationTestContext<ModelStateValidationStartup<WorkflowDbContext>, WorkflowDbContext>> |
| 14 | + { |
| 15 | + private readonly ExampleIntegrationTestContext<ModelStateValidationStartup<WorkflowDbContext>, WorkflowDbContext> _testContext; |
| 16 | + |
| 17 | + public WorkflowTests(ExampleIntegrationTestContext<ModelStateValidationStartup<WorkflowDbContext>, WorkflowDbContext> testContext) |
| 18 | + { |
| 19 | + _testContext = testContext; |
| 20 | + |
| 21 | + testContext.UseController<WorkflowsController>(); |
| 22 | + |
| 23 | + testContext.ConfigureServicesAfterStartup(services => |
| 24 | + { |
| 25 | + services.AddResourceDefinition<WorkflowDefinition>(); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public async Task Can_create_in_valid_stage() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var requestBody = new |
| 34 | + { |
| 35 | + data = new |
| 36 | + { |
| 37 | + type = "workflows", |
| 38 | + attributes = new |
| 39 | + { |
| 40 | + stage = WorkflowStage.Created |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const string route = "/workflows"; |
| 46 | + |
| 47 | + // Act |
| 48 | + (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAsync<Document>(route, requestBody); |
| 49 | + |
| 50 | + // Assert |
| 51 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.Created); |
| 52 | + |
| 53 | + responseDocument.SingleData.Should().NotBeNull(); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task Cannot_create_in_invalid_stage() |
| 58 | + { |
| 59 | + // Arrange |
| 60 | + var requestBody = new |
| 61 | + { |
| 62 | + data = new |
| 63 | + { |
| 64 | + type = "workflows", |
| 65 | + attributes = new |
| 66 | + { |
| 67 | + stage = WorkflowStage.Canceled |
| 68 | + } |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const string route = "/workflows"; |
| 73 | + |
| 74 | + // Act |
| 75 | + (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecutePostAsync<ErrorDocument>(route, requestBody); |
| 76 | + |
| 77 | + // Assert |
| 78 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.UnprocessableEntity); |
| 79 | + |
| 80 | + responseDocument.Errors.Should().HaveCount(1); |
| 81 | + |
| 82 | + Error error = responseDocument.Errors[0]; |
| 83 | + error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity); |
| 84 | + error.Title.Should().Be("Invalid workflow stage."); |
| 85 | + error.Detail.Should().Be("Initial stage of workflow must be 'Created'."); |
| 86 | + error.Source.Pointer.Should().Be("/data/attributes/stage"); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public async Task Cannot_transition_to_invalid_stage() |
| 91 | + { |
| 92 | + // Arrange |
| 93 | + var existingWorkflow = new Workflow |
| 94 | + { |
| 95 | + Stage = WorkflowStage.OnHold |
| 96 | + }; |
| 97 | + |
| 98 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 99 | + { |
| 100 | + dbContext.Workflows.Add(existingWorkflow); |
| 101 | + await dbContext.SaveChangesAsync(); |
| 102 | + }); |
| 103 | + |
| 104 | + var requestBody = new |
| 105 | + { |
| 106 | + data = new |
| 107 | + { |
| 108 | + type = "workflows", |
| 109 | + id = existingWorkflow.StringId, |
| 110 | + attributes = new |
| 111 | + { |
| 112 | + stage = WorkflowStage.Succeeded |
| 113 | + } |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + string route = "/workflows/" + existingWorkflow.StringId; |
| 118 | + |
| 119 | + // Act |
| 120 | + (HttpResponseMessage httpResponse, ErrorDocument responseDocument) = await _testContext.ExecutePatchAsync<ErrorDocument>(route, requestBody); |
| 121 | + |
| 122 | + // Assert |
| 123 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.UnprocessableEntity); |
| 124 | + |
| 125 | + responseDocument.Errors.Should().HaveCount(1); |
| 126 | + |
| 127 | + Error error = responseDocument.Errors[0]; |
| 128 | + error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity); |
| 129 | + error.Title.Should().Be("Invalid workflow stage."); |
| 130 | + error.Detail.Should().Be("Cannot transition from 'OnHold' to 'Succeeded'."); |
| 131 | + error.Source.Pointer.Should().Be("/data/attributes/stage"); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public async Task Can_transition_to_valid_stage() |
| 136 | + { |
| 137 | + // Arrange |
| 138 | + var existingWorkflow = new Workflow |
| 139 | + { |
| 140 | + Stage = WorkflowStage.InProgress |
| 141 | + }; |
| 142 | + |
| 143 | + await _testContext.RunOnDatabaseAsync(async dbContext => |
| 144 | + { |
| 145 | + dbContext.Workflows.Add(existingWorkflow); |
| 146 | + await dbContext.SaveChangesAsync(); |
| 147 | + }); |
| 148 | + |
| 149 | + var requestBody = new |
| 150 | + { |
| 151 | + data = new |
| 152 | + { |
| 153 | + type = "workflows", |
| 154 | + id = existingWorkflow.StringId, |
| 155 | + attributes = new |
| 156 | + { |
| 157 | + stage = WorkflowStage.Failed |
| 158 | + } |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + string route = "/workflows/" + existingWorkflow.StringId; |
| 163 | + |
| 164 | + // Act |
| 165 | + (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecutePatchAsync<string>(route, requestBody); |
| 166 | + |
| 167 | + // Assert |
| 168 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent); |
| 169 | + |
| 170 | + responseDocument.Should().BeEmpty(); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments