private static void SeedTagAttachment(PreservationContext dbContext, Tag tag)
        {
            var attachment = new TagAttachment(tag.Plant, KnownTestData.TagAttachmentBlobStorageId, "Fil1.txt");

            tag.AddAttachment(attachment);
            dbContext.SaveChangesAsync().Wait();
        }
Exemplo n.º 2
0
        public void Constructor_ShouldSetTagAttachmentSpecificProperties()
        {
            var dut = new TagAttachment(TestPlant, BlobStorageId, "FileA");

            Assert.AreEqual($"PlantA/Tag/{BlobStorageId.ToString()}", dut.BlobPath);
            // Other properties are tested in base class
        }
Exemplo n.º 3
0
        public async Task <Result <int> > Handle(UploadTagAttachmentCommand request, CancellationToken cancellationToken)
        {
            var tag = await _projectRepository.GetTagByTagIdAsync(request.TagId);

            var attachment = tag.GetAttachmentByFileName(request.FileName);

            if (!request.OverwriteIfExists && attachment != null)
            {
                throw new Exception($"Tag {tag.Id} already has an attachment with filename {request.FileName}");
            }

            if (attachment == null)
            {
                attachment = new TagAttachment(
                    _plantProvider.Plant,
                    Guid.NewGuid(),
                    request.FileName);
                tag.AddAttachment(attachment);
            }

            var fullBlobPath = attachment.GetFullBlobPath(_attachmentOptions.CurrentValue.BlobContainer);

            await _blobStorage.UploadAsync(fullBlobPath, request.Content, request.OverwriteIfExists, cancellationToken);

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(new SuccessResult <int>(attachment.Id));
        }
        protected override void SetupNewDatabase(DbContextOptions <PreservationContext> dbContextOptions)
        {
            _blobStorageMock       = new Mock <IBlobStorage>();
            _uri                   = new Uri("http://whatever/file.txt");
            _attachmentOptionsMock = new Mock <IOptionsMonitor <AttachmentOptions> >();
            var options = new AttachmentOptions
            {
                BlobContainer = BlobContainer
            };

            _attachmentOptionsMock
            .Setup(x => x.CurrentValue)
            .Returns(options);

            using var context = new PreservationContext(dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider);

            _testDataSet = AddTestDataSet(context);

            var tag = _testDataSet.Project1.Tags.First();

            _attachment = new TagAttachment(TestPlant, Guid.NewGuid(), "FileA");
            tag.AddAttachment(_attachment);

            context.SaveChangesAsync().Wait();

            _tagId        = tag.Id;
            _attachmentId = _attachment.Id;

            var fullBlobPath = _attachment.GetFullBlobPath(BlobContainer);

            _blobStorageMock
            .Setup(b => b.GetDownloadSasUri(fullBlobPath, It.IsAny <DateTimeOffset>(), It.IsAny <DateTimeOffset>()))
            .Returns(_uri);
        }
Exemplo n.º 5
0
        protected override void SetupNewDatabase(DbContextOptions <PreservationContext> dbContextOptions)
        {
            using (var context = new PreservationContext(dbContextOptions, _plantProvider, _eventDispatcher, _currentUserProvider))
            {
                _testDataSet = AddTestDataSet(context);

                var tag = _testDataSet.Project1.Tags.First();

                _attachment = new TagAttachment(TestPlant, Guid.NewGuid(), "FileA");
                tag.AddAttachment(_attachment);

                context.SaveChangesAsync().Wait();

                _tagId = tag.Id;
            }
        }
Exemplo n.º 6
0
        public void Setup()
        {
            _command = new DeleteTagAttachmentCommand(1, 3, _rowVersion);

            _projectRepositoryMock = new Mock <IProjectRepository>();
            _blobStorageMock       = new Mock <IBlobStorage>();

            var attachmentOptionsMock = new Mock <IOptionsMonitor <AttachmentOptions> >();
            var options = new AttachmentOptions
            {
                MaxSizeMb         = 2,
                BlobContainer     = BlobContainer,
                ValidFileSuffixes = new[] { ".gif", ".jpg" }
            };

            attachmentOptionsMock
            .Setup(x => x.CurrentValue)
            .Returns(options);

            var stepMock = new Mock <Step>();

            stepMock.SetupGet(s => s.Plant).Returns(TestPlant);

            var reqMock = new Mock <TagRequirement>();

            reqMock.SetupGet(s => s.Plant).Returns(TestPlant);

            _tag = new Tag(TestPlant, TagType.Standard, "", "", stepMock.Object, new List <TagRequirement> {
                reqMock.Object
            });

            var attachment = new TagAttachment(TestPlant, Guid.Empty, "Fil.txt");

            attachment.SetProtectedIdForTesting(_command.AttachmentId);
            _tag.AddAttachment(attachment);

            _projectRepositoryMock
            .Setup(r => r.GetTagByTagIdAsync(_command.TagId))
            .Returns(Task.FromResult(_tag));

            _dut = new DeleteTagAttachmentCommandHandler(
                _projectRepositoryMock.Object,
                UnitOfWorkMock.Object,
                _blobStorageMock.Object,
                attachmentOptionsMock.Object);
        }