Ejemplo n.º 1
0
        public async Task ValidateDataFilesForUpload_ReplacingDataFileWithFileOfSameName()
        {
            var releaseId = Guid.NewGuid();

            var contextId = Guid.NewGuid().ToString();

            await using (var context = InMemoryApplicationDbContext(contextId))
            {
                context.ReleaseFiles.Add(new ReleaseFile
                {
                    ReleaseId = releaseId,
                    File      = new File
                    {
                        Type     = FileType.Data,
                        Filename = "test.csv"
                    }
                });

                await context.SaveChangesAsync();
            }

            await using (var context = InMemoryApplicationDbContext(contextId))
            {
                var(service, mocks) = BuildService(context);

                // The replacement file here has the same name as the one it is replacing, so this should be ok.
                var dataFile = CreateSingleLineFormFile("test.csv", "test.csv");
                var metaFile = CreateSingleLineFormFile("test.meta.csv", "test.meta.csv");

                // The file being replaced here has the same name as the one being uploaded, but that's ok.
                var fileBeingReplaced = new File
                {
                    Filename = "test.csv"
                };

                mocks.fileTypeService
                .Setup(s => s.HasMatchingMimeType(dataFile, It.IsAny <IEnumerable <Regex> >()))
                .ReturnsAsync(() => true);
                mocks.fileTypeService
                .Setup(s => s.HasMatchingMimeType(metaFile, It.IsAny <IEnumerable <Regex> >()))
                .ReturnsAsync(() => true);
                mocks.fileTypeService
                .Setup(s => s.HasMatchingEncodingType(dataFile, It.IsAny <IEnumerable <string> >()))
                .Returns(() => true);
                mocks.fileTypeService
                .Setup(s => s.HasMatchingEncodingType(metaFile, It.IsAny <IEnumerable <string> >()))
                .Returns(() => true);

                var result = await service.ValidateDataFilesForUpload(
                    releaseId, dataFile, metaFile, fileBeingReplaced);

                VerifyAllMocks(mocks);

                result.AssertRight();
            }
        }
Ejemplo n.º 2
0
        public async Task ValidateDataFilesForUpload_ReplacingDataFileWithFileOfDifferentNameButClashesWithAnother()
        {
            var releaseId = Guid.NewGuid();

            var contextId = Guid.NewGuid().ToString();

            await using (var context = InMemoryApplicationDbContext(contextId))
            {
                context.ReleaseFiles.AddRange(new ReleaseFile
                {
                    ReleaseId = releaseId,
                    File      = new File
                    {
                        Type     = FileType.Data,
                        Filename = "test.csv"
                    }
                }, new ReleaseFile
                {
                    ReleaseId = releaseId,
                    File      = new File
                    {
                        Type     = FileType.Data,
                        Filename = "another.csv"
                    }
                });

                await context.SaveChangesAsync();
            }

            await using (var context = InMemoryApplicationDbContext(contextId))
            {
                var(service, _) = BuildService(context);

                // The replacement file here has the same name as another unrelated data file i.e. one that's not being
                // replaced here, which should be a problem as it would otherwise result in duplicate data file names
                // in this Release after the replacement is complete.
                var dataFile = CreateSingleLineFormFile("another.csv", "test.csv");
                var metaFile = CreateSingleLineFormFile("test.meta.csv", "test.meta.csv");

                var fileBeingReplaced = new File
                {
                    Filename = "test.csv"
                };

                var result = await service.ValidateDataFilesForUpload(
                    releaseId, dataFile, metaFile, fileBeingReplaced);

                result.AssertBadRequest(CannotOverwriteDataFile);
            }
        }