2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.FeedTokenTest do
|
2020-01-26 21:36:50 +01:00
|
|
|
use Mobilizon.Web.ConnCase
|
2020-01-26 20:34:25 +01:00
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
import Mobilizon.Factory
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
alias Mobilizon.GraphQL.AbsintheHelpers
|
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
setup %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
actor = insert(:actor, user: user, preferred_username: "test")
|
|
|
|
insert(:actor, user: user)
|
|
|
|
|
|
|
|
{:ok, conn: conn, actor: actor, user: user}
|
|
|
|
end
|
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
@user_feed_tokens_query """
|
|
|
|
query LoggedUserFeedTokens {
|
|
|
|
loggedUser {
|
|
|
|
feedTokens {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@logged_person_feed_tokens_query """
|
|
|
|
query LoggedPersonFeedTokens {
|
|
|
|
loggedPerson {
|
|
|
|
feedTokens {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
describe "Feed Token Resolver" do
|
2023-03-17 18:10:59 +01:00
|
|
|
@create_feed_token_for_actor_mutation """
|
|
|
|
mutation CreatePersonFeedToken($actorId: ID!) {
|
|
|
|
createFeedToken(actorId: $actorId) {
|
|
|
|
token
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
@create_feed_token_for_user_mutation """
|
|
|
|
mutation CreateFeedToken {
|
|
|
|
createFeedToken {
|
|
|
|
token
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2019-03-08 18:52:27 +01:00
|
|
|
test "create_feed_token/3 should create a feed token", %{conn: conn, user: user} do
|
|
|
|
actor2 = insert(:actor, user: user)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_feed_token_for_actor_mutation,
|
|
|
|
variables: [actorId: actor2.id]
|
|
|
|
)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["errors"] == nil
|
|
|
|
token = res["data"]["createFeedToken"]["token"]
|
2019-03-08 18:52:27 +01:00
|
|
|
assert is_binary(token)
|
2023-03-17 18:10:59 +01:00
|
|
|
|
|
|
|
assert res["data"]["createFeedToken"]["user"]["id"] ==
|
2019-03-08 18:52:27 +01:00
|
|
|
to_string(user.id)
|
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["data"]["createFeedToken"]["actor"]["id"] ==
|
2019-09-09 09:31:08 +02:00
|
|
|
to_string(actor2.id)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
|
|
|
# The token is present for the user
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @user_feed_tokens_query)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["data"]["loggedUser"] ==
|
2019-03-08 18:52:27 +01:00
|
|
|
%{
|
|
|
|
"feedTokens" => [%{"token" => token}]
|
|
|
|
}
|
|
|
|
|
|
|
|
# But not for this identity
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @logged_person_feed_tokens_query)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["data"]["loggedPerson"] ==
|
2019-03-08 18:52:27 +01:00
|
|
|
%{
|
|
|
|
"feedTokens" => []
|
|
|
|
}
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @create_feed_token_for_user_mutation)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["errors"] == nil
|
|
|
|
token2 = res["data"]["createFeedToken"]["token"]
|
2019-03-08 18:52:27 +01:00
|
|
|
assert is_binary(token2)
|
2023-03-17 18:10:59 +01:00
|
|
|
assert is_nil(res["data"]["createFeedToken"]["actor"])
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["data"]["createFeedToken"]["user"]["id"] ==
|
2019-03-08 18:52:27 +01:00
|
|
|
to_string(user.id)
|
|
|
|
|
|
|
|
# The token is present for the user
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(query: @user_feed_tokens_query)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert res["data"]["loggedUser"] ==
|
2019-03-08 18:52:27 +01:00
|
|
|
%{
|
|
|
|
"feedTokens" => [%{"token" => token}, %{"token" => token2}]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "create_feed_token/3 should check the actor is owned by the user", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
actor = insert(:actor)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
2023-03-17 18:10:59 +01:00
|
|
|
|> AbsintheHelpers.graphql_query(
|
|
|
|
query: @create_feed_token_for_actor_mutation,
|
|
|
|
variables: [actorId: actor.id]
|
|
|
|
)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert hd(res["errors"])["message"] =~ "not owned"
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_feed_token/3 should delete a feed token", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user,
|
|
|
|
actor: actor
|
|
|
|
} do
|
|
|
|
feed_token = insert(:feed_token, user: user, actor: actor)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedPerson {
|
|
|
|
feedTokens {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "loggedPerson"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"] ==
|
|
|
|
%{
|
|
|
|
"feedTokens" => [
|
|
|
|
%{
|
2021-03-26 19:01:55 +01:00
|
|
|
"token" => ShortUUID.encode!(feed_token.token)
|
2019-03-08 18:52:27 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteFeedToken(
|
2021-03-26 19:01:55 +01:00
|
|
|
token: "#{ShortUUID.encode!(feed_token.token)}",
|
2019-03-08 18:52:27 +01:00
|
|
|
) {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
},
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
2019-09-09 09:31:08 +02:00
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["deleteFeedToken"]["user"]["id"] ==
|
|
|
|
to_string(user.id)
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["deleteFeedToken"]["actor"]["id"] ==
|
|
|
|
to_string(actor.id)
|
2019-03-08 18:52:27 +01:00
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedPerson {
|
|
|
|
feedTokens {
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "loggedPerson"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedPerson"] ==
|
|
|
|
%{
|
|
|
|
"feedTokens" => []
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_feed_token/3 should check the user is logged in", %{conn: conn} do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteFeedToken(
|
|
|
|
token: "random",
|
|
|
|
) {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2023-03-17 18:10:59 +01:00
|
|
|
assert "You need to be logged in" == hd(json_response(res, 200)["errors"])["message"]
|
2019-03-08 18:52:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_feed_token/3 should check the correct user is logged in", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
user2 = insert(:user)
|
|
|
|
feed_token = insert(:feed_token, user: user2)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteFeedToken(
|
2021-03-26 19:01:55 +01:00
|
|
|
token: "#{ShortUUID.encode!(feed_token.token)}",
|
2019-03-08 18:52:27 +01:00
|
|
|
) {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "don't have permission"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_feed_token/3 should check the token is a valid UUID", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteFeedToken(
|
|
|
|
token: "really random"
|
|
|
|
) {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "Token is not a valid UUID"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_feed_token/3 should check the token exists", %{
|
|
|
|
conn: conn,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
deleteFeedToken(
|
2021-03-26 19:01:55 +01:00
|
|
|
token: "#{ShortUUID.encode!(uuid)}"
|
2019-03-08 18:52:27 +01:00
|
|
|
) {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] =~ "does not exist"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|