diff --git a/lib/groupher_server/cms/cms.ex b/lib/groupher_server/cms/cms.ex index 0c141f08d..ca452f48b 100644 --- a/lib/groupher_server/cms/cms.ex +++ b/lib/groupher_server/cms/cms.ex @@ -116,7 +116,6 @@ defmodule GroupherServer.CMS do # >> set flag on article, like: pin / unpin article defdelegate pin_article(thread, id, community_id), to: ArticleCommunity defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity - defdelegate lock_article_comment(thread, article_id), to: ArticleCommunity # >> community: set / unset defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity @@ -146,6 +145,8 @@ defmodule GroupherServer.CMS do defdelegate upvote_article_comment(comment_id, user), to: ArticleCommentAction defdelegate undo_upvote_article_comment(comment_id, user), to: ArticleCommentAction defdelegate reply_article_comment(comment_id, args, user), to: ArticleCommentAction + defdelegate lock_article_comment(thread, article_id), to: ArticleCommentAction + defdelegate undo_lock_article_comment(thread, article_id), to: ArticleCommentAction defdelegate pin_article_comment(comment_id), to: ArticleCommentAction defdelegate undo_pin_article_comment(comment_id), to: ArticleCommentAction diff --git a/lib/groupher_server/cms/delegates/article_comment.ex b/lib/groupher_server/cms/delegates/article_comment.ex index c2ee1fa0e..ea1aa15fe 100644 --- a/lib/groupher_server/cms/delegates/article_comment.ex +++ b/lib/groupher_server/cms/delegates/article_comment.ex @@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do CURD and operations for article comments """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1] + import Helper.Utils, only: [done: 1, ensure: 2] import Helper.ErrorCode import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3] @@ -22,6 +22,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do @default_emotions Embeds.ArticleCommentEmotion.default_emotions() @delete_hint ArticleComment.delete_hint() + @default_article_meta Embeds.ArticleMeta.default_meta() @default_comment_meta Embeds.ArticleCommentMeta.default_meta() @pinned_comment_limit ArticleComment.pinned_comment_limit() @@ -92,9 +93,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do """ def create_article_comment(thread, article_id, content, %User{} = user) do with {:ok, info} <- match(thread), - # make sure the article exsit - # author is passed by middleware, it's exsit for sure - {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do + {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]), + true <- can_comment?(article, user) do Multi.new() |> Multi.run(:create_article_comment, fn _, _ -> do_create_comment(content, info.foreign_key, article, user) @@ -113,9 +113,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do end) |> Repo.transaction() |> result() + else + false -> raise_error(:article_comment_locked, "this article is forbid comment") + {:error, error} -> {:error, error} end end + @doc "check is article can be comemnt or not" + # TODO: check if use is in author's block list? + def can_comment?(article, _user) do + article_meta = ensure(article.meta, @default_article_meta) + + not article_meta.is_comment_locked + end + @doc """ update a comment for article like psot, job ... """ diff --git a/lib/groupher_server/cms/delegates/article_comment_action.ex b/lib/groupher_server/cms/delegates/article_comment_action.ex index bcb9b3e93..cc021df09 100644 --- a/lib/groupher_server/cms/delegates/article_comment_action.ex +++ b/lib/groupher_server/cms/delegates/article_comment_action.ex @@ -3,11 +3,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do CURD and operations for article comments """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2] + import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2] import Helper.ErrorCode import GroupherServer.CMS.Delegate.ArticleComment, - only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2] + only: [ + add_participator_to_article: 2, + do_create_comment: 4, + update_article_comments_count: 2, + can_comment?: 2 + ] import GroupherServer.CMS.Helper.Matcher @@ -16,12 +21,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.User - alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply} + + alias CMS.{ + ArticleComment, + ArticlePinnedComment, + ArticleCommentUpvote, + ArticleCommentReply, + Embeds + } alias Ecto.Multi @article_threads get_config(:article, :threads) + @default_article_meta Embeds.ArticleMeta.default_meta() @max_parent_replies_count ArticleComment.max_parent_replies_count() @pinned_comment_limit ArticleComment.pinned_comment_limit() @@ -102,6 +115,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do ORM.find_by(ArticleComment, %{id: comment_id, is_deleted: false}), replying_comment <- Repo.preload(target_comment, reply_to: :author), {thread, article} <- get_article(replying_comment), + true <- can_comment?(article, user), {:ok, info} <- match(thread), parent_comment <- get_parent_comment(replying_comment) do Multi.new() @@ -136,6 +150,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do end) |> Repo.transaction() |> result() + else + false -> raise_error(:article_comment_locked, "this article is forbid comment") + {:error, error} -> {:error, error} end end @@ -192,6 +209,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do end end + @doc "lock comment of a article" + def lock_article_comment(thread, id) do + with {:ok, info} <- match(thread), + {:ok, article} <- ORM.find(info.model, id) do + article_meta = ensure(article.meta, @default_article_meta) + meta = Map.merge(article_meta, %{is_comment_locked: true}) + + ORM.update_meta(article, meta) + end + end + + @doc "undo lock comment of a article" + def undo_lock_article_comment(thread, id) do + with {:ok, info} <- match(thread), + {:ok, article} <- ORM.find(info.model, id) do + article_meta = ensure(article.meta, @default_article_meta) + meta = Map.merge(article_meta, %{is_comment_locked: false}) + + ORM.update_meta(article, meta) + end + end + defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do with {:ok, article} = get_full_comment(comment.id) do is_article_author_upvoted = article.author.id == user_id diff --git a/lib/groupher_server/cms/delegates/article_community.ex b/lib/groupher_server/cms/delegates/article_community.ex index 665f5578c..0cd8f9ab1 100644 --- a/lib/groupher_server/cms/delegates/article_community.ex +++ b/lib/groupher_server/cms/delegates/article_community.ex @@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do import Ecto.Query, warn: false import Helper.ErrorCode - import Helper.Utils, only: [strip_struct: 1, done: 1, ensure: 2] + import Helper.Utils, only: [strip_struct: 1, done: 1] import GroupherServer.CMS.Helper.Matcher alias Helper.Types, as: T @@ -144,17 +144,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do def update_edit_status(content, _), do: {:ok, content} - @doc "lock comment of a article" - def lock_article_comment(thread, id) do - with {:ok, info} <- match(thread), - {:ok, article} <- ORM.find(info.model, id) do - article_meta = ensure(article.meta, @default_article_meta) - meta = Map.merge(article_meta, %{is_comment_locked: true}) - - ORM.update_meta(article, meta) - end - end - # check if the thread has aready enough pinned articles defp check_pinned_article_count(community_id, thread) do thread_upcase = thread |> to_string |> String.upcase() diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index a52039c2b..8491b30c1 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -105,6 +105,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do # ####################### # thread reaction .. # ####################### + def lock_article_comment(_root, ~m(id thread)a, _info), do: CMS.lock_article_comment(thread, id) + + def undo_lock_article_comment(_root, ~m(id thread)a, _info) do + CMS.undo_lock_article_comment(thread, id) + end + def sink_article(_root, ~m(id thread)a, _info), do: CMS.sink_article(thread, id) def undo_sink_article(_root, ~m(id thread)a, _info), do: CMS.undo_sink_article(thread, id) diff --git a/lib/groupher_server_web/schema/Helper/mutations.ex b/lib/groupher_server_web/schema/Helper/mutations.ex index e44e586cd..edc8c638b 100644 --- a/lib/groupher_server_web/schema/Helper/mutations.ex +++ b/lib/groupher_server_web/schema/Helper/mutations.ex @@ -5,34 +5,6 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do alias GroupherServerWeb.Middleware, as: M alias GroupherServerWeb.Resolvers, as: R - defmacro article_sink_mutation(thread) do - quote do - @desc unquote("sink a #{thread}") - field unquote(:"sink_#{thread}"), :article do - arg(:id, non_null(:id)) - arg(:community_id, non_null(:id)) - arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink")) - resolve(&R.CMS.sink_article/3) - end - - @desc unquote("undo sink to #{thread}") - field unquote(:"undo_sink_#{thread}"), :article do - arg(:id, non_null(:id)) - arg(:community_id, non_null(:id)) - arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink")) - resolve(&R.CMS.undo_sink_article/3) - end - end - end - defmacro article_upvote_mutation(thread) do quote do @desc unquote("upvote to #{thread}") @@ -172,4 +144,60 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do end end end + + defmacro article_sink_mutation(thread) do + quote do + @desc unquote("sink a #{thread}") + field unquote(:"sink_#{thread}"), :article do + arg(:id, non_null(:id)) + arg(:community_id, non_null(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :community) + middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink")) + resolve(&R.CMS.sink_article/3) + end + + @desc unquote("undo sink to #{thread}") + field unquote(:"undo_sink_#{thread}"), :article do + arg(:id, non_null(:id)) + arg(:community_id, non_null(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :community) + middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink")) + resolve(&R.CMS.undo_sink_article/3) + end + end + end + + defmacro article_lock_comment_mutation(thread) do + quote do + @desc unquote("lock comment to a #{thread}") + field unquote(:"lock_#{thread}_comment"), :article do + arg(:id, non_null(:id)) + arg(:community_id, non_null(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :community) + middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.lock_comment")) + resolve(&R.CMS.lock_article_comment/3) + end + + @desc unquote("undo lock to a #{thread}") + field unquote(:"undo_lock_#{thread}_comment"), :article do + arg(:id, non_null(:id)) + arg(:community_id, non_null(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: :community) + middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_lock_comment")) + resolve(&R.CMS.undo_lock_article_comment/3) + end + end + end end diff --git a/lib/groupher_server_web/schema/cms/mutations/job.ex b/lib/groupher_server_web/schema/cms/mutations/job.ex index c2c0e8a5a..08c98e429 100644 --- a/lib/groupher_server_web/schema/cms/mutations/job.ex +++ b/lib/groupher_server_web/schema/cms/mutations/job.ex @@ -55,13 +55,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do end ############# - article_sink_mutation(:job) article_upvote_mutation(:job) article_pin_mutation(:job) article_mark_delete_mutation(:job) article_delete_mutation(:job) article_emotion_mutation(:job) article_report_mutation(:job) + article_sink_mutation(:job) + article_lock_comment_mutation(:job) ############# end end diff --git a/lib/groupher_server_web/schema/cms/mutations/post.ex b/lib/groupher_server_web/schema/cms/mutations/post.ex index 6c80201e9..059f238d3 100644 --- a/lib/groupher_server_web/schema/cms/mutations/post.ex +++ b/lib/groupher_server_web/schema/cms/mutations/post.ex @@ -45,13 +45,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do end ############# - article_sink_mutation(:post) article_upvote_mutation(:post) article_pin_mutation(:post) article_mark_delete_mutation(:post) article_delete_mutation(:post) article_emotion_mutation(:post) article_report_mutation(:post) + article_sink_mutation(:post) + article_lock_comment_mutation(:post) ############# end end diff --git a/lib/groupher_server_web/schema/cms/mutations/repo.ex b/lib/groupher_server_web/schema/cms/mutations/repo.ex index c7776ff43..84d699a11 100644 --- a/lib/groupher_server_web/schema/cms/mutations/repo.ex +++ b/lib/groupher_server_web/schema/cms/mutations/repo.ex @@ -71,12 +71,13 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do end ############# - article_sink_mutation(:repo) article_pin_mutation(:repo) article_mark_delete_mutation(:repo) article_delete_mutation(:repo) article_emotion_mutation(:repo) article_report_mutation(:repo) + article_sink_mutation(:repo) + article_lock_comment_mutation(:repo) ############# end end diff --git a/lib/helper/certification.ex b/lib/helper/certification.ex index 8d0ea2481..3afbd2075 100644 --- a/lib/helper/certification.ex +++ b/lib/helper/certification.ex @@ -72,6 +72,13 @@ defmodule Helper.Certification do "job.undo_sink", "repo.sink", "repo.undo_sink", + # lock/undo_lock article comment + "post.lock_comment", + "post.undo_lock_comment", + "job.lock_comment", + "job.undo_lock_comment", + "repo.lock_comment", + "repo.undo_lock_comment", # "post.mark_delete", "post.undo_mark_delete", diff --git a/lib/helper/error_code.ex b/lib/helper/error_code.ex index 247fab5c7..6e15d28c5 100644 --- a/lib/helper/error_code.ex +++ b/lib/helper/error_code.ex @@ -49,6 +49,7 @@ defmodule Helper.ErrorCode do def ecode(:mirror_article), do: @article_base + 5 def ecode(:invalid_domain_tag), do: @article_base + 6 def ecode(:undo_sink_old_article), do: @article_base + 7 + def ecode(:article_comment_locked), do: @article_base + 8 def ecode, do: @default_base # def ecode(_), do: @default_base diff --git a/test/groupher_server/cms/articles/post_meta_test.exs b/test/groupher_server/cms/articles/post_meta_test.exs index 53bda7276..6f62a3f53 100644 --- a/test/groupher_server/cms/articles/post_meta_test.exs +++ b/test/groupher_server/cms/articles/post_meta_test.exs @@ -42,7 +42,7 @@ defmodule GroupherServer.Test.CMS.PostMeta do assert post.meta.is_edited end - test "post's lock article should work", ~m(user community post_attrs)a do + test "post's lock/undo_lock article should work", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) assert not post.meta.is_comment_locked @@ -50,6 +50,11 @@ defmodule GroupherServer.Test.CMS.PostMeta do {:ok, post} = ORM.find_by(Post, id: post.id) assert post.meta.is_comment_locked + + {:ok, _} = CMS.undo_lock_article_comment(:post, post.id) + {:ok, post} = ORM.find_by(Post, id: post.id) + + assert not post.meta.is_comment_locked end # TODO: diff --git a/test/groupher_server/cms/comments/job_comment_test.exs b/test/groupher_server/cms/comments/job_comment_test.exs index 7571de272..4e5875df8 100644 --- a/test/groupher_server/cms/comments/job_comment_test.exs +++ b/test/groupher_server/cms/comments/job_comment_test.exs @@ -65,6 +65,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do assert job.active_at == job.inserted_at end + @tag :wip test "old job will not update active after comment created", ~m(user)a do active_period_days = Map.get(@active_period, :job) @@ -83,7 +84,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do Timex.shift(Timex.now(), days: -(active_period_days + 1)) |> Timex.to_datetime() {:ok, job} = db_insert(:job, %{inserted_at: inserted_at}) - Process.sleep(1000) + Process.sleep(3000) {:ok, _comment} = CMS.create_article_comment(:job, job.id, "job comment", user) {:ok, job} = ORM.find(Job, job.id) @@ -641,4 +642,30 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do assert comment.is_article_author end end + + describe "[lock/unlock job comment]" do + test "locked job can not be comment", ~m(user job)a do + {:ok, _} = CMS.create_article_comment(:job, job.id, "comment", user) + {:ok, _} = CMS.lock_article_comment(:job, job.id) + + {:error, reason} = CMS.create_article_comment(:job, job.id, "comment", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:job, job.id) + {:ok, _} = CMS.create_article_comment(:job, job.id, "comment", user) + end + + test "locked job can not by reply", ~m(user job)a do + {:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent_conent", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + + {:ok, _} = CMS.lock_article_comment(:job, job.id) + + {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:job, job.id) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + end + end end diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index 40eb2924d..4335e5c4b 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -66,7 +66,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do assert post.active_at == post.inserted_at end - @tag :wip test "old post will not update active after comment created", ~m(user)a do active_period_days = Map.get(@active_period, :post) @@ -85,7 +84,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do Timex.shift(Timex.now(), days: -(active_period_days + 1)) |> Timex.to_datetime() {:ok, post} = db_insert(:post, %{inserted_at: inserted_at}) - Process.sleep(1000) + Process.sleep(3000) {:ok, _comment} = CMS.create_article_comment(:post, post.id, "post comment", user) {:ok, post} = ORM.find(Post, post.id) @@ -643,4 +642,30 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do assert comment.is_article_author end end + + describe "[lock/unlock post comment]" do + test "locked post can not be comment", ~m(user post)a do + {:ok, _} = CMS.create_article_comment(:post, post.id, "comment", user) + {:ok, _} = CMS.lock_article_comment(:post, post.id) + + {:error, reason} = CMS.create_article_comment(:post, post.id, "comment", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:post, post.id) + {:ok, _} = CMS.create_article_comment(:post, post.id, "comment", user) + end + + test "locked post can not by reply", ~m(user post)a do + {:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent_conent", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + + {:ok, _} = CMS.lock_article_comment(:post, post.id) + + {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:post, post.id) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + end + end end diff --git a/test/groupher_server/cms/comments/repo_comment_test.exs b/test/groupher_server/cms/comments/repo_comment_test.exs index 984e05974..cec0f8b44 100644 --- a/test/groupher_server/cms/comments/repo_comment_test.exs +++ b/test/groupher_server/cms/comments/repo_comment_test.exs @@ -84,7 +84,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do Timex.shift(Timex.now(), days: -(active_period_days + 1)) |> Timex.to_datetime() {:ok, repo} = db_insert(:repo, %{inserted_at: inserted_at}) - Process.sleep(1000) + Process.sleep(3000) {:ok, _comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) {:ok, repo} = ORM.find(Repo, repo.id) @@ -642,4 +642,30 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do assert comment.is_article_author end end + + describe "[lock/unlock repo comment]" do + test "locked repo can not be comment", ~m(user repo)a do + {:ok, _} = CMS.create_article_comment(:repo, repo.id, "comment", user) + {:ok, _} = CMS.lock_article_comment(:repo, repo.id) + + {:error, reason} = CMS.create_article_comment(:repo, repo.id, "comment", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:repo, repo.id) + {:ok, _} = CMS.create_article_comment(:repo, repo.id, "comment", user) + end + + test "locked repo can not by reply", ~m(user repo)a do + {:ok, parent_comment} = CMS.create_article_comment(:repo, repo.id, "parent_conent", user) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + + {:ok, _} = CMS.lock_article_comment(:repo, repo.id) + + {:error, reason} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + assert reason |> is_error?(:article_comment_locked) + + {:ok, _} = CMS.undo_lock_article_comment(:repo, repo.id) + {:ok, _} = CMS.reply_article_comment(parent_comment.id, "reply_content", user) + 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 86eba207e..bd6c95e45 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 @@ -2,11 +2,14 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do use GroupherServer.TestTools alias GroupherServer.CMS + alias CMS.Job + + alias Helper.ORM setup do - {:ok, job} = db_insert(:job) {:ok, user} = db_insert(:user) {:ok, community} = db_insert(:community) + {:ok, job} = CMS.create_article(community, :job, mock_attrs(:job), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) @@ -24,7 +27,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "write article comment to a exsit job", ~m(job user_conn)a do comment = "a test comment" variables = %{thread: "JOB", id: job.id, content: comment} @@ -43,7 +45,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "login user can reply to a comment", ~m(job user user_conn)a do {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) variables = %{id: comment.id, content: "reply content"} @@ -63,7 +64,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "only owner can update a exsit comment", ~m(job user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) @@ -88,7 +88,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "only owner can delete a exsit comment", ~m(job user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) @@ -123,7 +122,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "login user can emotion to a comment", ~m(job user user_conn)a do {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) variables = %{id: comment.id, emotion: "BEER"} @@ -150,7 +148,6 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do } } """ - test "login user can undo emotion to a comment", ~m(job user owner_conn)a do {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) @@ -164,4 +161,62 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do assert not get_in(comment, ["emotions", "viewerHasBeered"]) end end + + describe "[article comment lock/unlock]" do + @query """ + mutation($id: ID!, $communityId: ID!){ + lockJobComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can lock a job's comment", ~m(community job)a do + variables = %{id: job.id, communityId: community.id} + passport_rules = %{community.raw => %{"job.lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "lockJobComment") + assert result["id"] == to_string(job.id) + + {:ok, job} = ORM.find(Job, job.id) + assert job.meta.is_comment_locked + end + + test "unauth user fails", ~m(guest_conn community job)a do + variables = %{id: job.id, communityId: community.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!, $communityId: ID!){ + undoLockJobComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can undo lock a job's comment", ~m(community job)a do + {:ok, _} = CMS.lock_article_comment(:job, job.id) + {:ok, job} = ORM.find(Job, job.id) + assert job.meta.is_comment_locked + + variables = %{id: job.id, communityId: community.id} + passport_rules = %{community.raw => %{"job.undo_lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "undoLockJobComment") + assert result["id"] == to_string(job.id) + + {:ok, job} = ORM.find(Job, job.id) + assert not job.meta.is_comment_locked + end + + test "unauth user undo fails", ~m(guest_conn community job)a do + variables = %{id: job.id, communityId: community.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 52d43f49c..77f9e8d8c 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 @@ -2,11 +2,14 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do use GroupherServer.TestTools alias GroupherServer.CMS + alias CMS.Post + + alias Helper.ORM setup do - {:ok, post} = db_insert(:post) {:ok, user} = db_insert(:user) {:ok, community} = db_insert(:community) + {:ok, post} = CMS.create_article(community, :post, mock_attrs(:post), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) @@ -24,7 +27,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "write article comment to a exsit post", ~m(post user_conn)a do comment = "a test comment" variables = %{thread: "POST", id: post.id, content: comment} @@ -43,7 +45,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "login user can reply to a comment", ~m(post user user_conn)a do {:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user) variables = %{id: comment.id, content: "reply content"} @@ -63,7 +64,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "only owner can update a exsit comment", ~m(post user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) @@ -88,7 +88,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "only owner can delete a exsit comment", ~m(post user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) @@ -123,7 +122,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "login user can emotion to a comment", ~m(post user user_conn)a do {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) variables = %{id: comment.id, emotion: "BEER"} @@ -150,7 +148,6 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do } } """ - test "login user can undo emotion to a comment", ~m(post user owner_conn)a do {:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) @@ -164,4 +161,62 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do assert not get_in(comment, ["emotions", "viewerHasBeered"]) end end + + describe "[article comment lock/unlock]" do + @query """ + mutation($id: ID!, $communityId: ID!){ + lockPostComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can lock a post's comment", ~m(community post)a do + variables = %{id: post.id, communityId: community.id} + passport_rules = %{community.raw => %{"post.lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "lockPostComment") + assert result["id"] == to_string(post.id) + + {:ok, post} = ORM.find(Post, post.id) + assert post.meta.is_comment_locked + end + + test "unauth user fails", ~m(guest_conn community post)a do + variables = %{id: post.id, communityId: community.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!, $communityId: ID!){ + undoLockPostComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can undo lock a post's comment", ~m(community post)a do + {:ok, _} = CMS.lock_article_comment(:post, post.id) + {:ok, post} = ORM.find(Post, post.id) + assert post.meta.is_comment_locked + + variables = %{id: post.id, communityId: community.id} + passport_rules = %{community.raw => %{"post.undo_lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "undoLockPostComment") + assert result["id"] == to_string(post.id) + + {:ok, post} = ORM.find(Post, post.id) + assert not post.meta.is_comment_locked + end + + test "unauth user undo fails", ~m(guest_conn community post)a do + variables = %{id: post.id, communityId: community.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/repo_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs index 1800187ba..ef0d5b6f6 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 @@ -2,11 +2,14 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do use GroupherServer.TestTools alias GroupherServer.CMS + alias CMS.Repo + + alias Helper.ORM setup do - {:ok, repo} = db_insert(:repo) {:ok, user} = db_insert(:user) {:ok, community} = db_insert(:community) + {:ok, repo} = CMS.create_article(community, :repo, mock_attrs(:repo), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) @@ -24,7 +27,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "write article comment to a exsit repo", ~m(repo user_conn)a do comment = "a test comment" variables = %{thread: "REPO", id: repo.id, content: comment} @@ -43,7 +45,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "login user can reply to a comment", ~m(repo user user_conn)a do {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "commment", user) variables = %{id: comment.id, content: "reply content"} @@ -63,7 +64,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "only owner can update a exsit comment", ~m(repo user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) @@ -88,7 +88,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "only owner can delete a exsit comment", ~m(repo user guest_conn user_conn owner_conn)a do {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) @@ -123,7 +122,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "login user can emotion to a comment", ~m(repo user user_conn)a do {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) variables = %{id: comment.id, emotion: "BEER"} @@ -150,7 +148,6 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do } } """ - test "login user can undo emotion to a comment", ~m(repo user owner_conn)a do {:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user) {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) @@ -164,4 +161,62 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do assert not get_in(comment, ["emotions", "viewerHasBeered"]) end end + + describe "[article comment lock/unlock]" do + @query """ + mutation($id: ID!, $communityId: ID!){ + lockRepoComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can lock a repo's comment", ~m(community repo)a do + variables = %{id: repo.id, communityId: community.id} + passport_rules = %{community.raw => %{"repo.lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "lockRepoComment") + assert result["id"] == to_string(repo.id) + + {:ok, repo} = ORM.find(Repo, repo.id) + assert repo.meta.is_comment_locked + end + + test "unauth user fails", ~m(guest_conn community repo)a do + variables = %{id: repo.id, communityId: community.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + + @query """ + mutation($id: ID!, $communityId: ID!){ + undoLockRepoComment(id: $id, communityId: $communityId) { + id + } + } + """ + + test "can undo lock a repo's comment", ~m(community repo)a do + {:ok, _} = CMS.lock_article_comment(:repo, repo.id) + {:ok, repo} = ORM.find(Repo, repo.id) + assert repo.meta.is_comment_locked + + variables = %{id: repo.id, communityId: community.id} + passport_rules = %{community.raw => %{"repo.undo_lock_comment" => true}} + rule_conn = simu_conn(:user, cms: passport_rules) + + result = rule_conn |> mutation_result(@query, variables, "undoLockRepoComment") + assert result["id"] == to_string(repo.id) + + {:ok, repo} = ORM.find(Repo, repo.id) + assert not repo.meta.is_comment_locked + end + + test "unauth user undo fails", ~m(guest_conn community repo)a do + variables = %{id: repo.id, communityId: community.id} + + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) + end + end end diff --git a/test/groupher_server_web/mutation/cms/sink/post_sink_test.exs b/test/groupher_server_web/mutation/cms/sink/post_sink_test.exs index 15b7a4829..70d326ce3 100644 --- a/test/groupher_server_web/mutation/cms/sink/post_sink_test.exs +++ b/test/groupher_server_web/mutation/cms/sink/post_sink_test.exs @@ -26,7 +26,6 @@ defmodule GroupherServer.Test.Mutation.Sink.PostSink do } } """ - test "login user can sink a post", ~m(community post)a do variables = %{id: post.id, communityId: community.id} passport_rules = %{community.raw => %{"post.sink" => true}}