From 8617382af2cb972236313271b6858c9a0366753d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 23 Aug 2023 14:39:24 +0200 Subject: [PATCH] test: add tests for push notification registration GraphQL resolver Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/push_subscription.ex | 14 ++ .../resolvers/push_subscription_test.exs | 149 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 test/graphql/resolvers/push_subscription_test.exs diff --git a/lib/graphql/resolvers/push_subscription.ex b/lib/graphql/resolvers/push_subscription.ex index 5501fa8b2..5be49a7b5 100644 --- a/lib/graphql/resolvers/push_subscription.ex +++ b/lib/graphql/resolvers/push_subscription.ex @@ -6,6 +6,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PushSubscription do alias Mobilizon.Storage.Page alias Mobilizon.Users alias Mobilizon.Users.{PushSubscription, User} + import Mobilizon.Web.Gettext @doc """ List all of an user's registered push subscriptions @@ -33,6 +34,19 @@ defmodule Mobilizon.GraphQL.Resolvers.PushSubscription do {:ok, %PushSubscription{}} -> {: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} -> require Logger Logger.error(inspect(err)) diff --git a/test/graphql/resolvers/push_subscription_test.exs b/test/graphql/resolvers/push_subscription_test.exs new file mode 100644 index 000000000..456aea443 --- /dev/null +++ b/test/graphql/resolvers/push_subscription_test.exs @@ -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