fp/services/bright/test/bright/torrents_test.exs

97 lines
3.7 KiB
Elixir

defmodule Bright.TorrentsTest do
use Bright.DataCase
alias Bright.Torrents
alias Bright.{Downloader,Cache}
@test_fixture "https://futureporn-b2.b-cdn.net/test-fixture.ts"
describe "torrents" do
alias Bright.Torrents.Torrent
import Bright.TorrentsFixtures
@invalid_attrs %{info_hash_v1: nil, info_hash_v2: nil, cdn_url: nil, magnet: nil}
test "list_torrents/0 returns all torrent" do
torrent = torrent_fixture()
assert Torrents.list_torrents() == [torrent]
end
test "get_torrent!/1 returns the torrent with given id" do
torrent = torrent_fixture()
assert Torrents.get_torrent!(torrent.id) == torrent
end
test "create_torrent/1 with valid data creates a torrent" do
valid_attrs = %{info_hash_v1: "some info_hash_v1", info_hash_v2: "some info_hash_v2", cdn_url: "some cdn_url", magnet: "some magnet"}
assert {:ok, %Torrent{} = torrent} = Torrents.create_torrent(valid_attrs)
assert torrent.info_hash_v1 == "some info_hash_v1"
assert torrent.info_hash_v2 == "some info_hash_v2"
assert torrent.cdn_url == "some cdn_url"
assert torrent.magnet == "some magnet"
end
test "create_torrent/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Torrents.create_torrent(@invalid_attrs)
end
test "update_torrent/2 with valid data updates the torrent" do
torrent = torrent_fixture()
update_attrs = %{info_hash_v1: "some updated info_hash_v1", info_hash_v2: "some updated info_hash_v2", cdn_url: "some updated cdn_url", magnet: "some updated magnet"}
assert {:ok, %Torrent{} = torrent} = Torrents.update_torrent(torrent, update_attrs)
assert torrent.info_hash_v1 == "some updated info_hash_v1"
assert torrent.info_hash_v2 == "some updated info_hash_v2"
assert torrent.cdn_url == "some updated cdn_url"
assert torrent.magnet == "some updated magnet"
end
test "update_torrent/2 with invalid data returns error changeset" do
torrent = torrent_fixture()
assert {:error, %Ecto.Changeset{}} = Torrents.update_torrent(torrent, @invalid_attrs)
assert torrent == Torrents.get_torrent!(torrent.id)
end
test "delete_torrent/1 deletes the torrent" do
torrent = torrent_fixture()
assert {:ok, %Torrent{}} = Torrents.delete_torrent(torrent)
assert_raise Ecto.NoResultsError, fn -> Torrents.get_torrent!(torrent.id) end
end
test "change_torrent/1 returns a torrent changeset" do
torrent = torrent_fixture()
assert %Ecto.Changeset{} = Torrents.change_torrent(torrent)
end
@tag :integration
test "generate_torrent_file/7" do
stream = stream_fixture()
vod = vod_fixture(%{stream_id: stream.id, s3_cdn_url: "https://futureporn-b2.b-cdn.net/test-fixture.ts"})
input_path = Path.absname("./test/fixtures/test-fixture.ts")
output_path = Cache.generate_filename("test", "torrent")
tracker_url = "https://tracker.futureporn.net/announce"
source_url = "https://futureporn.net/vods/69"
comment = "https://futureporn.net"
web_seed_url = @test_fixture
meta_version = 3
IO.puts "input_path=#{input_path} output_path=#{output_path} tracker_url=#{tracker_url} source_url=#{source_url}"
{:ok, %{local_path: local_path, magnet_link: magnet_link, basename: basename, info_hash_v1: info_hash_v1, info_hash_v2: info_hash_v2}}
= Torrent.create_torrent(input_path, output_path, web_seed_url, vod.id)
assert :ok
assert local_path === output_path
assert File.exists?(output_path)
assert String.starts_with?(magnet_link, "magnet:")
assert String.ends_with?(basename, ".torrent")
assert is_binary(info_hash_v1)
assert is_binary(info_hash_v2)
end
end
end