Skip to content

Commit

Permalink
Issue #330 + CRUD operations for Comments (#344)
Browse files Browse the repository at this point in the history
* Test "CanCreateTicketWithDueDate" can't run on PCs that use dd/MM/yyyy as time format

* Added Code to make tests work

* - Added support for issue #330
- Added support for CRUD operations of Comments on Articles and Posts
  • Loading branch information
rwjdk-test authored and mozts2005 committed Jun 22, 2018
1 parent 0143aaa commit 672ed15
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 6 deletions.
101 changes: 96 additions & 5 deletions src/ZendeskApi_v2/Requests/HelpCenter/Comments.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
#if ASYNC
#if ASYNC
using System.Threading.Tasks;
#endif
using ZendeskApi_v2.Extensions;
using ZendeskApi_v2.Models.HelpCenter.Comments;

namespace ZendeskApi_v2.Requests.HelpCenter
Expand All @@ -14,14 +12,29 @@ public interface IComments : ICore
GroupCommentResponse GetCommentsForArticle(long? articleId, int? perPage = null, int? page = null);
GroupCommentResponse GetCommentsForUser(long? userId);
GroupCommentResponse GetCommentsForCurrentUser();
GroupCommentResponse GetCommentsForPost(long? postId, int? perPage = null, int? page = null);
IndividualCommentsResponse CreateCommentForArticle(long articleId, Comment comment);
IndividualCommentsResponse CreateCommentForPost(long postId, Comment comment);
IndividualCommentsResponse UpdateCommentForArticle(long articleId, Comment comment);
IndividualCommentsResponse UpdateCommentForPost(long postId, Comment comment);
bool DeleteCommentForArticle(long articleId, long commentId);
bool DeleteCommentForPost(long postId, long commentId);
#endif
#if ASYNC
Task<GroupCommentResponse> GetCommentsForArticleAsync(long? articleId);
Task<GroupCommentResponse> GetCommentsForArticleAsync(long? articleId);
Task<GroupCommentResponse> GetCommentsForUserAsync(long? userId);
Task<GroupCommentResponse> GetCommentsForCurrentUserAsync();
Task<GroupCommentResponse> GetCommentsForPostAsync(long? postId);
Task<IndividualCommentsResponse> CreateCommentForArticleAsync(long articleId, Comment comment);
Task<IndividualCommentsResponse> CreateCommentForPostAsync(long postId, Comment comment);
Task<IndividualCommentsResponse> UpdateCommentForArticleAsync(long articleId, Comment comment);
Task<IndividualCommentsResponse> UpdateCommentForPostAsync(long postId, Comment comment);
Task<bool> DeleteCommentForArticleAsync(long articleId, long commentId);
Task<bool> DeleteCommentForPostAsync(long postId, long commentId);
#endif

}

}

/// <summary>
/// https://developer.zendesk.com/rest_api/docs/help_center/comments
Expand Down Expand Up @@ -49,6 +62,45 @@ public GroupCommentResponse GetCommentsForCurrentUser()
{
return GenericGet<GroupCommentResponse>("help_center/users/me/comments.json");
}

public GroupCommentResponse GetCommentsForPost(long? postId, int? perPage = null, int? page = null)
{
return GenericPagedGet<GroupCommentResponse>($"community/posts/{postId}/comments.json", perPage, page);
}

public IndividualCommentsResponse CreateCommentForArticle(long articleId, Comment comment)
{
var body = new { comment };
return GenericPost<IndividualCommentsResponse>($"help_center/articles/{articleId}/comments.json", body);
}

public IndividualCommentsResponse CreateCommentForPost(long postId, Comment comment)
{
var body = new { comment };
return GenericPost<IndividualCommentsResponse>($"community/posts/{postId}/comments.json", body);
}

public IndividualCommentsResponse UpdateCommentForArticle(long articleId, Comment comment)
{
var body = new { comment };
return GenericPut<IndividualCommentsResponse>($"help_center/articles/{articleId}/comments/{comment.Id}.json", body);
}

public IndividualCommentsResponse UpdateCommentForPost(long postId, Comment comment)
{
var body = new { comment };
return GenericPut<IndividualCommentsResponse>($"community/posts/{postId}/comments/{comment.Id}.json", body);
}

public bool DeleteCommentForArticle(long articleId, long commentId)
{
return GenericDelete($"help_center/articles/{articleId}/comments/{commentId}.json");
}

public bool DeleteCommentForPost(long postId, long commentId)
{
return GenericDelete($"community/posts/{postId}/comments/{commentId}.json");
}
#endif
#if ASYNC

Expand All @@ -66,6 +118,45 @@ public async Task<GroupCommentResponse> GetCommentsForCurrentUserAsync()
{
return await GenericGetAsync<GroupCommentResponse>("help_center/users/me/comments.json");
}

public async Task<GroupCommentResponse> GetCommentsForPostAsync(long? postId)
{
return await GenericGetAsync<GroupCommentResponse>($"community/posts/{postId}/comments.json");
}

public async Task<IndividualCommentsResponse> CreateCommentForArticleAsync(long articleId, Comment comment)
{
var body = new { comment };
return await GenericPostAsync<IndividualCommentsResponse>($"help_center/articles/{articleId}/comments.json", body);
}

public async Task<IndividualCommentsResponse> CreateCommentForPostAsync(long postId, Comment comment)
{
var body = new { comment };
return await GenericPostAsync<IndividualCommentsResponse>($"community/posts/{postId}/comments.json", body);
}

public async Task<IndividualCommentsResponse> UpdateCommentForArticleAsync(long articleId, Comment comment)
{
var body = new { comment };
return await GenericPutAsync<IndividualCommentsResponse>($"help_center/articles/{articleId}/comments/{comment.Id}.json", body);
}

public async Task<IndividualCommentsResponse> UpdateCommentForPostAsync(long postId, Comment comment)
{
var body = new { comment };
return await GenericPutAsync<IndividualCommentsResponse>($"community/posts/{postId}/comments/{comment.Id}.json", body);
}

public async Task<bool> DeleteCommentForArticleAsync(long articleId, long commentId)
{
return await GenericDeleteAsync($"help_center/articles/{articleId}/comments/{commentId}.json");
}

public async Task<bool> DeleteCommentForPostAsync(long postId, long commentId)
{
return await GenericDeleteAsync($"community/posts/{postId}/comments/{commentId}.json");
}
#endif
}
}
205 changes: 205 additions & 0 deletions test/ZendeskApi_v2.Test/HelpCenter/CommentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using System.Threading.Tasks;
using NUnit.Framework;
using ZendeskApi_v2;
using ZendeskApi_v2.Models.Articles;
using ZendeskApi_v2.Models.HelpCenter.Comments;
using ZendeskApi_v2.Models.HelpCenter.Post;

namespace Tests
{
[TestFixture]
public class CommentTests
{
private ZendeskApi api = new ZendeskApi(Settings.Site, Settings.AdminEmail, Settings.AdminPassword);
private long _testSectionIdForCommentsTest = 360000205286; //https://csharpapi.zendesk.com/hc/en-us/sections/360000205286-Test-Section-For-Comment-Tests
private long _testTopicIdForCommentsTest = 360000016546; //https://csharpapi.zendesk.com/hc/en-us/community/topics/360000016546-Test-Topic-For-Comment-Tests

[OneTimeSetUp]
public void OneTimeSetUp()
{
DeleteAllArticlesFromTestSection(); //Get rid of all articles in the test-section (there should be none but there could be if previous tests have failed)
DeleteAllPostsFromTestTopic(); //Get rid of all posts in the test-topic (there should be none but there could be if previous tests have failed)
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
DeleteAllArticlesFromTestSection(); //Clean-up after tests
DeleteAllPostsFromTestTopic(); //Clean-up after tests
}

private void DeleteAllArticlesFromTestSection()
{
var articlesBySectionId = api.HelpCenter.Articles.GetArticlesBySectionId(_testSectionIdForCommentsTest);
foreach (var article in articlesBySectionId.Articles)
{
api.HelpCenter.Articles.DeleteArticle(article.Id.Value);
}
}

private void DeleteAllPostsFromTestTopic()
{
var articlesBySectionId = api.HelpCenter.Posts.GetPostsByTopicId(_testTopicIdForCommentsTest);
foreach (var post in articlesBySectionId.Posts)
{
api.HelpCenter.Posts.DeletePost(post.Id.Value);
}
}

[Test]
public void CanCreateUpdateAndDeleteCommentsForArticle()
{
var articleId = CreateTestArticle("Test-Article-For-Comments");

//Create 3 Comments
var individualCommentsResponse1 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 1", Locale = "en-us"});
Assert.NotNull(individualCommentsResponse1.Comment);
Assert.Greater(individualCommentsResponse1.Comment.Id, 0);

var individualCommentsResponse2 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 2", Locale = "en-us" });
Assert.NotNull(individualCommentsResponse2.Comment);
Assert.Greater(individualCommentsResponse2.Comment.Id, 0);

var individualCommentsResponse3 = api.HelpCenter.Comments.CreateCommentForArticle(articleId, new Comment {Body = "Comment 3", Locale = "en-us" });
Assert.NotNull(individualCommentsResponse3.Comment);
Assert.Greater(individualCommentsResponse3.Comment.Id, 0);

Assert.AreEqual(3, api.HelpCenter.Comments.GetCommentsForArticle(articleId).Count); //That article have 3 comments

Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body);
Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body);
Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body);

//Update Comment
var updatedCommentBody = "Comment 2 Updated";
individualCommentsResponse2.Comment.Body = updatedCommentBody;
var updateCommentForArticle = api.HelpCenter.Comments.UpdateCommentForArticle(articleId, individualCommentsResponse2.Comment);
Assert.AreEqual(updatedCommentBody, updateCommentForArticle.Comment.Body);

//Delete Comment
api.HelpCenter.Comments.DeleteCommentForArticle(articleId, individualCommentsResponse2.Comment.Id.Value);
Assert.AreEqual(2, api.HelpCenter.Comments.GetCommentsForArticle(articleId).Count); //One less comments now
}

[Test]
public void CanCreateUpdateAndDeleteCommentsForPost()
{
var postId = CreateTestPost("Test-Post-For-Comments");

//Create 3 Comments
var individualCommentsResponse1 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 1" });
Assert.NotNull(individualCommentsResponse1.Comment);
Assert.Greater(individualCommentsResponse1.Comment.Id, 0);

var individualCommentsResponse2 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 2" });
Assert.NotNull(individualCommentsResponse2.Comment);
Assert.Greater(individualCommentsResponse2.Comment.Id, 0);

var individualCommentsResponse3 = api.HelpCenter.Comments.CreateCommentForPost(postId, new Comment { Body = "Comment 3" });
Assert.NotNull(individualCommentsResponse3.Comment);
Assert.Greater(individualCommentsResponse3.Comment.Id, 0);

Assert.AreEqual(3, api.HelpCenter.Comments.GetCommentsForPost(postId).Count); //That post have 3 comments

Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body);
Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body);
Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body);

//Update Comment
var updatedCommentBody = "Comment 2 Updated";
individualCommentsResponse2.Comment.Body = updatedCommentBody;
var updateCommentForPost = api.HelpCenter.Comments.UpdateCommentForPost(postId, individualCommentsResponse2.Comment);
Assert.AreEqual(updatedCommentBody, updateCommentForPost.Comment.Body);

//Delete Comment
api.HelpCenter.Comments.DeleteCommentForPost(postId, individualCommentsResponse2.Comment.Id.Value);
Assert.AreEqual(2, api.HelpCenter.Comments.GetCommentsForPost(postId).Count); //One less comments now
}

[Test]
public async Task CanCreateUpdateAndDeleteCommentsForArticleAsync()
{
var articleId = CreateTestArticle("Test-Article-For-Comments-Async");

//Create 3 Comments
var individualCommentsResponse1 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 1", Locale = "en-us"});
Assert.NotNull(individualCommentsResponse1.Comment);
Assert.Greater(individualCommentsResponse1.Comment.Id, 0);

var individualCommentsResponse2 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 2", Locale = "en-us" });
Assert.NotNull(individualCommentsResponse2.Comment);
Assert.Greater(individualCommentsResponse2.Comment.Id, 0);

var individualCommentsResponse3 = await api.HelpCenter.Comments.CreateCommentForArticleAsync(articleId, new Comment {Body = "Comment 3", Locale = "en-us" });
Assert.NotNull(individualCommentsResponse3.Comment);
Assert.Greater(individualCommentsResponse3.Comment.Id, 0);

Assert.AreEqual(3, (await api.HelpCenter.Comments.GetCommentsForArticleAsync(articleId)).Count); //That article have 3 comments

Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body);
Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body);
Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body);

//Update Comment
var updatedCommentBody = "Comment 2 Updated";
individualCommentsResponse2.Comment.Body = updatedCommentBody;
var updateCommentForArticle = await api.HelpCenter.Comments.UpdateCommentForArticleAsync(articleId, individualCommentsResponse2.Comment);
Assert.AreEqual(updatedCommentBody, updateCommentForArticle.Comment.Body);

//Delete Comment
await api.HelpCenter.Comments.DeleteCommentForArticleAsync(articleId, individualCommentsResponse2.Comment.Id.Value);
Assert.AreEqual(2, (await api.HelpCenter.Comments.GetCommentsForArticleAsync(articleId)).Count); //One less comments now
}

[Test]
public async Task CanCreateUpdateAndDeleteCommentsForPostAsync()
{
var postId = CreateTestPost("Test-Post-For-Comments-Async");

//Create 3 Comments
var individualCommentsResponse1 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 1" });
Assert.NotNull(individualCommentsResponse1.Comment);
Assert.Greater(individualCommentsResponse1.Comment.Id, 0);

var individualCommentsResponse2 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 2" });
Assert.NotNull(individualCommentsResponse2.Comment);
Assert.Greater(individualCommentsResponse2.Comment.Id, 0);

var individualCommentsResponse3 = await api.HelpCenter.Comments.CreateCommentForPostAsync(postId, new Comment { Body = "Comment 3" });
Assert.NotNull(individualCommentsResponse3.Comment);
Assert.Greater(individualCommentsResponse3.Comment.Id, 0);

Assert.AreEqual(3, (await api.HelpCenter.Comments.GetCommentsForPostAsync(postId)).Count); //That post have 3 comments

Assert.AreEqual("Comment 1", individualCommentsResponse1.Comment.Body);
Assert.AreEqual("Comment 2", individualCommentsResponse2.Comment.Body);
Assert.AreEqual("Comment 3", individualCommentsResponse3.Comment.Body);

//Update Comment
var updatedCommentBody = "Comment 2 Updated";
individualCommentsResponse2.Comment.Body = updatedCommentBody;
var updateCommentForPost = await api.HelpCenter.Comments.UpdateCommentForPostAsync(postId, individualCommentsResponse2.Comment);
Assert.AreEqual(updatedCommentBody, updateCommentForPost.Comment.Body);

//Delete Comment
await api.HelpCenter.Comments.DeleteCommentForPostAsync(postId, individualCommentsResponse2.Comment.Id.Value);
Assert.AreEqual(2, (await api.HelpCenter.Comments.GetCommentsForPostAsync(postId)).Count); //One less comments now
}

private long CreateTestArticle(string testArticleForComments)
{
var article = new Article {Title = testArticleForComments, Body = "Test"};

var individualArticleResponse = api.HelpCenter.Articles.CreateArticle(_testSectionIdForCommentsTest, article);
return individualArticleResponse.Article.Id.Value;
}

private long CreateTestPost(string testPostForComments)
{
var post = new Post {Title = testPostForComments, TopicId = _testTopicIdForCommentsTest};

var individualArticleResponse = api.HelpCenter.Posts.CreatePost(post);
return individualArticleResponse.Post.Id.Value;
}
}
}
14 changes: 13 additions & 1 deletion test/ZendeskApi_v2.Test/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
namespace Tests
using System.Net;
using NUnit.Framework;

[SetUpFixture]
public class InitializeTests
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
}
}
namespace Tests
{
public class Settings
{
Expand Down

0 comments on commit 672ed15

Please sign in to comment.