|
| 1 | +defmodule MastaniServer.Test.Mutation.RepoFlag do |
| 2 | + use MastaniServer.TestTools |
| 3 | + |
| 4 | + alias MastaniServer.CMS |
| 5 | + |
| 6 | + setup do |
| 7 | + {:ok, user} = db_insert(:user) |
| 8 | + {:ok, community} = db_insert(:community) |
| 9 | + |
| 10 | + {:ok, repo} = CMS.create_content(community, :repo, mock_attrs(:repo), user) |
| 11 | + |
| 12 | + guest_conn = simu_conn(:guest) |
| 13 | + user_conn = simu_conn(:user) |
| 14 | + owner_conn = simu_conn(:user, user) |
| 15 | + |
| 16 | + {:ok, ~m(user_conn guest_conn owner_conn community repo)a} |
| 17 | + end |
| 18 | + |
| 19 | + describe "[mutation repo flag curd]" do |
| 20 | + @query """ |
| 21 | + mutation($id: ID!, $communityId: ID!){ |
| 22 | + trashRepo(id: $id, communityId: $communityId) { |
| 23 | + id |
| 24 | + trash |
| 25 | + } |
| 26 | + } |
| 27 | + """ |
| 28 | + test "auth user can trash repo", ~m(community repo)a do |
| 29 | + variables = %{id: repo.id, communityId: community.id} |
| 30 | + |
| 31 | + passport_rules = %{community.raw => %{"repo.trash" => true}} |
| 32 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 33 | + |
| 34 | + updated = rule_conn |> mutation_result(@query, variables, "trashRepo") |
| 35 | + |
| 36 | + assert updated["id"] == to_string(repo.id) |
| 37 | + assert updated["trash"] == true |
| 38 | + end |
| 39 | + |
| 40 | + test "unauth user trash repo fails", ~m(user_conn guest_conn repo community)a do |
| 41 | + variables = %{id: repo.id, communityId: community.id} |
| 42 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 43 | + |
| 44 | + assert user_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 45 | + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) |
| 46 | + assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 47 | + end |
| 48 | + |
| 49 | + @query """ |
| 50 | + mutation($id: ID!, $communityId: ID!){ |
| 51 | + undoTrashRepo(id: $id, communityId: $communityId) { |
| 52 | + id |
| 53 | + trash |
| 54 | + } |
| 55 | + } |
| 56 | + """ |
| 57 | + test "auth user can undo trash repo", ~m(community repo)a do |
| 58 | + variables = %{id: repo.id, communityId: community.id} |
| 59 | + |
| 60 | + {:ok, _} = CMS.set_community_flags(repo, community.id, %{trash: true}) |
| 61 | + |
| 62 | + passport_rules = %{community.raw => %{"repo.undo_trash" => true}} |
| 63 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 64 | + |
| 65 | + updated = rule_conn |> mutation_result(@query, variables, "undoTrashRepo") |
| 66 | + |
| 67 | + assert updated["id"] == to_string(repo.id) |
| 68 | + assert updated["trash"] == false |
| 69 | + end |
| 70 | + |
| 71 | + test "unauth user undo trash repo fails", ~m(user_conn guest_conn community repo)a do |
| 72 | + variables = %{id: repo.id, communityId: community.id} |
| 73 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 74 | + |
| 75 | + assert user_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 76 | + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) |
| 77 | + assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 78 | + end |
| 79 | + |
| 80 | + @query """ |
| 81 | + mutation($id: ID!, $communityId: ID!){ |
| 82 | + pinRepo(id: $id, communityId: $communityId) { |
| 83 | + id |
| 84 | + } |
| 85 | + } |
| 86 | + """ |
| 87 | + test "auth user can pin repo", ~m(community repo)a do |
| 88 | + variables = %{id: repo.id, communityId: community.id} |
| 89 | + |
| 90 | + passport_rules = %{community.raw => %{"repo.pin" => true}} |
| 91 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 92 | + |
| 93 | + updated = rule_conn |> mutation_result(@query, variables, "pinRepo") |
| 94 | + |
| 95 | + assert updated["id"] == to_string(repo.id) |
| 96 | + end |
| 97 | + |
| 98 | + test "unauth user pin repo fails", ~m(user_conn guest_conn community repo)a do |
| 99 | + variables = %{id: repo.id, communityId: community.id} |
| 100 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 101 | + |
| 102 | + assert user_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 103 | + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) |
| 104 | + assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 105 | + end |
| 106 | + |
| 107 | + @query """ |
| 108 | + mutation($id: ID!, $communityId: ID!){ |
| 109 | + undoPinRepo(id: $id, communityId: $communityId) { |
| 110 | + id |
| 111 | + pin |
| 112 | + } |
| 113 | + } |
| 114 | + """ |
| 115 | + test "auth user can undo pin repo", ~m(community repo)a do |
| 116 | + variables = %{id: repo.id, communityId: community.id} |
| 117 | + |
| 118 | + passport_rules = %{community.raw => %{"repo.undo_pin" => true}} |
| 119 | + rule_conn = simu_conn(:user, cms: passport_rules) |
| 120 | + |
| 121 | + updated = rule_conn |> mutation_result(@query, variables, "undoPinRepo") |
| 122 | + |
| 123 | + assert updated["id"] == to_string(repo.id) |
| 124 | + assert updated["pin"] == false |
| 125 | + end |
| 126 | + |
| 127 | + test "unauth user undo pin repo fails", ~m(user_conn guest_conn community repo)a do |
| 128 | + variables = %{id: repo.id, communityId: community.id} |
| 129 | + rule_conn = simu_conn(:user, cms: %{"what.ever" => true}) |
| 130 | + |
| 131 | + assert user_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 132 | + assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) |
| 133 | + assert rule_conn |> mutation_get_error?(@query, variables, ecode(:passport)) |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments