From dc001d4fe7bf30968c7fcb5a2b2b95637059fa52 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 14 Jun 2021 16:04:07 +0800 Subject: [PATCH] refactor(article-comment): missing pin/unpin gq endpoint & test --- .../cms/delegates/comment_action.ex | 11 +--- lib/groupher_server/cms/helper/macros.ex | 12 +--- .../resolvers/cms_resolver.ex | 5 +- .../schema/cms/mutations/comment.ex | 22 +++++++ .../comments/post_comment_replies_test.exs | 1 - .../cms/comments/blog_comment_test.exs | 58 +++++++++++++++++++ .../cms/comments/job_comment_test.exs | 58 +++++++++++++++++++ .../cms/comments/post_comment_test.exs | 58 +++++++++++++++++++ .../cms/comments/repo_comment_test.exs | 58 +++++++++++++++++++ 9 files changed, 262 insertions(+), 21 deletions(-) diff --git a/lib/groupher_server/cms/delegates/comment_action.ex b/lib/groupher_server/cms/delegates/comment_action.ex index e2046d412..8972e207f 100644 --- a/lib/groupher_server/cms/delegates/comment_action.ex +++ b/lib/groupher_server/cms/delegates/comment_action.ex @@ -21,14 +21,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.Model.User - - alias CMS.Model.{ - Comment, - PinnedComment, - CommentUpvote, - CommentReply, - Embeds - } + alias CMS.Model.{Comment, PinnedComment, CommentUpvote, CommentReply, Embeds} alias Ecto.Multi @@ -38,8 +31,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do @max_parent_replies_count Comment.max_parent_replies_count() @pinned_comment_limit Comment.pinned_comment_limit() - @spec pin_comment(Integer.t()) :: {:ok, Comment.t()} @doc "pin a comment" + @spec pin_comment(Integer.t()) :: {:ok, Comment.t()} def pin_comment(comment_id) do with {:ok, comment} <- ORM.find(Comment, comment_id), {:ok, full_comment} <- get_full_comment(comment.id), diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index fbfe541a1..61a8e95d0 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -5,17 +5,9 @@ defmodule GroupherServer.CMS.Helper.Macros do import Helper.Utils, only: [get_config: 2] alias GroupherServer.{CMS, Accounts} - alias Accounts.Model.User - alias CMS.Model.{ - Embeds, - Author, - Community, - Comment, - ArticleTag, - ArticleUpvote, - ArticleCollect - } + alias Accounts.Model.User + alias CMS.Model.{Embeds, Author, Community, Comment, ArticleTag, ArticleUpvote, ArticleCollect} @article_threads get_config(:article, :threads) diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index 7f7220927..3c081d0ac 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -76,7 +76,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content) # ####################### - # content flag .. + # article actions # ####################### def pin_article(_root, ~m(id community_id thread)a, _info) do CMS.pin_article(thread, id, community_id) @@ -336,6 +336,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.undo_mark_comment_solution(id, user) end + def pin_comment(_root, ~m(id)a, _info), do: CMS.pin_comment(id) + def undo_pin_comment(_root, ~m(id)a, _info), do: CMS.undo_pin_comment(id) + ############ ############ ############ diff --git a/lib/groupher_server_web/schema/cms/mutations/comment.ex b/lib/groupher_server_web/schema/cms/mutations/comment.ex index 81586cecc..f35ba6b7f 100644 --- a/lib/groupher_server_web/schema/cms/mutations/comment.ex +++ b/lib/groupher_server_web/schema/cms/mutations/comment.ex @@ -104,5 +104,27 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do middleware(M.Authorize, :login) resolve(&R.CMS.undo_mark_comment_solution/3) end + + @desc "pin a comment" + field :pin_comment, :comment do + arg(:id, non_null(:id)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :comment) + middleware(M.Passport, claim: "owner") + + resolve(&R.CMS.pin_comment/3) + end + + @desc "undo pin a comment" + field :undo_pin_comment, :comment do + arg(:id, non_null(:id)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :comment) + middleware(M.Passport, claim: "owner") + + resolve(&R.CMS.undo_pin_comment/3) + end end end diff --git a/test/groupher_server/cms/comments/post_comment_replies_test.exs b/test/groupher_server/cms/comments/post_comment_replies_test.exs index 1aee4209f..09276381b 100644 --- a/test/groupher_server/cms/comments/post_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/post_comment_replies_test.exs @@ -119,7 +119,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do assert not exist_in?(List.last(reply_comment_list), parent_comment.replies) end - @tag :wip test "replyed user should appear in article comment participants", ~m(post user user2)a do {:ok, parent_comment} = CMS.create_comment(:post, post.id, mock_comment(), user) {:ok, _} = CMS.reply_comment(parent_comment.id, mock_comment(), user2) diff --git a/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs index fb5b3db45..088c18ec5 100644 --- a/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) end end + + describe "[article comment pin/unPin]" do + @query """ + mutation($id: ID!){ + pinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can pin a blog's comment", ~m(owner_conn blog user)a do + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "pinComment") + + assert result["id"] == to_string(comment.id) + assert result["isPinned"] + end + + @tag :wip + test "unauth user fails.", ~m(guest_conn blog user)a do + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!){ + undoPinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can undo pin a blog's comment", ~m(owner_conn blog user)a do + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "undoPinComment") + + assert result["id"] == to_string(comment.id) + assert not result["isPinned"] + end + + @tag :wip + test "unauth user undo fails.", ~m(guest_conn blog user)a do + {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + end end diff --git a/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs index 58c19a00e..e9dee6182 100644 --- a/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/job_comment_test.exs @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) end end + + describe "[article comment pin/unPin]" do + @query """ + mutation($id: ID!){ + pinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can pin a job's comment", ~m(owner_conn job user)a do + {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "pinComment") + + assert result["id"] == to_string(comment.id) + assert result["isPinned"] + end + + @tag :wip + test "unauth user fails.", ~m(guest_conn job user)a do + {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!){ + undoPinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can undo pin a job's comment", ~m(owner_conn job user)a do + {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "undoPinComment") + + assert result["id"] == to_string(comment.id) + assert not result["isPinned"] + end + + @tag :wip + test "unauth user undo fails.", ~m(guest_conn job user)a do + {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + end end diff --git a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs index 883043a05..644efe330 100644 --- a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs @@ -271,6 +271,64 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do end end + describe "[article comment pin/unPin]" do + @query """ + mutation($id: ID!){ + pinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can pin a post's comment", ~m(owner_conn post user)a do + {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "pinComment") + + assert result["id"] == to_string(comment.id) + assert result["isPinned"] + end + + @tag :wip + test "unauth user fails.", ~m(guest_conn post user)a do + {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!){ + undoPinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can undo pin a post's comment", ~m(owner_conn post user)a do + {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "undoPinComment") + + assert result["id"] == to_string(comment.id) + assert not result["isPinned"] + end + + @tag :wip + test "unauth user undo fails.", ~m(guest_conn post user)a do + {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + end + describe "[post only: article comment solution]" do @query """ mutation($id: ID!) { diff --git a/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs index 4b0cfc3a1..f7b0ab710 100644 --- a/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) end end + + describe "[article comment pin/unPin]" do + @query """ + mutation($id: ID!){ + pinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can pin a repo's comment", ~m(owner_conn repo user)a do + {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "pinComment") + + assert result["id"] == to_string(comment.id) + assert result["isPinned"] + end + + @tag :wip + test "unauth user fails.", ~m(guest_conn repo user)a do + {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!){ + undoPinComment(id: $id) { + id + isPinned + } + } + """ + @tag :wip + test "can undo pin a repo's comment", ~m(owner_conn repo user)a do + {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + + variables = %{id: comment.id} + result = owner_conn |> mutation_result(@query, variables, "undoPinComment") + + assert result["id"] == to_string(comment.id) + assert not result["isPinned"] + end + + @tag :wip + test "unauth user undo fails.", ~m(guest_conn repo user)a do + {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) + {:ok, _} = CMS.pin_comment(comment.id) + variables = %{id: comment.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + end end