aboutsummaryrefslogtreecommitdiff
path: root/test/pleroma/frontend_test.exs
blob: 3cd30d121b1b47e6ed4990f04714a4805d5cdd33 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.FrontendTest do
  use Pleroma.DataCase
  alias Pleroma.Frontend

  @dir "test/frontend_static_test"

  setup do
    File.mkdir_p!(@dir)
    clear_config([:instance, :static_dir], @dir)

    on_exit(fn ->
      File.rm_rf(@dir)
    end)
  end

  test "it downloads and unzips a known frontend" do
    frontend = %Frontend{
      ref: "fantasy",
      name: "pleroma",
      build_url: "http://gensokyo.2hu/builds/${ref}"
    }

    clear_config([:frontends, :available], %{"pleroma" => Frontend.to_map(frontend)})

    Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/builds/fantasy"} ->
      %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend_dist.zip")}
    end)

    Frontend.install(frontend)

    assert File.exists?(Path.join([@dir, "frontends", "pleroma", "fantasy", "test.txt"]))
  end

  test "it also works given a file" do
    frontend = %Frontend{
      ref: "fantasy",
      name: "pleroma",
      build_dir: "",
      file: "test/fixtures/tesla_mock/frontend.zip"
    }

    clear_config([:frontends, :available], %{"pleroma" => Frontend.to_map(frontend)})

    folder = Path.join([@dir, "frontends", "pleroma", "fantasy"])
    previously_existing = Path.join([folder, "temp"])
    File.mkdir_p!(folder)
    File.write!(previously_existing, "yey")
    assert File.exists?(previously_existing)

    Frontend.install(frontend)

    assert File.exists?(Path.join([folder, "test.txt"]))
    refute File.exists?(previously_existing)
  end

  test "it downloads and unzips unknown frontends" do
    frontend = %Frontend{
      ref: "baka",
      build_url: "http://gensokyo.2hu/madeup.zip",
      build_dir: ""
    }

    Tesla.Mock.mock(fn %{url: "http://gensokyo.2hu/madeup.zip"} ->
      %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/frontend.zip")}
    end)

    Frontend.install(frontend)

    assert File.exists?(Path.join([@dir, "frontends", "unknown", "baka", "test.txt"]))
  end

  test "merge/2 only overrides nil values" do
    fe1 = %Frontend{name: "pleroma"}
    fe2 = %Frontend{name: "soapbox", ref: "fantasy"}
    expected = %Frontend{name: "pleroma", ref: "fantasy"}
    assert Frontend.merge(fe1, fe2) == expected
  end

  test "validate!/1 raises if :ref isn't set" do
    fe = %Frontend{name: "pleroma"}
    assert_raise(RuntimeError, fn -> Frontend.validate!(fe) end)
  end

  test "validate!/1 returns the frontend" do
    fe = %Frontend{name: "pleroma", ref: "fantasy"}
    assert Frontend.validate!(fe) == fe
  end

  test "from_map/1 parses a map into a %Frontend{} struct" do
    map = %{"name" => "pleroma", "ref" => "fantasy"}
    expected = %Frontend{name: "pleroma", ref: "fantasy"}
    assert Frontend.from_map(map) == expected
  end

  test "to_map/1 returns the frontend as a map with string keys" do
    frontend = %Frontend{name: "pleroma", ref: "fantasy"}

    expected = %{
      "name" => "pleroma",
      "ref" => "fantasy",
      "build_dir" => nil,
      "build_url" => nil,
      "custom-http-headers" => nil,
      "file" => nil,
      "git" => nil
    }

    assert Frontend.to_map(frontend) == expected
  end

  test "parse_build_url/1 replaces ${ref}" do
    frontend = %Frontend{
      name: "pleroma",
      ref: "fantasy",
      build_url: "http://gensokyo.2hu/builds/${ref}"
    }

    expected = "http://gensokyo.2hu/builds/fantasy"
    assert Frontend.parse_build_url(frontend) == expected
  end

  test "dir/0 returns the frontend dir" do
    assert Frontend.dir() == "test/frontend_static_test/frontends"
  end

  test "get_named_frontend/1 returns a frontend from the config" do
    frontend = %Frontend{name: "pleroma", ref: "fantasy"}
    clear_config([:frontends, :available], %{"pleroma" => Frontend.to_map(frontend)})

    assert Frontend.get_named_frontend("pleroma") == frontend
  end

  describe "enable/2" do
    setup do
      clear_config(:configurable_from_database, true)
    end

    test "enables a primary frontend" do
      frontend = %Frontend{name: "soapbox", ref: "v1.2.3"}
      map = Frontend.to_map(frontend)

      clear_config([:frontends, :available], %{"soapbox" => map})
      Frontend.enable(frontend, :primary)

      assert Pleroma.Config.get([:frontends, :primary]) == map
    end

    test "enables an admin frontend" do
      frontend = %Frontend{name: "admin-fe", ref: "develop"}
      map = Frontend.to_map(frontend)

      clear_config([:frontends, :available], %{"admin-fe" => map})
      Frontend.enable(frontend, :admin)

      assert Pleroma.Config.get([:frontends, :admin]) == map
    end
  end
end