Exemplo n.º 1
0
        public virtual void DeleteQueuedEmailAttachment(QueuedEmailAttachment qea)
        {
            if (qea == null)
                throw new ArgumentNullException("qea");

            _queuedEmailAttachmentRepository.Delete(qea);

            _services.EventPublisher.EntityDeleted(qea);
        }
        public void Can_cascade_delete_attachment()
        {
            var account = new EmailAccount
            {
                Email = "*****@*****.**",
                Host = "127.0.0.1",
                Username = "******",
                Password = "******"
            };

            var download = new Download
            {
                ContentType = "text/plain",
                DownloadBinary = new byte[10],
                DownloadGuid = Guid.NewGuid(),
                Extension = "txt",
                Filename = "file"
            };

            // add attachment
            var attach = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.FileReference,
                Name = "file.txt",
                MimeType = "text/plain",
                File = download
            };

            var qe = new QueuedEmail
            {
                Priority = 1,
                From = "From",
                To = "To",
                Subject = "Subject",
                CreatedOnUtc = DateTime.UtcNow,
                EmailAccount = account
            };

            qe.Attachments.Add(attach);

            var fromDb = SaveAndLoadEntity(qe);
            fromDb.ShouldNotBeNull();

            Assert.AreEqual(fromDb.Attachments.Count, 1);
            attach = fromDb.Attachments.FirstOrDefault();
            attach.ShouldNotBeNull();

            download = attach.File;
            download.ShouldNotBeNull();

            var attachId = attach.Id;
            var downloadId = download.Id;

            // delete Attachment.Download
            context.Set<Download>().Remove(download);
            context.SaveChanges();
            base.ReloadContext();

            attach = context.Set<QueuedEmailAttachment>().Find(attachId);
            attach.ShouldBeNull();

            // add new attachment
            attach = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.FileReference,
                Name = "file.txt",
                MimeType = "text/plain"
            };

            qe = context.Set<QueuedEmail>().FirstOrDefault();
            qe.Attachments.Add(attach);

            fromDb = SaveAndLoadEntity(qe);
            fromDb.ShouldNotBeNull();

            // delete QueuedEmail
            context.Set<QueuedEmail>().Remove(fromDb);
            context.SaveChanges();
            base.ReloadContext();

            // Attachment should also be gone now
            attach = context.Set<QueuedEmailAttachment>().FirstOrDefault();
            attach.ShouldBeNull();
        }
        public void Can_convert_email()
        {
            var qe = new QueuedEmail
            {
                Bcc = "[email protected];[email protected]",
                Body = "Body",
                CC = "[email protected];[email protected]",
                CreatedOnUtc = DateTime.UtcNow,
                From = "*****@*****.**",
                FromName = "FromName",
                Priority = 10,
                ReplyTo = "*****@*****.**",
                ReplyToName = "ReplyToName",
                Subject = "Subject",
                To = "*****@*****.**",
                ToName = "ToName"
            };

            // load attachment file resource and save as file
            var asm = typeof(QueuedEmailServiceTests).Assembly;
            var pdfStream = asm.GetManifestResourceStream("{0}.Messages.Attachment.pdf".FormatInvariant(asm.GetName().Name));
            var pdfBinary = pdfStream.ToByteArray();
            pdfStream.Seek(0, SeekOrigin.Begin);
            var path1 = "~/Attachment.pdf";
            var path2 = CommonHelper.MapPath(path1, false);
            Assert.IsTrue(pdfStream.ToFile(path2));

            var attachBlob = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.Blob,
                Data = pdfBinary,
                Name = "blob.pdf",
                MimeType = "application/pdf"
            };
            var attachPath1 = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.Path,
                Path = path1,
                Name = "path1.pdf",
                MimeType = "application/pdf"
            };
            var attachPath2 = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.Path,
                Path = path2,
                Name = "path2.pdf",
                MimeType = "application/pdf"
            };
            var attachFile = new QueuedEmailAttachment
            {
                StorageLocation = EmailAttachmentStorageLocation.FileReference,
                Name = "file.pdf",
                MimeType = "application/pdf",
                File = new Download
                {
                    ContentType = "application/pdf",
                    DownloadBinary = pdfBinary,
                    Extension = ".pdf",
                    Filename = "file"
                }
            };

            qe.Attachments.Add(attachBlob);
            qe.Attachments.Add(attachFile);
            qe.Attachments.Add(attachPath1);
            qe.Attachments.Add(attachPath2);

            var msg = _queuedEmailService.ConvertEmail(qe);

            Assert.IsNotNull(msg);
            Assert.IsNotNull(msg.To);
            Assert.IsNotNull(msg.From);

            Assert.AreEqual(msg.ReplyTo.Count, 1);
            Assert.AreEqual(qe.ReplyTo, msg.ReplyTo.First().Address);
            Assert.AreEqual(qe.ReplyToName, msg.ReplyTo.First().DisplayName);

            Assert.AreEqual(msg.Cc.Count, 2);
            Assert.AreEqual(msg.Cc.First().Address, "*****@*****.**");
            Assert.AreEqual(msg.Cc.ElementAt(1).Address, "*****@*****.**");

            Assert.AreEqual(msg.Bcc.Count, 2);
            Assert.AreEqual(msg.Bcc.First().Address, "*****@*****.**");
            Assert.AreEqual(msg.Bcc.ElementAt(1).Address, "*****@*****.**");

            Assert.AreEqual(qe.Subject, msg.Subject);
            Assert.AreEqual(qe.Body, msg.Body);

            Assert.AreEqual(msg.Attachments.Count, 4);

            var attach1 = msg.Attachments.First();
            var attach2 = msg.Attachments.ElementAt(1);
            var attach3 = msg.Attachments.ElementAt(2);
            var attach4 = msg.Attachments.ElementAt(3);

            // test file names
            Assert.AreEqual(attach1.Name, "blob.pdf");
            Assert.AreEqual(attach2.Name, "file.pdf");
            Assert.AreEqual(attach3.Name, "path1.pdf");
            Assert.AreEqual(attach4.Name, "path2.pdf");

            // test file streams
            Assert.AreEqual(attach1.ContentStream.Length, pdfBinary.Length);
            Assert.AreEqual(attach2.ContentStream.Length, pdfBinary.Length);
            Assert.Greater(attach3.ContentStream.Length, 0);
            Assert.Greater(attach4.ContentStream.Length, 0);

            // cleanup
            msg.Attachments.Each(x => x.Dispose());
            msg.Attachments.Clear();

            // delete attachment file
            File.Delete(path2);
        }