test: add tests for push notification registration GraphQL resolver

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2023-08-23 14:39:24 +02:00
parent fdf87ea991
commit 8617382af2
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 163 additions and 0 deletions

View File

@ -6,6 +6,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PushSubscription do
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
alias Mobilizon.Users alias Mobilizon.Users
alias Mobilizon.Users.{PushSubscription, User} alias Mobilizon.Users.{PushSubscription, User}
import Mobilizon.Web.Gettext
@doc """ @doc """
List all of an user's registered push subscriptions List all of an user's registered push subscriptions
@ -33,6 +34,19 @@ defmodule Mobilizon.GraphQL.Resolvers.PushSubscription do
{:ok, %PushSubscription{}} -> {:ok, %PushSubscription{}} ->
{:ok, "OK"} {:ok, "OK"}
{:error,
%Ecto.Changeset{
errors: [
digest:
{"has already been taken",
[
constraint: :unique,
constraint_name: "user_push_subscriptions_user_id_digest_index"
]}
]
}} ->
{:error, dgettext("errors", "The same push subscription has already been registered")}
{:error, err} -> {:error, err} ->
require Logger require Logger
Logger.error(inspect(err)) Logger.error(inspect(err))

View File

@ -0,0 +1,149 @@
defmodule Mobilizon.GraphQL.Resolvers.PushSubscriptionTest do
use Mobilizon.Web.ConnCase
import Mobilizon.Factory
alias Mobilizon.GraphQL.AbsintheHelpers
describe "create a new push subscription" do
@register_push_mutation """
mutation RegisterPush($endpoint: String!, $auth: String!, $p256dh: String!) {
registerPush(endpoint: $endpoint, auth: $auth, p256dh: $p256dh)
}
"""
test "without auth", %{conn: conn} do
res =
AbsintheHelpers.graphql_query(conn,
query: @register_push_mutation,
variables: %{endpoint: "https://yolo.com/gfjgfd", auth: "gjrigf", p256dh: "gbgof"}
)
assert hd(res["errors"])["status_code"] == 401
assert hd(res["errors"])["message"] == "You need to be logged in"
end
test "succeeds", %{conn: conn} do
user = insert(:user)
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @register_push_mutation,
variables: %{endpoint: "https://yolo.com/gfjgfd", auth: "gjrigf", p256dh: "gbgof"}
)
assert res["errors"] == nil
assert res["data"]["registerPush"] == "OK"
end
test "fails on duplicate", %{conn: conn} do
user = insert(:user)
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @register_push_mutation,
variables: %{
endpoint: "https://yolo.com/duplicate",
auth: "duplicate",
p256dh: "duplicate"
}
)
assert res["errors"] == nil
assert res["data"]["registerPush"] == "OK"
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @register_push_mutation,
variables: %{
endpoint: "https://yolo.com/duplicate",
auth: "duplicate",
p256dh: "duplicate"
}
)
assert hd(res["errors"])["message"] ==
"The same push subscription has already been registered"
refute res["data"]["registerPush"] == "OK"
end
end
describe "unregister a push subscription" do
@unregister_push_mutation """
mutation UnRegisterPush($endpoint: String!) {
unregisterPush(endpoint: $endpoint)
}
"""
test "without auth", %{conn: conn} do
res =
AbsintheHelpers.graphql_query(conn,
query: @unregister_push_mutation,
variables: %{endpoint: "https://yolo.com/gfjgfd"}
)
assert hd(res["errors"])["status_code"] == 401
assert hd(res["errors"])["message"] == "You need to be logged in"
end
test "fails when not existing", %{conn: conn} do
user = insert(:user)
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @unregister_push_mutation,
variables: %{
endpoint: "https://yolo.com/duplicate",
auth: "duplicate",
p256dh: "duplicate"
}
)
assert hd(res["errors"])["status_code"] == 404
assert hd(res["errors"])["message"] == "Resource not found"
refute res["data"]["registerPush"] == "OK"
end
test "fails when wrong user", %{conn: conn} do
user = insert(:user)
push_subscription = insert(:push_subscription)
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @unregister_push_mutation,
variables: %{endpoint: push_subscription.endpoint}
)
assert hd(res["errors"])["status_code"] == 403
assert hd(res["errors"])["message"] == "You don't have permission to do this"
refute res["data"]["registerPush"] == "OK"
end
test "succeeds", %{conn: conn} do
user = insert(:user)
push_subscription = insert(:push_subscription, user: user)
res =
conn
|> auth_conn(user)
|> AbsintheHelpers.graphql_query(
query: @unregister_push_mutation,
variables: %{endpoint: push_subscription.endpoint}
)
assert res["errors"] == nil
assert res["data"]["unregisterPush"] == "OK"
end
end
end