total rebase
[anni] / test / mix / tasks / pleroma / instance_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.InstanceTest do
6   # Modifies the Application Environment, has to stay synchronous.
7   use Pleroma.DataCase
8
9   setup do
10     File.mkdir_p!(tmp_path())
11
12     on_exit(fn ->
13       File.rm_rf(tmp_path())
14       static_dir = Pleroma.Config.get([:instance, :static_dir], "test/instance_static/")
15
16       if File.exists?(static_dir) do
17         File.rm_rf(Path.join(static_dir, "robots.txt"))
18       end
19     end)
20
21     # Is being modified by the mix task.
22     clear_config([:instance, :static_dir])
23
24     :ok
25   end
26
27   @uuid Ecto.UUID.generate()
28   defp tmp_path do
29     "/tmp/generated_files/#{@uuid}/"
30   end
31
32   test "running gen" do
33     mix_task = fn ->
34       Mix.Tasks.Pleroma.Instance.run([
35         "gen",
36         "--output",
37         tmp_path() <> "generated_config.exs",
38         "--output-psql",
39         tmp_path() <> "setup.psql",
40         "--domain",
41         "test.pleroma.social",
42         "--instance-name",
43         "Pleroma",
44         "--admin-email",
45         "admin@example.com",
46         "--notify-email",
47         "notify@example.com",
48         "--dbhost",
49         "dbhost",
50         "--dbname",
51         "dbname",
52         "--dbuser",
53         "dbuser",
54         "--dbpass",
55         "dbpass",
56         "--indexable",
57         "y",
58         "--db-configurable",
59         "y",
60         "--rum",
61         "y",
62         "--listen-port",
63         "4000",
64         "--listen-ip",
65         "127.0.0.1",
66         "--uploads-dir",
67         "test/uploads",
68         "--static-dir",
69         "./test/../test/instance/static/",
70         "--strip-uploads-location",
71         "y",
72         "--read-uploads-description",
73         "y",
74         "--dedupe-uploads",
75         "n",
76         "--anonymize-uploads",
77         "n"
78       ])
79     end
80
81     ExUnit.CaptureIO.capture_io(fn ->
82       mix_task.()
83     end)
84
85     generated_config = File.read!(tmp_path() <> "generated_config.exs")
86     assert generated_config =~ "host: \"test.pleroma.social\""
87     assert generated_config =~ "name: \"Pleroma\""
88     assert generated_config =~ "email: \"admin@example.com\""
89     assert generated_config =~ "notify_email: \"notify@example.com\""
90     assert generated_config =~ "hostname: \"dbhost\""
91     assert generated_config =~ "database: \"dbname\""
92     assert generated_config =~ "username: \"dbuser\""
93     assert generated_config =~ "password: \"dbpass\""
94     assert generated_config =~ "configurable_from_database: true"
95     assert generated_config =~ "http: [ip: {127, 0, 0, 1}, port: 4000]"
96
97     assert generated_config =~
98              "filters: [Pleroma.Upload.Filter.Exiftool.StripLocation, Pleroma.Upload.Filter.Exiftool.ReadDescription]"
99
100     assert File.read!(tmp_path() <> "setup.psql") == generated_setup_psql()
101     assert File.exists?(Path.expand("./test/instance/static/robots.txt"))
102   end
103
104   defp generated_setup_psql do
105     ~s(CREATE USER dbuser WITH ENCRYPTED PASSWORD 'dbpass';\nCREATE DATABASE dbname OWNER dbuser;\n\\c dbname;\n--Extensions made by ecto.migrate that need superuser access\nCREATE EXTENSION IF NOT EXISTS citext;\nCREATE EXTENSION IF NOT EXISTS pg_trgm;\nCREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\nCREATE EXTENSION IF NOT EXISTS rum;\n)
106   end
107 end