예제 #1
0
        public async Task UploadPdfAttachment()
        {
            IPlanGridApi client     = PlanGridClient.Create();
            var          docName    = Guid.NewGuid().ToString();
            Stream       payload    = typeof(AttachmentTests).Assembly.GetManifestResourceStream("PlanGrid.Api.Tests.TestData.Sample.pdf");
            Attachment   attachment = await client.UploadPdfAttachment(TestData.Project2Uid, docName, payload, "test folder");

            Assert.AreEqual(docName, attachment.Name);
            Assert.AreEqual("test folder", attachment.Folder);
            Assert.AreEqual(TestData.ApiTestsUserUid, attachment.CreatedBy.Uid);
            Assert.AreNotEqual(attachment.CreatedAt, default(DateTime));

            using (var downloader = new HttpClient(new HttpClientHandler {
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, AllowAutoRedirect = true
            }))
            {
                Stream returnedPayload = await downloader.GetStreamAsync(attachment.Url);

                payload = typeof(AttachmentTests).Assembly.GetManifestResourceStream("PlanGrid.Api.Tests.TestData.Sample.pdf");
                var payloadBytes = new MemoryStream();
                await payload.CopyToAsync(payloadBytes);

                var returnedBytes = new MemoryStream();
                await returnedPayload.CopyToAsync(returnedBytes);

                Assert.IsTrue(payloadBytes.ToArray().SequenceEqual(returnedBytes.ToArray()));
            }

            Attachment retrievedAttachment = await client.GetAttachment(TestData.Project2Uid, attachment.Uid);

            Assert.IsFalse(retrievedAttachment.IsDeleted);
            var newDocName = Guid.NewGuid().ToString();
            await client.UpdateAttachment(TestData.Project2Uid, attachment.Uid, new AttachmentUpdate { Name = newDocName, Folder = "new folder" });

            retrievedAttachment = await client.GetAttachment(TestData.Project2Uid, attachment.Uid);

            Assert.AreEqual(newDocName, retrievedAttachment.Name);
            Assert.AreEqual("new folder", retrievedAttachment.Folder);

            await client.RemoveAttachment(TestData.Project2Uid, attachment.Uid);

            Attachment removedAttachment = await client.GetAttachment(TestData.Project2Uid, attachment.Uid);

            Assert.IsTrue(removedAttachment.IsDeleted);
        }