|
| 1 | +defmodule GroupherServer.Test.Mutation.CMS.ArticleArticleTags.CURD do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use GroupherServer.TestTools |
| 5 | + |
| 6 | + alias GroupherServer.CMS |
| 7 | + alias CMS.ArticleTag |
| 8 | + |
| 9 | + alias Helper.ORM |
| 10 | + |
| 11 | + setup do |
| 12 | + {:ok, community} = db_insert(:community) |
| 13 | + {:ok, thread} = db_insert(:thread) |
| 14 | + {:ok, user} = db_insert(:user) |
| 15 | + |
| 16 | + tag_attrs = mock_attrs(:tag) |
| 17 | + |
| 18 | + user_conn = simu_conn(:user) |
| 19 | + guest_conn = simu_conn(:guest) |
| 20 | + |
| 21 | + {:ok, ~m(user_conn guest_conn community thread user tag_attrs)a} |
| 22 | + end |
| 23 | + |
| 24 | + describe "[mutation cms tag]" do |
| 25 | + @create_tag_query """ |
| 26 | + mutation($thread: Thread!, $title: String!, $color: RainbowColor!, $communityId: ID!) { |
| 27 | + createArticleTag(thread: $thread, title: $title, color: $color, communityId: $communityId) { |
| 28 | + id |
| 29 | + title |
| 30 | + color |
| 31 | + thread |
| 32 | + community { |
| 33 | + id |
| 34 | + logo |
| 35 | + title |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + """ |
| 40 | + @tag :wip2 |
| 41 | + test "create tag with valid attrs, has default POST thread and default posts", |
| 42 | + ~m(community)a do |
| 43 | + variables = %{ |
| 44 | + title: "tag title", |
| 45 | + communityId: community.id, |
| 46 | + thread: "POST", |
| 47 | + color: "GREEN" |
| 48 | + } |
| 49 | + |
| 50 | + passport_rules = %{community.title => %{"post.article_tag.create" => true}} |
| 51 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 52 | + |
| 53 | + created = rule_conn |> mutation_result(@create_tag_query, variables, "createArticleTag") |
| 54 | + |
| 55 | + belong_community = created["community"] |
| 56 | + |
| 57 | + {:ok, found} = ArticleTag |> ORM.find(created["id"]) |
| 58 | + |
| 59 | + assert created["id"] == to_string(found.id) |
| 60 | + assert found.thread == "POST" |
| 61 | + assert belong_community["id"] == to_string(community.id) |
| 62 | + end |
| 63 | + |
| 64 | + @tag :wip2 |
| 65 | + test "unauth user create tag fails", ~m(community user_conn guest_conn)a do |
| 66 | + variables = %{ |
| 67 | + title: "tag title", |
| 68 | + communityId: community.id, |
| 69 | + thread: "POST", |
| 70 | + color: "GREEN" |
| 71 | + } |
| 72 | + |
| 73 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 74 | + |
| 75 | + assert user_conn |> mutation_get_error?(@create_tag_query, variables, ecode(:passport)) |
| 76 | + |
| 77 | + assert guest_conn |
| 78 | + |> mutation_get_error?(@create_tag_query, variables, ecode(:account_login)) |
| 79 | + |
| 80 | + assert rule_conn |> mutation_get_error?(@create_tag_query, variables, ecode(:passport)) |
| 81 | + end |
| 82 | + |
| 83 | + @update_tag_query """ |
| 84 | + mutation($id: ID!, $color: RainbowColor, $title: String, $communityId: ID!) { |
| 85 | + updateArticleTag(id: $id, color: $color, title: $title, communityId: $communityId) { |
| 86 | + id |
| 87 | + title |
| 88 | + color |
| 89 | + } |
| 90 | + } |
| 91 | + """ |
| 92 | + @tag :wip2 |
| 93 | + test "auth user can update a tag", ~m(tag_attrs community user)a do |
| 94 | + {:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user) |
| 95 | + |
| 96 | + variables = %{ |
| 97 | + id: article_tag.id, |
| 98 | + color: "YELLOW", |
| 99 | + title: "new title", |
| 100 | + communityId: community.id |
| 101 | + } |
| 102 | + |
| 103 | + passport_rules = %{community.title => %{"post.article_tag.update" => true}} |
| 104 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 105 | + |
| 106 | + updated = rule_conn |> mutation_result(@update_tag_query, variables, "updateArticleTag") |
| 107 | + |
| 108 | + assert updated["color"] == "YELLOW" |
| 109 | + assert updated["title"] == "new title" |
| 110 | + end |
| 111 | + |
| 112 | + @delete_tag_query """ |
| 113 | + mutation($id: ID!, $communityId: ID!){ |
| 114 | + deleteArticleTag(id: $id, communityId: $communityId) { |
| 115 | + id |
| 116 | + } |
| 117 | + } |
| 118 | + """ |
| 119 | + @tag :wip2 |
| 120 | + test "auth user can delete tag", ~m(tag_attrs community user)a do |
| 121 | + {:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user) |
| 122 | + |
| 123 | + variables = %{id: article_tag.id, communityId: community.id} |
| 124 | + |
| 125 | + rule_conn = |
| 126 | + simu_conn(:user, |
| 127 | + cms: %{community.title => %{"post.article_tag.delete" => true}} |
| 128 | + ) |
| 129 | + |
| 130 | + deleted = rule_conn |> mutation_result(@delete_tag_query, variables, "deleteArticleTag") |
| 131 | + |
| 132 | + assert deleted["id"] == to_string(article_tag.id) |
| 133 | + end |
| 134 | + |
| 135 | + @tag :wip2 |
| 136 | + test "unauth user delete tag fails", ~m(tag_attrs community user_conn guest_conn user)a do |
| 137 | + {:ok, article_tag} = CMS.create_article_tag(community, :post, tag_attrs, user) |
| 138 | + |
| 139 | + variables = %{id: article_tag.id, communityId: community.id} |
| 140 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 141 | + |
| 142 | + assert user_conn |> mutation_get_error?(@delete_tag_query, variables, ecode(:passport)) |
| 143 | + |
| 144 | + assert guest_conn |
| 145 | + |> mutation_get_error?(@delete_tag_query, variables, ecode(:account_login)) |
| 146 | + |
| 147 | + assert rule_conn |> mutation_get_error?(@delete_tag_query, variables, ecode(:passport)) |
| 148 | + end |
| 149 | + end |
| 150 | +end |
0 commit comments