Merge branch 'develop' into issue/1280
[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: oauth_access(["read"])
15
16 setup %{user: user} do
17 other_user = insert(:user)
18 host = Config.get([Pleroma.Web.Endpoint, :url, :host])
19 url500 = "http://test500?#{host}&#{user.nickname}"
20 url200 = "http://test200?#{host}&#{user.nickname}"
21
22 mock(fn
23 %{method: :get, url: ^url500} ->
24 %Tesla.Env{status: 500, body: "bad request"}
25
26 %{method: :get, url: ^url200} ->
27 %Tesla.Env{
28 status: 200,
29 body:
30 ~s([{"acct":"yj455","avatar":"https://social.heldscal.la/avatar/201.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/201.jpeg"}, {"acct":"#{
31 other_user.ap_id
32 }","avatar":"https://social.heldscal.la/avatar/202.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/202.jpeg"}])
33 }
34 end)
35
36 [other_user: other_user]
37 end
38
39 clear_config(:suggestions)
40
41 test "returns empty result when suggestions disabled", %{conn: conn} do
42 Config.put([:suggestions, :enabled], false)
43
44 res =
45 conn
46 |> get("/api/v1/suggestions")
47 |> json_response(200)
48
49 assert res == []
50 end
51
52 test "returns error", %{conn: conn} 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 |> get("/api/v1/suggestions")
60 |> json_response(500)
61
62 assert res == "Something went wrong"
63 end) =~ "Could not retrieve suggestions"
64 end
65
66 test "returns suggestions", %{conn: conn, other_user: other_user} do
67 Config.put([:suggestions, :enabled], true)
68 Config.put([:suggestions, :third_party_engine], "http://test200?{{host}}&{{user}}")
69
70 res =
71 conn
72 |> get("/api/v1/suggestions")
73 |> json_response(200)
74
75 assert res == [
76 %{
77 "acct" => "yj455",
78 "avatar" => "https://social.heldscal.la/avatar/201.jpeg",
79 "avatar_static" => "https://social.heldscal.la/avatar/s/201.jpeg",
80 "id" => 0
81 },
82 %{
83 "acct" => other_user.ap_id,
84 "avatar" => "https://social.heldscal.la/avatar/202.jpeg",
85 "avatar_static" => "https://social.heldscal.la/avatar/s/202.jpeg",
86 "id" => other_user.id
87 }
88 ]
89 end
90 end