|
| 1 | +using Microsoft.AspNetCore.Hosting; |
| 2 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
| 3 | +using Microsoft.Extensions.FileProviders; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using Moq; |
| 7 | +using NUnit.Framework; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.IO; |
| 11 | +using System.Text; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Umbraco.Cms.Core.Configuration.Models; |
| 14 | +using Umbraco.Cms.Core.IO; |
| 15 | +using Umbraco.Cms.Core.Models.PublishedContent; |
| 16 | +using Umbraco.Cms.Core.Routing; |
| 17 | + |
| 18 | +namespace Our.Umbraco.TagHelpers.Tests |
| 19 | +{ |
| 20 | + public class InlineSvgTagHelperTests |
| 21 | + { |
| 22 | + private TagHelperContext _context = null!; |
| 23 | + private TagHelperOutput _output = null!; |
| 24 | + |
| 25 | + [SetUp] |
| 26 | + public void Setup() |
| 27 | + { |
| 28 | + var attributes = new TagHelperAttributeList |
| 29 | + { |
| 30 | + { "src", "test-src" }, |
| 31 | + { "media-item", "test-media" } |
| 32 | + }; |
| 33 | + _context = new TagHelperContext(attributes, new Dictionary<object, object>(), "test"); |
| 34 | + _output = new TagHelperOutput("umb-svg", attributes, (result, encoder) => |
| 35 | + { |
| 36 | + var content = new DefaultTagHelperContent(); |
| 37 | + content.SetContent("Something else"); |
| 38 | + return Task.FromResult<TagHelperContent>(content); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + [Test] |
| 43 | + public void NoOutputIfNoMediaOrFileSet() |
| 44 | + { |
| 45 | + var tagHelper = new InlineSvgTagHelper(null, null, null); |
| 46 | + |
| 47 | + tagHelper.Process(_context, _output); |
| 48 | + |
| 49 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void NoOutputIfBothMediaAndFileSet() |
| 54 | + { |
| 55 | + var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media); |
| 56 | + var tagHelper = new InlineSvgTagHelper(null, null, null) |
| 57 | + { |
| 58 | + FileSource = "test.svg", |
| 59 | + MediaItem = umbContent |
| 60 | + }; |
| 61 | + |
| 62 | + tagHelper.Process(_context, _output); |
| 63 | + |
| 64 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void NoOutputIfFileNotSvg() |
| 69 | + { |
| 70 | + var tagHelper = new InlineSvgTagHelper(null, null, null) |
| 71 | + { |
| 72 | + FileSource = "test.notsvg" |
| 73 | + }; |
| 74 | + |
| 75 | + tagHelper.Process(_context, _output); |
| 76 | + |
| 77 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void NoOutputIfFileNotFound() |
| 82 | + { |
| 83 | + var fileProvider = new Mock<IFileProvider>(); |
| 84 | + fileProvider.Setup(p => p.GetFileInfo(It.IsAny<string>())).Returns(Mock.Of<IFileInfo>(f => !f.Exists)); |
| 85 | + var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object); |
| 86 | + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) |
| 87 | + { |
| 88 | + FileSource = "test.svg" |
| 89 | + }; |
| 90 | + |
| 91 | + tagHelper.Process(_context, _output); |
| 92 | + |
| 93 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public void ExpectedOutputIfValidFile() |
| 98 | + { |
| 99 | + var fileProvider = new Mock<IFileProvider>(); |
| 100 | + fileProvider.Setup(p => p.GetFileInfo(It.IsAny<string>())).Returns(Mock.Of<IFileInfo>(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("test svg")))); |
| 101 | + var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object); |
| 102 | + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) |
| 103 | + { |
| 104 | + FileSource = "test.svg" |
| 105 | + }; |
| 106 | + |
| 107 | + tagHelper.Process(_context, _output); |
| 108 | + |
| 109 | + Assert.IsNull(_output?.TagName); |
| 110 | + Assert.AreEqual(_output.Content.GetContent(), "test svg"); |
| 111 | + Assert.IsFalse(_output.Attributes.ContainsName("src")); |
| 112 | + Assert.IsFalse(_output.Attributes.ContainsName("media-item")); |
| 113 | + } |
| 114 | + |
| 115 | + [Test] |
| 116 | + public void NoOutputIfMediaUrlNull() |
| 117 | + { |
| 118 | + var urlProvider = new Mock<IPublishedUrlProvider>(); |
| 119 | + urlProvider.Setup(p => p.GetMediaUrl(It.IsAny<IPublishedContent>(), It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns((string)null!); |
| 120 | + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object) |
| 121 | + { |
| 122 | + MediaItem = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media) |
| 123 | + }; |
| 124 | + |
| 125 | + tagHelper.Process(_context, _output); |
| 126 | + |
| 127 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void NoOutputIfMediaNotSvg() |
| 132 | + { |
| 133 | + var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media); |
| 134 | + var urlProvider = new Mock<IPublishedUrlProvider>(); |
| 135 | + urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns("test.notsvg"); |
| 136 | + var tagHelper = new InlineSvgTagHelper(null, null, urlProvider.Object) |
| 137 | + { |
| 138 | + MediaItem = umbContent |
| 139 | + }; |
| 140 | + |
| 141 | + tagHelper.Process(_context, _output); |
| 142 | + |
| 143 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 144 | + } |
| 145 | + |
| 146 | + [Test] |
| 147 | + public void NoOutputIfMediaNotFound() |
| 148 | + { |
| 149 | + var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media); |
| 150 | + var urlProvider = new Mock<IPublishedUrlProvider>(); |
| 151 | + urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns("test.svg"); |
| 152 | + var fileSystem = Mock.Of<IFileSystem>(fs => !fs.FileExists(It.IsAny<string>())); |
| 153 | + var tagHelper = new InlineSvgTagHelper( |
| 154 | + new MediaFileManager(fileSystem, null, null, null, null, Mock.Of<IOptions<ContentSettings>>()), |
| 155 | + null, |
| 156 | + urlProvider.Object) |
| 157 | + { |
| 158 | + MediaItem = umbContent |
| 159 | + }; |
| 160 | + |
| 161 | + tagHelper.Process(_context, _output); |
| 162 | + |
| 163 | + Assert.IsTrue(_output.Content.IsEmptyOrWhiteSpace); |
| 164 | + } |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void ExpectedOutputIfValidMedia() |
| 168 | + { |
| 169 | + var umbContent = Mock.Of<IPublishedContent>(c => c.ContentType.ItemType == PublishedItemType.Media); |
| 170 | + var urlProvider = new Mock<IPublishedUrlProvider>(); |
| 171 | + urlProvider.Setup(p => p.GetMediaUrl(umbContent, It.IsAny<UrlMode>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Uri>())).Returns("test.svg"); |
| 172 | + var fileSystem = Mock.Of<IFileSystem>(fs => fs.FileExists(It.IsAny<string>()) && fs.OpenFile(It.IsAny<string>()) == new MemoryStream(Encoding.UTF8.GetBytes("test svg"))); |
| 173 | + var tagHelper = new InlineSvgTagHelper( |
| 174 | + new MediaFileManager(fileSystem, null, null, null, null, Mock.Of<IOptions<ContentSettings>>()), |
| 175 | + null, |
| 176 | + urlProvider.Object) |
| 177 | + { |
| 178 | + MediaItem = umbContent |
| 179 | + }; |
| 180 | + |
| 181 | + tagHelper.Process(_context, _output); |
| 182 | + |
| 183 | + Assert.IsNull(_output?.TagName); |
| 184 | + Assert.AreEqual("test svg", _output.Content.GetContent()); |
| 185 | + Assert.IsFalse(_output.Attributes.ContainsName("src")); |
| 186 | + Assert.IsFalse(_output.Attributes.ContainsName("media-item")); |
| 187 | + } |
| 188 | + |
| 189 | + [Test] |
| 190 | + public void SanitizesJavascript() |
| 191 | + { |
| 192 | + var fileProvider = new Mock<IFileProvider>(); |
| 193 | + fileProvider |
| 194 | + .Setup(p => p.GetFileInfo(It.IsAny<string>())) |
| 195 | + .Returns(Mock.Of<IFileInfo>(f => f.Exists && f.CreateReadStream() == new MemoryStream(Encoding.UTF8.GetBytes("<a xlink:href=\"javascript:alert('test');\">Click here</a><script attr=\"test\">test</script>end")))); |
| 196 | + var hostEnv = Mock.Of<IWebHostEnvironment>(e => e.WebRootFileProvider == fileProvider.Object); |
| 197 | + var tagHelper = new InlineSvgTagHelper(null, hostEnv, null) |
| 198 | + { |
| 199 | + FileSource = "test.svg" |
| 200 | + }; |
| 201 | + |
| 202 | + tagHelper.Process(_context, _output); |
| 203 | + |
| 204 | + Assert.AreEqual("<a xlink:href=\"syntax:error:alert('test');\">Click here</a>end", _output.Content.GetContent()); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments