AttachmentAsyncTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if !(NET20 || NET40)
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Threading.Tasks;
  6. using Redmine.Net.Api.Async;
  7. using Redmine.Net.Api.Types;
  8. using Xunit;
  9. namespace Padi.RedmineApi.Tests.Tests.Async
  10. {
  11. [Collection("RedmineCollection")]
  12. public class AttachmentAsyncTests
  13. {
  14. private const string ATTACHMENT_ID = "10";
  15. private readonly RedmineFixture fixture;
  16. public AttachmentAsyncTests(RedmineFixture fixture)
  17. {
  18. this.fixture = fixture;
  19. }
  20. [Fact]
  21. public async Task Should_Get_Attachment_By_Id()
  22. {
  23. var attachment = await fixture.RedmineManager.GetObjectAsync<Attachment>(ATTACHMENT_ID, null);
  24. Assert.NotNull(attachment);
  25. Assert.IsType<Attachment>(attachment);
  26. }
  27. [Fact]
  28. public async Task Should_Upload_Attachment()
  29. {
  30. //read document from specified path
  31. var documentPath = AppDomain.CurrentDomain.BaseDirectory + "/uploadAttachment.pages";
  32. var documentData = System.IO.File.ReadAllBytes(documentPath);
  33. //upload attachment to redmine
  34. var attachment = await fixture.RedmineManager.UploadFileAsync(documentData);
  35. //set attachment properties
  36. attachment.FileName = "uploadAttachment.pages";
  37. attachment.Description = "File uploaded using REST API";
  38. attachment.ContentType = "text/plain";
  39. //create list of attachments to be added to issue
  40. IList<Upload> attachments = new List<Upload>();
  41. attachments.Add(attachment);
  42. var icf = (IssueCustomField)IdentifiableName.Create<IssueCustomField>(13);
  43. icf.Values = new List<CustomFieldValue> { new CustomFieldValue { Info = "Issue custom field completed" } };
  44. var issue = new Issue
  45. {
  46. Project = IdentifiableName.Create<IdentifiableName>(9),
  47. Tracker = IdentifiableName.Create<IdentifiableName>(3),
  48. Status = IdentifiableName.Create<IdentifiableName>(6),
  49. Priority = IdentifiableName.Create<IdentifiableName>(9),
  50. Subject = "Issue with attachments",
  51. Description = "Issue description...",
  52. Category = IdentifiableName.Create<IdentifiableName>(18),
  53. FixedVersion = IdentifiableName.Create<IdentifiableName>(9),
  54. AssignedTo = IdentifiableName.Create<IdentifiableName>(8),
  55. ParentIssue = IdentifiableName.Create<IdentifiableName>(96),
  56. CustomFields = new List<IssueCustomField> {icf},
  57. IsPrivate = true,
  58. EstimatedHours = 12,
  59. StartDate = DateTime.Now,
  60. DueDate = DateTime.Now.AddMonths(1),
  61. Uploads = attachments,
  62. Watchers = new List<Watcher>
  63. {
  64. (Watcher) IdentifiableName.Create<Watcher>(8),
  65. (Watcher) IdentifiableName.Create<Watcher>(2)
  66. }
  67. };
  68. //create issue and attach document
  69. var issueWithAttachment = await fixture.RedmineManager.CreateObjectAsync(issue);
  70. issue = await fixture.RedmineManager.GetObjectAsync<Issue>(issueWithAttachment.Id.ToString(),
  71. new NameValueCollection { { "include", "attachments" } });
  72. Assert.NotNull(issue);
  73. Assert.IsType<Issue>(issue);
  74. Assert.True(issue.Attachments.Count == 1, "Attachments count != 1");
  75. Assert.True(issue.Attachments[0].FileName == attachment.FileName);
  76. }
  77. [Fact]
  78. public async Task Sould_Download_Attachment()
  79. {
  80. var attachment = await fixture.RedmineManager.GetObjectAsync<Attachment>(ATTACHMENT_ID, null);
  81. var document = await fixture.RedmineManager.DownloadFileAsync(attachment.ContentUrl);
  82. Assert.NotNull(document);
  83. }
  84. }
  85. }
  86. #endif