summaryrefslogtreecommitdiff
path: root/test/tasks/frontend_test.exs
blob: fb5da2d3094e072b4a542a7334858bcf38e8d4bc (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
# 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.FrontendTest do
  use ExUnit.Case
  use Pleroma.Tests.Helpers

  import Tesla.Mock, only: [mock_global: 1, json: 1]

  @bundle_zip_path Path.absname("test/fixtures/tesla_mock/fe-bundle.zip")

  @tmp "test/tmp"
  @dir "#{@tmp}/instance_static"

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

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

    :ok
  end

  setup do
    mock_global(fn
      %{method: :get, url: "https://git.pleroma.social/api/v4/projects/" <> rest} ->
        if String.ends_with?(rest, "repository/branches") do
          "test/fixtures/tesla_mock/gitlab-api-pleroma-fe-branches.json"
        else
          "test/fixtures/tesla_mock/gitlab-api-pleroma-fe-releases.json"
        end
        |> Path.absname()
        |> File.read!()
        |> Jason.decode!()
        |> json()

      %{method: :get, url: _download_url} ->
        %Tesla.Env{status: 200, body: File.read!(@bundle_zip_path)}
    end)

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

    clear_config([:instance, :static_dir], @dir)

    :ok
  end

  describe "Installations from local path" do
    test "Frontends with standard dist structure" do
      ~w(pleroma kenoma admin)
      |> Enum.each(fn frontend ->
        path = "test/fixtures/frontends/#{frontend}"
        Mix.Tasks.Pleroma.Frontend.run(~w(install #{frontend} --path #{path}))

        assert File.exists?("#{@dir}/frontends/#{frontend}/42/index.html")
        refute File.exists?("#{@dir}/frontends/#{frontend}/42/package.json")
      end)
    end

    test "Mastodon" do
      path = "test/fixtures/frontends/mastodon"
      Mix.Tasks.Pleroma.Frontend.run(~w(install mastodon --path #{path}))

      assert File.exists?("#{@dir}/frontends/mastodon/__local__/sw.js")
      assert File.exists?("#{@dir}/frontends/mastodon/__local__/packs/locales.js")
      refute File.exists?("#{@dir}/frontends/mastodon/__local__/unused_file")
      refute File.exists?("#{@dir}/frontends/mastodon/__local__/unused_dir")
    end
  end

  describe "Installation from web source" do
    test "develop" do
      if Pleroma.Utils.command_available?("yarn") do
        Mix.Tasks.Pleroma.Frontend.run([
          "install",
          "pleroma",
          "--develop"
        ])

        assert File.exists?(Path.join([@dir, "frontends/pleroma/d5457c32/index.html"]))
      end
    end

    test "stable" do
      if Pleroma.Utils.command_available?("yarn") do
        Mix.Tasks.Pleroma.Frontend.run(["install", "pleroma"])

        assert File.exists?(Path.join([@dir, "frontends/pleroma/5d49edc8/index.html"]))
      end
    end

    test "ref" do
      if Pleroma.Utils.command_available?("yarn") do
        Mix.Tasks.Pleroma.Frontend.run([
          "install",
          "pleroma",
          "--ref",
          "1.2.3"
        ])

        assert File.exists?(Path.join([@dir, "frontends/pleroma/1.2.3/index.html"]))
      end
    end
  end

  describe "Install all" do
    test "Normal config" do
      if Pleroma.Utils.command_available?("yarn") do
        config = [
          primary: %{"name" => "pleroma", "ref" => "1.2.3"},
          mastodon: %{"name" => "mastodon", "ref" => "2.3.4"},
          admin: %{"name" => "admin", "ref" => "3.4.5"}
        ]

        clear_config(:frontends, config)
        Mix.Tasks.Pleroma.Frontend.run(["install", "all"])

        assert File.exists?(Path.join([@dir, "frontends/pleroma/1.2.3/index.html"]))
        assert File.exists?(Path.join([@dir, "frontends/mastodon/2.3.4/sw.js"]))
        assert File.exists?(Path.join([@dir, "frontends/admin/3.4.5/index.html"]))
      end
    end

    test "Unconfigured frontends" do
      if Pleroma.Utils.command_available?("yarn") do
        config = [
          primary: %{"name" => "none", "ref" => "1.2.3"},
          mastodon: %{"name" => "mastodon", "ref" => "none"},
          admin: %{"name" => "admin", "ref" => "none"}
        ]

        clear_config(:frontends, config)
        Mix.Tasks.Pleroma.Frontend.run(["install", "all"])

        assert {:ok, []} == File.ls(@dir)
      end
    end

    test "Missing configs" do
      if Pleroma.Utils.command_available?("yarn") do
        clear_config(:frontends, [])
        Mix.Tasks.Pleroma.Frontend.run(["install", "all"])

        assert {:ok, []} == File.ls(@dir)
      end
    end
  end
end