public void ToFileInfo()
        {
            var methodologyFile = new MethodologyFile
            {
                MethodologyVersion = new MethodologyVersion(),
                File = new File
                {
                    Id       = Guid.NewGuid(),
                    RootPath = Guid.NewGuid(),
                    Filename = "image.png",
                    Type     = Image
                }
            };

            var result = methodologyFile.ToFileInfo(new BlobInfo
                                                    (
                                                        path: "Ignored",
                                                        size: "500 B",
                                                        contentType: "Ignored",
                                                        contentLength: -1L,
                                                        meta: new Dictionary <string, string>()
                                                    ));

            Assert.Equal(methodologyFile.File.Id, result.Id);
            Assert.Equal("png", result.Extension);
            Assert.Equal("image.png", result.FileName);
            Assert.Equal("500 B", result.Size);
            Assert.Equal(Image, result.Type);
        }
        public async Task <MethodologyFile> Create(Guid methodologyVersionId,
                                                   string filename,
                                                   FileType type,
                                                   Guid createdById)
        {
            if (!SupportedFileTypes.Contains(type))
            {
                throw new ArgumentOutOfRangeException(nameof(type), type, "Cannot create file for file type");
            }

            var methodologyFile = new MethodologyFile
            {
                MethodologyVersionId = methodologyVersionId,
                File = new File
                {
                    Created     = DateTime.UtcNow,
                    CreatedById = createdById,
                    RootPath    = methodologyVersionId,
                    Filename    = filename,
                    Type        = type
                }
            };

            var created = (await _contentDbContext.MethodologyFiles.AddAsync(methodologyFile)).Entity;
            await _contentDbContext.SaveChangesAsync();

            return(created);
        }
        public async Task GetFiles()
        {
            var methodologyVersion = new MethodologyVersion();

            var imageFile1 = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    Filename = "image1.png",
                    Type     = Image
                }
            };

            var imageFile2 = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    Filename = "image2.png",
                    Type     = Image
                }
            };

            var otherFile = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    Filename = "ancillary.pdf",
                    Type     = Ancillary
                }
            };

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

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.MethodologyVersions.AddAsync(methodologyVersion);

                await contentDbContext.MethodologyFiles.AddRangeAsync(imageFile1, imageFile2, otherFile);

                await contentDbContext.SaveChangesAsync();
            }

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                var service = SetupMethodologyService(contentDbContext);

                var result = await service.GetFiles(methodologyVersion.Id, Image);

                Assert.Equal(2, result.Count);
                Assert.Equal(imageFile1.File.Id, result[0].Id);
                Assert.Equal(imageFile2.File.Id, result[1].Id);
            }
        }
 public static FileInfo ToFileInfo(this MethodologyFile methodologyFile, BlobInfo blobInfo)
 {
     return(new FileInfo
     {
         Id = methodologyFile.File.Id,
         FileName = methodologyFile.File.Filename,
         Name = null,
         Size = blobInfo.Size,
         Type = methodologyFile.File.Type
     });
 }
        public void Path()
        {
            var methodologyFile = new MethodologyFile
            {
                File = new File
                {
                    Id       = Guid.NewGuid(),
                    RootPath = Guid.NewGuid(),
                    Filename = "ancillary.pdf",
                    Type     = Ancillary
                }
            };

            Assert.Equal(methodologyFile.File.Path(), methodologyFile.Path());
        }
        public async Task Stream_BlobDoesNotExist()
        {
            var methodologyVersion = new MethodologyVersion();

            var methodologyFile = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image.png",
                    Type     = Image
                }
            };

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

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.MethodologyVersions.AddAsync(methodologyVersion);

                await contentDbContext.MethodologyFiles.AddAsync(methodologyFile);

                await contentDbContext.SaveChangesAsync();
            }

            var blobStorageService = new Mock <IBlobStorageService>(MockBehavior.Strict);

            blobStorageService.Setup(mock =>
                                     mock.CheckBlobExists(PublicMethodologyFiles, It.IsAny <string>()))
            .ReturnsAsync(false);

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                var service = SetupMethodologyImageService(contentDbContext: contentDbContext,
                                                           blobStorageService: blobStorageService.Object);

                var result = await service.Stream(methodologyVersion.Id, methodologyFile.File.Id);

                result.AssertNotFound();

                blobStorageService.Verify(
                    mock => mock.CheckBlobExists(PublicMethodologyFiles, methodologyFile.Path()),
                    Times.Once());
            }

            MockUtils.VerifyAllMocks(blobStorageService);
        }
        public async Task Stream_MethodologyNotFound()
        {
            var methodologyFile = new MethodologyFile
            {
                MethodologyVersion = new MethodologyVersion(),
                File = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image.png",
                    Type     = Image
                }
            };

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

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.MethodologyFiles.AddAsync(methodologyFile);

                await contentDbContext.SaveChangesAsync();
            }

            var blobStorageService = new Mock <IBlobStorageService>(MockBehavior.Strict);

            await using (var contentDbContext = InMemoryContentDbContext())
            {
                var service = SetupMethodologyImageService(contentDbContext: contentDbContext,
                                                           blobStorageService: blobStorageService.Object);

                var result = await service.Stream(Guid.NewGuid(), methodologyFile.File.Id);

                result.AssertNotFound();
            }

            MockUtils.VerifyAllMocks(blobStorageService);
        }
예제 #8
0
        public async Task UpdateApprovalStatus_MethodologyHasImages()
        {
            var methodologyVersion = new MethodologyVersion
            {
                Methodology = new Methodology
                {
                    OwningPublicationTitle = "Publication title",
                    Publications           = ListOf(new PublicationMethodology
                    {
                        Owner       = true,
                        Publication = new Publication()
                    })
                }
            };

            var imageFile1 = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image1.png",
                    Type     = FileType.Image
                }
            };

            var imageFile2 = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image2.png",
                    Type     = FileType.Image
                }
            };

            var request = new MethodologyApprovalUpdateRequest
            {
                LatestInternalReleaseNote = "Test approval",
                PublishingStrategy        = Immediately,
                Status = Approved,
            };

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

            await using (var context = InMemoryApplicationDbContext(contentDbContextId))
            {
                await context.MethodologyVersions.AddAsync(methodologyVersion);

                await context.MethodologyFiles.AddRangeAsync(imageFile1, imageFile2);

                await context.SaveChangesAsync();
            }

            var contentService = new Mock <IMethodologyContentService>(Strict);
            var methodologyVersionRepository = new Mock <IMethodologyVersionRepository>(Strict);

            contentService.Setup(mock =>
                                 mock.GetContentBlocks <HtmlBlock>(methodologyVersion.Id))
            .ReturnsAsync(new List <HtmlBlock>
            {
                new()
                {
                    Body = $@"
    <img src=""/api/methodologies/{{methodologyId}}/images/{imageFile1.File.Id}""/>
    <img src=""/api/methodologies/{{methodologyId}}/images/{imageFile2.File.Id}""/>"
                }
            });
        public async Task Stream()
        {
            var methodologyVersion = new MethodologyVersion();

            var methodologyFile = new MethodologyFile
            {
                MethodologyVersion = methodologyVersion,
                File = new File
                {
                    RootPath = Guid.NewGuid(),
                    Filename = "image.png",
                    Type     = Image
                }
            };

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

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                await contentDbContext.MethodologyVersions.AddAsync(methodologyVersion);

                await contentDbContext.MethodologyFiles.AddAsync(methodologyFile);

                await contentDbContext.SaveChangesAsync();
            }

            var blobStorageService = new Mock <IBlobStorageService>(MockBehavior.Strict);

            var blob = new BlobInfo(
                path: methodologyFile.Path(),
                size: null,
                contentType: "image/png",
                contentLength: 0L,
                meta: GetMetaValuesReleaseDateTime(
                    releaseDateTime: DateTime.UtcNow.AddDays(-1)),
                created: null);

            blobStorageService.Setup(mock =>
                                     mock.CheckBlobExists(PublicMethodologyFiles, methodologyFile.Path()))
            .ReturnsAsync(true);

            blobStorageService.Setup(mock =>
                                     mock.GetBlob(PublicMethodologyFiles, methodologyFile.Path()))
            .ReturnsAsync(blob);

            blobStorageService.Setup(mock =>
                                     mock.DownloadToStream(PublicMethodologyFiles, methodologyFile.Path(),
                                                           It.IsAny <MemoryStream>(), null))
            .ReturnsAsync(new MemoryStream());

            await using (var contentDbContext = InMemoryContentDbContext(contentDbContextId))
            {
                var service = SetupMethodologyImageService(contentDbContext: contentDbContext,
                                                           blobStorageService: blobStorageService.Object);

                var result = await service.Stream(methodologyVersion.Id, methodologyFile.File.Id);

                Assert.True(result.IsRight);

                blobStorageService.Verify(
                    mock => mock.CheckBlobExists(PublicMethodologyFiles, methodologyFile.Path()),
                    Times.Once());

                blobStorageService.Verify(
                    mock => mock.GetBlob(PublicMethodologyFiles, methodologyFile.Path()),
                    Times.Once());

                blobStorageService.Verify(
                    mock =>
                    mock.DownloadToStream(
                        PublicMethodologyFiles, methodologyFile.Path(),
                        It.IsAny <MemoryStream>(), null),
                    Times.Once());

                Assert.Equal("image/png", result.Right.ContentType);
                Assert.Equal("image.png", result.Right.FileDownloadName);
                Assert.IsType <MemoryStream>(result.Right.FileStream);
            }

            MockUtils.VerifyAllMocks(blobStorageService);
        }
 public static string Path(this MethodologyFile methodologyFile)
 {
     return(methodologyFile.File.Path());
 }