-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements blocked attachments and allowed and blocked mime types Dyn…
- Loading branch information
1 parent
fe3db51
commit b195f5e
Showing
8 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/BlockedAttachmentException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions | ||
{ | ||
/// <summary> | ||
/// Exception raised when a file is uploaded and its file extension is blocked by the BlockedAttachment settings | ||
/// </summary> | ||
public class BlockedAttachmentException: Exception | ||
{ | ||
/// <summary> | ||
/// Default constructor | ||
/// </summary> | ||
internal BlockedAttachmentException(string fileName) : base($"The attachment with file name '{fileName}' is not a valid file extension type") | ||
{ | ||
|
||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/BlockedMimeTypeException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions | ||
{ | ||
/// <summary> | ||
/// Exception raised when a file is uploaded and its file MIME type is blocked by either the BlockedMimeType or AllowedMimeType settings | ||
/// </summary> | ||
public class BlockedMimeTypeException: Exception | ||
{ | ||
/// <summary> | ||
/// Default constructor | ||
/// </summary> | ||
internal BlockedMimeTypeException(string fileName, string mimeType) : base($"The MIME Type '{mimeType}' for file name '{fileName}' is not a valid") | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/FakeXrmEasy.Core/FileStorage/OrganizationFileSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace FakeXrmEasy.Core.FileStorage | ||
{ | ||
/// <summary> | ||
/// Contains the current organization settings that are relevant to file storage | ||
/// </summary> | ||
internal class OrganizationFileSettings | ||
{ | ||
internal string[] BlockedAttachments { get; set; } | ||
internal string[] AllowedMimeTypes { get; set; } | ||
internal string[] BlockedMimeTypes { get; set; } | ||
|
||
private const char separator = ';'; | ||
|
||
private const string DEFAULT_BLOCKED_ATTACHMENTS = | ||
@"ade;adp;app;asa;ashx;asmx;asp;bas;bat;cdx;cer;chm;class;cmd;com;config;cpl;crt;csh;dll;exe;fxp;hlp;hta;htr;htw;ida;idc;idq;inf;ins;isp;its;jar;js;jse;ksh;lnk;mad;maf;mag;mam;maq;mar;mas;mat;mau;mav;maw;mda;mdb;mde;mdt;mdw;mdz;msc;msh;msh1;msh1xml;msh2;msh2xml;mshxml;msi;msp;mst;ops;pcd;pif;prf;prg;printer;pst;reg;rem;scf;scr;sct;shb;shs;shtm;shtml;soap;stm;tmp;url;vb;vbe;vbs;vsmacros;vss;vst;vsw;ws;wsc;wsf;wsh;svg"; | ||
|
||
internal OrganizationFileSettings() | ||
{ | ||
BlockedAttachments = FromCommaSeparated(DEFAULT_BLOCKED_ATTACHMENTS); | ||
AllowedMimeTypes = new string[] { }; | ||
BlockedMimeTypes = new string[] { }; | ||
} | ||
|
||
internal static string[] FromCommaSeparated(string commaSeparatedValues) | ||
{ | ||
if (commaSeparatedValues.IndexOf(separator) >= 0) | ||
{ | ||
return commaSeparatedValues.Split(separator); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(commaSeparatedValues)) | ||
{ | ||
return new string[] { commaSeparatedValues }; | ||
} | ||
|
||
return new string[] { }; | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
tests/FakeXrmEasy.Core.Tests/FileStorage/Db/BlockedAttachmentsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using DataverseEntities; | ||
using FakeXrmEasy.Core.Db; | ||
using FakeXrmEasy.Core.FileStorage.Db; | ||
using FakeXrmEasy.Core.FileStorage.Db.Exceptions; | ||
using Microsoft.Xrm.Sdk; | ||
using Xunit; | ||
using FileAttachment = FakeXrmEasy.Core.FileStorage.Db.FileAttachment; | ||
|
||
namespace FakeXrmEasy.Core.Tests.FileStorage.Db | ||
{ | ||
public class BlockedAttachmentsTests: FakeXrmEasyTestsBase | ||
{ | ||
private const string FILE_ATTRIBUTE_NAME = "dv_file"; | ||
|
||
private readonly InMemoryDb _db; | ||
private readonly InMemoryFileDb _fileDb; | ||
private readonly Entity _entity; | ||
private readonly FileAttachment _file; | ||
|
||
private readonly Entity _organization; | ||
|
||
private const string BLOCKED_ATTACHMENTS_ATTRIBUTE = "blockedattachments"; | ||
|
||
public BlockedAttachmentsTests() | ||
{ | ||
_db = (_context as XrmFakedContext).Db; | ||
_fileDb = (_context as XrmFakedContext).FileDb; | ||
|
||
_entity = new Entity(dv_test.EntityLogicalName) | ||
{ | ||
Id = Guid.NewGuid(), | ||
}; | ||
|
||
_file = new FileAttachment() | ||
{ | ||
Id = Guid.NewGuid().ToString(), | ||
MimeType = "application/pdf", | ||
FileName = "TestFile.pdf", | ||
Target = _entity.ToEntityReference(), | ||
AttributeName = FILE_ATTRIBUTE_NAME, | ||
Content = new byte[] { 1, 2, 3, 4 } | ||
}; | ||
|
||
_organization = new Entity(Organization.EntityLogicalName) | ||
{ | ||
Id = Guid.NewGuid() | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void Should_throw_blocked_attachment_exception_if_a_file_extension_is_not_allowed() | ||
{ | ||
_file.FileName = "TestFile.ade"; | ||
Assert.Throws<BlockedAttachmentException>(() => _fileDb.AddFile(_file)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null, "File.ade")] | ||
[InlineData("asmx", "File.ade")] | ||
[InlineData("asmx;php", "File.ade")] | ||
[InlineData("asmx;php", "File.pdf")] | ||
public void Should_not_throw_blocked_attachment_exception_with_a_custom_blocked_attachments_file_extension(string blockedAttachments, string fileName) | ||
{ | ||
_organization[BLOCKED_ATTACHMENTS_ATTRIBUTE] = blockedAttachments; | ||
|
||
_context.Initialize(new List<Entity>() | ||
{ | ||
_organization, _entity | ||
}); | ||
|
||
_file.FileName = fileName; | ||
_fileDb.AddFile(_file); | ||
|
||
var fileAdded = _fileDb.GetAllFiles().FirstOrDefault(f => f.Id == _file.Id); | ||
Assert.NotNull(fileAdded); | ||
} | ||
|
||
[Theory] | ||
[InlineData("asmx", "File.asmx")] | ||
[InlineData("asmx;php", "File.asmx")] | ||
[InlineData("asmx;php", "File.php")] | ||
public void Should_throw_blocked_attachment_exception_with_a_custom_blocked_attachments_file_extension(string blockedAttachments, string fileName) | ||
{ | ||
_organization[BLOCKED_ATTACHMENTS_ATTRIBUTE] = blockedAttachments; | ||
|
||
_context.Initialize(new List<Entity>() | ||
{ | ||
_organization, _entity | ||
}); | ||
|
||
_file.FileName = fileName; | ||
Assert.Throws<BlockedAttachmentException>(() => _fileDb.AddFile(_file)); | ||
} | ||
} | ||
} |
Oops, something went wrong.