summaryrefslogtreecommitdiff
path: root/test/mix/tasks/pleroma/emoji_test.exs
blob: 2e10bbdc8484bed720ed7fb86c5fdef8d9f367e7 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Mix.Tasks.Pleroma.EmojiTest do
  use ExUnit.Case, async: true

  import ExUnit.CaptureIO
  import Tesla.Mock

  alias Mix.Tasks.Pleroma.Emoji

  describe "ls-packs" do
    test "with default manifest as url" do
      mock(fn
        %{
          method: :get,
          url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
          }
      end)

      capture_io(fn -> Emoji.run(["ls-packs"]) end) =~
        "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
    end

    test "with passed manifest as file" do
      capture_io(fn ->
        Emoji.run(["ls-packs", "-m", "test/fixtures/emoji/packs/manifest.json"])
      end) =~ "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
    end
  end

  describe "get-packs" do
    test "download pack from default manifest" do
      mock(fn
        %{
          method: :get,
          url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
          }

        %{
          method: :get,
          url: "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
          }

        %{
          method: :get,
          url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/finmoji.json"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/finmoji.json")
          }
      end)

      assert capture_io(fn -> Emoji.run(["get-packs", "finmoji"]) end) =~ "Writing pack.json for"

      emoji_path =
        Path.join(
          Pleroma.Config.get!([:instance, :static_dir]),
          "emoji"
        )

      assert File.exists?(Path.join([emoji_path, "finmoji", "pack.json"]))
      on_exit(fn -> File.rm_rf!("test/instance_static/emoji/finmoji") end)
    end

    test "install local emoji pack" do
      assert capture_io(fn ->
               Emoji.run([
                 "get-packs",
                 "local",
                 "--manifest",
                 "test/instance_static/local_pack/manifest.json"
               ])
             end) =~ "Writing pack.json for"

      on_exit(fn -> File.rm_rf!("test/instance_static/emoji/local") end)
    end

    test "pack not found" do
      mock(fn
        %{
          method: :get,
          url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
          }
      end)

      assert capture_io(fn -> Emoji.run(["get-packs", "not_found"]) end) =~
               "No pack named \"not_found\" found"
    end

    test "raise on bad sha256" do
      mock(fn
        %{
          method: :get,
          url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
        } ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
          }
      end)

      assert_raise RuntimeError, ~r/^Bad SHA256 for blobs.gg/, fn ->
        capture_io(fn ->
          Emoji.run(["get-packs", "blobs.gg", "-m", "test/fixtures/emoji/packs/manifest.json"])
        end)
      end
    end
  end

  describe "gen-pack" do
    setup do
      url = "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"

      mock(fn %{
                method: :get,
                url: ^url
              } ->
        %Tesla.Env{status: 200, body: File.read!("test/fixtures/emoji/packs/blank.png.zip")}
      end)

      {:ok, url: url}
    end

    test "with default extensions", %{url: url} do
      name = "pack1"
      pack_json = "#{name}.json"
      files_json = "#{name}_file.json"
      refute File.exists?(pack_json)
      refute File.exists?(files_json)

      captured =
        capture_io(fn ->
          Emoji.run([
            "gen-pack",
            url,
            "--name",
            name,
            "--license",
            "license",
            "--homepage",
            "homepage",
            "--description",
            "description",
            "--files",
            files_json,
            "--extensions",
            ".png .gif"
          ])
        end)

      assert captured =~ "#{pack_json} has been created with the pack1 pack"
      assert captured =~ "Using .png .gif extensions"

      assert File.exists?(pack_json)
      assert File.exists?(files_json)

      on_exit(fn ->
        File.rm!(pack_json)
        File.rm!(files_json)
      end)
    end

    test "with custom extensions and update existing files", %{url: url} do
      name = "pack2"
      pack_json = "#{name}.json"
      files_json = "#{name}_file.json"
      refute File.exists?(pack_json)
      refute File.exists?(files_json)

      captured =
        capture_io(fn ->
          Emoji.run([
            "gen-pack",
            url,
            "--name",
            name,
            "--license",
            "license",
            "--homepage",
            "homepage",
            "--description",
            "description",
            "--files",
            files_json,
            "--extensions",
            " .png   .gif    .jpeg "
          ])
        end)

      assert captured =~ "#{pack_json} has been created with the pack2 pack"
      assert captured =~ "Using .png .gif .jpeg extensions"

      assert File.exists?(pack_json)
      assert File.exists?(files_json)

      captured =
        capture_io(fn ->
          Emoji.run([
            "gen-pack",
            url,
            "--name",
            name,
            "--license",
            "license",
            "--homepage",
            "homepage",
            "--description",
            "description",
            "--files",
            files_json,
            "--extensions",
            " .png   .gif    .jpeg "
          ])
        end)

      assert captured =~ "#{pack_json} has been updated with the pack2 pack"

      on_exit(fn ->
        File.rm!(pack_json)
        File.rm!(files_json)
      end)
    end
  end
end