From fbed45c3aad467384bb69454b62fa48657049816 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sat, 1 Jul 2023 14:53:43 +0800 Subject: [PATCH] chore: tests --- lib/groupher_server/cms/cms.ex | 2 + .../cms/delegates/article_curd.ex | 80 ++++++++++++++----- .../cms/delegates/community_curd.ex | 2 +- .../resolvers/cms_resolver.ex | 8 ++ .../schema/Helper/mutations.ex | 26 ++++++ .../schema/cms/cms_types.ex | 4 + .../cms/articles/blog_test.exs | 44 ++++++++++ .../cms/articles/changelog_test.exs | 44 ++++++++++ .../groupher_server/cms/articles/doc_test.exs | 44 ++++++++++ .../cms/articles/post_test.exs | 44 ++++++++++ .../mutation/cms/flags/blog_flag_test.exs | 73 ++++++++++++++++- .../cms/flags/changelog_flag_test.exs | 73 ++++++++++++++++- .../mutation/cms/flags/doc_flag_test.exs | 71 +++++++++++++++- .../mutation/cms/flags/post_flag_test.exs | 71 +++++++++++++++- 14 files changed, 558 insertions(+), 28 deletions(-) diff --git a/lib/groupher_server/cms/cms.ex b/lib/groupher_server/cms/cms.ex index bea2b03..39d486c 100644 --- a/lib/groupher_server/cms/cms.ex +++ b/lib/groupher_server/cms/cms.ex @@ -123,6 +123,8 @@ defmodule GroupherServer.CMS do defdelegate undo_sink_article(thread, id), to: ArticleCURD defdelegate archive_articles(thread), to: ArticleCURD + defdelegate batch_mark_delete_articles(community, thread, id_list), to: ArticleCURD + defdelegate batch_undo_mark_delete_articles(community, thread, id_list), to: ArticleCURD defdelegate paged_citing_contents(type, id, filter), to: CitedArtiment diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index ea5ef42..aca19b5 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -60,15 +60,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do @doc """ read articles for un-logined user """ - def read_article(community_slug, thread, id) when thread in @article_threads do - with {:ok, article} <- check_article_pending(community_slug, thread, id) do + def read_article(community_slug, thread, inner_id) when thread in @article_threads do + with {:ok, article} <- if_article_legal(community_slug, thread, inner_id) do do_read_article(article, thread) end end - def read_article(community_slug, thread, id, %User{id: user_id} = user) + def read_article(community_slug, thread, inner_id, %User{id: user_id} = user) when thread in @article_threads do - with {:ok, article} <- check_article_pending(community_slug, thread, id, user) do + with {:ok, article} <- if_article_legal(community_slug, thread, inner_id, user) do Multi.new() |> Multi.run(:normal_read, fn _, _ -> do_read_article(article, thread) end) |> Multi.run(:add_viewed_user, fn _, %{normal_read: article} -> @@ -591,6 +591,47 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do end end + @doc """ + make sure the given ids are deleted + """ + def batch_mark_delete_articles(community, thread, inner_id_list) do + do_batch_mark_delete_articles(community, thread, inner_id_list, true) + end + + @doc """ + make sure the given ids are deleted + """ + def batch_undo_mark_delete_articles(community, thread, inner_id_list) do + do_batch_mark_delete_articles(community, thread, inner_id_list, false) + end + + defp do_batch_mark_delete_articles(community, thread, inner_id_list, delete_flag) do + with {:ok, info} <- match(thread) do + batch_query = + info.model + |> where([article], article.original_community_slug == ^community) + |> where([article], article.inner_id in ^inner_id_list) + + Multi.new() + |> Multi.run(:update_articles, fn _, _ -> + batch_query + |> Repo.update_all(set: [mark_delete: delete_flag]) + |> done + end) + |> Multi.run(:update_community_article_count, fn _, _ -> + communities = + from(a in batch_query, preload: :communities) + |> Repo.all() + |> Enum.map(& &1.communities) + |> Enum.at(0) + + CommunityCURD.update_community_count_field(communities, thread) + end) + |> Repo.transaction() + |> result() + end + end + @doc """ remove article forever """ @@ -669,43 +710,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do |> result() end - defp check_article_pending(community_slug, thread, id, user) + defp if_article_legal(community_slug, thread, inner_id, user) when thread in @article_threads do - clauses = %{original_community_slug: community_slug, inner_id: id} + clauses = %{original_community_slug: community_slug, inner_id: inner_id} with {:ok, info} <- match(thread), {:ok, article} <- ORM.find_by(info.model, clauses, preload: :author) do - check_article_pending(article, user) + if_article_legal(article, user) end end - defp check_article_pending(community_slug, thread, id) + defp if_article_legal(community_slug, thread, inner_id) when thread in @article_threads do - clauses = %{original_community_slug: community_slug, inner_id: id} + clauses = %{original_community_slug: community_slug, inner_id: inner_id} with {:ok, info} <- match(thread), {:ok, article} <- ORM.find_by(info.model, clauses) do - check_article_pending(article) + if_article_legal(article) end end # pending article can be seen is viewer is author - defp check_article_pending(thread, id, %User{} = user) when thread in @article_threads do + defp if_article_legal(thread, id, %User{} = user) when thread in @article_threads do with {:ok, info} <- match(thread), {:ok, article} <- ORM.find(info.model, id, preload: :author) do - check_article_pending(article, user) + if_article_legal(article, user) end end - defp check_article_pending(%{pending: @audit_legal} = article, _) do + defp if_article_legal(%{pending: @audit_legal} = article, _) do {:ok, article} end - defp check_article_pending(%{pending: @audit_failed} = article, _) do + defp if_article_legal(%{pending: @audit_failed} = article, _) do {:ok, article} end - defp check_article_pending(%{pending: @audit_illegal} = article, %User{id: user_id}) do + defp if_article_legal(%{pending: @audit_illegal} = article, %User{id: user_id}) do case article.author.user_id == user_id do true -> {:ok, article} false -> raise_error(:pending, "this article is under audition") @@ -713,18 +754,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do end # pending article should not be seen - defp check_article_pending(thread, id) when thread in @article_threads do + defp if_article_legal(thread, id) when thread in @article_threads do with {:ok, info} <- match(thread), {:ok, article} <- ORM.find(info.model, id) do - check_article_pending(article) + if_article_legal(article) end end - defp check_article_pending(%{pending: @audit_illegal}) do + defp if_article_legal(%{pending: @audit_illegal}) do raise_error(:pending, "this article is under audition") end - defp check_article_pending(article), do: {:ok, article} + defp if_article_legal(article), do: {:ok, article} defp add_pin_articles_ifneed(articles, querable, %{community: community} = filter) do thread = module_to_atom(querable) @@ -853,6 +894,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do defp result({:ok, %{update_edit_status: result}}), do: {:ok, result} defp result({:ok, %{update_article: result}}), do: {:ok, result} + defp result({:ok, %{update_articles: result}}), do: {:ok, %{done: true}} defp result({:ok, %{delete_article: result}}), do: {:ok, result} # NOTE: for read article, order is import defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done() diff --git a/lib/groupher_server/cms/delegates/community_curd.ex b/lib/groupher_server/cms/delegates/community_curd.ex index 1c3171f..dab338b 100644 --- a/lib/groupher_server/cms/delegates/community_curd.ex +++ b/lib/groupher_server/cms/delegates/community_curd.ex @@ -267,7 +267,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do end def update_community_count_field(communities, thread) when is_list(communities) do - case Enum.all?(communities, &({:ok, _} = update_community_count_field(&1, thread))) do + case Enum.all?(Enum.uniq(communities), &({:ok, _} = update_community_count_field(&1, thread))) do true -> {:ok, :pass} false -> {:error, "update_community_count_field"} end diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index 62266f1..9194a2e 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -153,6 +153,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.mark_delete_article(thread, id) end + def batch_mark_delete_articles(_root, ~m(community thread ids)a, _info) do + CMS.batch_mark_delete_articles(community, thread, ids) + end + + def batch_undo_mark_delete_articles(_root, ~m(community thread ids)a, _info) do + CMS.batch_undo_mark_delete_articles(community, thread, ids) + end + def undo_mark_delete_article(_root, ~m(id thread)a, _info) do CMS.undo_mark_delete_article(thread, id) end diff --git a/lib/groupher_server_web/schema/Helper/mutations.ex b/lib/groupher_server_web/schema/Helper/mutations.ex index 683d089..f9ca3eb 100644 --- a/lib/groupher_server_web/schema/Helper/mutations.ex +++ b/lib/groupher_server_web/schema/Helper/mutations.ex @@ -26,6 +26,8 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do alias GroupherServerWeb.Middleware, as: M alias GroupherServerWeb.Resolvers, as: R + import Helper.Utils, only: [plural: 1] + @doc """ add basic mutation reactions to article """ @@ -135,6 +137,30 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do resolve(&R.CMS.undo_mark_delete_article/3) end + + @desc unquote("batch mark delete #{plural(thread)} type article, aka soft-delete") + field unquote(:"batch_mark_delete_#{plural(thread)}"), :done_state do + arg(:community, non_null(:string)) + arg(:ids, list_of(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.Passport, claim: unquote("cms->#{to_string(thread)}.mark_delete")) + + resolve(&R.CMS.batch_mark_delete_articles/3) + end + + @desc unquote("batch undo mark delete #{plural(thread)} type article, aka soft-delete") + field unquote(:"batch_undo_mark_delete_#{plural(thread)}"), :done_state do + arg(:community, non_null(:string)) + arg(:ids, list_of(:id)) + arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread)) + + middleware(M.Authorize, :login) + middleware(M.Passport, claim: unquote("cms->#{to_string(thread)}.mark_delete")) + + resolve(&R.CMS.batch_undo_mark_delete_articles/3) + end end end diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index 3ab54ba..6b45a38 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -19,6 +19,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:exist, :boolean) end + object :done_state do + field(:done, :boolean) + end + ###### # common stands for minimal info of the type # usually used in abuse_report, feeds, etc .. diff --git a/test/groupher_server/cms/articles/blog_test.exs b/test/groupher_server/cms/articles/blog_test.exs index 9fd1563..aee3a9e 100644 --- a/test/groupher_server/cms/articles/blog_test.exs +++ b/test/groupher_server/cms/articles/blog_test.exs @@ -248,6 +248,50 @@ defmodule GroupherServer.Test.CMS.Articles.Blog do end end + describe "[cms blog batch delete]" do + test "can batch delete blogs with inner_ids", ~m(user community blog_attrs)a do + {:ok, blog1} = CMS.create_article(community, :blog, blog_attrs, user) + {:ok, blog2} = CMS.create_article(community, :blog, blog_attrs, user) + {:ok, blog3} = CMS.create_article(community, :blog, blog_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :blog, [ + blog1.inner_id, + blog2.inner_id + ]) + + {:ok, blog1} = ORM.find(Blog, blog1.id) + {:ok, blog2} = ORM.find(Blog, blog2.id) + {:ok, blog3} = ORM.find(Blog, blog3.id) + + assert blog1.mark_delete == true + assert blog2.mark_delete == true + assert blog3.mark_delete == false + end + + test "can undo batch delete blogs with inner_ids", ~m(user community blog_attrs)a do + {:ok, blog1} = CMS.create_article(community, :blog, blog_attrs, user) + {:ok, blog2} = CMS.create_article(community, :blog, blog_attrs, user) + {:ok, blog3} = CMS.create_article(community, :blog, blog_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :blog, [ + blog1.inner_id, + blog2.inner_id + ]) + + CMS.batch_undo_mark_delete_articles(community.slug, :blog, [ + blog1.inner_id, + blog2.inner_id + ]) + + {:ok, blog1} = ORM.find(Blog, blog1.id) + {:ok, blog2} = ORM.find(Blog, blog2.id) + {:ok, blog3} = ORM.find(Blog, blog3.id) + + assert blog1.mark_delete == false + assert blog2.mark_delete == false + end + end + describe "[cms blog document]" do test "will create related document after create", ~m(user community blog_attrs)a do {:ok, blog} = CMS.create_article(community, :blog, blog_attrs, user) diff --git a/test/groupher_server/cms/articles/changelog_test.exs b/test/groupher_server/cms/articles/changelog_test.exs index 5553805..7bdf48a 100644 --- a/test/groupher_server/cms/articles/changelog_test.exs +++ b/test/groupher_server/cms/articles/changelog_test.exs @@ -265,6 +265,50 @@ defmodule GroupherServer.Test.CMS.Articles.Changelog do end end + describe "[cms changelog batch delete]" do + test "can batch delete changelogs with inner_ids", ~m(user community changelog_attrs)a do + {:ok, changelog1} = CMS.create_article(community, :changelog, changelog_attrs, user) + {:ok, changelog2} = CMS.create_article(community, :changelog, changelog_attrs, user) + {:ok, changelog3} = CMS.create_article(community, :changelog, changelog_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :changelog, [ + changelog1.inner_id, + changelog2.inner_id + ]) + + {:ok, changelog1} = ORM.find(Changelog, changelog1.id) + {:ok, changelog2} = ORM.find(Changelog, changelog2.id) + {:ok, changelog3} = ORM.find(Changelog, changelog3.id) + + assert changelog1.mark_delete == true + assert changelog2.mark_delete == true + assert changelog3.mark_delete == false + end + + test "can undo batch delete changelogs with inner_ids", ~m(user community changelog_attrs)a do + {:ok, changelog1} = CMS.create_article(community, :changelog, changelog_attrs, user) + {:ok, changelog2} = CMS.create_article(community, :changelog, changelog_attrs, user) + {:ok, changelog3} = CMS.create_article(community, :changelog, changelog_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :changelog, [ + changelog1.inner_id, + changelog2.inner_id + ]) + + CMS.batch_undo_mark_delete_articles(community.slug, :changelog, [ + changelog1.inner_id, + changelog2.inner_id + ]) + + {:ok, changelog1} = ORM.find(Changelog, changelog1.id) + {:ok, changelog2} = ORM.find(Changelog, changelog2.id) + {:ok, changelog3} = ORM.find(Changelog, changelog3.id) + + assert changelog1.mark_delete == false + assert changelog2.mark_delete == false + end + end + describe "[cms changelog document]" do test "will create related document after create", ~m(user community changelog_attrs)a do {:ok, changelog} = CMS.create_article(community, :changelog, changelog_attrs, user) diff --git a/test/groupher_server/cms/articles/doc_test.exs b/test/groupher_server/cms/articles/doc_test.exs index 57a16b0..eb2fef3 100644 --- a/test/groupher_server/cms/articles/doc_test.exs +++ b/test/groupher_server/cms/articles/doc_test.exs @@ -247,6 +247,50 @@ defmodule GroupherServer.Test.CMS.Articles.Doc do end end + describe "[cms doc batch delete]" do + test "can batch delete docs with inner_ids", ~m(user community doc_attrs)a do + {:ok, doc1} = CMS.create_article(community, :doc, doc_attrs, user) + {:ok, doc2} = CMS.create_article(community, :doc, doc_attrs, user) + {:ok, doc3} = CMS.create_article(community, :doc, doc_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :doc, [ + doc1.inner_id, + doc2.inner_id + ]) + + {:ok, doc1} = ORM.find(Doc, doc1.id) + {:ok, doc2} = ORM.find(Doc, doc2.id) + {:ok, doc3} = ORM.find(Doc, doc3.id) + + assert doc1.mark_delete == true + assert doc2.mark_delete == true + assert doc3.mark_delete == false + end + + test "can undo batch delete docs with inner_ids", ~m(user community doc_attrs)a do + {:ok, doc1} = CMS.create_article(community, :doc, doc_attrs, user) + {:ok, doc2} = CMS.create_article(community, :doc, doc_attrs, user) + {:ok, doc3} = CMS.create_article(community, :doc, doc_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :doc, [ + doc1.inner_id, + doc2.inner_id + ]) + + CMS.batch_undo_mark_delete_articles(community.slug, :doc, [ + doc1.inner_id, + doc2.inner_id + ]) + + {:ok, doc1} = ORM.find(Doc, doc1.id) + {:ok, doc2} = ORM.find(Doc, doc2.id) + {:ok, doc3} = ORM.find(Doc, doc3.id) + + assert doc1.mark_delete == false + assert doc2.mark_delete == false + end + end + describe "[cms doc document]" do test "will create related document after create", ~m(user community doc_attrs)a do {:ok, doc} = CMS.create_article(community, :doc, doc_attrs, user) diff --git a/test/groupher_server/cms/articles/post_test.exs b/test/groupher_server/cms/articles/post_test.exs index bfc0a86..752d5a7 100644 --- a/test/groupher_server/cms/articles/post_test.exs +++ b/test/groupher_server/cms/articles/post_test.exs @@ -244,6 +244,50 @@ defmodule GroupherServer.Test.CMS.Articles.Post do end end + describe "[cms post batch delete]" do + test "can batch delete posts with inner_ids", ~m(user community post_attrs)a do + {:ok, post1} = CMS.create_article(community, :post, post_attrs, user) + {:ok, post2} = CMS.create_article(community, :post, post_attrs, user) + {:ok, post3} = CMS.create_article(community, :post, post_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :post, [ + post1.inner_id, + post2.inner_id + ]) + + {:ok, post1} = ORM.find(Post, post1.id) + {:ok, post2} = ORM.find(Post, post2.id) + {:ok, post3} = ORM.find(Post, post3.id) + + assert post1.mark_delete == true + assert post2.mark_delete == true + assert post3.mark_delete == false + end + + test "can undo batch delete posts with inner_ids", ~m(user community post_attrs)a do + {:ok, post1} = CMS.create_article(community, :post, post_attrs, user) + {:ok, post2} = CMS.create_article(community, :post, post_attrs, user) + {:ok, post3} = CMS.create_article(community, :post, post_attrs, user) + + CMS.batch_mark_delete_articles(community.slug, :post, [ + post1.inner_id, + post2.inner_id + ]) + + CMS.batch_undo_mark_delete_articles(community.slug, :post, [ + post1.inner_id, + post2.inner_id + ]) + + {:ok, post1} = ORM.find(Post, post1.id) + {:ok, post2} = ORM.find(Post, post2.id) + {:ok, post3} = ORM.find(Post, post3.id) + + assert post1.mark_delete == false + assert post2.mark_delete == false + end + end + describe "[cms post document]" do test "will create related document after create", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) diff --git a/test/groupher_server_web/mutation/cms/flags/blog_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/blog_flag_test.exs index a54c244..cf502ae 100644 --- a/test/groupher_server_web/mutation/cms/flags/blog_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/blog_flag_test.exs @@ -2,7 +2,7 @@ defmodule GroupherServer.Test.Mutation.Flags.BlogFlag do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.Community + alias CMS.Model.{Community, Blog} alias Helper.ORM @@ -11,12 +11,14 @@ defmodule GroupherServer.Test.Mutation.Flags.BlogFlag do {:ok, community} = db_insert(:community) {:ok, blog} = CMS.create_article(community, :blog, mock_attrs(:blog), user) + {:ok, blog2} = CMS.create_article(community, :blog, mock_attrs(:blog), user) + {:ok, blog3} = CMS.create_article(community, :blog, mock_attrs(:blog), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) owner_conn = simu_conn(:user, user) - {:ok, ~m(user_conn guest_conn owner_conn community user blog)a} + {:ok, ~m(user_conn guest_conn owner_conn community user blog blog2 blog3)a} end describe "[mutation blog flag curd]" do @@ -119,6 +121,73 @@ defmodule GroupherServer.Test.Mutation.Flags.BlogFlag do assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) end + @query """ + mutation($community: String!, $ids: [ID]!){ + batchMarkDeleteBlogs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch mark delete blogs", + ~m(community blog blog2 blog3)a do + variables = %{ + community: community.slug, + ids: [blog.inner_id, blog2.inner_id] + } + + passport_rules = %{"blog.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchMarkDeleteBlogs") + + assert updated["done"] == true + + {:ok, blog} = ORM.find(Blog, blog.id) + {:ok, blog2} = ORM.find(Blog, blog2.id) + {:ok, blog3} = ORM.find(Blog, blog3.id) + + assert blog.mark_delete == true + assert blog2.mark_delete == true + assert blog3.mark_delete == false + end + + @query """ + mutation($community: String!, $ids: [ID]!){ + batchUndoMarkDeleteBlogs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch undo mark delete blogs", + ~m(community blog blog2 blog3)a do + CMS.batch_mark_delete_articles(community.slug, :blog, [ + blog.inner_id, + blog2.inner_id + ]) + + variables = %{ + community: community.slug, + ids: [blog.inner_id, blog2.inner_id] + } + + passport_rules = %{"blog.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchUndoMarkDeleteBlogs") + + assert updated["done"] == true + + {:ok, blog} = ORM.find(Blog, blog.id) + {:ok, blog2} = ORM.find(Blog, blog2.id) + {:ok, blog3} = ORM.find(Blog, blog3.id) + + assert blog.mark_delete == false + assert blog2.mark_delete == false + assert blog3.mark_delete == false + end + @query """ mutation($id: ID!, $communityId: ID!){ pinBlog(id: $id, communityId: $communityId) { diff --git a/test/groupher_server_web/mutation/cms/flags/changelog_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/changelog_flag_test.exs index 8a15f6b..8e645ad 100644 --- a/test/groupher_server_web/mutation/cms/flags/changelog_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/changelog_flag_test.exs @@ -2,7 +2,7 @@ defmodule GroupherServer.Test.Mutation.Flags.ChangelogFlag do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.Community + alias CMS.Model.{Community, Changelog} alias Helper.ORM @@ -11,12 +11,14 @@ defmodule GroupherServer.Test.Mutation.Flags.ChangelogFlag do {:ok, community} = db_insert(:community) {:ok, changelog} = CMS.create_article(community, :changelog, mock_attrs(:changelog), user) + {:ok, changelog2} = CMS.create_article(community, :changelog, mock_attrs(:changelog), user) + {:ok, changelog3} = CMS.create_article(community, :changelog, mock_attrs(:changelog), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) owner_conn = simu_conn(:user, user) - {:ok, ~m(user_conn guest_conn owner_conn community user changelog)a} + {:ok, ~m(user_conn guest_conn owner_conn community user changelog changelog2 changelog3)a} end describe "[mutation changelog flag curd]" do @@ -120,6 +122,73 @@ defmodule GroupherServer.Test.Mutation.Flags.ChangelogFlag do assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) end + @query """ + mutation($community: String!, $ids: [ID]!){ + batchMarkDeleteChangelogs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch mark delete changelogs", + ~m(community changelog changelog2 changelog3)a do + variables = %{ + community: community.slug, + ids: [changelog.inner_id, changelog2.inner_id] + } + + passport_rules = %{"changelog.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchMarkDeleteChangelogs") + + assert updated["done"] == true + + {:ok, changelog} = ORM.find(Changelog, changelog.id) + {:ok, changelog2} = ORM.find(Changelog, changelog2.id) + {:ok, changelog3} = ORM.find(Changelog, changelog3.id) + + assert changelog.mark_delete == true + assert changelog2.mark_delete == true + assert changelog3.mark_delete == false + end + + @query """ + mutation($community: String!, $ids: [ID]!){ + batchUndoMarkDeleteChangelogs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch undo mark delete changelogs", + ~m(community changelog changelog2 changelog3)a do + CMS.batch_mark_delete_articles(community.slug, :changelog, [ + changelog.inner_id, + changelog2.inner_id + ]) + + variables = %{ + community: community.slug, + ids: [changelog.inner_id, changelog2.inner_id] + } + + passport_rules = %{"changelog.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchUndoMarkDeleteChangelogs") + + assert updated["done"] == true + + {:ok, changelog} = ORM.find(Changelog, changelog.id) + {:ok, changelog2} = ORM.find(Changelog, changelog2.id) + {:ok, changelog3} = ORM.find(Changelog, changelog3.id) + + assert changelog.mark_delete == false + assert changelog2.mark_delete == false + assert changelog3.mark_delete == false + end + @query """ mutation($id: ID!, $communityId: ID!){ pinChangelog(id: $id, communityId: $communityId) { diff --git a/test/groupher_server_web/mutation/cms/flags/doc_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/doc_flag_test.exs index e36977f..325c258 100644 --- a/test/groupher_server_web/mutation/cms/flags/doc_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/doc_flag_test.exs @@ -2,7 +2,7 @@ defmodule GroupherServer.Test.Mutation.Flags.DocFlag do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.Community + alias CMS.Model.{Community, Doc} alias Helper.ORM @@ -11,12 +11,14 @@ defmodule GroupherServer.Test.Mutation.Flags.DocFlag do {:ok, community} = db_insert(:community) {:ok, doc} = CMS.create_article(community, :doc, mock_attrs(:doc), user) + {:ok, doc2} = CMS.create_article(community, :doc, mock_attrs(:doc), user) + {:ok, doc3} = CMS.create_article(community, :doc, mock_attrs(:doc), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) owner_conn = simu_conn(:user, user) - {:ok, ~m(user_conn guest_conn owner_conn community user doc)a} + {:ok, ~m(user_conn guest_conn owner_conn community user doc doc2 doc3)a} end describe "[mutation doc flag curd]" do @@ -120,6 +122,71 @@ defmodule GroupherServer.Test.Mutation.Flags.DocFlag do assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) end + @query """ + mutation($community: String!, $ids: [ID]!){ + batchMarkDeleteDocs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch mark delete docs", ~m(community doc doc2 doc3)a do + variables = %{ + community: community.slug, + ids: [doc.inner_id, doc2.inner_id] + } + + passport_rules = %{"doc.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchMarkDeleteDocs") + + assert updated["done"] == true + + {:ok, doc} = ORM.find(Doc, doc.id) + {:ok, doc2} = ORM.find(Doc, doc2.id) + {:ok, doc3} = ORM.find(Doc, doc3.id) + + assert doc.mark_delete == true + assert doc2.mark_delete == true + assert doc3.mark_delete == false + end + + @query """ + mutation($community: String!, $ids: [ID]!){ + batchUndoMarkDeleteDocs(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch undo mark delete docs", ~m(community doc doc2 doc3)a do + CMS.batch_mark_delete_articles(community.slug, :doc, [ + doc.inner_id, + doc2.inner_id + ]) + + variables = %{ + community: community.slug, + ids: [doc.inner_id, doc2.inner_id] + } + + passport_rules = %{"doc.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchUndoMarkDeleteDocs") + + assert updated["done"] == true + + {:ok, doc} = ORM.find(Doc, doc.id) + {:ok, doc2} = ORM.find(Doc, doc2.id) + {:ok, doc3} = ORM.find(Doc, doc3.id) + + assert doc.mark_delete == false + assert doc2.mark_delete == false + assert doc3.mark_delete == false + end + @query """ mutation($id: ID!, $communityId: ID!){ pinDoc(id: $id, communityId: $communityId) { diff --git a/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs index b4a1f3a..d8f1d93 100644 --- a/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs @@ -2,7 +2,7 @@ defmodule GroupherServer.Test.Mutation.Flags.PostFlag do use GroupherServer.TestTools alias GroupherServer.CMS - alias CMS.Model.Community + alias CMS.Model.{Community, Post} alias Helper.ORM @@ -11,12 +11,14 @@ defmodule GroupherServer.Test.Mutation.Flags.PostFlag do {:ok, community} = db_insert(:community) {:ok, post} = CMS.create_article(community, :post, mock_attrs(:post), user) + {:ok, post2} = CMS.create_article(community, :post, mock_attrs(:post), user) + {:ok, post3} = CMS.create_article(community, :post, mock_attrs(:post), user) guest_conn = simu_conn(:guest) user_conn = simu_conn(:user) owner_conn = simu_conn(:user, user) - {:ok, ~m(user_conn guest_conn owner_conn community user post)a} + {:ok, ~m(user_conn guest_conn owner_conn community user post post2 post3)a} end describe "[mutation post flag curd]" do @@ -117,6 +119,71 @@ defmodule GroupherServer.Test.Mutation.Flags.PostFlag do assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) end + @query """ + mutation($community: String!, $ids: [ID]!){ + batchMarkDeletePosts(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch mark delete posts", ~m(community post post2 post3)a do + variables = %{ + community: community.slug, + ids: [post.inner_id, post2.inner_id] + } + + passport_rules = %{"post.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchMarkDeletePosts") + + assert updated["done"] == true + + {:ok, post} = ORM.find(Post, post.id) + {:ok, post2} = ORM.find(Post, post2.id) + {:ok, post3} = ORM.find(Post, post3.id) + + assert post.mark_delete == true + assert post2.mark_delete == true + assert post3.mark_delete == false + end + + @query """ + mutation($community: String!, $ids: [ID]!){ + batchUndoMarkDeletePosts(community: $community, ids: $ids) { + done + } + } + """ + + test "auth user can batch undo mark delete posts", ~m(community post post2 post3)a do + CMS.batch_mark_delete_articles(community.slug, :post, [ + post.inner_id, + post2.inner_id + ]) + + variables = %{ + community: community.slug, + ids: [post.inner_id, post2.inner_id] + } + + passport_rules = %{"post.mark_delete" => true} + rule_conn = simu_conn(:user, cms: passport_rules) + + updated = rule_conn |> mutation_result(@query, variables, "batchUndoMarkDeletePosts") + + assert updated["done"] == true + + {:ok, post} = ORM.find(Post, post.id) + {:ok, post2} = ORM.find(Post, post2.id) + {:ok, post3} = ORM.find(Post, post3.id) + + assert post.mark_delete == false + assert post2.mark_delete == false + assert post3.mark_delete == false + end + @query """ mutation($id: ID!, $communityId: ID!){ pinPost(id: $id, communityId: $communityId) {