Skip to content

Commit

Permalink
Adding Image upload/download tests DynamicsValue/fake-xrm-easy#157
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 15, 2024
1 parent b195f5e commit 6cd45ff
Show file tree
Hide file tree
Showing 14 changed files with 692 additions and 20 deletions.
21 changes: 16 additions & 5 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,24 @@ private void ValidateMaxFileSize(FileUploadSession fileUploadSession, FileAttach
return;
}

var fileAttributeMetadata = attributeMetadata as FileAttributeMetadata;

if ((decimal) fileAttachment.Content.Length / 1024 > fileAttributeMetadata.MaxSizeInKB)
if (attributeMetadata is FileAttributeMetadata fileAttributeMetadata)
{
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
fileAttributeMetadata.MaxSizeInKB.Value);
if ((decimal) fileAttachment.Content.Length / 1024 > fileAttributeMetadata.MaxSizeInKB)
{
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
fileAttributeMetadata.MaxSizeInKB.Value);
}
}

if (attributeMetadata is ImageAttributeMetadata imageAttributeMetadata)
{
if ((decimal) fileAttachment.Content.Length / 1024 > imageAttributeMetadata.MaxSizeInKB)
{
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
imageAttributeMetadata.MaxSizeInKB.Value);
}
}

}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class BlockedAttachmentsTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class BlockedMimeTypeTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class DeleteFileTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using FakeXrmEasy.Core.FileStorage;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Upload;
using Xunit;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class FileUploadSessionTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class InMemoryFileDbDownloaderTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Microsoft.Xrm.Sdk.Metadata;
#endif

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class InMemoryFileDbUploaderTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Files
{
public class UpdateFileTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using DataverseEntities;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Download;
using Microsoft.Xrm.Sdk;
using Xunit;
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Images
{
public class DeleteFileTests
{
private const string IMAGE_ATTRIBUTE_NAME = "dv_image";

private readonly InMemoryDb _db;
private readonly InMemoryFileDb _fileDb;
private readonly Entity _entity;
private readonly FileAttachment _file;

public DeleteFileTests()
{
_db = new InMemoryDb();
_fileDb = new InMemoryFileDb(_db);

_entity = new Entity(dv_test.EntityLogicalName)
{
Id = Guid.NewGuid(),
};

_file = new FileAttachment()
{
Id = Guid.NewGuid().ToString(),
MimeType = "image/png",
FileName = "MyImage.png",
Target = _entity.ToEntityReference(),
AttributeName = IMAGE_ATTRIBUTE_NAME,
Content = new byte[] { 1, 2, 3, 4 }
};

_entity[IMAGE_ATTRIBUTE_NAME] = _file.Id;
}

[Fact]
public void Should_be_empty_when_no_files_are_added()
{
Assert.Empty(_fileDb.GetAllFiles());
}

[Fact]
public void Should_throw_exception_when_a_file_doesnt_exists()
{
Assert.Throws<CouldNotDeleteFileException>(() => _fileDb.DeleteFile("invalid id"));
}

[Fact]
public void Should_delete_an_existing_file()
{
_db.AddEntityRecord(_entity);
_fileDb.AddFile(_file);

_fileDb.DeleteFile(_file.Id);

Assert.Empty(_fileDb.GetAllFiles());

var entityAfter = _db.GetTable(_entity.LogicalName).GetById(_entity.Id);
Assert.Null(entityAfter[_file.AttributeName]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Upload;
using Xunit;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db.Images
{
public class FileUploadSessionTests
{
private readonly FileUploadSession _session;

public FileUploadSessionTests()
{
_session = new FileUploadSession() { FileUploadSessionId = new Guid().ToString() };
}

[Fact]
public void Should_be_empty_when_created()
{
Assert.Empty(_session.GetAllBlocks());
}

[Fact]
public void Should_add_new_block_to_existing_session()
{
var blockId = Guid.NewGuid().ToString();
_session.AddFileBlock(new UploadBlockProperties()
{
BlockId = blockId,
BlockContents = new byte[] { 1, 2, 3, 4 },
FileContinuationToken = _session.FileUploadSessionId
});

var fileBlock = _session.GetFileBlock(blockId);
Assert.NotNull(fileBlock);
Assert.Equal(new byte[] {1 , 2, 3, 4}, fileBlock.Content);
}

[Fact]
public void Should_throw_exception_if_a_block_already_exists()
{
var blockId = Guid.NewGuid().ToString();
var uploadBlockProperties = new UploadBlockProperties()
{
BlockId = blockId,
BlockContents = new byte[] { 1, 2, 3, 4 },
FileContinuationToken = _session.FileUploadSessionId
};

//First attempt ok
_session.AddFileBlock(uploadBlockProperties);

//Second fails
Assert.Throws<DuplicateFileBlockException>(() => _session.AddFileBlock(uploadBlockProperties));
}

[Fact]
public void Should_convert_file_upload_session_to_file_attachment()
{
//Add two blocks
var blockId1 = Guid.NewGuid().ToString();
var uploadBlockProperties = new UploadBlockProperties()
{
BlockId = blockId1,
BlockContents = new byte[] { 1, 2, 3, 4 },
FileContinuationToken = _session.FileUploadSessionId
};
_session.AddFileBlock(uploadBlockProperties);

var blockId2 = Guid.NewGuid().ToString();
uploadBlockProperties = new UploadBlockProperties()
{
BlockId = blockId2,
BlockContents = new byte[] { 5, 6, 7, 8 },
FileContinuationToken = _session.FileUploadSessionId
};
_session.AddFileBlock(uploadBlockProperties);

var commitProperties = new CommitFileUploadSessionProperties()
{
FileUploadSessionId = _session.FileUploadSessionId,
FileName = "MyImage.png",
MimeType = "image/png",
BlockIdsListSequence = new[] { blockId2, blockId1 } //2 comes first
};

var fileAttachment = _session.ToFileAttachment(commitProperties);
Assert.NotNull(fileAttachment);
Assert.Equal(new byte[] { 5, 6, 7, 8, 1, 2, 3, 4 }, fileAttachment.Content);
Assert.Equal(commitProperties.FileName, fileAttachment.FileName);
Assert.Equal(commitProperties.MimeType, fileAttachment.MimeType);
}
}
}
Loading

0 comments on commit 6cd45ff

Please sign in to comment.