2019-10-01 09:45:04 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.Config
|
|
|
|
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
import Pleroma.Factory
|
|
|
|
import Tesla.Mock
|
|
|
|
|
2019-12-19 15:23:27 +01:00
|
|
|
setup do: oauth_access(["read"])
|
|
|
|
|
|
|
|
setup %{user: user} do
|
2019-10-01 09:45:04 +02:00
|
|
|
other_user = insert(:user)
|
|
|
|
host = Config.get([Pleroma.Web.Endpoint, :url, :host])
|
|
|
|
url500 = "http://test500?#{host}&#{user.nickname}"
|
|
|
|
url200 = "http://test200?#{host}&#{user.nickname}"
|
|
|
|
|
|
|
|
mock(fn
|
|
|
|
%{method: :get, url: ^url500} ->
|
|
|
|
%Tesla.Env{status: 500, body: "bad request"}
|
|
|
|
|
|
|
|
%{method: :get, url: ^url200} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
~s([{"acct":"yj455","avatar":"https://social.heldscal.la/avatar/201.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/201.jpeg"}, {"acct":"#{
|
|
|
|
other_user.ap_id
|
|
|
|
}","avatar":"https://social.heldscal.la/avatar/202.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/202.jpeg"}])
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2019-12-19 15:23:27 +01:00
|
|
|
[other_user: other_user]
|
2019-10-01 09:45:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
clear_config(:suggestions)
|
|
|
|
|
2019-12-19 15:23:27 +01:00
|
|
|
test "returns empty result when suggestions disabled", %{conn: conn} do
|
2019-10-01 09:45:04 +02:00
|
|
|
Config.put([:suggestions, :enabled], false)
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/suggestions")
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert res == []
|
|
|
|
end
|
|
|
|
|
2019-12-19 15:23:27 +01:00
|
|
|
test "returns error", %{conn: conn} do
|
2019-10-01 09:45:04 +02:00
|
|
|
Config.put([:suggestions, :enabled], true)
|
|
|
|
Config.put([:suggestions, :third_party_engine], "http://test500?{{host}}&{{user}}")
|
|
|
|
|
|
|
|
assert capture_log(fn ->
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/suggestions")
|
|
|
|
|> json_response(500)
|
|
|
|
|
|
|
|
assert res == "Something went wrong"
|
|
|
|
end) =~ "Could not retrieve suggestions"
|
|
|
|
end
|
|
|
|
|
2019-12-19 15:23:27 +01:00
|
|
|
test "returns suggestions", %{conn: conn, other_user: other_user} do
|
2019-10-01 09:45:04 +02:00
|
|
|
Config.put([:suggestions, :enabled], true)
|
|
|
|
Config.put([:suggestions, :third_party_engine], "http://test200?{{host}}&{{user}}")
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/suggestions")
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert res == [
|
|
|
|
%{
|
|
|
|
"acct" => "yj455",
|
|
|
|
"avatar" => "https://social.heldscal.la/avatar/201.jpeg",
|
|
|
|
"avatar_static" => "https://social.heldscal.la/avatar/s/201.jpeg",
|
|
|
|
"id" => 0
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"acct" => other_user.ap_id,
|
|
|
|
"avatar" => "https://social.heldscal.la/avatar/202.jpeg",
|
|
|
|
"avatar_static" => "https://social.heldscal.la/avatar/s/202.jpeg",
|
|
|
|
"id" => other_user.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|