From e2fb00f90223866982cc9a9f77da9df99d36704b Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 4 Jul 2024 14:12:25 -0400 Subject: [PATCH] Add tests --- test/philomena/channels/subscription_test.exs | 38 ++++++++++++++ test/philomena/comments/subscription_test.exs | 34 +++++++++++++ test/philomena/forums/subscription_test.exs | 16 ++++++ .../philomena/galleries/subscription_test.exs | 16 ++++++ test/philomena/images/subscription_test.exs | 45 +++++++++++++++++ test/philomena/posts/subscription_test.exs | 32 ++++++++++++ test/philomena/topics/subscription_test.exs | 49 +++++++++++++++++++ test/support/fixtures/attribution_fixtures.ex | 18 +++++++ test/support/fixtures/channels_fixtures.ex | 23 +++++++++ test/support/fixtures/forums_fixtures.ex | 25 ++++++++++ test/support/fixtures/galleries_fixtures.ex | 26 ++++++++++ test/support/fixtures/images_fixtures.ex | 40 +++++++++++++++ test/support/fixtures/topics_fixtures.ex | 34 +++++++++++++ 13 files changed, 396 insertions(+) create mode 100644 test/philomena/channels/subscription_test.exs create mode 100644 test/philomena/comments/subscription_test.exs create mode 100644 test/philomena/forums/subscription_test.exs create mode 100644 test/philomena/galleries/subscription_test.exs create mode 100644 test/philomena/images/subscription_test.exs create mode 100644 test/philomena/posts/subscription_test.exs create mode 100644 test/philomena/topics/subscription_test.exs create mode 100644 test/support/fixtures/attribution_fixtures.ex create mode 100644 test/support/fixtures/channels_fixtures.ex create mode 100644 test/support/fixtures/forums_fixtures.ex create mode 100644 test/support/fixtures/galleries_fixtures.ex create mode 100644 test/support/fixtures/images_fixtures.ex create mode 100644 test/support/fixtures/topics_fixtures.ex diff --git a/test/philomena/channels/subscription_test.exs b/test/philomena/channels/subscription_test.exs new file mode 100644 index 00000000..7b9a75c8 --- /dev/null +++ b/test/philomena/channels/subscription_test.exs @@ -0,0 +1,38 @@ +defmodule Philomena.Channels.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.Channels + alias Philomena.{ChannelsFixtures, UsersFixtures} + + describe "subscriptions/2" do + setup do + user = UsersFixtures.user_fixture() + channel = ChannelsFixtures.channel_fixture() + + %{user: user, channel: channel} + end + + test "returns no subscriptions with nil user", %{channel: channel} do + assert %{} == Channels.subscriptions([channel], nil) + end + + test "returns no subscriptions with non-subscribed user", %{channel: channel, user: user} do + assert %{} == Channels.subscriptions([channel], user) + end + + test "returns subscription with subscribed user", %{channel: channel, user: user} do + {:ok, _} = Channels.create_subscription(channel, user) + assert %{channel.id => true} == Channels.subscriptions([channel], user) + end + end + + describe "create_subscription/2, delete_subscription/2" do + test "allows subscription and unsubscription" do + user = UsersFixtures.user_fixture() + channel = ChannelsFixtures.channel_fixture() + + assert {:ok, _} = Channels.create_subscription(channel, user) + assert {:ok, _} = Channels.delete_subscription(channel, user) + end + end +end diff --git a/test/philomena/comments/subscription_test.exs b/test/philomena/comments/subscription_test.exs new file mode 100644 index 00000000..b34f2c13 --- /dev/null +++ b/test/philomena/comments/subscription_test.exs @@ -0,0 +1,34 @@ +defmodule Philomena.Comments.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.{Images, Comments} + alias Philomena.{AttributionFixtures, ImagesFixtures, UsersFixtures} + + @params %{"body" => "hello world", "anonymous" => false} + + describe "create_comment/3" do + setup do + image = ImagesFixtures.image_fixture() + user = UsersFixtures.user_fixture() + %{image: image, user: user} + end + + test "succeeds when user is nil", %{image: image} do + attribution = AttributionFixtures.attribution_fixture() + assert {:ok, _comment} = Comments.create_comment(image, attribution, @params) + end + + test "succeeds and subscribes the user by default", %{image: image, user: user} do + attribution = AttributionFixtures.attribution_fixture(user) + assert {:ok, _comment} = Comments.create_comment(image, attribution, @params) + assert Images.subscribed?(image, user) + end + + test "succeeds and does not subscribe the user when requested", %{image: image, user: user} do + user = %{user | watch_on_reply: false} + attribution = AttributionFixtures.attribution_fixture(user) + assert {:ok, _comment} = Comments.create_comment(image, attribution, @params) + refute Images.subscribed?(image, user) + end + end +end diff --git a/test/philomena/forums/subscription_test.exs b/test/philomena/forums/subscription_test.exs new file mode 100644 index 00000000..3922772a --- /dev/null +++ b/test/philomena/forums/subscription_test.exs @@ -0,0 +1,16 @@ +defmodule Philomena.Forums.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.Forums + alias Philomena.{ForumsFixtures, UsersFixtures} + + describe "create_subscription/2, delete_subscription/2" do + test "allows subscription and unsubscription" do + user = UsersFixtures.user_fixture() + forum = ForumsFixtures.forum_fixture() + + assert {:ok, _} = Forums.create_subscription(forum, user) + assert {:ok, _} = Forums.delete_subscription(forum, user) + end + end +end diff --git a/test/philomena/galleries/subscription_test.exs b/test/philomena/galleries/subscription_test.exs new file mode 100644 index 00000000..b8157bf2 --- /dev/null +++ b/test/philomena/galleries/subscription_test.exs @@ -0,0 +1,16 @@ +defmodule Philomena.Galleries.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.Galleries + alias Philomena.{GalleriesFixtures, UsersFixtures} + + describe "create_subscription/2, delete_subscription/2" do + test "allows subscription and unsubscription" do + user = UsersFixtures.user_fixture() + gallery = GalleriesFixtures.gallery_fixture() + + assert {:ok, _} = Galleries.create_subscription(gallery, user) + assert {:ok, _} = Galleries.delete_subscription(gallery, user) + end + end +end diff --git a/test/philomena/images/subscription_test.exs b/test/philomena/images/subscription_test.exs new file mode 100644 index 00000000..99921314 --- /dev/null +++ b/test/philomena/images/subscription_test.exs @@ -0,0 +1,45 @@ +defmodule Philomena.Images.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.Images + alias Philomena.{AttributionFixtures, ImagesFixtures, UsersFixtures} + + describe "create_image/2" do + setup do + %{user: UsersFixtures.user_fixture()} + end + + test "succeeds when user is nil" do + attribution = AttributionFixtures.attribution_fixture() + assert {:ok, _changes} = Images.create_image(attribution, ImagesFixtures.upload_attrs()) + end + + test "succeeds and subscribes the user by default", %{user: user} do + attribution = AttributionFixtures.attribution_fixture(user) + + assert {:ok, %{image: image}} = + Images.create_image(attribution, ImagesFixtures.upload_attrs()) + + assert Images.subscribed?(image, user) + end + + test "succeeds and does not subscribe the user when requested", %{user: user} do + attribution = AttributionFixtures.attribution_fixture(%{user | watch_on_upload: false}) + + assert {:ok, %{image: image}} = + Images.create_image(attribution, ImagesFixtures.upload_attrs()) + + refute Images.subscribed?(image, user) + end + end + + describe "create_subscription/2, delete_subscription/2" do + test "allows subscription and unsubscription" do + user = UsersFixtures.user_fixture() + image = ImagesFixtures.image_fixture() + + assert {:ok, _} = Images.create_subscription(image, user) + assert {:ok, _} = Images.delete_subscription(image, user) + end + end +end diff --git a/test/philomena/posts/subscription_test.exs b/test/philomena/posts/subscription_test.exs new file mode 100644 index 00000000..507f8cda --- /dev/null +++ b/test/philomena/posts/subscription_test.exs @@ -0,0 +1,32 @@ +defmodule Philomena.Posts.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.{Topics, Posts} + alias Philomena.{AttributionFixtures, TopicsFixtures, UsersFixtures} + + describe "create_post/3" do + setup do + topic = TopicsFixtures.topic_fixture() + user = UsersFixtures.user_fixture() + %{topic: topic, user: user} + end + + test "succeeds when user is nil", %{topic: topic} do + attribution = AttributionFixtures.attribution_fixture() + assert {:ok, _changes} = Posts.create_post(topic, attribution, TopicsFixtures.post_attrs()) + end + + test "succeeds and subscribes the user by default", %{topic: topic, user: user} do + attribution = AttributionFixtures.attribution_fixture(user) + assert {:ok, _changes} = Posts.create_post(topic, attribution, TopicsFixtures.post_attrs()) + assert Topics.subscribed?(topic, user) + end + + test "succeeds and does not subscribe the user when requested", %{topic: topic, user: user} do + user = %{user | watch_on_reply: false} + attribution = AttributionFixtures.attribution_fixture(user) + assert {:ok, _changes} = Posts.create_post(topic, attribution, TopicsFixtures.post_attrs()) + refute Topics.subscribed?(topic, user) + end + end +end diff --git a/test/philomena/topics/subscription_test.exs b/test/philomena/topics/subscription_test.exs new file mode 100644 index 00000000..64e13cc8 --- /dev/null +++ b/test/philomena/topics/subscription_test.exs @@ -0,0 +1,49 @@ +defmodule Philomena.Topics.SubscriptionTest do + use Philomena.DataCase + + alias Philomena.Topics + alias Philomena.{AttributionFixtures, ForumsFixtures, TopicsFixtures, UsersFixtures} + + describe "create_topic/3" do + setup do + user = UsersFixtures.user_fixture() + forum = ForumsFixtures.forum_fixture() + %{user: user, forum: forum} + end + + test "succeeds when the user is nil", %{forum: forum} do + attribution = AttributionFixtures.attribution_fixture() + + assert {:ok, _changes} = + Topics.create_topic(forum, attribution, TopicsFixtures.topic_attrs()) + end + + test "succeeds and subscribes the user by default", %{forum: forum, user: user} do + attribution = AttributionFixtures.attribution_fixture(user) + + assert {:ok, %{topic: topic}} = + Topics.create_topic(forum, attribution, TopicsFixtures.topic_attrs()) + + assert Topics.subscribed?(topic, user) + end + + test "succeeds and does not subscribe the user when requested", %{forum: forum, user: user} do + attribution = AttributionFixtures.attribution_fixture(%{user | watch_on_new_topic: false}) + + assert {:ok, %{topic: topic}} = + Topics.create_topic(forum, attribution, TopicsFixtures.topic_attrs()) + + refute Topics.subscribed?(topic, user) + end + end + + describe "create_subscription/2, delete_subscription/2" do + test "allows subscription and unsubscription" do + user = UsersFixtures.user_fixture() + topic = TopicsFixtures.topic_fixture() + + assert {:ok, _} = Topics.create_subscription(topic, user) + assert {:ok, _} = Topics.delete_subscription(topic, user) + end + end +end diff --git a/test/support/fixtures/attribution_fixtures.ex b/test/support/fixtures/attribution_fixtures.ex new file mode 100644 index 00000000..e4f00beb --- /dev/null +++ b/test/support/fixtures/attribution_fixtures.ex @@ -0,0 +1,18 @@ +defmodule Philomena.AttributionFixtures do + @moduledoc """ + This module defines test helpers for creating attribution. + """ + + def attribution_fixture(user \\ nil) do + {:ok, ip} = EctoNetwork.INET.cast("127.0.0.1") + fingerprint = to_string(:io_lib.format(~c"d~14.16.0b", [:rand.uniform(2 ** 53)])) + + [ + ip: ip, + fingerprint: fingerprint, + referrer: "", + user: user, + user_agent: "" + ] + end +end diff --git a/test/support/fixtures/channels_fixtures.ex b/test/support/fixtures/channels_fixtures.ex new file mode 100644 index 00000000..c0806315 --- /dev/null +++ b/test/support/fixtures/channels_fixtures.ex @@ -0,0 +1,23 @@ +defmodule Philomena.ChannelsFixtures do + @moduledoc """ + This module defines test helpers for creating entities via the `Philomena.Channels` context. + """ + + alias Philomena.Channels + + def unique_name, do: "channel#{System.unique_integer()}" + + def channel_fixture(attrs \\ %{}) do + name = unique_name() + + {:ok, channel} = + attrs + |> Enum.into(%{ + short_name: name, + type: "PicartoChannel" + }) + |> Channels.create_channel() + + channel + end +end diff --git a/test/support/fixtures/forums_fixtures.ex b/test/support/fixtures/forums_fixtures.ex new file mode 100644 index 00000000..23dd65bd --- /dev/null +++ b/test/support/fixtures/forums_fixtures.ex @@ -0,0 +1,25 @@ +defmodule Philomena.ForumsFixtures do + @moduledoc """ + This module defines test helpers for creating entities via the `Philomena.Forums` context. + """ + + alias Philomena.Forums + + def unique_name do + for _ <- 1..32, into: <<>>, do: <> + end + + def forum_fixture(attrs \\ %{}) do + {:ok, forum} = + attrs + |> Enum.into(%{ + name: unique_name(), + short_name: unique_name(), + description: unique_name(), + access_level: "normal" + }) + |> Forums.create_forum() + + forum + end +end diff --git a/test/support/fixtures/galleries_fixtures.ex b/test/support/fixtures/galleries_fixtures.ex new file mode 100644 index 00000000..076ffbc6 --- /dev/null +++ b/test/support/fixtures/galleries_fixtures.ex @@ -0,0 +1,26 @@ +defmodule Philomena.GalleriesFixtures do + @moduledoc """ + This module defines test helpers for creating entities via the `Philomena.Galleries` context. + """ + + alias Philomena.Galleries + alias Philomena.{ImagesFixtures, UsersFixtures} + + def gallery_fixture(attrs \\ %{}) do + user = UsersFixtures.user_fixture() + image = ImagesFixtures.image_fixture() + + attrs = + Enum.into(attrs, %{ + title: "Gallery Fixture", + thumbnail_id: image.id, + spoiler_warning: "Spoiler warning", + description: "Description", + order_position_asc: false + }) + + {:ok, gallery} = Galleries.create_gallery(user, attrs) + + gallery + end +end diff --git a/test/support/fixtures/images_fixtures.ex b/test/support/fixtures/images_fixtures.ex new file mode 100644 index 00000000..8691be03 --- /dev/null +++ b/test/support/fixtures/images_fixtures.ex @@ -0,0 +1,40 @@ +defmodule Philomena.ImagesFixtures do + @moduledoc """ + This module defines test helpers for creating entities via the `Philomena.Images` context. + """ + + alias Philomena.Images + alias Philomena.{AttributionFixtures, UsersFixtures} + + def image_fixture(opts \\ []) do + user = Keyword.get_lazy(opts, :user, fn -> UsersFixtures.user_fixture() end) + + attribution = + Keyword.get_lazy(opts, :attribution, fn -> AttributionFixtures.attribution_fixture(user) end) + + {:ok, %{image: image}} = Images.create_image(attribution, upload_attrs()) + + image + end + + def upload_attrs do + path = Plug.Upload.random_file!("test-image") + File.write!(path, random_png()) + + %{ + "tag_input" => "safe, qr code, test fixture", + "image" => %Plug.Upload{ + filename: "test-image", + content_type: "application/octet-stream", + path: path + }, + "anonymous" => false + } + end + + defp random_png do + 128 + |> :crypto.strong_rand_bytes() + |> QRCode.to_png() + end +end diff --git a/test/support/fixtures/topics_fixtures.ex b/test/support/fixtures/topics_fixtures.ex new file mode 100644 index 00000000..c786474c --- /dev/null +++ b/test/support/fixtures/topics_fixtures.ex @@ -0,0 +1,34 @@ +defmodule Philomena.TopicsFixtures do + @moduledoc """ + This module defines test helpers for creating entities via the `Philomena.Topics` context. + """ + + alias Philomena.Topics + alias Philomena.{AttributionFixtures, ForumsFixtures, UsersFixtures} + + def unique_name do + for _ <- 1..32, into: <<>>, do: <> + end + + def topic_fixture(opts \\ []) do + forum = Keyword.get_lazy(opts, :forum, fn -> ForumsFixtures.forum_fixture() end) + user = Keyword.get_lazy(opts, :user, fn -> UsersFixtures.user_fixture() end) + attribution = AttributionFixtures.attribution_fixture(user) + + {:ok, %{topic: topic}} = Topics.create_topic(forum, attribution, topic_attrs()) + + topic + end + + def topic_attrs do + %{ + title: unique_name(), + anonymous: false, + posts: [post_attrs()] + } + end + + def post_attrs do + %{body: unique_name()} + end +end