Beispiel #1
0
        public async Task Upload_File_And_Save_Local_Disk_Test()
        {
            if (!_context.AllowMySQL)
            {
                Assert.True(true);
                return;
            }

            // 1. Arrange
            Mock <IFormFile> mockFile     = new Mock <IFormFile>();
            FileStream       sourceImg    = System.IO.File.OpenRead(@"Artifacts\connect-iot-to-internet.jpg");
            MemoryStream     memoryStream = new MemoryStream();
            await sourceImg.CopyToAsync(memoryStream);

            sourceImg.Close();
            memoryStream.Position = 0;
            string fileName = "connect-iot-to-internet.jpg";

            mockFile.Setup(f => f.Length).Returns(memoryStream.Length).Verifiable();
            mockFile.Setup(f => f.FileName).Returns(fileName).Verifiable();
            mockFile.Setup(f => f.OpenReadStream()).Returns(memoryStream).Verifiable();
            mockFile
            .Setup(f => f.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns((Stream stream, CancellationToken token) => memoryStream.CopyToAsync(stream))
            .Verifiable();

            DiskStorageOptions diskOptions = new DiskStorageOptions
            {
                AllowDayFolder = true,
                Path           = "~/UploadFiles"
            };

            DatabaseOptions databaseOptions = new Core.Persistences.DatabaseOptions
            {
                ConnectionString = _context.MySqlDatabaseConnection.ConnectionString,
                ConnectionType   = Core.Persistences.ConnectionType.MySQL,
                Datasource       = _context.MySqlDatabaseConnection.DataSource
            };

            LetPortal.Portal.Options.Files.FileOptions localFileOptions = FileOptions;
            localFileOptions.DiskStorageOptions = diskOptions;
            localFileOptions.FileStorageType    = FileStorageType.Disk;

#pragma warning disable CA2000 // Dispose objects before losing scope
            FileEFRepository fileRepository = new FileEFRepository(_context.GetMySQLContext());
#pragma warning restore CA2000 // Dispose objects before losing scope
            IOptionsMonitor <FilePublishOptions> filePublishOptionsMock = Mock.Of <IOptionsMonitor <FilePublishOptions> >(_ => _.CurrentValue == FilePublishOptions);
            IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> fileOptionsMock = Mock.Of <IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> >(_ => _.CurrentValue == localFileOptions);
            IOptionsMonitor <FileValidatorOptions> fileValidatorMock      = Mock.Of <IOptionsMonitor <FileValidatorOptions> >(_ => _.CurrentValue == localFileOptions.ValidatorOptions);
            IOptionsMonitor <DiskStorageOptions>   diskStorageOptionsMock = Mock.Of <IOptionsMonitor <DiskStorageOptions> >(_ => _.CurrentValue == diskOptions);
            CheckFileExtensionRule checkFileExtensionRule = new CheckFileExtensionRule(fileValidatorMock);
            CheckFileSizeRule      checkFileSizeRule      = new CheckFileSizeRule(fileValidatorMock);

            DiskFileConnectorExecution diskStorage = new DiskFileConnectorExecution(diskStorageOptionsMock);

            FileService fileService = new FileService(
                fileOptionsMock,
                filePublishOptionsMock,
                new List <IFileConnectorExecution>
            {
                diskStorage
            }, new List <IFileValidatorRule>
            {
                checkFileExtensionRule,
                checkFileSizeRule
            }, fileRepository);

            // Act
            LetPortal.Portal.Models.Files.ResponseUploadFile result = await fileService.UploadFileAsync(mockFile.Object, "tester", false);

            memoryStream.Close();
            fileRepository.Dispose();
            // 3. Assert
            Assert.True(!string.IsNullOrEmpty(result.FileId));
        }
        public async Task Upload_File_Test()
        {
            // 1. Arrange
            Mock <IFormFile> mockFile     = new Mock <IFormFile>();
            FileStream       sourceImg    = System.IO.File.OpenRead(@"Artifacts\connect-iot-to-internet.jpg");
            MemoryStream     memoryStream = new MemoryStream();
            await sourceImg.CopyToAsync(memoryStream);

            sourceImg.Close();
            memoryStream.Position = 0;
            string fileName = "connect-iot-to-internet.jpg";

            mockFile.Setup(f => f.Length).Returns(memoryStream.Length).Verifiable();
            mockFile.Setup(f => f.FileName).Returns(fileName).Verifiable();
            mockFile.Setup(f => f.OpenReadStream()).Returns(memoryStream).Verifiable();
            mockFile
            .Setup(f => f.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns((Stream stream, CancellationToken token) => memoryStream.CopyToAsync(stream))
            .Verifiable();
            Mock <IFileRepository> mockFileRepository = new Mock <IFileRepository>();

            mockFileRepository
            .Setup(a => a.AddAsync(It.IsAny <LetPortal.Portal.Entities.Files.File>()))
            .Returns(Task.CompletedTask);

            DatabaseOptions databaseOptions = new Core.Persistences.DatabaseOptions
            {
                ConnectionString = "mongodb://localhost:27017",
                ConnectionType   = Core.Persistences.ConnectionType.MongoDB,
                Datasource       = "letportal"
            };
            IOptionsMonitor <FilePublishOptions> filePublishOptionsMock = Mock.Of <IOptionsMonitor <FilePublishOptions> >(_ => _.CurrentValue == FilePublishOptions);
            IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> fileOptionsMock = Mock.Of <IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> >(_ => _.CurrentValue == FileOptions);
            IOptionsMonitor <FileValidatorOptions>   fileValidatorMock          = Mock.Of <IOptionsMonitor <FileValidatorOptions> >(_ => _.CurrentValue == FileOptions.ValidatorOptions);
            IOptionsMonitor <DatabaseOptions>        databaseOptionsMock        = Mock.Of <IOptionsMonitor <DatabaseOptions> >(_ => _.CurrentValue == databaseOptions);
            IOptionsMonitor <DatabaseStorageOptions> databaseStorageOptionsMock = Mock.Of <IOptionsMonitor <DatabaseStorageOptions> >(_ => _.CurrentValue == FileOptions.DatabaseStorageOptions);

            CheckFileExtensionRule checkFileExtensionRule = new CheckFileExtensionRule(fileValidatorMock);
            CheckFileSizeRule      checkFileSizeRule      = new CheckFileSizeRule(fileValidatorMock);

            Mock <IFileConnectorExecution> mockDatabaseFileConnectorExecution = new Mock <IFileConnectorExecution>();

            mockDatabaseFileConnectorExecution
            .Setup(a => a.FileStorageType)
            .Returns(FileStorageType.Database);

            mockDatabaseFileConnectorExecution
            .Setup(a => a.StoreFileAsync(It.IsAny <IFormFile>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new StoredFile
            {
                DownloadableUrl       = "localhost",
                FileIdentifierOptions = "test",
                UseServerHost         = true
            }));

            FileService fileService = new FileService(
                fileOptionsMock,
                filePublishOptionsMock,
                new List <IFileConnectorExecution>
            {
                mockDatabaseFileConnectorExecution.Object
            }, new List <IFileValidatorRule>
            {
                checkFileExtensionRule,
                checkFileSizeRule
            }, mockFileRepository.Object);

            // Act
            LetPortal.Portal.Models.Files.ResponseUploadFile result = await fileService.UploadFileAsync(mockFile.Object, "tester", false);

            memoryStream.Close();

            // 3. Assert
            Assert.True(!string.IsNullOrEmpty(result.FileId));
        }
Beispiel #3
0
        public async Task Upload_Then_Download_File_Sql_Server_Test()
        {
            if (!_context.AllowSQLServer)
            {
                Assert.True(true);
                return;
            }
            // 1. Arrange
            Mock <IFormFile> mockFile     = new Mock <IFormFile>();
            FileStream       sourceImg    = System.IO.File.OpenRead(@"Artifacts\connect-iot-to-internet.jpg");
            MemoryStream     memoryStream = new MemoryStream();
            await sourceImg.CopyToAsync(memoryStream);

            sourceImg.Close();
            memoryStream.Position = 0;
            string fileName = "connect-iot-to-internet.jpg";

            mockFile.Setup(f => f.Length).Returns(memoryStream.Length).Verifiable();
            mockFile.Setup(f => f.FileName).Returns(fileName).Verifiable();
            mockFile.Setup(f => f.OpenReadStream()).Returns(memoryStream).Verifiable();
            mockFile
            .Setup(f => f.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns((Stream stream, CancellationToken token) => memoryStream.CopyToAsync(stream))
            .Verifiable();

            DatabaseOptions databaseOptions = new Core.Persistences.DatabaseOptions
            {
                ConnectionString = _context.SqlServerDatabaseConnection.ConnectionString,
                ConnectionType   = Core.Persistences.ConnectionType.SQLServer,
                Datasource       = _context.SqlServerDatabaseConnection.DataSource
            };

#pragma warning disable CA2000 // Dispose objects before losing scope
            FileEFRepository fileRepository = new FileEFRepository(_context.GetSQLServerContext());
#pragma warning restore CA2000 // Dispose objects before losing scope
            IOptionsMonitor <FilePublishOptions> filePublishOptionsMock = Mock.Of <IOptionsMonitor <FilePublishOptions> >(_ => _.CurrentValue == FilePublishOptions);
            IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> fileOptionsMock = Mock.Of <IOptionsMonitor <LetPortal.Portal.Options.Files.FileOptions> >(_ => _.CurrentValue == FileOptions);
            IOptionsMonitor <FileValidatorOptions>   fileValidatorMock          = Mock.Of <IOptionsMonitor <FileValidatorOptions> >(_ => _.CurrentValue == FileOptions.ValidatorOptions);
            IOptionsMonitor <DatabaseOptions>        databaseOptionsMock        = Mock.Of <IOptionsMonitor <DatabaseOptions> >(_ => _.CurrentValue == databaseOptions);
            IOptionsMonitor <DatabaseStorageOptions> databaseStorageOptionsMock = Mock.Of <IOptionsMonitor <DatabaseStorageOptions> >(_ => _.CurrentValue == FileOptions.DatabaseStorageOptions);

            CheckFileExtensionRule checkFileExtensionRule = new CheckFileExtensionRule(fileValidatorMock);
            CheckFileSizeRule      checkFileSizeRule      = new CheckFileSizeRule(fileValidatorMock);

            List <IStoreFileDatabase> storeFileDatabases = new List <IStoreFileDatabase>
            {
                new SqlServerStoreFileDatabase()
            };
            DatabaseFileConnectorExecution databaseFileConnectorExecution = new DatabaseFileConnectorExecution(
                databaseOptionsMock,
                databaseStorageOptionsMock,
                storeFileDatabases);

            FileService fileService = new FileService(
                fileOptionsMock,
                filePublishOptionsMock,
                new List <IFileConnectorExecution>
            {
                databaseFileConnectorExecution
            }, new List <IFileValidatorRule>
            {
                checkFileExtensionRule,
                checkFileSizeRule
            }, fileRepository);

            // Act
            LetPortal.Portal.Models.Files.ResponseUploadFile result = await fileService.UploadFileAsync(mockFile.Object, "tester", false);

            memoryStream.Close();
            LetPortal.Portal.Models.Files.ResponseDownloadFile response = await fileService.DownloadFileAsync(result.FileId, false);

            fileRepository.Dispose();
            // 3. Assert
            Assert.NotNull(response.FileBytes);
        }