Merge branch 'develop' into issue/1383
[akkoma] / test / web / mastodon_api / controllers / suggestion_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9
10 import ExUnit.CaptureLog
11 import Pleroma.Factory
12 import Tesla.Mock
13
14 setup do
15 user = insert(:user)
16 other_user = insert(:user)
17 host = Config.get([Pleroma.Web.Endpoint, :url, :host])
18 url500 = "http://test500?#{host}&#{user.nickname}"
19 url200 = "http://test200?#{host}&#{user.nickname}"
20
21 mock(fn
22 %{method: :get, url: ^url500} ->
23 %Tesla.Env{status: 500, body: "bad request"}
24
25 %{method: :get, url: ^url200} ->
26 %Tesla.Env{
27 status: 200,
28 body:
29 ~s([{"acct":"yj455","avatar":"https://social.heldscal.la/avatar/201.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/201.jpeg"}, {"acct":"#{
30 other_user.ap_id
31 }","avatar":"https://social.heldscal.la/avatar/202.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/202.jpeg"}])
32 }
33 end)
34
35 [user: user, other_user: other_user]
36 end
37
38 clear_config(:suggestions)
39
40 test "returns empty result when suggestions disabled", %{conn: conn, user: user} do
41 Config.put([:suggestions, :enabled], false)
42
43 res =
44 conn
45 |> assign(:user, user)
46 |> get("/api/v1/suggestions")
47 |> json_response(200)
48
49 assert res == []
50 end
51
52 test "returns error", %{conn: conn, user: user} do
53 Config.put([:suggestions, :enabled], true)
54 Config.put([:suggestions, :third_party_engine], "http://test500?{{host}}&{{user}}")
55
56 assert capture_log(fn ->
57 res =
58 conn
59 |> assign(:user, user)
60 |> get("/api/v1/suggestions")
61 |> json_response(500)
62
63 assert res == "Something went wrong"
64 end) =~ "Could not retrieve suggestions"
65 end
66
67 test "returns suggestions", %{conn: conn, user: user, other_user: other_user} do
68 Config.put([:suggestions, :enabled], true)
69 Config.put([:suggestions, :third_party_engine], "http://test200?{{host}}&{{user}}")
70
71 res =
72 conn
73 |> assign(:user, user)
74 |> get("/api/v1/suggestions")
75 |> json_response(200)
76
77 assert res == [
78 %{
79 "acct" => "yj455",
80 "avatar" => "https://social.heldscal.la/avatar/201.jpeg",
81 "avatar_static" => "https://social.heldscal.la/avatar/s/201.jpeg",
82 "id" => 0
83 },
84 %{
85 "acct" => other_user.ap_id,
86 "avatar" => "https://social.heldscal.la/avatar/202.jpeg",
87 "avatar_static" => "https://social.heldscal.la/avatar/s/202.jpeg",
88 "id" => other_user.id
89 }
90 ]
91 end
92 end