Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do

defp add_rich_text_attrs(attrs), do: attrs

# except Job, other article will just pass, should use set_article_tags function instead
# defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}

defp update_viewed_user_list(%{meta: nil} = article, user_id) do
new_ids = Enum.uniq([user_id] ++ @default_article_meta.viewed_user_ids)
meta = @default_article_meta |> Map.merge(%{viewed_user_ids: new_ids})
Expand Down
16 changes: 6 additions & 10 deletions lib/groupher_server/cms/delegates/article_tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
end

# check if the tag to be set is in same community & thread
defp is_article_tag_in_some_thread?(article_tags, filter) do
defp is_article_tag_in_some_thread?(article_tag_ids, filter) do
with {:ok, paged_article_tags} <- paged_article_tags(filter) do
domain_tags_ids = Enum.map(paged_article_tags.entries, &to_string(&1.id))
cur_tags_ids = Enum.map(article_tags, &to_string(&1.id))
article_tag_ids = Enum.map(article_tag_ids, &to_string(&1))

Enum.all?(cur_tags_ids, &Enum.member?(domain_tags_ids, &1))
Enum.all?(article_tag_ids, &Enum.member?(domain_tags_ids, &1))
end
end

Expand All @@ -86,15 +86,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do

used for create article with article_tags in args
"""
def set_article_tags(%Community{id: cid}, thread, article, %{article_tags: article_tags}) do
def set_article_tags(%Community{id: cid}, thread, article, %{article_tags: article_tag_ids}) do
check_filter = %{page: 1, size: 100, community_id: cid, thread: thread}

with true <- is_article_tag_in_some_thread?(article_tags, check_filter) do
Enum.each(article_tags, fn article_tag ->
set_article_tag(thread, article, article_tag.id)
end)

{:ok, :pass}
with true <- is_article_tag_in_some_thread?(article_tag_ids, check_filter) do
Enum.each(article_tag_ids, &set_article_tag(thread, article, &1)) |> done
else
false -> raise_error(:invalid_domain_tag, "tag not in same community & thread")
end
Expand Down
4 changes: 0 additions & 4 deletions lib/groupher_server_web/schema/Helper/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ defmodule GroupherServerWeb.Schema.Helper.Metrics do
pagination_fields()
end

input_object :ids do
field(:id, :id)
end

input_object :common_paged_filter do
pagination_args()
field(:sort, :inserted_sort_enum, default_value: :desc_inserted)
Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server_web/schema/cms/mutations/blog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do
arg(:community_id, non_null(:id))
arg(:link_addr, :string)
arg(:thread, :thread, default_value: :blog)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
middleware(M.PublishThrottle)
Expand All @@ -34,7 +34,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Blog do

arg(:company, :string)
arg(:company_link, :string)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

# ...

Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server_web/schema/cms/mutations/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
arg(:copy_right, :string)

arg(:thread, :thread, default_value: :job)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
middleware(M.PublishThrottle)
Expand All @@ -42,7 +42,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do

arg(:company, :string)
arg(:company_link, :string)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

# ...

Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server_web/schema/cms/mutations/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
arg(:copy_right, :string)
arg(:community_id, non_null(:id))
arg(:thread, :thread, default_value: :post)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
# middleware(M.PublishThrottle)
Expand All @@ -34,7 +34,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
arg(:digest, :string)
arg(:copy_right, :string)
arg(:link_addr, :string)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :post)
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server_web/schema/cms/mutations/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do

arg(:community_id, non_null(:id))
arg(:thread, :thread, default_value: :repo)
arg(:article_tags, list_of(:ids))
arg(:article_tags, list_of(:id))

middleware(M.Authorize, :login)
middleware(M.PublishThrottle)
Expand Down
18 changes: 8 additions & 10 deletions test/groupher_server/cms/article_tags/blog_tag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ defmodule GroupherServer.Test.CMS.ArticleTag.BlogTag do
article_tag_attrs = mock_attrs(:article_tag)
article_tag_attrs2 = mock_attrs(:article_tag)

post_attrs = mock_attrs(:blog)
blog_attrs = mock_attrs(:blog)

{:ok, ~m(user community blog post_attrs article_tag_attrs article_tag_attrs2)a}
{:ok, ~m(user community blog blog_attrs article_tag_attrs article_tag_attrs2)a}
end

describe "[blog tag CURD]" do
Expand Down Expand Up @@ -80,30 +80,28 @@ defmodule GroupherServer.Test.CMS.ArticleTag.BlogTag do

describe "[create/update blog with tags]" do
test "can create blog with exsited article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user blog_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, article_tag} = CMS.create_article_tag(community, :blog, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community, :blog, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
blog_with_tags = Map.merge(blog_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:ok, created} = CMS.create_article(community, :blog, post_with_tags, user)
{:ok, created} = CMS.create_article(community, :blog, blog_with_tags, user)
{:ok, blog} = ORM.find(Blog, created.id, preload: :article_tags)

assert exist_in?(article_tag, blog.article_tags)
assert exist_in?(article_tag2, blog.article_tags)
end

test "can not create blog with other community's article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user blog_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, community2} = db_insert(:community)
{:ok, article_tag} = CMS.create_article_tag(community, :blog, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community2, :blog, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
blog_with_tags = Map.merge(blog_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:error, reason} = CMS.create_article(community, :blog, post_with_tags, user)
{:error, reason} = CMS.create_article(community, :blog, blog_with_tags, user)
is_error?(reason, :invalid_domain_tag)
end
end
Expand Down
18 changes: 8 additions & 10 deletions test/groupher_server/cms/article_tags/job_tag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ defmodule GroupherServer.Test.CMS.ArticleTag.JobTag do
article_tag_attrs = mock_attrs(:article_tag)
article_tag_attrs2 = mock_attrs(:article_tag)

post_attrs = mock_attrs(:job)
job_attrs = mock_attrs(:job)

{:ok, ~m(user community job post_attrs article_tag_attrs article_tag_attrs2)a}
{:ok, ~m(user community job job_attrs article_tag_attrs article_tag_attrs2)a}
end

describe "[job tag CURD]" do
Expand Down Expand Up @@ -80,30 +80,28 @@ defmodule GroupherServer.Test.CMS.ArticleTag.JobTag do

describe "[create/update job with tags]" do
test "can create job with exsited article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user job_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, article_tag} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community, :job, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
job_with_tags = Map.merge(job_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:ok, created} = CMS.create_article(community, :job, post_with_tags, user)
{:ok, created} = CMS.create_article(community, :job, job_with_tags, user)
{:ok, job} = ORM.find(Job, created.id, preload: :article_tags)

assert exist_in?(article_tag, job.article_tags)
assert exist_in?(article_tag2, job.article_tags)
end

test "can not create job with other community's article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user job_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, community2} = db_insert(:community)
{:ok, article_tag} = CMS.create_article_tag(community, :job, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community2, :job, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
job_with_tags = Map.merge(job_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:error, reason} = CMS.create_article(community, :job, post_with_tags, user)
{:error, reason} = CMS.create_article(community, :job, job_with_tags, user)
is_error?(reason, :invalid_domain_tag)
end
end
Expand Down
6 changes: 2 additions & 4 deletions test/groupher_server/cms/article_tags/post_tag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ defmodule GroupherServer.Test.CMS.ArticleTag.PostTag do
{:ok, article_tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community, :post, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
post_with_tags = Map.merge(post_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:ok, created} = CMS.create_article(community, :post, post_with_tags, user)
{:ok, post} = ORM.find(Post, created.id, preload: :article_tags)
Expand All @@ -100,8 +99,7 @@ defmodule GroupherServer.Test.CMS.ArticleTag.PostTag do
{:ok, article_tag} = CMS.create_article_tag(community, :post, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community2, :post, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
post_with_tags = Map.merge(post_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:error, reason} = CMS.create_article(community, :post, post_with_tags, user)
is_error?(reason, :invalid_domain_tag)
Expand Down
18 changes: 8 additions & 10 deletions test/groupher_server/cms/article_tags/repo_tag_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ defmodule GroupherServer.Test.CMS.ArticleTag.RepoTag do
article_tag_attrs = mock_attrs(:article_tag)
article_tag_attrs2 = mock_attrs(:article_tag)

post_attrs = mock_attrs(:repo)
repo_attrs = mock_attrs(:repo)

{:ok, ~m(user community repo post_attrs article_tag_attrs article_tag_attrs2)a}
{:ok, ~m(user community repo repo_attrs article_tag_attrs article_tag_attrs2)a}
end

describe "[repo tag CURD]" do
Expand Down Expand Up @@ -80,30 +80,28 @@ defmodule GroupherServer.Test.CMS.ArticleTag.RepoTag do

describe "[create/update repo with tags]" do
test "can create repo with exsited article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user repo_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, article_tag} = CMS.create_article_tag(community, :repo, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community, :repo, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
repo_with_tags = Map.merge(repo_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:ok, created} = CMS.create_article(community, :repo, post_with_tags, user)
{:ok, created} = CMS.create_article(community, :repo, repo_with_tags, user)
{:ok, repo} = ORM.find(Repo, created.id, preload: :article_tags)

assert exist_in?(article_tag, repo.article_tags)
assert exist_in?(article_tag2, repo.article_tags)
end

test "can not create repo with other community's article tags",
~m(community user post_attrs article_tag_attrs article_tag_attrs2)a do
~m(community user repo_attrs article_tag_attrs article_tag_attrs2)a do
{:ok, community2} = db_insert(:community)
{:ok, article_tag} = CMS.create_article_tag(community, :repo, article_tag_attrs, user)
{:ok, article_tag2} = CMS.create_article_tag(community2, :repo, article_tag_attrs2, user)

post_with_tags =
Map.merge(post_attrs, %{article_tags: [%{id: article_tag.id}, %{id: article_tag2.id}]})
repo_with_tags = Map.merge(repo_attrs, %{article_tags: [article_tag.id, article_tag2.id]})

{:error, reason} = CMS.create_article(community, :repo, post_with_tags, user)
{:error, reason} = CMS.create_article(community, :repo, repo_with_tags, user)
is_error?(reason, :invalid_domain_tag)
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/groupher_server_web/mutation/accounts/mailbox_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GroupherServer.Test.Query.Accounts.Mailbox do
defmodule GroupherServer.Test.Mutaion.Accounts.Mailbox do
use GroupherServer.TestTools

alias GroupherServer.Delivery
Expand Down Expand Up @@ -30,7 +30,7 @@ defmodule GroupherServer.Test.Query.Accounts.Mailbox do
mention = mentions.entries |> List.first()

variables = %{ids: [mention.id], type: "MENTION"}
result = user_conn |> mutation_result(@query, variables, "markRead")
user_conn |> mutation_result(@query, variables, "markRead")

{:ok, mentions} = Delivery.fetch(:mention, user, %{page: 1, size: 10, read: true})
mention = mentions.entries |> List.first()
Expand All @@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Query.Accounts.Mailbox do
notify = notifications.entries |> List.first()

variables = %{ids: [notify.id], type: "NOTIFICATION"}
result = user_conn |> mutation_result(@query, variables, "markRead")
user_conn |> mutation_result(@query, variables, "markRead")

{:ok, notifications} = Delivery.fetch(:notification, user, %{page: 1, size: 10, read: true})
notify = notifications.entries |> List.first()
Expand All @@ -68,7 +68,7 @@ defmodule GroupherServer.Test.Query.Accounts.Mailbox do
assert mentions.total_count == 2

variables = %{type: "MENTION"}
result = user_conn |> mutation_result(@query, variables, "markReadAll")
user_conn |> mutation_result(@query, variables, "markReadAll")

{:ok, mentions} = Delivery.fetch(:mention, user, %{page: 1, size: 10, read: true})
assert mentions.total_count == 2
Expand All @@ -81,7 +81,7 @@ defmodule GroupherServer.Test.Query.Accounts.Mailbox do
assert notifications.total_count == 1

variables = %{type: "NOTIFICATION"}
result = user_conn |> mutation_result(@query, variables, "markReadAll")
user_conn |> mutation_result(@query, variables, "markReadAll")

{:ok, notifications} = Delivery.fetch(:notification, user, %{page: 1, size: 10, read: true})
assert notifications.total_count == 1
Expand Down
20 changes: 18 additions & 2 deletions test/groupher_server_web/mutation/cms/articles/blog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do
setup do
{:ok, blog} = db_insert(:blog)
{:ok, user} = db_insert(:user)
{:ok, community} = db_insert(:community)

guest_conn = simu_conn(:guest)
user_conn = simu_conn(:user)
owner_conn = simu_conn(:owner, blog)

{:ok, ~m(user_conn guest_conn owner_conn user blog)a}
{:ok, ~m(user_conn guest_conn owner_conn community user blog)a}
end

describe "[mutation blog curd]" do
Expand All @@ -25,7 +26,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do
$digest: String!,
$length: Int,
$communityId: ID!,
$articleTags: [Ids]
$articleTags: [Id]
) {
createBlog(
title: $title,
Expand Down Expand Up @@ -67,6 +68,21 @@ defmodule GroupherServer.Test.Mutation.Articles.Blog do
assert created["id"] == to_string(found.id)
end

test "create blog with valid tags id list", ~m(user_conn user community)a do
article_tag_attrs = mock_attrs(:article_tag)
{:ok, article_tag} = CMS.create_article_tag(community, :blog, article_tag_attrs, user)

blog_attr = mock_attrs(:blog)

variables =
blog_attr |> Map.merge(%{communityId: community.id, articleTags: [article_tag.id]})

created = user_conn |> mutation_result(@create_blog_query, variables, "createBlog")
{:ok, blog} = ORM.find(Blog, created["id"], preload: :article_tags)

assert exist_in?(%{id: article_tag.id}, blog.article_tags)
end

test "create blog should excape xss attracts" do
{:ok, user} = db_insert(:user)
user_conn = simu_conn(:user, user)
Expand Down
Loading