Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 7801738

Browse files
committed
chore(comments): tests re-org
1 parent 53a5a1f commit 7801738

File tree

5 files changed

+203
-1
lines changed

5 files changed

+203
-1
lines changed

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
2222
ArticlePinedComment,
2323
ArticleCommentUpvote,
2424
ArticleCommentReply,
25-
Embeds,
25+
# TODO: remove spec type
2626
Post,
2727
Job
2828
}

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
322322
CMS.delete_article_comment(comment)
323323
end
324324

325+
def reply_article_comment(_root, ~m(id content)a, %{context: %{cur_user: user}}) do
326+
CMS.reply_article_comment(id, content, user)
327+
end
328+
325329
def emotion_to_comment(_root, ~m(id emotion)a, %{context: %{cur_user: user}}) do
326330
CMS.emotion_to_comment(id, emotion, user)
327331
end

lib/groupher_server_web/schema/cms/mutations/comment.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
4444
resolve(&R.CMS.delete_article_comment/3)
4545
end
4646

47+
@desc "reply to a comment"
48+
field :reply_article_comment, :article_comment do
49+
arg(:id, non_null(:id))
50+
arg(:content, non_null(:string))
51+
52+
middleware(M.Authorize, :login)
53+
# TODO: 文章作者可以删除评论,文章可以设置禁止评论
54+
resolve(&R.CMS.reply_article_comment/3)
55+
middleware(M.Statistics.MakeContribute, for: :user)
56+
end
57+
4758
@desc "emotion to a comment"
4859
field :emotion_to_comment, :article_comment do
4960
arg(:id, non_null(:id))
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

test/groupher_server_web/mutation/cms/comments/post_comment_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
3535
assert result["bodyHtml"] == comment
3636
end
3737

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(post user user_conn)a do
48+
{:ok, comment} = CMS.create_article_comment(:post, post.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+
3858
@update_comment_query """
3959
mutation($id: ID!, $content: String!) {
4060
updateArticleComment(id: $id, content: $content) {

0 commit comments

Comments
 (0)