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

Commit ff0d2ea

Browse files
authored
refactor(article-community): more tests (#357)
* refactor: more test * refactor: more test * refactor: add thread var * refactor: fix thread error
1 parent 0d5289c commit ff0d2ea

File tree

9 files changed

+533
-30
lines changed

9 files changed

+533
-30
lines changed

test/groupher_server/cms/article_community/job_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Job do
2525
assert job.original_community_id == community.id
2626
end
2727

28-
@tag :wip2
28+
@tag :wip3
2929
test "job can be move to other community", ~m(user community community2 job_attrs)a do
3030
{:ok, job} = CMS.create_article(community, :job, job_attrs, user)
3131
assert job.original_community_id == community.id

test/groupher_server/cms/article_community/post_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Post do
2525
assert post.original_community_id == community.id
2626
end
2727

28-
@tag :wip2
28+
@tag :wip3
2929
test "post can be move to other community", ~m(user community community2 post_attrs)a do
3030
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
3131
assert post.original_community_id == community.id

test/groupher_server/cms/article_community/repo_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Repo do
2525
assert repo.original_community_id == community.id
2626
end
2727

28-
@tag :wip2
28+
@tag :wip3
2929
test "repo can be move to other community", ~m(user community community2 repo_attrs)a do
3030
{:ok, repo} = CMS.create_article(community, :repo, repo_attrs, user)
3131
assert repo.original_community_id == community.id
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
defmodule GroupherServer.Test.Mutation.ArticleCommunity.Job do
2+
use GroupherServer.TestTools
3+
4+
alias Helper.ORM
5+
alias GroupherServer.CMS
6+
7+
setup do
8+
{:ok, job} = db_insert(:job)
9+
{:ok, community} = db_insert(:community)
10+
11+
guest_conn = simu_conn(:guest)
12+
user_conn = simu_conn(:user)
13+
owner_conn = simu_conn(:owner, job)
14+
15+
{:ok, ~m(user_conn guest_conn owner_conn community job)a}
16+
end
17+
18+
describe "[mutation job tag]" do
19+
@set_tag_query """
20+
mutation($id: ID!, $thread: Thread, $tagId: ID! $communityId: ID!) {
21+
setTag(id: $id, thread: $thread, tagId: $tagId, communityId: $communityId) {
22+
id
23+
title
24+
}
25+
}
26+
"""
27+
@tag :wip2
28+
test "auth user can set a valid tag to job", ~m(job)a do
29+
{:ok, community} = db_insert(:community)
30+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community})
31+
32+
passport_rules = %{community.title => %{"job.tag.set" => true}}
33+
rule_conn = simu_conn(:user, cms: passport_rules)
34+
35+
variables = %{id: job.id, thread: "JOB", tagId: tag.id, communityId: community.id}
36+
rule_conn |> mutation_result(@set_tag_query, variables, "setTag")
37+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
38+
39+
assoc_tags = found.tags |> Enum.map(& &1.id)
40+
assert tag.id in assoc_tags
41+
end
42+
43+
# TODO: should fix in auth layer
44+
# test "auth user set a other community tag to job fails", ~m(job)a do
45+
# {:ok, community} = db_insert(:community)
46+
# {:ok, tag} = db_insert(:tag, %{thread: "job"})
47+
48+
# passport_rules = %{community.title => %{"job.tag.set" => true}}
49+
# rule_conn = simu_conn(:user, cms: passport_rules)
50+
51+
# variables = %{id: job.id, tagId: tag.id, communityId: community.id}
52+
# assert rule_conn |> mutation_get_error?(@set_tag_query, variables)
53+
# end
54+
55+
@tag :wip2
56+
test "can set multi tag to a job", ~m(job)a do
57+
{:ok, community} = db_insert(:community)
58+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community})
59+
{:ok, tag2} = db_insert(:tag, %{thread: "job", community: community})
60+
61+
passport_rules = %{community.title => %{"job.tag.set" => true}}
62+
rule_conn = simu_conn(:user, cms: passport_rules)
63+
64+
variables = %{id: job.id, thread: "JOB", tagId: tag.id, communityId: community.id}
65+
rule_conn |> mutation_result(@set_tag_query, variables, "setTag")
66+
67+
variables2 = %{id: job.id, thread: "JOB", tagId: tag2.id, communityId: community.id}
68+
rule_conn |> mutation_result(@set_tag_query, variables2, "setTag")
69+
70+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
71+
72+
assoc_tags = found.tags |> Enum.map(& &1.id)
73+
assert tag.id in assoc_tags
74+
assert tag2.id in assoc_tags
75+
end
76+
77+
@unset_tag_query """
78+
mutation($id: ID!, $thread: Thread, $tagId: ID! $communityId: ID!) {
79+
unsetTag(id: $id, thread: $thread, tagId: $tagId, communityId: $communityId) {
80+
id
81+
title
82+
}
83+
}
84+
"""
85+
@tag :wip2
86+
test "can unset tag to a job", ~m(job)a do
87+
{:ok, community} = db_insert(:community)
88+
89+
passport_rules = %{community.title => %{"job.tag.set" => true}}
90+
rule_conn = simu_conn(:user, cms: passport_rules)
91+
92+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community})
93+
{:ok, tag2} = db_insert(:tag, %{thread: "job", community: community})
94+
95+
variables = %{id: job.id, thread: "JOB", tagId: tag.id, communityId: community.id}
96+
rule_conn |> mutation_result(@set_tag_query, variables, "setTag")
97+
98+
variables2 = %{id: job.id, thread: "JOB", tagId: tag2.id, communityId: community.id}
99+
rule_conn |> mutation_result(@set_tag_query, variables2, "setTag")
100+
101+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
102+
103+
assoc_tags = found.tags |> Enum.map(& &1.id)
104+
assert tag.id in assoc_tags
105+
assert tag2.id in assoc_tags
106+
107+
passport_rules2 = %{community.title => %{"job.tag.unset" => true}}
108+
rule_conn2 = simu_conn(:user, cms: passport_rules2)
109+
110+
rule_conn2 |> mutation_result(@unset_tag_query, variables, "unsetTag")
111+
112+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
113+
assoc_tags = found.tags |> Enum.map(& &1.id)
114+
115+
assert tag.id not in assoc_tags
116+
assert tag2.id in assoc_tags
117+
end
118+
end
119+
120+
describe "[mirror/unmirror/move psot to/from community]" do
121+
@mirror_article_query """
122+
mutation($id: ID!, $thread: Thread, $communityId: ID!) {
123+
mirrorArticle(id: $id, thread: $thread, communityId: $communityId) {
124+
id
125+
}
126+
}
127+
"""
128+
test "auth user can mirror a job to other community", ~m(job)a do
129+
passport_rules = %{"job.community.mirror" => true}
130+
rule_conn = simu_conn(:user, cms: passport_rules)
131+
132+
{:ok, community} = db_insert(:community)
133+
variables = %{id: job.id, thread: "JOB", communityId: community.id}
134+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
135+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :communities)
136+
137+
assoc_communities = found.communities |> Enum.map(& &1.id)
138+
assert community.id in assoc_communities
139+
end
140+
141+
test "unauth user cannot mirror a job to a community", ~m(user_conn guest_conn job)a do
142+
{:ok, community} = db_insert(:community)
143+
variables = %{id: job.id, thread: "JOB", communityId: community.id}
144+
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
145+
146+
assert user_conn
147+
|> mutation_get_error?(@mirror_article_query, variables, ecode(:passport))
148+
149+
assert guest_conn
150+
|> mutation_get_error?(@mirror_article_query, variables, ecode(:account_login))
151+
152+
assert rule_conn
153+
|> mutation_get_error?(@mirror_article_query, variables, ecode(:passport))
154+
end
155+
156+
test "auth user can mirror multi job to other communities", ~m(job)a do
157+
passport_rules = %{"job.community.mirror" => true}
158+
rule_conn = simu_conn(:user, cms: passport_rules)
159+
160+
{:ok, community} = db_insert(:community)
161+
{:ok, community2} = db_insert(:community)
162+
163+
variables = %{id: job.id, thread: "JOB", communityId: community.id}
164+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
165+
166+
variables = %{id: job.id, thread: "JOB", communityId: community2.id}
167+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
168+
169+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :communities)
170+
171+
assoc_communities = found.communities |> Enum.map(& &1.id)
172+
assert community.id in assoc_communities
173+
assert community2.id in assoc_communities
174+
end
175+
176+
@unmirror_article_query """
177+
mutation($id: ID!, $thread: Thread, $communityId: ID!) {
178+
unmirrorArticle(id: $id, thread: $thread, communityId: $communityId) {
179+
id
180+
}
181+
}
182+
"""
183+
@tag :wip3
184+
test "auth user can unmirror job to a community", ~m(job)a do
185+
passport_rules = %{"job.community.mirror" => true}
186+
rule_conn = simu_conn(:user, cms: passport_rules)
187+
188+
{:ok, community} = db_insert(:community)
189+
{:ok, community2} = db_insert(:community)
190+
191+
variables = %{id: job.id, thread: "JOB", communityId: community.id}
192+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
193+
194+
variables2 = %{id: job.id, thread: "JOB", communityId: community2.id}
195+
rule_conn |> mutation_result(@mirror_article_query, variables2, "mirrorArticle")
196+
197+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :communities)
198+
199+
assoc_communities = found.communities |> Enum.map(& &1.id)
200+
assert community.id in assoc_communities
201+
assert community2.id in assoc_communities
202+
203+
passport_rules = %{"job.community.unmirror" => true}
204+
rule_conn = simu_conn(:user, cms: passport_rules)
205+
206+
rule_conn |> mutation_result(@unmirror_article_query, variables, "unmirrorArticle")
207+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :communities)
208+
assoc_communities = found.communities |> Enum.map(& &1.id)
209+
assert community.id not in assoc_communities
210+
assert community2.id in assoc_communities
211+
end
212+
213+
@move_article_query """
214+
mutation($id: ID!, $thread: Thread, $communityId: ID!) {
215+
moveArticle(id: $id, thread: $thread, communityId: $communityId) {
216+
id
217+
}
218+
}
219+
"""
220+
@tag :wip2
221+
test "auth user can move job to other community", ~m(job)a do
222+
passport_rules = %{"job.community.mirror" => true}
223+
rule_conn = simu_conn(:user, cms: passport_rules)
224+
225+
{:ok, community} = db_insert(:community)
226+
{:ok, community2} = db_insert(:community)
227+
228+
variables = %{id: job.id, thread: "JOB", communityId: community.id}
229+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
230+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: [:original_community, :communities])
231+
assoc_communities = found.communities |> Enum.map(& &1.id)
232+
assert community.id in assoc_communities
233+
234+
passport_rules = %{"job.community.move" => true}
235+
rule_conn = simu_conn(:user, cms: passport_rules)
236+
237+
pre_original_community_id = found.original_community.id
238+
239+
variables = %{id: job.id, thread: "JOB", communityId: community2.id}
240+
rule_conn |> mutation_result(@move_article_query, variables, "moveArticle")
241+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: [:original_community, :communities])
242+
assoc_communities = found.communities |> Enum.map(& &1.id)
243+
assert pre_original_community_id not in assoc_communities
244+
assert community2.id in assoc_communities
245+
assert community2.id == found.original_community_id
246+
247+
assert found.original_community.id == community2.id
248+
end
249+
end
250+
end

0 commit comments

Comments
 (0)