mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Relocates real-resource tests into integration projects and improves data access unit tests
This commit is contained in:
1 parent
551259ad81
commit
042c5e4729
56 files changed
+5642
-4136
No files matched your search
@@ -6,10 +6,8 @@ using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using Lumina.Plugins.Calibre.Fixtures.Core.Metadata;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -58,149 +56,6 @@ public class CalibreArtworkProviderTests
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenCoverHrefIsABareFileName_ShouldReturnTheLocalCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
WriteOpf(bookDirectory, coverHref: "cover.jpg");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "cover.jpg"), "image-bytes");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(Path.Combine(bookDirectory, "cover.jpg"), result!.LocalPath);
|
||||
Assert.Null(result.RemoteUrl);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenCoverHrefIsAnAbsolutePath_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
WriteOpf(bookDirectory, coverHref: @"C:\Windows\System32\drivers\etc\hosts");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("../cover.jpg")] // parent directory escape
|
||||
[InlineData("sub/cover.jpg")] // path with a separator
|
||||
[InlineData("sub\\cover.jpg")] // path with a backslash separator
|
||||
public async Task GetArtworkAsync_WhenCoverHrefContainsPathTraversalOrSeparators_ShouldReturnNull(string coverHref)
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
WriteOpf(bookDirectory, coverHref);
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenCoverFileIsMissing_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
WriteOpf(bookDirectory, coverHref: "cover.jpg");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenOpfFileDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenOpfHasNoCoverReference_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), """<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Test</dc:title></metadata></package>""");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
ArtworkDto? result = await _sut.GetArtworkAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenLookupIsNotABookLookup_ShouldReturnNull()
|
||||
{
|
||||
@@ -227,26 +82,4 @@ public class CalibreArtworkProviderTests
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an OPF file that references the given cover into the book directory.
|
||||
/// </summary>
|
||||
/// <param name="bookDirectory">The file system path of the directory the OPF file is written into.</param>
|
||||
/// <param name="coverHref">The href of the cover the OPF file references.</param>
|
||||
private static void WriteOpf(string bookDirectory, string coverHref)
|
||||
{
|
||||
string opf = $$"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Test</dc:title></metadata><guide><reference type="cover" href="{{coverHref}}"/></guide></package>""";
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), opf);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-calibre-artwork-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
-107
@@ -3,10 +3,8 @@ using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -22,8 +20,6 @@ public class CalibreBookMetadataProviderTests
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
private readonly CalibreBookMetadataProvider _sut = new();
|
||||
|
||||
private const string OPF_DOCUMENT = """<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:title>Test Book Title</dc:title><dc:creator opf:role="aut">Test Author</dc:creator></metadata></package>""";
|
||||
|
||||
[Fact]
|
||||
public void Name_WhenCalled_ShouldReturnTheProviderDisplayName()
|
||||
{
|
||||
@@ -56,52 +52,6 @@ public class CalibreBookMetadataProviderTests
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnTheMappedMetadata()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal("Test Book Title", result!.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfFileDoesNotExist_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenLookupPathIsEmpty_ShouldReturnNull()
|
||||
{
|
||||
@@ -114,61 +64,4 @@ public class CalibreBookMetadataProviderTests
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnASingleMetadataCandidate()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<BookMetadataDto> result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
BookMetadataDto metadata = Assert.IsType<BookMetadataDto>(Assert.Single(result));
|
||||
Assert.Equal("Test Book Title", metadata.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSearchResultsAsync_WhenOpfFileDoesNotExist_ShouldReturnAnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<BookMetadataDto> result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-calibre-metadata-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Calibre.Common.Models.DTO.Opf;
|
||||
using Lumina.Plugins.Calibre.Core.Opf;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.UnitTests.Core.Opf;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="OpfReader"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OpfReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileContainsAllSupportedFields_ShouldParseThemAll()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="uuid_id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"><dc:title>Test Book Title</dc:title><dc:creator opf:role="aut">Test Author</dc:creator><dc:creator opf:role="oth">Second Creator</dc:creator><dc:contributor opf:role="bkp">calibre (3.48.0) [https://calibre-ebook.com]</dc:contributor><dc:contributor opf:role="trl">Test Translator</dc:contributor><dc:description><p>A test description with <b>markup</b>.</p></dc:description><dc:publisher>Test Publisher</dc:publisher><dc:date>2015-06-15</dc:date><dc:language>en</dc:language><dc:identifier opf:scheme="ISBN">978-0-306-40615-7</dc:identifier><dc:identifier opf:scheme="goodreads">123456</dc:identifier><dc:identifier opf:scheme="ASIN">B00TEST1</dc:identifier><dc:identifier opf:scheme="LCCN">test-lccn</dc:identifier><dc:identifier opf:scheme="oclc">ocm12345678</dc:identifier><dc:identifier opf:scheme="openlibrary">OL100M</dc:identifier><dc:identifier opf:scheme="google">google-id</dc:identifier><dc:subject>Science fiction</dc:subject><dc:subject>Space opera</dc:subject><meta name="calibre:series" content="The Series"/><meta name="calibre:series_index" content="2.5"/><meta name="calibre:rating" content="8"/></metadata><guide><reference type="cover" href="cover.jpg"/><reference type="toc" href="toc.xhtml"/></guide></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Test Book Title", result.Title);
|
||||
Assert.Equal("<p>A test description with <b>markup</b>.</p>", result.Description);
|
||||
Assert.Equal("Test Publisher", result.Publisher);
|
||||
Assert.Equal(2015, result.PublishDate!.Value.Year);
|
||||
Assert.Equal(6, result.PublishDate.Value.Month);
|
||||
Assert.Equal(15, result.PublishDate.Value.Day);
|
||||
Assert.Equal("en", result.LanguageCode);
|
||||
Assert.Equal("The Series", result.Series);
|
||||
Assert.Equal(2.5, result.SeriesIndex);
|
||||
Assert.Equal(8, result.Rating);
|
||||
Assert.Equal("cover.jpg", result.CoverHref);
|
||||
Assert.Equal(2, result.Creators.Count);
|
||||
Assert.Equal("Test Author", result.Creators[0].Name);
|
||||
Assert.Equal("aut", result.Creators[0].Role);
|
||||
Assert.Equal(2, result.Contributors.Count);
|
||||
Assert.Equal("calibre (3.48.0) [https://calibre-ebook.com]", result.Contributors[0].Name);
|
||||
Assert.Equal("bkp", result.Contributors[0].Role);
|
||||
Assert.Equal(7, result.Identifiers.Count);
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ISBN" && identifier.Value == "978-0-306-40615-7");
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "goodreads" && identifier.Value == "123456");
|
||||
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ASIN" && identifier.Value == "B00TEST1");
|
||||
Assert.Equal(2, result.Subjects.Count);
|
||||
Assert.Equal("Science fiction", result.Subjects[0]);
|
||||
Assert.Equal("Space opera", result.Subjects[1]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileDoesNotExist_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(Path.Combine(Path.GetTempPath(), $"missing-{Guid.NewGuid():N}.opf"));
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
Assert.Empty(result.Creators);
|
||||
Assert.Empty(result.Subjects);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenMetadataContainsWhitespaceOnlyValues_ShouldSkipThem()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title> </dc:title><dc:creator opf:role="aut"> </dc:creator><dc:subject></dc:subject></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Creators);
|
||||
Assert.Empty(result.Subjects);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenMetadataElementIsMissing_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileDeclaresADoctypeWithEntities_ShouldNotExpandThem()
|
||||
{
|
||||
// Arrange
|
||||
// the DTD is prohibited, so the document must not be parsed and the entity must not be expanded into the title
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><!DOCTYPE package [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&xxe;</dc:title></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileContainsBillionLaughsStyleEntityExpansion_ShouldNotExpandThem()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<?xml version="1.0" encoding="utf-8"?><!DOCTYPE lolz [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"><!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&lol3;</dc:title></metadata></package>""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileIsOversized_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = Path.Combine(Path.GetTempPath(), $"oversized-{Guid.NewGuid():N}.opf");
|
||||
string oversizedContent = $"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>{new string('a', (5 * 1024 * 1024) + 100)}</dc:title></metadata></package>""";
|
||||
File.WriteAllText(opfFilePath, oversizedContent);
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_WhenOpfFileIsMalformedXml_ShouldReturnEmptyDocument()
|
||||
{
|
||||
// Arrange
|
||||
string opfFilePath = CreateOpfFile("""<package xmlns="http://www.idpf.org/2007/opf"><metadata><dc:title>Unclosed""");
|
||||
|
||||
try
|
||||
{
|
||||
// Act
|
||||
OpfDocumentDto result = OpfReader.Read(opfFilePath);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.Title);
|
||||
Assert.Empty(result.Identifiers);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(opfFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a temporary OPF file with the given content and returns its path.
|
||||
/// </summary>
|
||||
/// <param name="content">The content of the OPF file to create.</param>
|
||||
/// <returns>The file system path of the created OPF file.</returns>
|
||||
private static string CreateOpfFile(string content)
|
||||
{
|
||||
string opfFilePath = Path.Combine(Path.GetTempPath(), $"opf-{Guid.NewGuid():N}.opf");
|
||||
File.WriteAllText(opfFilePath, content);
|
||||
return opfFilePath;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user