mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Uses contributor references on the book page
This commit is contained in:
1 parent
dab6bac9a7
commit
ad072eeffc
28 files changed
+217
-199
No files matched your search
-47
@@ -1,47 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Presentation.Web.Common.DTO.MediaContributors;
|
||||
using Nemera.Presentation.Web.Common.Enums.MediaContributors;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Web.Fixtures.Common.DTO.MediaContributors;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaContributorDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaContributorDtoFixture
|
||||
{
|
||||
private readonly MediaContributorNameDtoFixture _mediaContributorNameDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MediaContributorDto"/> instance with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="displayName">Optional. The name by which the contributor is popularly known.</param>
|
||||
/// <param name="legalName">Optional. The legal name of the contributor.</param>
|
||||
/// <param name="role">Optional. The canonical role of the contributor.</param>
|
||||
/// <returns>A configured <see cref="MediaContributorDto"/> instance.</returns>
|
||||
public MediaContributorDto Create(
|
||||
string? displayName = null,
|
||||
string? legalName = null,
|
||||
MediaContributorRole? role = null)
|
||||
{
|
||||
return new MediaContributorDto
|
||||
{
|
||||
Name = _mediaContributorNameDtoFixture.Create(displayName, legalName),
|
||||
Role = role ?? MediaContributorRole.Author
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="MediaContributorDto"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="MediaContributorDto"/> instances.</returns>
|
||||
public List<MediaContributorDto> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Nemera.Presentation.Web.Common.DTO.MediaContributors;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Web.Fixtures.Common.DTO.MediaContributors;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaContributorNameDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaContributorNameDtoFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MediaContributorNameDto"/> instance with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="displayName">Optional. The name by which the contributor is popularly known.</param>
|
||||
/// <param name="legalName">Optional. The legal name of the contributor.</param>
|
||||
/// <returns>A configured <see cref="MediaContributorNameDto"/> instance.</returns>
|
||||
public MediaContributorNameDto Create(
|
||||
string? displayName = null,
|
||||
string? legalName = null)
|
||||
{
|
||||
return new MediaContributorNameDto
|
||||
{
|
||||
DisplayName = displayName ?? _faker.Name.FullName(),
|
||||
LegalName = legalName ?? _faker.Name.FullName()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="MediaContributorNameDto"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="MediaContributorNameDto"/> instances.</returns>
|
||||
public List<MediaContributorNameDto> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -23,7 +23,7 @@ public class BookDetailsDtoFixture
|
||||
private readonly Faker _faker = new();
|
||||
private readonly WrittenContentMetadataDtoFixture _writtenContentMetadataDtoFixture = new();
|
||||
private readonly IsbnDtoFixture _isbnDtoFixture = new();
|
||||
private readonly MediaContributorDtoFixture _mediaContributorDtoFixture = new();
|
||||
private readonly MediaContributorReferenceDtoFixture _mediaContributorReferenceDtoFixture = new();
|
||||
private readonly BookRatingDtoFixture _bookRatingDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +61,7 @@ public class BookDetailsDtoFixture
|
||||
BarnesAndNobleId = _faker.Random.String2(10, "0123456789"),
|
||||
AppleBooksId = $"id{_faker.Random.Number(1, 999999)}",
|
||||
ISBNs = [.. _isbnDtoFixture.CreateMany(_faker.Random.Number(1, 3))],
|
||||
Contributors = [.. _mediaContributorDtoFixture.CreateMany(_faker.Random.Number(1, 3))],
|
||||
Contributors = [.. _mediaContributorReferenceDtoFixture.CreateMany(_faker.Random.Number(1, 3))],
|
||||
Ratings = [.. _bookRatingDtoFixture.CreateMany(_faker.Random.Number(1, 3))],
|
||||
MetadataStatus = _faker.Random.String2(_faker.Random.Number(1, 20)),
|
||||
LastMetadataUpdateUtc = _faker.Date.Recent(),
|
||||
|
||||
+2
-2
@@ -24,7 +24,7 @@ public class UpdateBookRequestFixture
|
||||
private readonly WrittenContentMetadataDtoFixture _writtenContentMetadataDtoFixture = new();
|
||||
private readonly BookSeriesDtoFixture _bookSeriesDtoFixture = new();
|
||||
private readonly IsbnDtoFixture _isbnDtoFixture = new();
|
||||
private readonly MediaContributorDtoFixture _mediaContributorDtoFixture = new();
|
||||
private readonly MediaContributorReferenceDtoFixture _mediaContributorReferenceDtoFixture = new();
|
||||
private readonly BookRatingDtoFixture _bookRatingDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -91,7 +91,7 @@ public class UpdateBookRequestFixture
|
||||
BarnesAndNobleId: includeBarnesAndNobleId ? _faker.Random.String2(10, "0123456789") : null,
|
||||
AppleBooksId: includeAppleBooksId ? $"id{_faker.Random.Number(1, 999999)}" : null,
|
||||
ISBNs: includeIsbns ? [.. _isbnDtoFixture.CreateMany(_faker.Random.Number(1, 3))] : null,
|
||||
Contributors: includeContributors ? [.. _mediaContributorDtoFixture.CreateMany(_faker.Random.Number(1, 3))] : null,
|
||||
Contributors: includeContributors ? [.. _mediaContributorReferenceDtoFixture.CreateMany(_faker.Random.Number(1, 3))] : null,
|
||||
Ratings: includeRatings ? [.. _bookRatingDtoFixture.CreateMany(_faker.Random.Number(1, 3))] : null
|
||||
);
|
||||
}
|
||||
|
||||
+3
@@ -50,5 +50,8 @@ public class EditBookViewEndpointTests : IClassFixture<NemeraWebFactory>
|
||||
Assert.Equal("text/html", response.Content.Headers.ContentType!.MediaType);
|
||||
Assert.Contains("A book", content);
|
||||
Assert.Contains("btn-delete-book", content);
|
||||
Assert.Contains("contributor-id", content);
|
||||
Assert.DoesNotContain("contributor-name", content);
|
||||
Assert.Contains(expectedBook.Contributors![0].ContributorId.ToString(), content);
|
||||
}
|
||||
}
|
||||
+86
@@ -1,5 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Presentation.Web.Common.DTO.Common;
|
||||
using Nemera.Presentation.Web.Common.DTO.WrittenContentLibrary.BookLibrary;
|
||||
using Nemera.Presentation.Web.Common.Exceptions;
|
||||
using Nemera.Presentation.Web.Common.Requests.Library.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Nemera.Presentation.Web.Core.Endpoints.Library.WrittenContentLibrary.BookLibrary.Books.SaveBook;
|
||||
using Nemera.Presentation.Web.Fixtures.Common.DTO.WrittenContentLibrary.BookLibrary;
|
||||
@@ -172,6 +174,90 @@ public class SaveBookEndpointTests : IClassFixture<NemeraWebFactory>
|
||||
Assert.DoesNotContain("at Nemera", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveBook_WhenContributorIdIsEmpty_ShouldForwardItAndReturnASanitizedApiError()
|
||||
{
|
||||
// Arrange
|
||||
_apiFactory.ApiClientStub.Reset();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
string endpoint = $"libraries/{libraryId}/books/{bookId}";
|
||||
UpdateBookRequest request = _updateBookRequestFixture.Create(id: bookId, libraryId: libraryId);
|
||||
request.Contributors![0].ContributorId = Guid.Empty;
|
||||
_apiFactory.ApiClientStub.RegisterPutException(endpoint, new ApiException(
|
||||
new ProblemDetailsDto
|
||||
{
|
||||
Type = "https://tools.ietf.org/html/rfc4918#section-11.2",
|
||||
Title = "General.Validation",
|
||||
Status = 422,
|
||||
Detail = "MediaContributorIdCannotBeEmpty"
|
||||
},
|
||||
(HttpStatusCode)422,
|
||||
endpoint));
|
||||
AuthenticatedWebClient webClient = await WebTestHelpers.CreateAuthenticatedClientAsync(_apiFactory);
|
||||
HttpRequestMessage saveRequest = CreateSaveRequest(bookId.ToString(), request);
|
||||
saveRequest.Headers.Add("RequestVerificationToken", webClient.AntiforgeryToken);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await webClient.Client.SendAsync(saveRequest);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
using JsonDocument json = JsonDocument.Parse(content);
|
||||
Assert.False(json.RootElement.GetProperty("success").GetBoolean());
|
||||
Assert.Contains("media contributor", json.RootElement.GetProperty("errorMessage").GetString(), StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains(_apiFactory.ApiClientStub.PutRequests, putRequest =>
|
||||
putRequest.Endpoint == endpoint &&
|
||||
putRequest.Data is UpdateBookRequest forwardedRequest &&
|
||||
forwardedRequest.Contributors![0].ContributorId == Guid.Empty);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("at Nemera", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveBook_WhenContributorDoesNotExist_ShouldForwardItAndReturnASanitizedApiError()
|
||||
{
|
||||
// Arrange
|
||||
_apiFactory.ApiClientStub.Reset();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid unknownContributorId = Guid.NewGuid();
|
||||
string endpoint = $"libraries/{libraryId}/books/{bookId}";
|
||||
UpdateBookRequest request = _updateBookRequestFixture.Create(id: bookId, libraryId: libraryId);
|
||||
request.Contributors![0].ContributorId = unknownContributorId;
|
||||
_apiFactory.ApiClientStub.RegisterPutException(endpoint, new ApiException(
|
||||
new ProblemDetailsDto
|
||||
{
|
||||
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
Title = "General.NotFound",
|
||||
Status = 404,
|
||||
Detail = "MediaContributorNotFound"
|
||||
},
|
||||
HttpStatusCode.NotFound,
|
||||
endpoint));
|
||||
AuthenticatedWebClient webClient = await WebTestHelpers.CreateAuthenticatedClientAsync(_apiFactory);
|
||||
HttpRequestMessage saveRequest = CreateSaveRequest(bookId.ToString(), request);
|
||||
saveRequest.Headers.Add("RequestVerificationToken", webClient.AntiforgeryToken);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await webClient.Client.SendAsync(saveRequest);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
||||
using JsonDocument json = JsonDocument.Parse(content);
|
||||
Assert.False(json.RootElement.GetProperty("success").GetBoolean());
|
||||
Assert.Contains(_apiFactory.ApiClientStub.PutRequests, putRequest =>
|
||||
putRequest.Endpoint == endpoint &&
|
||||
putRequest.Data is UpdateBookRequest forwardedRequest &&
|
||||
forwardedRequest.Contributors![0].ContributorId == unknownContributorId);
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("SqliteException", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("at Nemera", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveBook_WhenCalledWithoutAuthentication_ShouldRedirectToLogin()
|
||||
{
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Nemera.Presentation.Web.Common.DTO.WrittenContentLibrary.BookLibrary;
|
||||
using Nemera.Presentation.Web.Common.Enums.MediaContributors;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#endregion
|
||||
|
||||
namespace Nemera.Presentation.Web.UnitTests.Common.DTO.WrittenContentLibrary.BookLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BookDetailsDto"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookDetailsDtoTests
|
||||
{
|
||||
private static readonly JsonSerializerOptions s_jsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter() }
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void Deserialize_WhenBookResponseHasContributorReferences_ShouldBindContributorIdAndRole()
|
||||
{
|
||||
// Arrange
|
||||
Guid firstContributorId = Guid.NewGuid();
|
||||
Guid secondContributorId = Guid.NewGuid();
|
||||
string json = $$"""
|
||||
{
|
||||
"id": "{{Guid.NewGuid()}}",
|
||||
"libraryId": "{{Guid.NewGuid()}}",
|
||||
"path": "C:\\books\\book.epub",
|
||||
"contributors": [
|
||||
{ "contributorId": "{{firstContributorId}}", "role": "Author" },
|
||||
{ "contributorId": "{{secondContributorId}}", "role": "Illustrator" }
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
// Act
|
||||
BookDetailsDto? result = JsonSerializer.Deserialize<BookDetailsDto>(json, s_jsonOptions);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Contributors);
|
||||
Assert.Equal(2, result.Contributors.Count);
|
||||
Assert.Equal(firstContributorId, result.Contributors[0].ContributorId);
|
||||
Assert.Equal(MediaContributorRole.Author, result.Contributors[0].Role);
|
||||
Assert.Equal(secondContributorId, result.Contributors[1].ContributorId);
|
||||
Assert.Equal(MediaContributorRole.Illustrator, result.Contributors[1].Role);
|
||||
}
|
||||
}
|
||||
+25
@@ -62,6 +62,31 @@ public class SaveBookEndpointTests
|
||||
Assert.Equal(originalBookId, request.BookId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenContributorsAreProvided_ShouldForwardContributorReferencesToApi()
|
||||
{
|
||||
// Arrange
|
||||
string routeId = Guid.NewGuid().ToString();
|
||||
UpdateBookRequest request = _updateBookRequestFixture.Create();
|
||||
BookDetailsDto expectedBook = _bookDetailsDtoFixture.Create();
|
||||
_mockApiHttpClient.PutAsync<BookDetailsDto, UpdateBookRequest>(Arg.Any<string>(), Arg.Any<UpdateBookRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(expectedBook);
|
||||
ConfigureRequest(routeId);
|
||||
|
||||
// Act
|
||||
await _sut.ExecuteAsync(request, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockApiHttpClient.Received(1).PutAsync<BookDetailsDto, UpdateBookRequest>(
|
||||
Arg.Any<string>(),
|
||||
Arg.Is<UpdateBookRequest>(forwardedRequest =>
|
||||
forwardedRequest.Contributors != null &&
|
||||
forwardedRequest.Contributors.Count == request.Contributors!.Count &&
|
||||
forwardedRequest.Contributors[0].ContributorId == request.Contributors[0].ContributorId &&
|
||||
forwardedRequest.Contributors[0].Role == request.Contributors[0].Role),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenRouteIdDiffersFromBodyId_ShouldForwardTheRouteIdToApi()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user