total rebase
[anni] / test / pleroma / web / pleroma_api / controllers / mascot_controller_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 Pleroma.Web.PleromaAPI.MascotControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.UnstubbedConfigMock, as: ConfigMock
9   alias Pleroma.User
10
11   import Mox
12
13   test "mascot upload" do
14     %{conn: conn} = oauth_access(["write:accounts"])
15
16     non_image_file = %Plug.Upload{
17       content_type: "audio/mpeg",
18       path: Path.absname("test/fixtures/sound.mp3"),
19       filename: "sound.mp3"
20     }
21
22     ret_conn =
23       conn
24       |> put_req_header("content-type", "multipart/form-data")
25       |> put("/api/v1/pleroma/mascot", %{"file" => non_image_file})
26
27     assert json_response_and_validate_schema(ret_conn, 415)
28
29     file = %Plug.Upload{
30       content_type: "image/jpeg",
31       path: Path.absname("test/fixtures/image.jpg"),
32       filename: "an_image.jpg"
33     }
34
35     ConfigMock
36     |> stub_with(Pleroma.Test.StaticConfig)
37
38     conn =
39       conn
40       |> put_req_header("content-type", "multipart/form-data")
41       |> put("/api/v1/pleroma/mascot", %{"file" => file})
42
43     assert %{"id" => _, "type" => _image} = json_response_and_validate_schema(conn, 200)
44   end
45
46   test "mascot retrieving" do
47     %{user: user, conn: conn} = oauth_access(["read:accounts", "write:accounts"])
48
49     # When user hasn't set a mascot, we should just get pleroma tan back
50     ret_conn = get(conn, "/api/v1/pleroma/mascot")
51
52     assert %{"url" => url} = json_response_and_validate_schema(ret_conn, 200)
53     assert url =~ "pleroma-fox-tan-smol"
54
55     # When a user sets their mascot, we should get that back
56     file = %Plug.Upload{
57       content_type: "image/jpeg",
58       path: Path.absname("test/fixtures/image.jpg"),
59       filename: "an_image.jpg"
60     }
61
62     ConfigMock
63     |> stub_with(Pleroma.Test.StaticConfig)
64
65     ret_conn =
66       conn
67       |> put_req_header("content-type", "multipart/form-data")
68       |> put("/api/v1/pleroma/mascot", %{"file" => file})
69
70     assert json_response_and_validate_schema(ret_conn, 200)
71
72     user = User.get_cached_by_id(user.id)
73
74     conn =
75       conn
76       |> assign(:user, user)
77       |> get("/api/v1/pleroma/mascot")
78
79     assert %{"url" => url, "type" => "image"} = json_response_and_validate_schema(conn, 200)
80     assert url =~ "an_image"
81   end
82 end