Skip to content

Commit

Permalink
Added redact API functionality (#517)
Browse files Browse the repository at this point in the history
* Added redact API functionality

* added test for redacting functionality
* added new test to check file has been redacted
  • Loading branch information
JeremyCrookshank committed Apr 21, 2021
1 parent 2b2291c commit c3a39b1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/ZendeskApi_v2/Models/Requests/IndividualAttachmentResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using ZendeskApi_v2.Models.Shared;

namespace ZendeskApi_v2.Models.Requests
{
public class IndividualAttachmentResponse
{
[JsonProperty("attachment")]
public Attachment Attachment { get; set; }
}
}
16 changes: 15 additions & 1 deletion src/ZendeskApi_v2/Requests/Attachments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.HelpCenter.Attachments;
using ZendeskApi_v2.Models.Requests;
using ZendeskApi_v2.Models.Shared;

namespace ZendeskApi_v2.Requests
Expand All @@ -22,6 +23,7 @@ public interface IAttachments : ICore
Upload UploadAttachment(ZenFile file, int? timeout = null);
Upload UploadAttachments(IEnumerable<ZenFile> files, int? timeout = null);
bool DeleteUpload(Upload upload);
IndividualAttachmentResponse RedactCommentAttachment(long attachmentId, long ticketId, long commentId);
#endif

#if ASYNC
Expand All @@ -37,7 +39,7 @@ public interface IAttachments : ICore
Task<Upload> UploadAttachmentsAsync(IEnumerable<ZenFile> files, int? timeout = null);
Task<bool> DeleteUploadAsync(Upload upload);
Task<ZenFile> DownloadAttachmentAsync(Attachment attachment);

Task<IndividualAttachmentResponse> RedactCommentAttachmentAsync(long attachmentId, long ticketId, long commentId);
#endif
}

Expand Down Expand Up @@ -104,6 +106,12 @@ public bool DeleteUpload(Upload upload)
{
return (upload?.Token == null ? false : GenericDelete($"/uploads/{upload.Token}.json"));
}

public IndividualAttachmentResponse RedactCommentAttachment(long attachmentId, long ticketId, long commentId)
{
var resource = $"/tickets/{ticketId}/comments/{commentId}/attachments/{attachmentId}/redact";
return RunRequest<IndividualAttachmentResponse>(resource, RequestMethod.Put);
}
#endif

#if ASYNC
Expand Down Expand Up @@ -175,6 +183,12 @@ public async Task<ZenFile> DownloadAttachmentAsync(Attachment attachment)
return file;
}

public async Task<IndividualAttachmentResponse> RedactCommentAttachmentAsync(long attachmentId, long ticketId, long commentId)
{
var resource = $"/tickets/{ticketId}/comments/{commentId}/attachments/{attachmentId}/redact";
return await RunRequestAsync<IndividualAttachmentResponse>(resource, RequestMethod.Put);
}

#endif


Expand Down
40 changes: 40 additions & 0 deletions test/ZendeskApi_v2.Test/AttachmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Net;

namespace Tests
{
Expand Down Expand Up @@ -65,5 +66,44 @@ public async Task CanDowloadAttachment()
Assert.That(api.Tickets.Delete(t1.Ticket.Id.Value), Is.True);
Assert.That(api.Attachments.DeleteUpload(res));
}

[Test]
public async Task CanRedactAttachment()
{
//This could probably be brought into a helper for above two tests perhaps
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "testupload.txt");

var res = await api.Attachments.UploadAttachmentAsync(new ZenFile()
{
ContentType = "text/plain",
FileName = "testupload.txt",
FileData = File.ReadAllBytes(path)
});

var ticket = new Ticket()
{
Subject = "testing attachments",
Priority = TicketPriorities.Normal,
Comment = new Comment()
{
Body = "comments are required for attachments",
Public = true,
Uploads = new List<string>() { res.Token }
},
};

var t1 = await api.Tickets.CreateTicketAsync(ticket);

var comments = api.Tickets.GetTicketComments(t1.Ticket.Id.Value);

var attach = comments.Comments[0].Attachments[0];

var delRes = api.Attachments.RedactCommentAttachment(attach.Id, t1.Ticket.Id.Value, comments.Comments[0].Id.Value);
//Returned correct attachment
Assert.That(delRes.Attachment.Id, Is.EqualTo(attach.Id));

//Check the file has been replaced by redacted.txt
Assert.That(api.Tickets.GetTicketComments(t1.Ticket.Id.Value).Comments[0].Attachments[0].FileName, Is.EqualTo("redacted.txt"));
}
}
}

0 comments on commit c3a39b1

Please sign in to comment.