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

Commit 01c91fd

Browse files
committed
refactor: more tests on pin/trash system
1 parent fd939b5 commit 01c91fd

File tree

22 files changed

+798
-110
lines changed

22 files changed

+798
-110
lines changed

lib/helper/certification.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,27 @@ defmodule Helper.Certification do
8787
"post.tag.delete",
8888
"post.tag.set",
8989
"post.tag.unset",
90+
# post flag
91+
"post.pin",
92+
"post.undo_pin",
93+
"post.trash",
94+
"post.undo_trash",
9095
# job tag
9196
"job.tag.create",
9297
"job.tag.update",
9398
"job.tag.delete",
9499
"job.tag.set",
95-
"job.tag.unset"
100+
"job.tag.unset",
101+
# job flag
102+
"job.pin",
103+
"job.undo_pin",
104+
"job.trash",
105+
"job.undo_trash",
106+
# video flag
107+
"video.pin",
108+
"video.undo_pin",
109+
"video.trash",
110+
"video.undo_trash"
96111
]
97112
}
98113
end

lib/mastani_server/cms/delegates/article_operation.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
1212

1313
alias MastaniServer.CMS.{
1414
Community,
15-
PinState,
1615
Post,
1716
PostCommunityFlag,
1817
Job,
@@ -99,7 +98,7 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
9998
set tag for post / tuts / videos ...
10099
"""
101100
# check community first
102-
def set_tag(%Community{id: communitId}, thread, %Tag{id: tag_id}, content_id) do
101+
def set_tag(%Community{id: _communitId}, thread, %Tag{id: tag_id}, content_id) do
103102
with {:ok, action} <- match_action(thread, :tag),
104103
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
105104
{:ok, tag} <- ORM.find(action.reactor, tag_id) do

lib/mastani_server/cms/repo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule MastaniServer.CMS.Repo do
77
alias MastaniServer.CMS.{Author, Community, RepoBuilder, RepoCommunityFlag, Tag}
88

99
@required_fields ~w(repo_name desc readme language producer producer_link repo_link repo_star_count repo_fork_count repo_watch_count)a
10-
@optional_fields ~w(views pin trash last_fetch_time)
10+
@optional_fields ~w(views last_fetch_time)
1111

1212
@type t :: %Repo{}
1313
schema "cms_repos" do

lib/mastani_server/cms/utils/matcher.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ defmodule MastaniServer.CMS.Utils.Matcher do
6262
do: {:ok, %{target: PostComment, reactor: PostCommentDislike}}
6363

6464
# videos ...
65+
def match_action(:video, :self), do: {:ok, %{target: Video, reactor: Video, preload: :author}}
66+
6567
def match_action(:video, :community),
6668
do: {:ok, %{target: Video, reactor: Community, flag: VideoCommunityFlag}}
6769

6870
# repos ...
71+
def match_action(:repo, :self), do: {:ok, %{target: Repo, reactor: Repo, preload: :author}}
72+
6973
def match_action(:repo, :community),
7074
do: {:ok, %{target: Repo, reactor: Community, flag: RepoCommunityFlag}}
7175

lib/mastani_server/cms/video.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule MastaniServer.CMS.Video do
77
alias MastaniServer.CMS.{Author, Community, VideoCommunityFlag, Tag}
88

99
@required_fields ~w(title poster desc duration duration_sec source)a
10-
@optional_fields ~w(link original_author original_author_link publish_at pin trash)
10+
@optional_fields ~w(link original_author original_author_link publish_at)
1111

1212
@type t :: %Video{}
1313
schema "cms_videos" do

lib/mastani_server_web/schema/cms/cms_types.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ defmodule MastaniServerWeb.Schema.CMS.Types do
235235
field(:original_author_link, :string)
236236
field(:views, :integer)
237237

238+
field(:pin, :boolean)
239+
field(:trash, :boolean)
238240
# TODO: remove
239241
# field(:pin, :boolean)
240242
# field(:trash, :boolean)
@@ -263,6 +265,8 @@ defmodule MastaniServerWeb.Schema.CMS.Types do
263265
field(:repo_watch_count, :integer)
264266
field(:views, :integer)
265267

268+
field(:pin, :boolean)
269+
field(:trash, :boolean)
266270
# TODO: remove
267271
# field(:pin, :boolean)
268272
# field(:trash, :boolean)
@@ -287,6 +291,9 @@ defmodule MastaniServerWeb.Schema.CMS.Types do
287291
field(:body, :string)
288292
field(:views, :integer)
289293

294+
field(:pin, :boolean)
295+
field(:trash, :boolean)
296+
290297
field(:author, :user, resolve: dataloader(CMS, :author))
291298
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
292299
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

lib/mastani_server_web/schema/cms/mutations/job.ex

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,56 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Job do
2525
resolve(&R.CMS.create_content/3)
2626
end
2727

28+
@desc "pin a job"
29+
field :pin_job, :job do
30+
arg(:id, non_null(:id))
31+
arg(:type, :job_type, default_value: :job)
32+
arg(:community_id, non_null(:id))
33+
34+
middleware(M.Authorize, :login)
35+
middleware(M.PassportLoader, source: :community)
36+
middleware(M.Passport, claim: "cms->c?->job.pin")
37+
resolve(&R.CMS.pin_content/3)
38+
end
39+
40+
@desc "unpin a job"
41+
field :undo_pin_job, :job do
42+
arg(:id, non_null(:id))
43+
arg(:type, :job_type, default_value: :job)
44+
arg(:community_id, non_null(:id))
45+
46+
middleware(M.Authorize, :login)
47+
middleware(M.PassportLoader, source: :community)
48+
middleware(M.Passport, claim: "cms->c?->job.undo_pin")
49+
resolve(&R.CMS.undo_pin_content/3)
50+
end
51+
52+
@desc "trash a job, not delete"
53+
field :trash_job, :job do
54+
arg(:id, non_null(:id))
55+
arg(:type, :job_type, default_value: :job)
56+
arg(:community_id, non_null(:id))
57+
58+
middleware(M.Authorize, :login)
59+
middleware(M.PassportLoader, source: :community)
60+
middleware(M.Passport, claim: "cms->c?->job.trash")
61+
62+
resolve(&R.CMS.trash_content/3)
63+
end
64+
65+
@desc "trash a job, not delete"
66+
field :undo_trash_job, :job do
67+
arg(:id, non_null(:id))
68+
arg(:type, :job_type, default_value: :job)
69+
arg(:community_id, non_null(:id))
70+
71+
middleware(M.Authorize, :login)
72+
middleware(M.PassportLoader, source: :community)
73+
middleware(M.Passport, claim: "cms->c?->job.undo_trash")
74+
75+
resolve(&R.CMS.undo_trash_content/3)
76+
end
77+
2878
@desc "delete a job"
2979
field :delete_job, :job do
3080
arg(:id, non_null(:id))

lib/mastani_server_web/schema/cms/mutations/post.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Post do
2929
arg(:community_id, non_null(:id))
3030

3131
middleware(M.Authorize, :login)
32-
middleware(M.Passport, claim: "cms->post.pin")
32+
middleware(M.PassportLoader, source: :community)
33+
middleware(M.Passport, claim: "cms->c?->post.pin")
3334
resolve(&R.CMS.pin_content/3)
3435
end
3536

@@ -40,7 +41,8 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Post do
4041
arg(:community_id, non_null(:id))
4142

4243
middleware(M.Authorize, :login)
43-
middleware(M.Passport, claim: "cms->post.undo_pin")
44+
middleware(M.PassportLoader, source: :community)
45+
middleware(M.Passport, claim: "cms->c?->post.undo_pin")
4446
resolve(&R.CMS.undo_pin_content/3)
4547
end
4648

@@ -51,7 +53,8 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Post do
5153
arg(:community_id, non_null(:id))
5254

5355
middleware(M.Authorize, :login)
54-
middleware(M.Passport, claim: "cms->post.trash")
56+
middleware(M.PassportLoader, source: :community)
57+
middleware(M.Passport, claim: "cms->c?->post.trash")
5558

5659
resolve(&R.CMS.trash_content/3)
5760
end
@@ -63,7 +66,8 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Post do
6366
arg(:community_id, non_null(:id))
6467

6568
middleware(M.Authorize, :login)
66-
middleware(M.Passport, claim: "cms->post.undo_trash")
69+
middleware(M.PassportLoader, source: :community)
70+
middleware(M.Passport, claim: "cms->c?->post.undo_trash")
6771

6872
resolve(&R.CMS.undo_trash_content/3)
6973
end

lib/mastani_server_web/schema/cms/mutations/video.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,35 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Video do
2626
field :pin_video, :video do
2727
arg(:id, non_null(:id))
2828
arg(:type, :video_type, default_value: :video)
29+
arg(:community_id, non_null(:id))
2930

3031
middleware(M.Authorize, :login)
31-
middleware(M.Passport, claim: "cms->video.pin")
32+
middleware(M.PassportLoader, source: :community)
33+
middleware(M.Passport, claim: "cms->c?->video.pin")
3234
resolve(&R.CMS.pin_content/3)
3335
end
3436

3537
@desc "unpin a video"
3638
field :undo_pin_video, :video do
3739
arg(:id, non_null(:id))
3840
arg(:type, :video_type, default_value: :video)
41+
arg(:community_id, non_null(:id))
3942

4043
middleware(M.Authorize, :login)
41-
middleware(M.Passport, claim: "cms->video.undo_pin")
44+
middleware(M.PassportLoader, source: :community)
45+
middleware(M.Passport, claim: "cms->c?->video.undo_pin")
4246
resolve(&R.CMS.undo_pin_content/3)
4347
end
4448

4549
@desc "trash a video, not delete"
4650
field :trash_video, :video do
4751
arg(:id, non_null(:id))
4852
arg(:type, :video_type, default_value: :video)
53+
arg(:community_id, non_null(:id))
4954

5055
middleware(M.Authorize, :login)
51-
middleware(M.Passport, claim: "cms->video.trash")
56+
middleware(M.PassportLoader, source: :community)
57+
middleware(M.Passport, claim: "cms->c?->video.trash")
5258

5359
resolve(&R.CMS.trash_content/3)
5460
end
@@ -57,9 +63,11 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Video do
5763
field :undo_trash_video, :video do
5864
arg(:id, non_null(:id))
5965
arg(:type, :video_type, default_value: :video)
66+
arg(:community_id, non_null(:id))
6067

6168
middleware(M.Authorize, :login)
62-
middleware(M.Passport, claim: "cms->video.undo_trash")
69+
middleware(M.PassportLoader, source: :community)
70+
middleware(M.Passport, claim: "cms->c?->video.undo_trash")
6371

6472
resolve(&R.CMS.undo_trash_content/3)
6573
end

test/mastani_server/cms/content_flag_test.exs

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,109 @@ defmodule MastaniServer.Test.ContentFlags do
33

44
alias MastaniServer.CMS
55

6-
# alias CMS.{Post, PostCommunityFlag, Repo, Video}
7-
alias CMS.{Post, PostCommunityFlag}
6+
alias CMS.{
7+
Post,
8+
PostCommunityFlag,
9+
Repo,
10+
RepoCommunityFlag,
11+
Job,
12+
JobCommunityFlag,
13+
Video,
14+
VideoCommunityFlag
15+
}
16+
817
alias Helper.ORM
918

1019
setup do
1120
{:ok, user} = db_insert(:user)
12-
{:ok, post} = db_insert(:post)
13-
{:ok, video} = db_insert(:video)
14-
{:ok, repo} = db_insert(:repo)
21+
{:ok, community} = db_insert(:community)
22+
23+
{:ok, post} = CMS.create_content(community, :post, mock_attrs(:post), user)
24+
{:ok, job} = CMS.create_content(community, :job, mock_attrs(:job), user)
25+
{:ok, video} = CMS.create_content(community, :video, mock_attrs(:video), user)
26+
{:ok, repo} = CMS.create_content(community, :repo, mock_attrs(:repo), user)
1527

16-
{:ok, ~m(user post video repo)a}
28+
{:ok, ~m(user community post job video repo)a}
1729
end
1830

1931
describe "[cms post flag]" do
20-
test "user can set pin flag on post based on community", ~m(post)a do
21-
community1_id = post.communities |> List.first() |> Map.get(:id)
22-
CMS.set_community_flags(%Post{id: post.id}, community1_id, %{pin: true})
32+
test "user can set pin/trash flag on post based on community", ~m(community post)a do
33+
community_id = community.id
34+
post_id = post.id
35+
{:ok, found} = PostCommunityFlag |> ORM.find_by(~m(post_id community_id)a)
36+
assert found.pin == false
37+
assert found.trash == false
2338

24-
{:ok, found} =
25-
ORM.find_by(PostCommunityFlag, %{post_id: post.id, community_id: community1_id})
39+
CMS.set_community_flags(%Post{id: post.id}, community.id, %{pin: true, trash: true})
40+
41+
{:ok, found} = PostCommunityFlag |> ORM.find_by(~m(post_id community_id)a)
2642

2743
assert found.pin == true
44+
assert found.trash == true
2845
assert found.post_id == post.id
29-
assert found.community_id == community1_id
46+
assert found.community_id == community.id
3047
end
3148

3249
# TODO: set twice .. staff
3350
end
3451

35-
# describe "[cms video flag]" do
36-
# test "user can set pin flag on a video", ~m(user video)a do
37-
# true
38-
# end
39-
40-
# test "user can set trash flag on a video", ~m(user video)a do
41-
# true
42-
# end
43-
# end
44-
45-
# describe "[cms repo flag]" do
46-
# test "user can set pin flag on a repo", ~m(user repo)a do
47-
# true
48-
# end
49-
50-
# test "user can set trash flag on a repo", ~m(user repo)a do
51-
# true
52-
# end
53-
# end
52+
describe "[cms job flag]" do
53+
test "user can set pin/trash flag on job", ~m(community job)a do
54+
community_id = community.id
55+
job_id = job.id
56+
57+
{:ok, found} = JobCommunityFlag |> ORM.find_by(~m(job_id community_id)a)
58+
assert found.pin == false
59+
assert found.trash == false
60+
61+
CMS.set_community_flags(%Job{id: job.id}, community.id, %{pin: true, trash: true})
62+
63+
{:ok, found} = JobCommunityFlag |> ORM.find_by(~m(job_id community_id)a)
64+
65+
assert found.pin == true
66+
assert found.trash == true
67+
assert found.job_id == job.id
68+
assert found.community_id == community.id
69+
end
70+
end
71+
72+
describe "[cms video flag]" do
73+
test "user can set pin/trash flag on a video", ~m(community video)a do
74+
community_id = community.id
75+
video_id = video.id
76+
77+
{:ok, found} = VideoCommunityFlag |> ORM.find_by(~m(video_id community_id)a)
78+
assert found.pin == false
79+
assert found.trash == false
80+
81+
CMS.set_community_flags(%Video{id: video.id}, community.id, %{pin: true, trash: true})
82+
83+
{:ok, found} = VideoCommunityFlag |> ORM.find_by(~m(video_id community_id)a)
84+
85+
assert found.pin == true
86+
assert found.trash == true
87+
assert found.video_id == video.id
88+
assert found.community_id == community.id
89+
end
90+
end
91+
92+
describe "[cms repo flag]" do
93+
test "user can set pin/trash flag on repo", ~m(community repo)a do
94+
community_id = community.id
95+
repo_id = repo.id
96+
97+
{:ok, found} = RepoCommunityFlag |> ORM.find_by(~m(repo_id community_id)a)
98+
assert found.pin == false
99+
assert found.trash == false
100+
101+
CMS.set_community_flags(%Repo{id: repo.id}, community.id, %{pin: true, trash: true})
102+
103+
{:ok, found} = RepoCommunityFlag |> ORM.find_by(~m(repo_id community_id)a)
104+
105+
assert found.pin == true
106+
assert found.trash == true
107+
assert found.repo_id == repo.id
108+
assert found.community_id == community.id
109+
end
110+
end
54111
end

0 commit comments

Comments
 (0)