diff --git a/lib/groupher_server_web/schema/Helper/mutations.ex b/lib/groupher_server_web/schema/Helper/mutations.ex index 0e5a945d6..e4cc7034f 100644 --- a/lib/groupher_server_web/schema/Helper/mutations.ex +++ b/lib/groupher_server_web/schema/Helper/mutations.ex @@ -26,4 +26,78 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do end end end + + defmacro article_pin_mutation(thread) do + quote do + @desc unquote("pin to #{thread}") + field unquote(:"pin_#{thread}"), unquote(thread) 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)}.pin")) + resolve(&R.CMS.pin_article/3) + end + + @desc unquote("undo pin to #{thread}") + field unquote(:"undo_pin_#{thread}"), unquote(thread) 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_pin")) + resolve(&R.CMS.undo_pin_article/3) + end + end + end + + defmacro article_trash_mutation(thread) do + quote do + @desc unquote("trash a #{thread}, not delete") + field unquote(:"trash_#{thread}"), unquote(thread) 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)}.trash")) + + resolve(&R.CMS.trash_content/3) + end + + @desc unquote("undo trash a #{thread}, not delete") + field unquote(:"undo_trash_#{thread}"), unquote(thread) 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_trash")) + + resolve(&R.CMS.undo_trash_content/3) + end + end + end + + # TODO: if post belongs to multi communities, unset instead delete + defmacro article_delete_mutation(thread) do + quote do + @desc unquote("delete a #{thread}, not delete") + field unquote(:"delete_#{thread}"), unquote(thread) do + arg(:id, non_null(:id)) + + middleware(M.Authorize, :login) + middleware(M.PassportLoader, source: unquote(thread)) + middleware(M.Passport, claim: unquote("owner;cms->c?->#{to_string(thread)}.delete")) + + resolve(&R.CMS.delete_content/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 0d05b06dd..942baf6bb 100644 --- a/lib/groupher_server_web/schema/cms/mutations/job.ex +++ b/lib/groupher_server_web/schema/cms/mutations/job.ex @@ -72,67 +72,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do ############# article_upvote_mutation(:job) + article_pin_mutation(:job) + article_trash_mutation(:job) + article_delete_mutation(:job) ############# - - @desc "pin a job" - field :pin_job, :job do - arg(:id, non_null(:id)) - arg(:thread, :job_thread, default_value: :job) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->job.pin") - resolve(&R.CMS.pin_article/3) - end - - @desc "unpin a job" - field :undo_pin_job, :job do - arg(:id, non_null(:id)) - arg(:thread, :job_thread, default_value: :job) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->job.undo_pin") - resolve(&R.CMS.undo_pin_article/3) - end - - @desc "trash a job, not delete" - field :trash_job, :job do - arg(:id, non_null(:id)) - arg(:thread, :job_thread, default_value: :job) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->job.trash") - - resolve(&R.CMS.trash_content/3) - end - - @desc "trash a job, not delete" - field :undo_trash_job, :job do - arg(:id, non_null(:id)) - arg(:thread, :job_thread, default_value: :job) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->job.undo_trash") - - resolve(&R.CMS.undo_trash_content/3) - end - - @desc "delete a job" - field :delete_job, :job do - arg(:id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :job) - middleware(M.Passport, claim: "owner;cms->c?->job.delete") - - resolve(&R.CMS.delete_content/3) - end 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 cdd72c6b2..e4b6b0ff2 100644 --- a/lib/groupher_server_web/schema/cms/mutations/post.ex +++ b/lib/groupher_server_web/schema/cms/mutations/post.ex @@ -46,68 +46,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do ############# article_upvote_mutation(:post) + article_pin_mutation(:post) + article_trash_mutation(:post) + article_delete_mutation(:post) ############# - - @desc "pin a post" - field :pin_post, :post do - arg(:id, non_null(:id)) - arg(:community_id, non_null(:id)) - arg(:thread, :post_thread, default_value: :post) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->post.pin") - resolve(&R.CMS.pin_article/3) - end - - @desc "unpin a post" - field :undo_pin_post, :post do - arg(:id, non_null(:id)) - arg(:thread, :post_thread, default_value: :post) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->post.undo_pin") - resolve(&R.CMS.undo_pin_article/3) - end - - @desc "trash a post, not delete" - field :trash_post, :post do - arg(:id, non_null(:id)) - arg(:thread, :post_thread, default_value: :post) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->post.trash") - - resolve(&R.CMS.trash_content/3) - end - - @desc "trash a post, not delete" - field :undo_trash_post, :post do - arg(:id, non_null(:id)) - arg(:thread, :post_thread, default_value: :post) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->post.undo_trash") - - resolve(&R.CMS.undo_trash_content/3) - end - - @desc "delete a cms/post" - # TODO: if post belongs to multi communities, unset instead delete - field :delete_post, :post do - arg(:id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :post) - middleware(M.Passport, claim: "owner;cms->c?->post.delete") - - resolve(&R.CMS.delete_content/3) - end 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 934fbd673..01ed8cf3a 100644 --- a/lib/groupher_server_web/schema/cms/mutations/repo.ex +++ b/lib/groupher_server_web/schema/cms/mutations/repo.ex @@ -3,6 +3,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do CMS mutations for job """ use Helper.GqlSchemaSuite + import GroupherServerWeb.Schema.Helper.Mutations object :cms_repo_mutations do @desc "create a repo" @@ -69,55 +70,10 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do resolve(&R.CMS.update_content/3) end - @desc "pin a repo" - field :pin_repo, :repo do - arg(:id, non_null(:id)) - arg(:thread, :repo_thread, default_value: :repo) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->repo.pin") - resolve(&R.CMS.pin_article/3) - end - - @desc "unpin a repo" - field :undo_pin_repo, :repo do - arg(:id, non_null(:id)) - arg(:thread, :repo_thread, default_value: :repo) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->repo.undo_pin") - resolve(&R.CMS.undo_pin_article/3) - end - - @desc "trash a repo, not delete" - field :trash_repo, :repo do - arg(:id, non_null(:id)) - arg(:thread, :repo_thread, default_value: :repo) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->repo.trash") - - resolve(&R.CMS.trash_content/3) - end - - @desc "trash a repo, not delete" - field :undo_trash_repo, :repo do - arg(:id, non_null(:id)) - arg(:thread, :repo_thread, default_value: :repo) - arg(:community_id, non_null(:id)) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->repo.undo_trash") - - resolve(&R.CMS.undo_trash_content/3) - end + ############# + article_pin_mutation(:repo) + article_trash_mutation(:repo) + ############# @desc "delete a repo" field :delete_repo, :repo do diff --git a/test/groupher_server_web/mutation/cms/job_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/job_flag_test.exs similarity index 96% rename from test/groupher_server_web/mutation/cms/job_flag_test.exs rename to test/groupher_server_web/mutation/cms/flags/job_flag_test.exs index 6d18874ab..d91b8591e 100644 --- a/test/groupher_server_web/mutation/cms/job_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/job_flag_test.exs @@ -1,4 +1,4 @@ -defmodule GroupherServer.Test.Mutation.JobFlag do +defmodule GroupherServer.Test.Mutation.Flags.JobFlag do use GroupherServer.TestTools alias GroupherServer.CMS @@ -25,6 +25,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do } } """ + @tag :wip2 test "auth user can trash job", ~m(community job)a do variables = %{id: job.id, communityId: community.id} @@ -37,6 +38,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do assert updated["trash"] == true end + @tag :wip2 test "unauth user trash job fails", ~m(user_conn guest_conn job community)a do variables = %{id: job.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -54,6 +56,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do } } """ + @tag :wip2 test "auth user can undo trash job", ~m(community job)a do variables = %{id: job.id, communityId: community.id} @@ -84,6 +87,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do } } """ + @tag :wip2 test "auth user can pin job", ~m(community job)a do variables = %{id: job.id, communityId: community.id} @@ -95,6 +99,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do assert updated["id"] == to_string(job.id) end + @tag :wip2 test "unauth user pin job fails", ~m(user_conn guest_conn community job)a do variables = %{id: job.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -112,7 +117,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do } } """ - @tag :wip + @tag :wip2 test "auth user can undo pin job", ~m(community job)a do variables = %{id: job.id, communityId: community.id} @@ -126,6 +131,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do assert updated["isPinned"] == false end + @tag :wip2 test "unauth user undo pin job fails", ~m(user_conn guest_conn community job)a do variables = %{id: job.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/post_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs similarity index 96% rename from test/groupher_server_web/mutation/cms/post_flag_test.exs rename to test/groupher_server_web/mutation/cms/flags/post_flag_test.exs index 87830602d..f54f9f9e2 100644 --- a/test/groupher_server_web/mutation/cms/post_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/post_flag_test.exs @@ -1,4 +1,4 @@ -defmodule GroupherServer.Test.Mutation.PostFlag do +defmodule GroupherServer.Test.Mutation.Flags.PostFlag do use GroupherServer.TestTools alias GroupherServer.CMS @@ -25,6 +25,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do } } """ + @tag :wip2 test "auth user can trash post", ~m(community post)a do variables = %{id: post.id, communityId: community.id} @@ -37,6 +38,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do assert updated["trash"] == true end + @tag :wip2 test "unauth user trash post fails", ~m(user_conn guest_conn post community)a do variables = %{id: post.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -54,6 +56,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do } } """ + @tag :wip2 test "auth user can undo trash post", ~m(community post)a do variables = %{id: post.id, communityId: community.id} @@ -68,6 +71,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do assert updated["trash"] == false end + @tag :wip2 test "unauth user undo trash post fails", ~m(user_conn guest_conn community post)a do variables = %{id: post.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -84,7 +88,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do } } """ - @tag :wip + @tag :wip2 test "auth user can pin post", ~m(community post)a do variables = %{id: post.id, communityId: community.id} @@ -96,6 +100,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do assert updated["id"] == to_string(post.id) end + @tag :wip2 test "unauth user pin post fails", ~m(user_conn guest_conn community post)a do variables = %{id: post.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -113,7 +118,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do } } """ - @tag :wip + @tag :wip2 test "auth user can undo pin post", ~m(community post)a do variables = %{id: post.id, communityId: community.id} @@ -126,6 +131,7 @@ defmodule GroupherServer.Test.Mutation.PostFlag do assert updated["id"] == to_string(post.id) end + @tag :wip2 test "unauth user undo pin post fails", ~m(user_conn guest_conn community post)a do variables = %{id: post.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/repo_flag_test.exs b/test/groupher_server_web/mutation/cms/flags/repo_flag_test.exs similarity index 96% rename from test/groupher_server_web/mutation/cms/repo_flag_test.exs rename to test/groupher_server_web/mutation/cms/flags/repo_flag_test.exs index 882cc6e9a..2243a0125 100644 --- a/test/groupher_server_web/mutation/cms/repo_flag_test.exs +++ b/test/groupher_server_web/mutation/cms/flags/repo_flag_test.exs @@ -1,4 +1,4 @@ -defmodule GroupherServer.Test.Mutation.RepoFlag do +defmodule GroupherServer.Test.Mutation.Flags.RepoFlag do use GroupherServer.TestTools alias GroupherServer.CMS @@ -25,6 +25,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do } } """ + @tag :wip2 test "auth user can trash repo", ~m(community repo)a do variables = %{id: repo.id, communityId: community.id} @@ -37,6 +38,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do assert updated["trash"] == true end + @tag :wip2 test "unauth user trash repo fails", ~m(user_conn guest_conn repo community)a do variables = %{id: repo.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -54,6 +56,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do } } """ + @tag :wip2 test "auth user can undo trash repo", ~m(community repo)a do variables = %{id: repo.id, communityId: community.id} @@ -68,6 +71,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do assert updated["trash"] == false end + @tag :wip2 test "unauth user undo trash repo fails", ~m(user_conn guest_conn community repo)a do variables = %{id: repo.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -84,6 +88,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do } } """ + @tag :wip2 test "auth user can pin repo", ~m(community repo)a do variables = %{id: repo.id, communityId: community.id} @@ -95,6 +100,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do assert updated["id"] == to_string(repo.id) end + @tag :wip2 test "unauth user pin repo fails", ~m(user_conn guest_conn community repo)a do variables = %{id: repo.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) @@ -112,7 +118,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do } } """ - @tag :wip + @tag :wip2 test "auth user can undo pin repo", ~m(community repo)a do variables = %{id: repo.id, communityId: community.id} @@ -125,6 +131,7 @@ defmodule GroupherServer.Test.Mutation.RepoFlag do assert updated["id"] == to_string(repo.id) end + @tag :wip2 test "unauth user undo pin repo fails", ~m(user_conn guest_conn community repo)a do variables = %{id: repo.id, communityId: community.id} rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) diff --git a/test/groupher_server_web/mutation/cms/job_test.exs b/test/groupher_server_web/mutation/cms/job_test.exs index 2f0c8dd21..9528aaac6 100644 --- a/test/groupher_server_web/mutation/cms/job_test.exs +++ b/test/groupher_server_web/mutation/cms/job_test.exs @@ -282,6 +282,7 @@ defmodule GroupherServer.Test.Mutation.Job do } } """ + @tag :wip2 test "can delete a job by job's owner", ~m(owner_conn job)a do deleted = owner_conn |> mutation_result(@query, %{id: job.id}, "deleteJob") diff --git a/test/groupher_server_web/mutation/cms/post_test.exs b/test/groupher_server_web/mutation/cms/post_test.exs index 8a8bd35d7..706ebf38a 100644 --- a/test/groupher_server_web/mutation/cms/post_test.exs +++ b/test/groupher_server_web/mutation/cms/post_test.exs @@ -142,6 +142,7 @@ defmodule GroupherServer.Test.Mutation.Post do } } """ + @tag :wip2 test "delete a post by post's owner", ~m(owner_conn post)a do deleted = owner_conn |> mutation_result(@query, %{id: post.id}, "deletePost")