summaryrefslogtreecommitdiff
path: root/test/mix/tasks/pleroma/uploads_test.exs
blob: d69e149a808dadc94d69adf02b3b40d89fbf46ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Mix.Tasks.Pleroma.UploadsTest do
  alias Pleroma.Upload
  use Pleroma.DataCase

  import Mock

  setup_all do
    Mix.shell(Mix.Shell.Process)

    on_exit(fn ->
      Mix.shell(Mix.Shell.IO)
    end)

    :ok
  end

  describe "running migrate_local" do
    test "uploads migrated" do
      with_mock Upload,
        store: fn %Upload{name: _file, path: _path}, _opts -> {:ok, %{}} end do
        Mix.Tasks.Pleroma.Uploads.run(["migrate_local", "S3"])

        assert_received {:mix_shell, :info, [message]}
        assert message =~ "Migrating files from local"

        assert_received {:mix_shell, :info, [message]}

        assert %{"total_count" => total_count} =
                 Regex.named_captures(~r"^Found (?<total_count>\d+) uploads$", message)

        assert_received {:mix_shell, :info, [message]}

        # @logevery in Mix.Tasks.Pleroma.Uploads
        count =
          min(50, String.to_integer(total_count))
          |> to_string()

        assert %{"count" => ^count, "total_count" => ^total_count} =
                 Regex.named_captures(
                   ~r"^Uploaded (?<count>\d+)/(?<total_count>\d+) files$",
                   message
                 )
      end
    end

    test "nonexistent uploader" do
      assert_raise RuntimeError, ~r/The uploader .* is not an existing/, fn ->
        Mix.Tasks.Pleroma.Uploads.run(["migrate_local", "nonexistent"])
      end
    end
  end
end