logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

search_controller_test.exs (9158B)


      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.SearchControllerTest do
      6   use Pleroma.Web.ConnCase
      7 
      8   alias Pleroma.Object
      9   alias Pleroma.Web
     10   alias Pleroma.Web.CommonAPI
     11   import Pleroma.Factory
     12   import ExUnit.CaptureLog
     13   import Tesla.Mock
     14   import Mock
     15 
     16   setup do
     17     mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
     18     :ok
     19   end
     20 
     21   describe ".search2" do
     22     test "it returns empty result if user or status search return undefined error", %{conn: conn} do
     23       with_mocks [
     24         {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
     25         {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
     26       ] do
     27         capture_log(fn ->
     28           results =
     29             conn
     30             |> get("/api/v2/search", %{"q" => "2hu"})
     31             |> json_response(200)
     32 
     33           assert results["accounts"] == []
     34           assert results["statuses"] == []
     35         end) =~
     36           "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
     37       end
     38     end
     39 
     40     test "search", %{conn: conn} do
     41       user = insert(:user)
     42       user_two = insert(:user, %{nickname: "shp@shitposter.club"})
     43       user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
     44 
     45       {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu private"})
     46 
     47       {:ok, _activity} =
     48         CommonAPI.post(user, %{
     49           "status" => "This is about 2hu, but private",
     50           "visibility" => "private"
     51         })
     52 
     53       {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
     54 
     55       conn = get(conn, "/api/v2/search", %{"q" => "2hu #private"})
     56 
     57       assert results = json_response(conn, 200)
     58 
     59       [account | _] = results["accounts"]
     60       assert account["id"] == to_string(user_three.id)
     61 
     62       assert results["hashtags"] == [
     63                %{"name" => "private", "url" => "#{Web.base_url()}/tag/private"}
     64              ]
     65 
     66       [status] = results["statuses"]
     67       assert status["id"] == to_string(activity.id)
     68     end
     69   end
     70 
     71   describe ".account_search" do
     72     test "account search", %{conn: conn} do
     73       user = insert(:user)
     74       user_two = insert(:user, %{nickname: "shp@shitposter.club"})
     75       user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
     76 
     77       results =
     78         conn
     79         |> assign(:user, user)
     80         |> get("/api/v1/accounts/search", %{"q" => "shp"})
     81         |> json_response(200)
     82 
     83       result_ids = for result <- results, do: result["acct"]
     84 
     85       assert user_two.nickname in result_ids
     86       assert user_three.nickname in result_ids
     87 
     88       results =
     89         conn
     90         |> assign(:user, user)
     91         |> get("/api/v1/accounts/search", %{"q" => "2hu"})
     92         |> json_response(200)
     93 
     94       result_ids = for result <- results, do: result["acct"]
     95 
     96       assert user_three.nickname in result_ids
     97     end
     98 
     99     test "returns account if query contains a space", %{conn: conn} do
    100       user = insert(:user, %{nickname: "shp@shitposter.club"})
    101 
    102       results =
    103         conn
    104         |> assign(:user, user)
    105         |> get("/api/v1/accounts/search", %{"q" => "shp@shitposter.club xxx "})
    106         |> json_response(200)
    107 
    108       assert length(results) == 1
    109     end
    110   end
    111 
    112   describe ".search" do
    113     test "it returns empty result if user or status search return undefined error", %{conn: conn} do
    114       with_mocks [
    115         {Pleroma.User, [], [search: fn _q, _o -> raise "Oops" end]},
    116         {Pleroma.Activity, [], [search: fn _u, _q, _o -> raise "Oops" end]}
    117       ] do
    118         capture_log(fn ->
    119           results =
    120             conn
    121             |> get("/api/v1/search", %{"q" => "2hu"})
    122             |> json_response(200)
    123 
    124           assert results["accounts"] == []
    125           assert results["statuses"] == []
    126         end) =~
    127           "[error] Elixir.Pleroma.Web.MastodonAPI.SearchController search error: %RuntimeError{message: \"Oops\"}"
    128       end
    129     end
    130 
    131     test "search", %{conn: conn} do
    132       user = insert(:user)
    133       user_two = insert(:user, %{nickname: "shp@shitposter.club"})
    134       user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
    135 
    136       {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
    137 
    138       {:ok, _activity} =
    139         CommonAPI.post(user, %{
    140           "status" => "This is about 2hu, but private",
    141           "visibility" => "private"
    142         })
    143 
    144       {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
    145 
    146       conn =
    147         conn
    148         |> get("/api/v1/search", %{"q" => "2hu"})
    149 
    150       assert results = json_response(conn, 200)
    151 
    152       [account | _] = results["accounts"]
    153       assert account["id"] == to_string(user_three.id)
    154 
    155       assert results["hashtags"] == []
    156 
    157       [status] = results["statuses"]
    158       assert status["id"] == to_string(activity.id)
    159     end
    160 
    161     test "search fetches remote statuses", %{conn: conn} do
    162       capture_log(fn ->
    163         conn =
    164           conn
    165           |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
    166 
    167         assert results = json_response(conn, 200)
    168 
    169         [status] = results["statuses"]
    170 
    171         assert status["uri"] ==
    172                  "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
    173       end)
    174     end
    175 
    176     test "search doesn't show statuses that it shouldn't", %{conn: conn} do
    177       {:ok, activity} =
    178         CommonAPI.post(insert(:user), %{
    179           "status" => "This is about 2hu, but private",
    180           "visibility" => "private"
    181         })
    182 
    183       capture_log(fn ->
    184         conn =
    185           conn
    186           |> get("/api/v1/search", %{"q" => Object.normalize(activity).data["id"]})
    187 
    188         assert results = json_response(conn, 200)
    189 
    190         [] = results["statuses"]
    191       end)
    192     end
    193 
    194     test "search fetches remote accounts", %{conn: conn} do
    195       user = insert(:user)
    196 
    197       conn =
    198         conn
    199         |> assign(:user, user)
    200         |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
    201 
    202       assert results = json_response(conn, 200)
    203       [account] = results["accounts"]
    204       assert account["acct"] == "shp@social.heldscal.la"
    205     end
    206 
    207     test "search doesn't fetch remote accounts if resolve is false", %{conn: conn} do
    208       conn =
    209         conn
    210         |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "false"})
    211 
    212       assert results = json_response(conn, 200)
    213       assert [] == results["accounts"]
    214     end
    215 
    216     test "search with limit and offset", %{conn: conn} do
    217       user = insert(:user)
    218       _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
    219       _user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
    220 
    221       {:ok, _activity1} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
    222       {:ok, _activity2} = CommonAPI.post(user, %{"status" => "This is also about 2hu"})
    223 
    224       result =
    225         conn
    226         |> get("/api/v1/search", %{"q" => "2hu", "limit" => 1})
    227 
    228       assert results = json_response(result, 200)
    229       assert [%{"id" => activity_id1}] = results["statuses"]
    230       assert [_] = results["accounts"]
    231 
    232       results =
    233         conn
    234         |> get("/api/v1/search", %{"q" => "2hu", "limit" => 1, "offset" => 1})
    235         |> json_response(200)
    236 
    237       assert [%{"id" => activity_id2}] = results["statuses"]
    238       assert [] = results["accounts"]
    239 
    240       assert activity_id1 != activity_id2
    241     end
    242 
    243     test "search returns results only for the given type", %{conn: conn} do
    244       user = insert(:user)
    245       _user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
    246 
    247       {:ok, _activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
    248 
    249       assert %{"statuses" => [_activity], "accounts" => [], "hashtags" => []} =
    250                conn
    251                |> get("/api/v1/search", %{"q" => "2hu", "type" => "statuses"})
    252                |> json_response(200)
    253 
    254       assert %{"statuses" => [], "accounts" => [_user_two], "hashtags" => []} =
    255                conn
    256                |> get("/api/v1/search", %{"q" => "2hu", "type" => "accounts"})
    257                |> json_response(200)
    258     end
    259 
    260     test "search uses account_id to filter statuses by the author", %{conn: conn} do
    261       user = insert(:user, %{nickname: "shp@shitposter.club"})
    262       user_two = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
    263 
    264       {:ok, activity1} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
    265       {:ok, activity2} = CommonAPI.post(user_two, %{"status" => "This is also about 2hu"})
    266 
    267       results =
    268         conn
    269         |> get("/api/v1/search", %{"q" => "2hu", "account_id" => user.id})
    270         |> json_response(200)
    271 
    272       assert [%{"id" => activity_id1}] = results["statuses"]
    273       assert activity_id1 == activity1.id
    274       assert [_] = results["accounts"]
    275 
    276       results =
    277         conn
    278         |> get("/api/v1/search", %{"q" => "2hu", "account_id" => user_two.id})
    279         |> json_response(200)
    280 
    281       assert [%{"id" => activity_id2}] = results["statuses"]
    282       assert activity_id2 == activity2.id
    283     end
    284   end
    285 end