[Enhancement] Add Apprise support (#170)
* [WIP] add settings sidebar entry and placeholder page * [WIP] added placeholder UI and logic for settings form * Added column and UI for apprise server * Add some tests * Added placeholder command runner for apprise * [WIP] Adding apprise package * Added apprise command runner * Hooked up apprise notification module * Ensured apprise was running in verbose mode * Updated wording of apprise notification * Added apprise to README
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
defmodule Pinchflat.Notifications.CommandRunnerTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
alias Pinchflat.Notifications.CommandRunner, as: Runner
|
||||
|
||||
@original_executable Application.compile_env(:pinchflat, :apprise_executable)
|
||||
|
||||
setup do
|
||||
on_exit(&reset_executable/0)
|
||||
end
|
||||
|
||||
describe "run/2" do
|
||||
test "returns :ok when the command succeeds" do
|
||||
assert {:ok, _} = Runner.run("server_1", [])
|
||||
end
|
||||
|
||||
test "includes the servers as the first argument" do
|
||||
assert {:ok, output} = Runner.run(["server_1", "server_2"], [])
|
||||
|
||||
assert String.contains?(output, "server_1 server_2")
|
||||
end
|
||||
|
||||
test "lets you pass a single server as a string" do
|
||||
assert {:ok, output} = Runner.run("server_1", [])
|
||||
|
||||
assert String.contains?(output, "server_1")
|
||||
end
|
||||
|
||||
test "passes all arguments to the command" do
|
||||
assert {:ok, output} = Runner.run("server_1", ["--dry-run"])
|
||||
|
||||
assert String.contains?(output, "--dry-run")
|
||||
end
|
||||
|
||||
test "returns the output when the command fails" do
|
||||
wrap_executable("/bin/false", fn ->
|
||||
assert {:error, ""} = Runner.run("server_1", [])
|
||||
end)
|
||||
end
|
||||
|
||||
test "returns a relevant error if no servers are provided" do
|
||||
assert {:error, :no_servers} = Runner.run(nil, [])
|
||||
assert {:error, :no_servers} = Runner.run("", [])
|
||||
assert {:error, :no_servers} = Runner.run([], [])
|
||||
end
|
||||
end
|
||||
|
||||
describe "version/0" do
|
||||
test "adds the version arg" do
|
||||
assert {:ok, output} = Runner.version()
|
||||
|
||||
assert String.contains?(output, "--version")
|
||||
end
|
||||
end
|
||||
|
||||
defp wrap_executable(new_executable, fun) do
|
||||
Application.put_env(:pinchflat, :apprise_executable, new_executable)
|
||||
fun.()
|
||||
reset_executable()
|
||||
end
|
||||
|
||||
def reset_executable do
|
||||
Application.put_env(:pinchflat, :apprise_executable, @original_executable)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,100 @@
|
||||
defmodule Pinchflat.Notifications.SourceNotificationsTest do
|
||||
use Pinchflat.DataCase
|
||||
|
||||
import Mox
|
||||
import Pinchflat.MediaFixtures
|
||||
import Pinchflat.SourcesFixtures
|
||||
|
||||
alias Pinchflat.Notifications.SourceNotifications
|
||||
|
||||
@apprise_servers ["server_1", "server_2"]
|
||||
|
||||
setup :verify_on_exit!
|
||||
|
||||
describe "wrap_new_media_notification/3" do
|
||||
test "sends a notification when the pending count changes" do
|
||||
source = source_fixture()
|
||||
|
||||
expect(AppriseRunnerMock, :run, fn servers, opts ->
|
||||
assert servers == @apprise_servers
|
||||
|
||||
assert opts == [
|
||||
title: "[Pinchflat] New media found",
|
||||
body: "Found 1 new media item(s) for #{source.custom_name}. Downloading them now"
|
||||
]
|
||||
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
SourceNotifications.wrap_new_media_notification(@apprise_servers, source, fn ->
|
||||
media_item_fixture(%{source_id: source.id, media_filepath: nil})
|
||||
end)
|
||||
end
|
||||
|
||||
test "sends a notification when the downloaded count changes" do
|
||||
source = source_fixture()
|
||||
|
||||
expect(AppriseRunnerMock, :run, fn servers, opts ->
|
||||
assert servers == @apprise_servers
|
||||
|
||||
assert opts == [
|
||||
title: "[Pinchflat] New media found",
|
||||
body: "Found 1 new media item(s) for #{source.custom_name}. Downloading them now"
|
||||
]
|
||||
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
SourceNotifications.wrap_new_media_notification(@apprise_servers, source, fn ->
|
||||
media_item_fixture(%{source_id: source.id, media_filepath: "file.mp4"})
|
||||
end)
|
||||
end
|
||||
|
||||
test "does not send a notification when the count does not change" do
|
||||
source = source_fixture()
|
||||
|
||||
expect(AppriseRunnerMock, :run, 0, fn _, _ -> {:ok, ""} end)
|
||||
|
||||
SourceNotifications.wrap_new_media_notification(@apprise_servers, source, fn ->
|
||||
media_item_fixture(%{source_id: source.id, prevent_download: true, media_filepath: nil})
|
||||
end)
|
||||
end
|
||||
|
||||
test "returns the value of the function" do
|
||||
source = source_fixture()
|
||||
expect(AppriseRunnerMock, :run, 0, fn _, _ -> {:ok, ""} end)
|
||||
|
||||
retval = SourceNotifications.wrap_new_media_notification(@apprise_servers, source, fn -> "value" end)
|
||||
|
||||
assert retval == "value"
|
||||
end
|
||||
end
|
||||
|
||||
describe "send_new_media_notification/3" do
|
||||
test "sends a notification when count is positive" do
|
||||
source = source_fixture()
|
||||
|
||||
expect(AppriseRunnerMock, :run, fn servers, opts ->
|
||||
assert servers == @apprise_servers
|
||||
|
||||
assert opts == [
|
||||
title: "[Pinchflat] New media found",
|
||||
body: "Found 1 new media item(s) for #{source.custom_name}. Downloading them now"
|
||||
]
|
||||
|
||||
{:ok, ""}
|
||||
end)
|
||||
|
||||
:ok = SourceNotifications.send_new_media_notification(@apprise_servers, source, 1)
|
||||
end
|
||||
|
||||
test "does not send a notification when count not positive" do
|
||||
source = source_fixture()
|
||||
|
||||
expect(AppriseRunnerMock, :run, 0, fn _, _ -> {:ok, ""} end)
|
||||
|
||||
:ok = SourceNotifications.send_new_media_notification(@apprise_servers, source, 0)
|
||||
:ok = SourceNotifications.send_new_media_notification(@apprise_servers, source, -1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user