|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using NUnit.Framework; |
| 3 | +using Umbraco.Cms.Core.Configuration.Models; |
| 4 | +using Umbraco.Cms.Core.Models.TemporaryFile; |
| 5 | +using Umbraco.Cms.Core.Services; |
| 6 | +using Umbraco.Cms.Core.Services.OperationStatus; |
| 7 | +using Umbraco.Cms.Tests.Common.Testing; |
| 8 | +using Umbraco.Cms.Tests.Integration.Attributes; |
| 9 | +using Umbraco.Cms.Tests.Integration.Testing; |
| 10 | + |
| 11 | +namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services; |
| 12 | + |
| 13 | +[TestFixture] |
| 14 | +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)] |
| 15 | +public class TemporaryFileServiceTests : UmbracoIntegrationTest |
| 16 | +{ |
| 17 | + private ITemporaryFileService TemporaryFileService => GetRequiredService<ITemporaryFileService>(); |
| 18 | + |
| 19 | + public static void ConfigureAllowedUploadedFileExtensions(IUmbracoBuilder builder) |
| 20 | + { |
| 21 | + builder.Services.Configure<ContentSettings>(config => |
| 22 | + config.AllowedUploadedFileExtensions = ["txt"]); |
| 23 | + } |
| 24 | + |
| 25 | + [Test] |
| 26 | + [ConfigureBuilder(ActionName = nameof(ConfigureAllowedUploadedFileExtensions))] |
| 27 | + public async Task Can_Create_Get_And_Delete_Temporary_File() |
| 28 | + { |
| 29 | + var key = Guid.NewGuid(); |
| 30 | + const string FileName = "test.txt"; |
| 31 | + const string FileContents = "test"; |
| 32 | + var model = new CreateTemporaryFileModel |
| 33 | + { |
| 34 | + FileName = FileName, |
| 35 | + Key = key, |
| 36 | + OpenReadStream = () => |
| 37 | + { |
| 38 | + var stream = new MemoryStream(); |
| 39 | + var writer = new StreamWriter(stream); |
| 40 | + writer.Write(FileContents); |
| 41 | + writer.Flush(); |
| 42 | + stream.Position = 0; |
| 43 | + return stream; |
| 44 | + } |
| 45 | + }; |
| 46 | + var createAttempt = await TemporaryFileService.CreateAsync(model); |
| 47 | + Assert.IsTrue(createAttempt.Success); |
| 48 | + |
| 49 | + TemporaryFileModel? fileModel = await TemporaryFileService.GetAsync(key); |
| 50 | + Assert.IsNotNull(fileModel); |
| 51 | + Assert.AreEqual(key, fileModel.Key); |
| 52 | + Assert.AreEqual(FileName, fileModel.FileName); |
| 53 | + |
| 54 | + using (var reader = new StreamReader(fileModel.OpenReadStream())) |
| 55 | + { |
| 56 | + string fileContents = reader.ReadToEnd(); |
| 57 | + Assert.AreEqual(FileContents, fileContents); |
| 58 | + } |
| 59 | + |
| 60 | + var deleteAttempt = await TemporaryFileService.DeleteAsync(key); |
| 61 | + Assert.IsTrue(createAttempt.Success); |
| 62 | + |
| 63 | + fileModel = await TemporaryFileService.GetAsync(key); |
| 64 | + Assert.IsNull(fileModel); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + [ConfigureBuilder(ActionName = nameof(ConfigureAllowedUploadedFileExtensions))] |
| 69 | + public async Task Cannot_Create_File_Outside_Of_Temporary_Files_Root() |
| 70 | + { |
| 71 | + var key = Guid.NewGuid(); |
| 72 | + const string FileName = "../test.txt"; |
| 73 | + var model = new CreateTemporaryFileModel |
| 74 | + { |
| 75 | + FileName = FileName, |
| 76 | + Key = key, |
| 77 | + OpenReadStream = () => |
| 78 | + { |
| 79 | + var stream = new MemoryStream(); |
| 80 | + var writer = new StreamWriter(stream); |
| 81 | + writer.Write(string.Empty); |
| 82 | + writer.Flush(); |
| 83 | + stream.Position = 0; |
| 84 | + return stream; |
| 85 | + } |
| 86 | + }; |
| 87 | + var createAttempt = await TemporaryFileService.CreateAsync(model); |
| 88 | + Assert.IsFalse(createAttempt.Success); |
| 89 | + Assert.AreEqual(TemporaryFileOperationStatus.InvalidFileName, createAttempt.Status); |
| 90 | + } |
| 91 | +} |
0 commit comments