feed_controller_test.exs (8355B)
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.Feed.FeedControllerTest do 6 use Pleroma.Web.ConnCase 7 8 import Pleroma.Factory 9 import SweetXml 10 11 alias Pleroma.Object 12 alias Pleroma.User 13 14 clear_config([:feed]) 15 16 test "gets a feed", %{conn: conn} do 17 Pleroma.Config.put( 18 [:feed, :post_title], 19 %{max_length: 10, omission: "..."} 20 ) 21 22 activity = insert(:note_activity) 23 24 note = 25 insert(:note, 26 data: %{ 27 "content" => "This is :moominmamma: note ", 28 "attachment" => [ 29 %{ 30 "url" => [%{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}] 31 } 32 ], 33 "inReplyTo" => activity.data["id"] 34 } 35 ) 36 37 note_activity = insert(:note_activity, note: note) 38 user = User.get_cached_by_ap_id(note_activity.data["actor"]) 39 40 note2 = 41 insert(:note, 42 user: user, 43 data: %{"content" => "42 This is :moominmamma: note ", "inReplyTo" => activity.data["id"]} 44 ) 45 46 _note_activity2 = insert(:note_activity, note: note2) 47 object = Object.normalize(note_activity) 48 49 resp = 50 conn 51 |> put_req_header("content-type", "application/atom+xml") 52 |> get("/users/#{user.nickname}/feed.atom") 53 |> response(200) 54 55 activity_titles = 56 resp 57 |> SweetXml.parse() 58 |> SweetXml.xpath(~x"//entry/title/text()"l) 59 60 assert activity_titles == ['42 This...', 'This is...'] 61 assert resp =~ object.data["content"] 62 end 63 64 test "returns 404 for a missing feed", %{conn: conn} do 65 conn = 66 conn 67 |> put_req_header("content-type", "application/atom+xml") 68 |> get("/users/nonexisting/feed.atom") 69 70 assert response(conn, 404) 71 end 72 73 describe "feed_redirect" do 74 test "undefined format. it redirects to feed", %{conn: conn} do 75 note_activity = insert(:note_activity) 76 user = User.get_cached_by_ap_id(note_activity.data["actor"]) 77 78 response = 79 conn 80 |> put_req_header("accept", "application/xml") 81 |> get("/users/#{user.nickname}") 82 |> response(302) 83 84 assert response == 85 "<html><body>You are being <a href=\"#{Pleroma.Web.base_url()}/users/#{ 86 user.nickname 87 }/feed.atom\">redirected</a>.</body></html>" 88 end 89 90 test "undefined format. it returns error when user not found", %{conn: conn} do 91 response = 92 conn 93 |> put_req_header("accept", "application/xml") 94 |> get("/users/jimm") 95 |> response(404) 96 97 assert response == ~S({"error":"Not found"}) 98 end 99 100 test "activity+json format. it redirects on actual feed of user", %{conn: conn} do 101 note_activity = insert(:note_activity) 102 user = User.get_cached_by_ap_id(note_activity.data["actor"]) 103 104 response = 105 conn 106 |> put_req_header("accept", "application/activity+json") 107 |> get("/users/#{user.nickname}") 108 |> json_response(200) 109 110 assert response["endpoints"] == %{ 111 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize", 112 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps", 113 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token", 114 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox", 115 "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media" 116 } 117 118 assert response["@context"] == [ 119 "https://www.w3.org/ns/activitystreams", 120 "http://localhost:4001/schemas/litepub-0.1.jsonld", 121 %{"@language" => "und"} 122 ] 123 124 assert Map.take(response, [ 125 "followers", 126 "following", 127 "id", 128 "inbox", 129 "manuallyApprovesFollowers", 130 "name", 131 "outbox", 132 "preferredUsername", 133 "summary", 134 "tag", 135 "type", 136 "url" 137 ]) == %{ 138 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers", 139 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following", 140 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}", 141 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox", 142 "manuallyApprovesFollowers" => false, 143 "name" => user.name, 144 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox", 145 "preferredUsername" => user.nickname, 146 "summary" => user.bio, 147 "tag" => [], 148 "type" => "Person", 149 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}" 150 } 151 end 152 153 test "activity+json format. it returns error whe use not found", %{conn: conn} do 154 response = 155 conn 156 |> put_req_header("accept", "application/activity+json") 157 |> get("/users/jimm") 158 |> json_response(404) 159 160 assert response == "Not found" 161 end 162 163 test "json format. it redirects on actual feed of user", %{conn: conn} do 164 note_activity = insert(:note_activity) 165 user = User.get_cached_by_ap_id(note_activity.data["actor"]) 166 167 response = 168 conn 169 |> put_req_header("accept", "application/json") 170 |> get("/users/#{user.nickname}") 171 |> json_response(200) 172 173 assert response["endpoints"] == %{ 174 "oauthAuthorizationEndpoint" => "#{Pleroma.Web.base_url()}/oauth/authorize", 175 "oauthRegistrationEndpoint" => "#{Pleroma.Web.base_url()}/api/v1/apps", 176 "oauthTokenEndpoint" => "#{Pleroma.Web.base_url()}/oauth/token", 177 "sharedInbox" => "#{Pleroma.Web.base_url()}/inbox", 178 "uploadMedia" => "#{Pleroma.Web.base_url()}/api/ap/upload_media" 179 } 180 181 assert response["@context"] == [ 182 "https://www.w3.org/ns/activitystreams", 183 "http://localhost:4001/schemas/litepub-0.1.jsonld", 184 %{"@language" => "und"} 185 ] 186 187 assert Map.take(response, [ 188 "followers", 189 "following", 190 "id", 191 "inbox", 192 "manuallyApprovesFollowers", 193 "name", 194 "outbox", 195 "preferredUsername", 196 "summary", 197 "tag", 198 "type", 199 "url" 200 ]) == %{ 201 "followers" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/followers", 202 "following" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/following", 203 "id" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}", 204 "inbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/inbox", 205 "manuallyApprovesFollowers" => false, 206 "name" => user.name, 207 "outbox" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}/outbox", 208 "preferredUsername" => user.nickname, 209 "summary" => user.bio, 210 "tag" => [], 211 "type" => "Person", 212 "url" => "#{Pleroma.Web.base_url()}/users/#{user.nickname}" 213 } 214 end 215 216 test "json format. it returns error whe use not found", %{conn: conn} do 217 response = 218 conn 219 |> put_req_header("accept", "application/json") 220 |> get("/users/jimm") 221 |> json_response(404) 222 223 assert response == "Not found" 224 end 225 226 test "html format. it redirects on actual feed of user", %{conn: conn} do 227 note_activity = insert(:note_activity) 228 user = User.get_cached_by_ap_id(note_activity.data["actor"]) 229 230 response = 231 conn 232 |> get("/users/#{user.nickname}") 233 |> response(200) 234 235 assert response == 236 Fallback.RedirectController.redirector_with_meta( 237 conn, 238 %{user: user} 239 ).resp_body 240 end 241 242 test "html format. it returns error when user not found", %{conn: conn} do 243 response = 244 conn 245 |> get("/users/jimm") 246 |> json_response(404) 247 248 assert response == %{"error" => "Not found"} 249 end 250 end 251 end