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

refactor(article-comment): missing pin/unpin gq endpoint & test #398

Merged
merged 1 commit into from
Jun 14, 2021
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
11 changes: 2 additions & 9 deletions lib/groupher_server/cms/delegates/comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.Model.User

alias CMS.Model.{
Comment,
PinnedComment,
CommentUpvote,
CommentReply,
Embeds
}
alias CMS.Model.{Comment, PinnedComment, CommentUpvote, CommentReply, Embeds}

alias Ecto.Multi

Expand All @@ -38,8 +31,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
@max_parent_replies_count Comment.max_parent_replies_count()
@pinned_comment_limit Comment.pinned_comment_limit()

@spec pin_comment(Integer.t()) :: {:ok, Comment.t()}
@doc "pin a comment"
@spec pin_comment(Integer.t()) :: {:ok, Comment.t()}
def pin_comment(comment_id) do
with {:ok, comment} <- ORM.find(Comment, comment_id),
{:ok, full_comment} <- get_full_comment(comment.id),
Expand Down
12 changes: 2 additions & 10 deletions lib/groupher_server/cms/helper/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ defmodule GroupherServer.CMS.Helper.Macros do
import Helper.Utils, only: [get_config: 2]

alias GroupherServer.{CMS, Accounts}
alias Accounts.Model.User

alias CMS.Model.{
Embeds,
Author,
Community,
Comment,
ArticleTag,
ArticleUpvote,
ArticleCollect
}
alias Accounts.Model.User
alias CMS.Model.{Embeds, Author, Community, Comment, ArticleTag, ArticleUpvote, ArticleCollect}

@article_threads get_config(:article, :threads)

Expand Down
5 changes: 4 additions & 1 deletion lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content)

# #######################
# content flag ..
# article actions
# #######################
def pin_article(_root, ~m(id community_id thread)a, _info) do
CMS.pin_article(thread, id, community_id)
Expand Down Expand Up @@ -336,6 +336,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.undo_mark_comment_solution(id, user)
end

def pin_comment(_root, ~m(id)a, _info), do: CMS.pin_comment(id)
def undo_pin_comment(_root, ~m(id)a, _info), do: CMS.undo_pin_comment(id)

############
############
############
Expand Down
22 changes: 22 additions & 0 deletions lib/groupher_server_web/schema/cms/mutations/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,27 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
middleware(M.Authorize, :login)
resolve(&R.CMS.undo_mark_comment_solution/3)
end

@desc "pin a comment"
field :pin_comment, :comment do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :comment)
middleware(M.Passport, claim: "owner")

resolve(&R.CMS.pin_comment/3)
end

@desc "undo pin a comment"
field :undo_pin_comment, :comment do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :comment)
middleware(M.Passport, claim: "owner")

resolve(&R.CMS.undo_pin_comment/3)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
assert not exist_in?(List.last(reply_comment_list), parent_comment.replies)
end

@tag :wip
test "replyed user should appear in article comment participants", ~m(post user user2)a do
{:ok, parent_comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
{:ok, _} = CMS.reply_comment(parent_comment.id, mock_comment(), user2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end

describe "[article comment pin/unPin]" do
@query """
mutation($id: ID!){
pinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can pin a blog's comment", ~m(owner_conn blog user)a do
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "pinComment")

assert result["id"] == to_string(comment.id)
assert result["isPinned"]
end

@tag :wip
test "unauth user fails.", ~m(guest_conn blog user)a do
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end

@query """
mutation($id: ID!){
undoPinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can undo pin a blog's comment", ~m(owner_conn blog user)a do
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")

assert result["id"] == to_string(comment.id)
assert not result["isPinned"]
end

@tag :wip
test "unauth user undo fails.", ~m(guest_conn blog user)a do
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end

describe "[article comment pin/unPin]" do
@query """
mutation($id: ID!){
pinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can pin a job's comment", ~m(owner_conn job user)a do
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "pinComment")

assert result["id"] == to_string(comment.id)
assert result["isPinned"]
end

@tag :wip
test "unauth user fails.", ~m(guest_conn job user)a do
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end

@query """
mutation($id: ID!){
undoPinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can undo pin a job's comment", ~m(owner_conn job user)a do
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")

assert result["id"] == to_string(comment.id)
assert not result["isPinned"]
end

@tag :wip
test "unauth user undo fails.", ~m(guest_conn job user)a do
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,64 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
end
end

describe "[article comment pin/unPin]" do
@query """
mutation($id: ID!){
pinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can pin a post's comment", ~m(owner_conn post user)a do
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "pinComment")

assert result["id"] == to_string(comment.id)
assert result["isPinned"]
end

@tag :wip
test "unauth user fails.", ~m(guest_conn post user)a do
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end

@query """
mutation($id: ID!){
undoPinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can undo pin a post's comment", ~m(owner_conn post user)a do
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")

assert result["id"] == to_string(comment.id)
assert not result["isPinned"]
end

@tag :wip
test "unauth user undo fails.", ~m(guest_conn post user)a do
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end

describe "[post only: article comment solution]" do
@query """
mutation($id: ID!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end

describe "[article comment pin/unPin]" do
@query """
mutation($id: ID!){
pinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can pin a repo's comment", ~m(owner_conn repo user)a do
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "pinComment")

assert result["id"] == to_string(comment.id)
assert result["isPinned"]
end

@tag :wip
test "unauth user fails.", ~m(guest_conn repo user)a do
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end

@query """
mutation($id: ID!){
undoPinComment(id: $id) {
id
isPinned
}
}
"""
@tag :wip
test "can undo pin a repo's comment", ~m(owner_conn repo user)a do
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)

variables = %{id: comment.id}
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")

assert result["id"] == to_string(comment.id)
assert not result["isPinned"]
end

@tag :wip
test "unauth user undo fails.", ~m(guest_conn repo user)a do
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
{:ok, _} = CMS.pin_comment(comment.id)
variables = %{id: comment.id}

assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
end
end
end