|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Bogus; |
| 6 | +using JsonApiDotNetCore.Serialization.Objects; |
| 7 | +using JsonApiDotNetCoreExample.Models; |
| 8 | +using JsonApiDotNetCoreExampleTests.Helpers.Models; |
| 9 | +using Microsoft.EntityFrameworkCore; |
| 10 | +using Newtonsoft.Json; |
| 11 | +using Xunit; |
| 12 | +using Person = JsonApiDotNetCoreExample.Models.Person; |
| 13 | + |
| 14 | +namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec |
| 15 | +{ |
| 16 | + public sealed class ResourceTypeMismatchTests : FunctionalTestCollection<StandardApplicationFactory> |
| 17 | + { |
| 18 | + public ResourceTypeMismatchTests(StandardApplicationFactory factory) : base(factory) { } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public async Task Posting_Resource_With_Mismatching_Resource_Type_Returns_Conflict() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + string content = JsonConvert.SerializeObject(new |
| 25 | + { |
| 26 | + data = new |
| 27 | + { |
| 28 | + type = "people" |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + // Act |
| 33 | + var (body, response) = await Post("/api/v1/todoItems", content); |
| 34 | + |
| 35 | + // Assert |
| 36 | + var errorDocument = JsonConvert.DeserializeObject<ErrorDocument>(body); |
| 37 | + Assert.Single(errorDocument.Errors); |
| 38 | + Assert.Equal(HttpStatusCode.Conflict, errorDocument.Errors[0].StatusCode); |
| 39 | + Assert.Equal("Resource type mismatch between request body and endpoint URL.", errorDocument.Errors[0].Title); |
| 40 | + Assert.Equal("Expected resource of type 'todoItems' in POST request body at endpoint '/api/v1/todoItems', instead of 'people'.", errorDocument.Errors[0].Detail); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task Patching_Resource_With_Mismatching_Resource_Type_Returns_Conflict() |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + string content = JsonConvert.SerializeObject(new |
| 48 | + { |
| 49 | + data = new |
| 50 | + { |
| 51 | + type = "people", |
| 52 | + id = 1 |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + // Act |
| 57 | + var (body, response) = await Patch("/api/v1/todoItems/1", content); |
| 58 | + |
| 59 | + // Assert |
| 60 | + |
| 61 | + var errorDocument = JsonConvert.DeserializeObject<ErrorDocument>(body); |
| 62 | + Assert.Single(errorDocument.Errors); |
| 63 | + Assert.Equal(HttpStatusCode.Conflict, errorDocument.Errors[0].StatusCode); |
| 64 | + Assert.Equal("Resource type mismatch between request body and endpoint URL.", errorDocument.Errors[0].Title); |
| 65 | + Assert.Equal("Expected resource of type 'todoItems' in PATCH request body at endpoint '/api/v1/todoItems/1', instead of 'people'.", errorDocument.Errors[0].Detail); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task Patching_Through_Relationship_Link_With_Mismatching_Resource_Type_Returns_Conflict() |
| 70 | + { |
| 71 | + // Arrange |
| 72 | + string content = JsonConvert.SerializeObject(new |
| 73 | + { |
| 74 | + data = new |
| 75 | + { |
| 76 | + type = "todoItems", |
| 77 | + id = 1 |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // Act |
| 82 | + var (body, response) = await Patch("/api/v1/todoItems/1/relationships/owner", content); |
| 83 | + |
| 84 | + // Assert |
| 85 | + |
| 86 | + var errorDocument = JsonConvert.DeserializeObject<ErrorDocument>(body); |
| 87 | + Assert.Single(errorDocument.Errors); |
| 88 | + Assert.Equal(HttpStatusCode.Conflict, errorDocument.Errors[0].StatusCode); |
| 89 | + Assert.Equal("Resource type mismatch between request body and endpoint URL.", errorDocument.Errors[0].Title); |
| 90 | + Assert.Equal("Expected resource of type 'people' in PATCH request body at endpoint '/api/v1/todoItems/1/relationships/owner', instead of 'todoItems'.", errorDocument.Errors[0].Detail); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments