Add tests

This commit is contained in:
Liam 2024-07-04 14:12:25 -04:00
parent 5678862038
commit e2fb00f902
13 changed files with 396 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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: <<Enum.random(?a..?z)>>
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

View file

@ -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

View file

@ -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

View file

@ -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: <<Enum.random(?a..?z)>>
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