2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.CommentTest do
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web.ConnCase
|
2020-01-26 20:34:25 +01:00
|
|
|
|
2019-01-25 13:59:58 +01:00
|
|
|
import Mobilizon.Factory
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.{Event, EventOptions}
|
2020-01-26 20:34:25 +01:00
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
@comment_text "I love this event"
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
setup %{conn: conn} do
|
2019-01-25 13:59:58 +01:00
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user)
|
2019-11-15 18:36:47 +01:00
|
|
|
event = insert(:event)
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
{:ok, conn: conn, actor: actor, user: user, event: event}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Comment Resolver" do
|
2020-10-05 17:42:53 +02:00
|
|
|
@create_comment_mutation """
|
2020-11-19 17:06:28 +01:00
|
|
|
mutation CreateComment($text: String!, $eventId: ID, $inReplyToCommentId: ID) {
|
|
|
|
createComment(text: $text, eventId: $eventId, inReplyToCommentId: $inReplyToCommentId) {
|
2020-10-05 17:42:53 +02:00
|
|
|
id,
|
|
|
|
text,
|
|
|
|
uuid,
|
|
|
|
inReplyToComment {
|
|
|
|
id,
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
test "create_comment/3 creates a comment", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
event: event
|
|
|
|
} do
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-10-05 17:42:53 +02:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_comment_mutation,
|
2020-11-19 17:06:28 +01:00
|
|
|
variables: %{text: @comment_text, eventId: event.id}
|
2020-10-05 17:42:53 +02:00
|
|
|
)
|
2019-11-15 18:36:47 +01:00
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
assert res["data"]["createComment"]["text"] == @comment_text
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
test "create_comment/3 doesn't allow creating events if it's disabled", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
event: event
|
|
|
|
} do
|
|
|
|
{:ok, %Event{options: %EventOptions{comment_moderation: :closed}}} =
|
|
|
|
Events.update_event(event, %{options: %{comment_moderation: :closed}})
|
2019-11-15 18:36:47 +01:00
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_comment_mutation,
|
2020-11-19 17:06:28 +01:00
|
|
|
variables: %{text: @comment_text, eventId: event.id}
|
2020-10-05 17:42:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"You don't have permission to do this"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_comment/3 allows creating events if it's disabled but we're the organizer", %{
|
|
|
|
conn: conn,
|
|
|
|
actor: actor,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
event = insert(:event, organizer_actor: actor, options: %{comment_moderation: :closed})
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
2020-10-05 17:42:53 +02:00
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_comment_mutation,
|
2020-11-19 17:06:28 +01:00
|
|
|
variables: %{text: @comment_text, eventId: event.id}
|
2020-10-05 17:42:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert is_nil(res["errors"])
|
|
|
|
assert res["data"]["createComment"]["text"] == @comment_text
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_comment/3 requires that the user needs to be authenticated", %{
|
|
|
|
conn: conn,
|
|
|
|
event: event
|
|
|
|
} do
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_comment_mutation,
|
2020-11-19 17:06:28 +01:00
|
|
|
variables: %{text: @comment_text, eventId: event.id}
|
2020-10-05 17:42:53 +02:00
|
|
|
)
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"You are not allowed to create a comment if not connected"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_comment/3 creates a reply to a comment", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
event: event
|
|
|
|
} do
|
|
|
|
comment = insert(:comment)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2020-10-05 17:42:53 +02:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_comment_mutation,
|
|
|
|
variables: %{
|
|
|
|
text: @comment_text,
|
|
|
|
eventId: event.id,
|
|
|
|
inReplyToCommentId: comment.id
|
|
|
|
}
|
|
|
|
)
|
2019-11-15 18:36:47 +01:00
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
assert is_nil(res["errors"])
|
|
|
|
assert res["data"]["createComment"]["text"] == @comment_text
|
2019-11-15 18:36:47 +01:00
|
|
|
uuid = res["data"]["createComment"]["uuid"]
|
|
|
|
|
2020-10-05 17:42:53 +02:00
|
|
|
assert res["data"]["createComment"]["inReplyToComment"]["id"] ==
|
2019-11-15 18:36:47 +01:00
|
|
|
to_string(comment.id)
|
|
|
|
|
|
|
|
query = """
|
2020-10-06 12:34:34 +02:00
|
|
|
query {
|
|
|
|
thread(id: #{comment.id}) {
|
|
|
|
text,
|
|
|
|
uuid
|
2019-11-15 18:36:47 +01:00
|
|
|
}
|
2020-10-06 12:34:34 +02:00
|
|
|
}
|
2019-11-15 18:36:47 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(query: query, variables: %{})
|
|
|
|
|
|
|
|
assert res["errors"] == nil
|
2020-10-05 17:42:53 +02:00
|
|
|
assert res["data"]["thread"] == [%{"uuid" => uuid, "text" => @comment_text}]
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@delete_comment """
|
2020-08-18 15:06:56 +02:00
|
|
|
mutation DeleteComment($commentId: ID!) {
|
|
|
|
deleteComment(commentId: $commentId) {
|
2019-11-15 18:36:47 +01:00
|
|
|
id,
|
|
|
|
deletedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
test "deletes a comment", %{conn: conn, user: user, actor: actor} do
|
|
|
|
comment = insert(:comment, actor: actor)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_comment,
|
2020-08-18 15:06:56 +02:00
|
|
|
variables: %{commentId: comment.id}
|
2019-11-15 18:36:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"You are not allowed to delete a comment if not connected"
|
|
|
|
|
2020-08-18 15:06:56 +02:00
|
|
|
# Change the current actor for user
|
2019-11-15 18:36:47 +01:00
|
|
|
actor2 = insert(:actor, user: user)
|
2021-09-10 11:35:32 +02:00
|
|
|
Mobilizon.Users.update_user_default_actor(user.id, actor2)
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_comment,
|
2020-08-18 15:06:56 +02:00
|
|
|
variables: %{commentId: comment.id}
|
2019-11-15 18:36:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert hd(res["errors"])["message"] ==
|
|
|
|
"You cannot delete this comment"
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
Mobilizon.Users.update_user_default_actor(user.id, actor)
|
2020-08-18 15:06:56 +02:00
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_comment,
|
2020-08-18 15:06:56 +02:00
|
|
|
variables: %{commentId: comment.id}
|
2019-11-15 18:36:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert res["errors"] == nil
|
|
|
|
assert res["data"]["deleteComment"]["id"] == to_string(comment.id)
|
|
|
|
refute is_nil(res["data"]["deleteComment"]["deletedAt"])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_comment/3 allows a comment being deleted by a moderator and creates a entry in actionLogs",
|
|
|
|
%{
|
|
|
|
conn: conn,
|
|
|
|
user: _user,
|
|
|
|
actor: _actor
|
|
|
|
} do
|
|
|
|
user_moderator = insert(:user, role: :moderator)
|
|
|
|
actor_moderator = insert(:actor, user: user_moderator)
|
|
|
|
|
|
|
|
actor2 = insert(:actor)
|
|
|
|
comment = insert(:comment, actor: actor2)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @delete_comment,
|
2020-08-18 15:06:56 +02:00
|
|
|
variables: %{commentId: comment.id}
|
2019-11-15 18:36:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert res["data"]["deleteComment"]["id"] == to_string(comment.id)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
actionLogs {
|
2021-04-27 17:53:11 +02:00
|
|
|
total
|
|
|
|
elements {
|
|
|
|
action,
|
|
|
|
actor {
|
|
|
|
preferredUsername
|
2019-11-15 18:36:47 +01:00
|
|
|
},
|
2021-04-27 17:53:11 +02:00
|
|
|
object {
|
|
|
|
... on Report {
|
|
|
|
id,
|
|
|
|
status
|
|
|
|
},
|
|
|
|
... on ReportNote {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
... on Event {
|
|
|
|
id,
|
|
|
|
title
|
|
|
|
},
|
|
|
|
... on Comment {
|
|
|
|
id,
|
|
|
|
text
|
|
|
|
}
|
2019-11-15 18:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user_moderator)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "actionLogs"))
|
|
|
|
|
|
|
|
refute json_response(res, 200)["errors"]
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2021-04-27 17:53:11 +02:00
|
|
|
assert hd(json_response(res, 200)["data"]["actionLogs"]["elements"]) == %{
|
2019-11-15 18:36:47 +01:00
|
|
|
"action" => "COMMENT_DELETION",
|
|
|
|
"actor" => %{"preferredUsername" => actor_moderator.preferred_username},
|
|
|
|
"object" => %{"text" => comment.text, "id" => to_string(comment.id)}
|
|
|
|
}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|