summaryrefslogtreecommitdiff
path: root/test/frontend_test.exs
blob: ad85680927939183e5d612a9990d8f2784f119f2 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
defmodule Pleroma.FrontendTest do
  use ExUnit.Case
  use Pleroma.Tests.Helpers

  describe "get_config/1" do
    test "Primary config" do
      config = %{"name" => "monsta", "ref" => "pika"}

      clear_config([:frontends, :primary], config)

      fe_config = Pleroma.Frontend.get_config()
      assert fe_config["config"] == config
      assert fe_config["controller"] == Pleroma.Web.Frontend.MonstaController
    end

    test "Headless" do
      config = %{"name" => "none", "ref" => "void"}

      clear_config([:frontends, :primary], config)

      fe_config = Pleroma.Frontend.get_config()
      assert fe_config["config"] == %{}
      assert fe_config["controller"] == Pleroma.Web.Frontend.HeadlessController
    end
  end

  describe "file_path/2" do
    @dir "test/tmp/instance_static"
    @filename "gif.bat"

    setup do
      File.mkdir_p!(@dir)

      config = %{"name" => "monsta", "ref" => "mew"}

      clear_config([:frontends, :primary], config)
      clear_config([:instance, :static_dir], @dir)

      fe_path = Path.join([@dir, "frontends", config["name"], config["ref"]])
      File.mkdir_p!(fe_path)
      priv_path = Application.app_dir(:pleroma, ["priv", "static"])

      on_exit(fn ->
        File.rm_rf(@dir)
        File.rm(Path.join(priv_path, @filename))
      end)

      {:ok, %{frontend_path: fe_path, priv_path: priv_path}}
    end

    test "instance static path priority", %{frontend_path: fp, priv_path: pp} do
      Enum.each([@dir, fp, pp], &File.write!(Path.join(&1, @filename), "sup"))

      assert Pleroma.Frontend.file_path(@filename) == {:ok, Path.join(@dir, @filename)}
    end

    test "frontend path priority", %{frontend_path: fp, priv_path: pp} do
      Enum.each([fp, pp], &File.write!(Path.join(&1, @filename), "sup"))

      assert Pleroma.Frontend.file_path(@filename) == {:ok, Path.join(fp, @filename)}
    end

    test "priv path fallback", %{priv_path: pp} do
      File.write!(Path.join(pp, @filename), "sup")

      assert Pleroma.Frontend.file_path(@filename) == {:ok, Path.join(pp, @filename)}
    end

    test "non-existing file" do
      assert {:error, error} = Pleroma.Frontend.file_path("miseeen.jgp.pgn.mp5.avee")

      assert String.valid?(error)
    end
  end
end