|
| 1 | +defmodule GroupherServer.Test.Mutation.Comments.JobComment do |
| 2 | + use GroupherServer.TestTools |
| 3 | + |
| 4 | + alias GroupherServer.CMS |
| 5 | + |
| 6 | + setup do |
| 7 | + {:ok, job} = db_insert(:job) |
| 8 | + {:ok, user} = db_insert(:user) |
| 9 | + {:ok, community} = db_insert(:community) |
| 10 | + |
| 11 | + guest_conn = simu_conn(:guest) |
| 12 | + user_conn = simu_conn(:user) |
| 13 | + owner_conn = simu_conn(:user, user) |
| 14 | + |
| 15 | + {:ok, ~m(user_conn user guest_conn owner_conn community job)a} |
| 16 | + end |
| 17 | + |
| 18 | + describe "[article comment CURD]" do |
| 19 | + @write_comment_query """ |
| 20 | + mutation($thread: CmsThread!, $id: ID!, $content: String!) { |
| 21 | + createArticleComment(thread: $thread,id: $id, content: $content) { |
| 22 | + id |
| 23 | + bodyHtml |
| 24 | + } |
| 25 | + } |
| 26 | + """ |
| 27 | + @tag :wip3 |
| 28 | + test "write article comment to a exsit job", ~m(job user_conn)a do |
| 29 | + comment = "a test comment" |
| 30 | + variables = %{thread: "JOB", id: job.id, content: comment} |
| 31 | + |
| 32 | + result = |
| 33 | + user_conn |> mutation_result(@write_comment_query, variables, "createArticleComment") |
| 34 | + |
| 35 | + assert result["bodyHtml"] == comment |
| 36 | + end |
| 37 | + |
| 38 | + @reply_comment_query """ |
| 39 | + mutation($id: ID!, $content: String!) { |
| 40 | + replyArticleComment(id: $id, content: $content) { |
| 41 | + id |
| 42 | + bodyHtml |
| 43 | + } |
| 44 | + } |
| 45 | + """ |
| 46 | + @tag :wip2 |
| 47 | + test "login user can reply to a comment", ~m(job user user_conn)a do |
| 48 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "commment", user) |
| 49 | + variables = %{id: comment.id, content: "reply content"} |
| 50 | + |
| 51 | + result = |
| 52 | + user_conn |
| 53 | + |> mutation_result(@reply_comment_query, variables, "replyArticleComment") |
| 54 | + |
| 55 | + assert result["bodyHtml"] == "reply content" |
| 56 | + end |
| 57 | + |
| 58 | + @update_comment_query """ |
| 59 | + mutation($id: ID!, $content: String!) { |
| 60 | + updateArticleComment(id: $id, content: $content) { |
| 61 | + id |
| 62 | + bodyHtml |
| 63 | + } |
| 64 | + } |
| 65 | + """ |
| 66 | + @tag :wip3 |
| 67 | + test "only owner can update a exsit comment", |
| 68 | + ~m(job user guest_conn user_conn owner_conn)a do |
| 69 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) |
| 70 | + variables = %{id: comment.id, content: "updated comment"} |
| 71 | + |
| 72 | + assert user_conn |> mutation_get_error?(@update_comment_query, variables, ecode(:passport)) |
| 73 | + |
| 74 | + assert guest_conn |
| 75 | + |> mutation_get_error?(@update_comment_query, variables, ecode(:account_login)) |
| 76 | + |
| 77 | + updated = |
| 78 | + owner_conn |> mutation_result(@update_comment_query, variables, "updateArticleComment") |
| 79 | + |
| 80 | + assert updated["bodyHtml"] == "updated comment" |
| 81 | + end |
| 82 | + |
| 83 | + @delete_comment_query """ |
| 84 | + mutation($id: ID!) { |
| 85 | + deleteArticleComment(id: $id) { |
| 86 | + id |
| 87 | + isDeleted |
| 88 | + } |
| 89 | + } |
| 90 | + """ |
| 91 | + @tag :wip3 |
| 92 | + test "only owner can delete a exsit comment", |
| 93 | + ~m(job user guest_conn user_conn owner_conn)a do |
| 94 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) |
| 95 | + variables = %{id: comment.id} |
| 96 | + |
| 97 | + assert user_conn |> mutation_get_error?(@delete_comment_query, variables, ecode(:passport)) |
| 98 | + |
| 99 | + assert guest_conn |
| 100 | + |> mutation_get_error?(@delete_comment_query, variables, ecode(:account_login)) |
| 101 | + |
| 102 | + deleted = |
| 103 | + owner_conn |> mutation_result(@delete_comment_query, variables, "deleteArticleComment") |
| 104 | + |
| 105 | + assert deleted["id"] == to_string(comment.id) |
| 106 | + assert deleted["isDeleted"] |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + describe "[article comment emotion]" do |
| 111 | + @emotion_comment_query """ |
| 112 | + mutation($id: ID!, $emotion: ArticleCommentEmotion!) { |
| 113 | + emotionToComment(id: $id, emotion: $emotion) { |
| 114 | + id |
| 115 | + emotions { |
| 116 | + beerCount |
| 117 | + viewerHasBeered |
| 118 | + latestBeerUsers { |
| 119 | + login |
| 120 | + nickname |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + """ |
| 126 | + @tag :wip3 |
| 127 | + test "login user can emotion to a comment", ~m(job user user_conn)a do |
| 128 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) |
| 129 | + variables = %{id: comment.id, emotion: "BEER"} |
| 130 | + |
| 131 | + comment = |
| 132 | + user_conn |> mutation_result(@emotion_comment_query, variables, "emotionToComment") |
| 133 | + |
| 134 | + assert comment |> get_in(["emotions", "beerCount"]) == 1 |
| 135 | + assert get_in(comment, ["emotions", "viewerHasBeered"]) |
| 136 | + end |
| 137 | + |
| 138 | + @emotion_comment_query """ |
| 139 | + mutation($id: ID!, $emotion: ArticleCommentEmotion!) { |
| 140 | + undoEmotionToComment(id: $id, emotion: $emotion) { |
| 141 | + id |
| 142 | + emotions { |
| 143 | + beerCount |
| 144 | + viewerHasBeered |
| 145 | + latestBeerUsers { |
| 146 | + login |
| 147 | + nickname |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + """ |
| 153 | + @tag :wip3 |
| 154 | + test "login user can undo emotion to a comment", ~m(job user owner_conn)a do |
| 155 | + {:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user) |
| 156 | + {:ok, _} = CMS.emotion_to_comment(comment.id, :beer, user) |
| 157 | + |
| 158 | + variables = %{id: comment.id, emotion: "BEER"} |
| 159 | + |
| 160 | + comment = |
| 161 | + owner_conn |> mutation_result(@emotion_comment_query, variables, "undoEmotionToComment") |
| 162 | + |
| 163 | + assert comment |> get_in(["emotions", "beerCount"]) == 0 |
| 164 | + assert not get_in(comment, ["emotions", "viewerHasBeered"]) |
| 165 | + end |
| 166 | + end |
| 167 | +end |
0 commit comments