Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1406 lines
37KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule HttpRequestMock do
  5. require Logger
  6. def activitypub_object_headers, do: [{"content-type", "application/activity+json"}]
  7. def request(
  8. %Tesla.Env{
  9. url: url,
  10. method: method,
  11. headers: headers,
  12. query: query,
  13. body: body
  14. } = _env
  15. ) do
  16. with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
  17. res
  18. else
  19. error ->
  20. with {:error, message} <- error do
  21. Logger.warn(to_string(message))
  22. end
  23. {_, _r} = error
  24. end
  25. end
  26. # GET Requests
  27. #
  28. def get(url, query \\ [], body \\ [], headers \\ [])
  29. def get("https://osada.macgirvin.com/channel/mike", _, _, _) do
  30. {:ok,
  31. %Tesla.Env{
  32. status: 200,
  33. body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com_channel_mike.json"),
  34. headers: activitypub_object_headers()
  35. }}
  36. end
  37. def get("https://shitposter.club/users/moonman", _, _, _) do
  38. {:ok,
  39. %Tesla.Env{
  40. status: 200,
  41. body: File.read!("test/fixtures/tesla_mock/moonman@shitposter.club.json"),
  42. headers: activitypub_object_headers()
  43. }}
  44. end
  45. def get("https://mastodon.social/users/emelie/statuses/101849165031453009", _, _, _) do
  46. {:ok,
  47. %Tesla.Env{
  48. status: 200,
  49. body: File.read!("test/fixtures/tesla_mock/status.emelie.json"),
  50. headers: activitypub_object_headers()
  51. }}
  52. end
  53. def get("https://mastodon.social/users/emelie/statuses/101849165031453404", _, _, _) do
  54. {:ok,
  55. %Tesla.Env{
  56. status: 404,
  57. body: ""
  58. }}
  59. end
  60. def get("https://mastodon.social/users/emelie", _, _, _) do
  61. {:ok,
  62. %Tesla.Env{
  63. status: 200,
  64. body: File.read!("test/fixtures/tesla_mock/emelie.json"),
  65. headers: activitypub_object_headers()
  66. }}
  67. end
  68. def get("https://mastodon.social/users/not_found", _, _, _) do
  69. {:ok, %Tesla.Env{status: 404}}
  70. end
  71. def get("https://mastodon.sdf.org/users/rinpatch", _, _, _) do
  72. {:ok,
  73. %Tesla.Env{
  74. status: 200,
  75. body: File.read!("test/fixtures/tesla_mock/rinpatch.json"),
  76. headers: activitypub_object_headers()
  77. }}
  78. end
  79. def get("https://mastodon.sdf.org/users/rinpatch/collections/featured", _, _, _) do
  80. {:ok,
  81. %Tesla.Env{
  82. status: 200,
  83. body:
  84. File.read!("test/fixtures/users_mock/masto_featured.json")
  85. |> String.replace("{{domain}}", "mastodon.sdf.org")
  86. |> String.replace("{{nickname}}", "rinpatch"),
  87. headers: [{"content-type", "application/activity+json"}]
  88. }}
  89. end
  90. def get("https://patch.cx/objects/tesla_mock/poll_attachment", _, _, _) do
  91. {:ok,
  92. %Tesla.Env{
  93. status: 200,
  94. body: File.read!("test/fixtures/tesla_mock/poll_attachment.json"),
  95. headers: activitypub_object_headers()
  96. }}
  97. end
  98. def get(
  99. "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/emelie",
  100. _,
  101. _,
  102. _
  103. ) do
  104. {:ok,
  105. %Tesla.Env{
  106. status: 200,
  107. body: File.read!("test/fixtures/tesla_mock/webfinger_emelie.json"),
  108. headers: activitypub_object_headers()
  109. }}
  110. end
  111. def get(
  112. "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
  113. _,
  114. _,
  115. [{"accept", "application/xrd+xml,application/jrd+json"}]
  116. ) do
  117. {:ok,
  118. %Tesla.Env{
  119. status: 200,
  120. body: File.read!("test/fixtures/tesla_mock/mike@osada.macgirvin.com.json"),
  121. headers: [{"content-type", "application/jrd+json"}]
  122. }}
  123. end
  124. def get(
  125. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
  126. _,
  127. _,
  128. [{"accept", "application/xrd+xml,application/jrd+json"}]
  129. ) do
  130. {:ok,
  131. %Tesla.Env{
  132. status: 200,
  133. body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_29191.xml")
  134. }}
  135. end
  136. def get(
  137. "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
  138. _,
  139. _,
  140. [{"accept", "application/xrd+xml,application/jrd+json"}]
  141. ) do
  142. {:ok,
  143. %Tesla.Env{
  144. status: 200,
  145. body: File.read!("test/fixtures/tesla_mock/https___pawoo.net_users_pekorino.xml")
  146. }}
  147. end
  148. def get(
  149. "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
  150. _,
  151. _,
  152. [{"accept", "application/xrd+xml,application/jrd+json"}]
  153. ) do
  154. {:ok,
  155. %Tesla.Env{
  156. status: 200,
  157. body: File.read!("test/fixtures/tesla_mock/atarifrosch_webfinger.xml")
  158. }}
  159. end
  160. def get(
  161. "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
  162. _,
  163. _,
  164. [{"accept", "application/xrd+xml,application/jrd+json"}]
  165. ) do
  166. {:ok,
  167. %Tesla.Env{
  168. status: 200,
  169. body: File.read!("test/fixtures/tesla_mock/nonexistant@social.heldscal.la.xml")
  170. }}
  171. end
  172. def get(
  173. "https://squeet.me/xrd/?uri=acct:lain@squeet.me",
  174. _,
  175. _,
  176. [{"accept", "application/xrd+xml,application/jrd+json"}]
  177. ) do
  178. {:ok,
  179. %Tesla.Env{
  180. status: 200,
  181. body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml"),
  182. headers: [{"content-type", "application/xrd+xml"}]
  183. }}
  184. end
  185. def get(
  186. "https://mst3k.interlinked.me/users/luciferMysticus",
  187. _,
  188. _,
  189. [{"accept", "application/activity+json"}]
  190. ) do
  191. {:ok,
  192. %Tesla.Env{
  193. status: 200,
  194. body: File.read!("test/fixtures/tesla_mock/lucifermysticus.json"),
  195. headers: activitypub_object_headers()
  196. }}
  197. end
  198. def get("https://prismo.news/@mxb", _, _, _) do
  199. {:ok,
  200. %Tesla.Env{
  201. status: 200,
  202. body: File.read!("test/fixtures/tesla_mock/https___prismo.news__mxb.json"),
  203. headers: activitypub_object_headers()
  204. }}
  205. end
  206. def get(
  207. "https://hubzilla.example.org/channel/kaniini",
  208. _,
  209. _,
  210. [{"accept", "application/activity+json"}]
  211. ) do
  212. {:ok,
  213. %Tesla.Env{
  214. status: 200,
  215. body: File.read!("test/fixtures/tesla_mock/kaniini@hubzilla.example.org.json"),
  216. headers: activitypub_object_headers()
  217. }}
  218. end
  219. def get("https://niu.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
  220. {:ok,
  221. %Tesla.Env{
  222. status: 200,
  223. body: File.read!("test/fixtures/tesla_mock/rye.json"),
  224. headers: activitypub_object_headers()
  225. }}
  226. end
  227. def get("https://n1u.moe/users/rye", _, _, [{"accept", "application/activity+json"}]) do
  228. {:ok,
  229. %Tesla.Env{
  230. status: 200,
  231. body: File.read!("test/fixtures/tesla_mock/rye.json"),
  232. headers: activitypub_object_headers()
  233. }}
  234. end
  235. def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _, _, _) do
  236. {:ok,
  237. %Tesla.Env{
  238. status: 200,
  239. body:
  240. File.read!(
  241. "test/fixtures/tesla_mock/http___mastodon.example.org_users_admin_status_1234.json"
  242. )
  243. }}
  244. end
  245. def get("https://puckipedia.com/", _, _, [{"accept", "application/activity+json"}]) do
  246. {:ok,
  247. %Tesla.Env{
  248. status: 200,
  249. body: File.read!("test/fixtures/tesla_mock/puckipedia.com.json"),
  250. headers: activitypub_object_headers()
  251. }}
  252. end
  253. def get("https://peertube.moe/accounts/7even", _, _, _) do
  254. {:ok,
  255. %Tesla.Env{
  256. status: 200,
  257. body: File.read!("test/fixtures/tesla_mock/7even.json"),
  258. headers: activitypub_object_headers()
  259. }}
  260. end
  261. def get("https://peertube.stream/accounts/createurs", _, _, _) do
  262. {:ok,
  263. %Tesla.Env{
  264. status: 200,
  265. body: File.read!("test/fixtures/peertube/actor-person.json"),
  266. headers: activitypub_object_headers()
  267. }}
  268. end
  269. def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3", _, _, _) do
  270. {:ok,
  271. %Tesla.Env{
  272. status: 200,
  273. body: File.read!("test/fixtures/tesla_mock/peertube.moe-vid.json"),
  274. headers: activitypub_object_headers()
  275. }}
  276. end
  277. def get("https://framatube.org/accounts/framasoft", _, _, _) do
  278. {:ok,
  279. %Tesla.Env{
  280. status: 200,
  281. body: File.read!("test/fixtures/tesla_mock/https___framatube.org_accounts_framasoft.json"),
  282. headers: activitypub_object_headers()
  283. }}
  284. end
  285. def get("https://framatube.org/videos/watch/6050732a-8a7a-43d4-a6cd-809525a1d206", _, _, _) do
  286. {:ok,
  287. %Tesla.Env{
  288. status: 200,
  289. body: File.read!("test/fixtures/tesla_mock/framatube.org-video.json"),
  290. headers: activitypub_object_headers()
  291. }}
  292. end
  293. def get("https://peertube.social/accounts/craigmaloney", _, _, _) do
  294. {:ok,
  295. %Tesla.Env{
  296. status: 200,
  297. body: File.read!("test/fixtures/tesla_mock/craigmaloney.json"),
  298. headers: activitypub_object_headers()
  299. }}
  300. end
  301. def get("https://peertube.social/videos/watch/278d2b7c-0f38-4aaa-afe6-9ecc0c4a34fe", _, _, _) do
  302. {:ok,
  303. %Tesla.Env{
  304. status: 200,
  305. body: File.read!("test/fixtures/tesla_mock/peertube-social.json"),
  306. headers: activitypub_object_headers()
  307. }}
  308. end
  309. def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _, _, [
  310. {"accept", "application/activity+json"}
  311. ]) do
  312. {:ok,
  313. %Tesla.Env{
  314. status: 200,
  315. body: File.read!("test/fixtures/tesla_mock/mobilizon.org-event.json"),
  316. headers: activitypub_object_headers()
  317. }}
  318. end
  319. def get("https://mobilizon.org/@tcit", _, _, [{"accept", "application/activity+json"}]) do
  320. {:ok,
  321. %Tesla.Env{
  322. status: 200,
  323. body: File.read!("test/fixtures/tesla_mock/mobilizon.org-user.json"),
  324. headers: activitypub_object_headers()
  325. }}
  326. end
  327. def get("https://baptiste.gelez.xyz/@/BaptisteGelez", _, _, _) do
  328. {:ok,
  329. %Tesla.Env{
  330. status: 200,
  331. body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-user.json"),
  332. headers: activitypub_object_headers()
  333. }}
  334. end
  335. def get("https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/", _, _, _) do
  336. {:ok,
  337. %Tesla.Env{
  338. status: 200,
  339. body: File.read!("test/fixtures/tesla_mock/baptiste.gelex.xyz-article.json"),
  340. headers: activitypub_object_headers()
  341. }}
  342. end
  343. def get("https://wedistribute.org/wp-json/pterotype/v1/object/85810", _, _, _) do
  344. {:ok,
  345. %Tesla.Env{
  346. status: 200,
  347. body: File.read!("test/fixtures/tesla_mock/wedistribute-article.json"),
  348. headers: activitypub_object_headers()
  349. }}
  350. end
  351. def get("https://wedistribute.org/wp-json/pterotype/v1/actor/-blog", _, _, _) do
  352. {:ok,
  353. %Tesla.Env{
  354. status: 200,
  355. body: File.read!("test/fixtures/tesla_mock/wedistribute-user.json"),
  356. headers: activitypub_object_headers()
  357. }}
  358. end
  359. def get("http://mastodon.example.org/users/admin", _, _, _) do
  360. {:ok,
  361. %Tesla.Env{
  362. status: 200,
  363. body: File.read!("test/fixtures/tesla_mock/admin@mastdon.example.org.json"),
  364. headers: activitypub_object_headers()
  365. }}
  366. end
  367. def get("http://mastodon.example.org/users/relay", _, _, [
  368. {"accept", "application/activity+json"}
  369. ]) do
  370. {:ok,
  371. %Tesla.Env{
  372. status: 200,
  373. body: File.read!("test/fixtures/tesla_mock/relay@mastdon.example.org.json"),
  374. headers: activitypub_object_headers()
  375. }}
  376. end
  377. def get("http://mastodon.example.org/users/gargron", _, _, [
  378. {"accept", "application/activity+json"}
  379. ]) do
  380. {:error, :nxdomain}
  381. end
  382. def get("http://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
  383. {:ok,
  384. %Tesla.Env{
  385. status: 404,
  386. body: ""
  387. }}
  388. end
  389. def get("https://osada.macgirvin.com/.well-known/host-meta", _, _, _) do
  390. {:ok,
  391. %Tesla.Env{
  392. status: 404,
  393. body: ""
  394. }}
  395. end
  396. def get("http://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
  397. {:ok,
  398. %Tesla.Env{
  399. status: 200,
  400. body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
  401. }}
  402. end
  403. def get("https://mastodon.sdf.org/.well-known/host-meta", _, _, _) do
  404. {:ok,
  405. %Tesla.Env{
  406. status: 200,
  407. body: File.read!("test/fixtures/tesla_mock/sdf.org_host_meta")
  408. }}
  409. end
  410. def get(
  411. "https://mastodon.sdf.org/.well-known/webfinger?resource=https://mastodon.sdf.org/users/snowdusk",
  412. _,
  413. _,
  414. _
  415. ) do
  416. {:ok,
  417. %Tesla.Env{
  418. status: 200,
  419. body: File.read!("test/fixtures/tesla_mock/snowdusk@sdf.org_host_meta.json")
  420. }}
  421. end
  422. def get("http://mstdn.jp/.well-known/host-meta", _, _, _) do
  423. {:ok,
  424. %Tesla.Env{
  425. status: 200,
  426. body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
  427. }}
  428. end
  429. def get("https://mstdn.jp/.well-known/host-meta", _, _, _) do
  430. {:ok,
  431. %Tesla.Env{
  432. status: 200,
  433. body: File.read!("test/fixtures/tesla_mock/mstdn.jp_host_meta")
  434. }}
  435. end
  436. def get("https://mstdn.jp/.well-known/webfinger?resource=kpherox@mstdn.jp", _, _, _) do
  437. {:ok,
  438. %Tesla.Env{
  439. status: 200,
  440. body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml")
  441. }}
  442. end
  443. def get("http://mamot.fr/.well-known/host-meta", _, _, _) do
  444. {:ok,
  445. %Tesla.Env{
  446. status: 200,
  447. body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
  448. }}
  449. end
  450. def get("https://mamot.fr/.well-known/host-meta", _, _, _) do
  451. {:ok,
  452. %Tesla.Env{
  453. status: 200,
  454. body: File.read!("test/fixtures/tesla_mock/mamot.fr_host_meta")
  455. }}
  456. end
  457. def get("http://pawoo.net/.well-known/host-meta", _, _, _) do
  458. {:ok,
  459. %Tesla.Env{
  460. status: 200,
  461. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  462. }}
  463. end
  464. def get("https://pawoo.net/.well-known/host-meta", _, _, _) do
  465. {:ok,
  466. %Tesla.Env{
  467. status: 200,
  468. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  469. }}
  470. end
  471. def get(
  472. "https://pawoo.net/.well-known/webfinger?resource=https://pawoo.net/users/pekorino",
  473. _,
  474. _,
  475. _
  476. ) do
  477. {:ok,
  478. %Tesla.Env{
  479. status: 200,
  480. body: File.read!("test/fixtures/tesla_mock/pekorino@pawoo.net_host_meta.json"),
  481. headers: activitypub_object_headers()
  482. }}
  483. end
  484. def get("http://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
  485. {:ok,
  486. %Tesla.Env{
  487. status: 200,
  488. body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
  489. }}
  490. end
  491. def get("https://pleroma.soykaf.com/.well-known/host-meta", _, _, _) do
  492. {:ok,
  493. %Tesla.Env{
  494. status: 200,
  495. body: File.read!("test/fixtures/tesla_mock/soykaf.com_host_meta")
  496. }}
  497. end
  498. def get("http://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
  499. {:ok,
  500. %Tesla.Env{
  501. status: 200,
  502. body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
  503. }}
  504. end
  505. def get("https://social.stopwatchingus-heidelberg.de/.well-known/host-meta", _, _, _) do
  506. {:ok,
  507. %Tesla.Env{
  508. status: 200,
  509. body: File.read!("test/fixtures/tesla_mock/stopwatchingus-heidelberg.de_host_meta")
  510. }}
  511. end
  512. def get(
  513. "http://mastodon.example.org/@admin/99541947525187367",
  514. _,
  515. _,
  516. _
  517. ) do
  518. {:ok,
  519. %Tesla.Env{
  520. status: 200,
  521. body: File.read!("test/fixtures/mastodon-note-object.json"),
  522. headers: activitypub_object_headers()
  523. }}
  524. end
  525. def get("http://mastodon.example.org/@admin/99541947525187368", _, _, _) do
  526. {:ok,
  527. %Tesla.Env{
  528. status: 404,
  529. body: ""
  530. }}
  531. end
  532. def get("https://shitposter.club/notice/7369654", _, _, _) do
  533. {:ok,
  534. %Tesla.Env{
  535. status: 200,
  536. body: File.read!("test/fixtures/tesla_mock/7369654.html")
  537. }}
  538. end
  539. def get("https://mstdn.io/users/mayuutann", _, _, [{"accept", "application/activity+json"}]) do
  540. {:ok,
  541. %Tesla.Env{
  542. status: 200,
  543. body: File.read!("test/fixtures/tesla_mock/mayumayu.json"),
  544. headers: activitypub_object_headers()
  545. }}
  546. end
  547. def get(
  548. "https://mstdn.io/users/mayuutann/statuses/99568293732299394",
  549. _,
  550. _,
  551. [{"accept", "application/activity+json"}]
  552. ) do
  553. {:ok,
  554. %Tesla.Env{
  555. status: 200,
  556. body: File.read!("test/fixtures/tesla_mock/mayumayupost.json"),
  557. headers: activitypub_object_headers()
  558. }}
  559. end
  560. def get(url, _, _, [{"accept", "application/xrd+xml,application/jrd+json"}])
  561. when url in [
  562. "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
  563. "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
  564. ] do
  565. {:ok,
  566. %Tesla.Env{
  567. status: 200,
  568. body: File.read!("test/fixtures/tesla_mock/https___pleroma.soykaf.com_users_lain.xml")
  569. }}
  570. end
  571. def get(
  572. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
  573. _,
  574. _,
  575. [{"accept", "application/xrd+xml,application/jrd+json"}]
  576. ) do
  577. {:ok,
  578. %Tesla.Env{
  579. status: 200,
  580. body: File.read!("test/fixtures/tesla_mock/https___shitposter.club_user_1.xml")
  581. }}
  582. end
  583. def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
  584. {:ok, %Tesla.Env{status: 200}}
  585. end
  586. def get(
  587. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
  588. _,
  589. _,
  590. [{"accept", "application/xrd+xml,application/jrd+json"}]
  591. ) do
  592. {:ok,
  593. %Tesla.Env{
  594. status: 200,
  595. body: File.read!("test/fixtures/tesla_mock/spc_5381_xrd.xml")
  596. }}
  597. end
  598. def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
  599. {:ok,
  600. %Tesla.Env{
  601. status: 200,
  602. body: File.read!("test/fixtures/tesla_mock/shitposter.club_host_meta")
  603. }}
  604. end
  605. def get("https://shitposter.club/notice/4027863", _, _, _) do
  606. {:ok,
  607. %Tesla.Env{
  608. status: 200,
  609. body: File.read!("test/fixtures/tesla_mock/7369654.html")
  610. }}
  611. end
  612. def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
  613. {:ok,
  614. %Tesla.Env{
  615. status: 200,
  616. body: File.read!("test/fixtures/tesla_mock/social.sakamoto.gq_host_meta")
  617. }}
  618. end
  619. def get(
  620. "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
  621. _,
  622. _,
  623. [{"accept", "application/xrd+xml,application/jrd+json"}]
  624. ) do
  625. {:ok,
  626. %Tesla.Env{
  627. status: 200,
  628. body: File.read!("test/fixtures/tesla_mock/eal_sakamoto.xml")
  629. }}
  630. end
  631. def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
  632. {:ok,
  633. %Tesla.Env{
  634. status: 200,
  635. body: File.read!("test/fixtures/tesla_mock/mastodon.social_host_meta")
  636. }}
  637. end
  638. def get(
  639. "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
  640. _,
  641. _,
  642. [{"accept", "application/xrd+xml,application/jrd+json"}]
  643. ) do
  644. {:ok,
  645. %Tesla.Env{
  646. status: 200,
  647. body:
  648. File.read!("test/fixtures/tesla_mock/https___mastodon.social_users_lambadalambda.xml")
  649. }}
  650. end
  651. def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
  652. {:ok,
  653. %Tesla.Env{
  654. status: 200,
  655. body: File.read!("test/fixtures/tesla_mock/gs.example.org_host_meta")
  656. }}
  657. end
  658. def get(
  659. "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
  660. _,
  661. _,
  662. [{"accept", "application/xrd+xml,application/jrd+json"}]
  663. ) do
  664. {:ok,
  665. %Tesla.Env{
  666. status: 200,
  667. body:
  668. File.read!("test/fixtures/tesla_mock/http___gs.example.org_4040_index.php_user_1.xml")
  669. }}
  670. end
  671. def get(
  672. "http://gs.example.org:4040/index.php/user/1",
  673. _,
  674. _,
  675. [{"accept", "application/activity+json"}]
  676. ) do
  677. {:ok, %Tesla.Env{status: 406, body: ""}}
  678. end
  679. def get("http://squeet.me/.well-known/host-meta", _, _, _) do
  680. {:ok,
  681. %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/squeet.me_host_meta")}}
  682. end
  683. def get(
  684. "https://squeet.me/xrd?uri=lain@squeet.me",
  685. _,
  686. _,
  687. [{"accept", "application/xrd+xml,application/jrd+json"}]
  688. ) do
  689. {:ok,
  690. %Tesla.Env{
  691. status: 200,
  692. body: File.read!("test/fixtures/tesla_mock/lain_squeet.me_webfinger.xml")
  693. }}
  694. end
  695. def get(
  696. "https://social.heldscal.la/.well-known/webfinger?resource=acct:shp@social.heldscal.la",
  697. _,
  698. _,
  699. [{"accept", "application/xrd+xml,application/jrd+json"}]
  700. ) do
  701. {:ok,
  702. %Tesla.Env{
  703. status: 200,
  704. body: File.read!("test/fixtures/tesla_mock/shp@social.heldscal.la.xml"),
  705. headers: [{"content-type", "application/xrd+xml"}]
  706. }}
  707. end
  708. def get(
  709. "https://social.heldscal.la/.well-known/webfinger?resource=acct:invalid_content@social.heldscal.la",
  710. _,
  711. _,
  712. [{"accept", "application/xrd+xml,application/jrd+json"}]
  713. ) do
  714. {:ok, %Tesla.Env{status: 200, body: "", headers: [{"content-type", "application/jrd+json"}]}}
  715. end
  716. def get("http://framatube.org/.well-known/host-meta", _, _, _) do
  717. {:ok,
  718. %Tesla.Env{
  719. status: 200,
  720. body: File.read!("test/fixtures/tesla_mock/framatube.org_host_meta")
  721. }}
  722. end
  723. def get(
  724. "http://framatube.org/main/xrd?uri=acct:framasoft@framatube.org",
  725. _,
  726. _,
  727. [{"accept", "application/xrd+xml,application/jrd+json"}]
  728. ) do
  729. {:ok,
  730. %Tesla.Env{
  731. status: 200,
  732. headers: [{"content-type", "application/jrd+json"}],
  733. body: File.read!("test/fixtures/tesla_mock/framasoft@framatube.org.json")
  734. }}
  735. end
  736. def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
  737. {:ok,
  738. %Tesla.Env{
  739. status: 200,
  740. body: File.read!("test/fixtures/tesla_mock/gnusocial.de_host_meta")
  741. }}
  742. end
  743. def get(
  744. "http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de",
  745. _,
  746. _,
  747. [{"accept", "application/xrd+xml,application/jrd+json"}]
  748. ) do
  749. {:ok,
  750. %Tesla.Env{
  751. status: 200,
  752. body: File.read!("test/fixtures/tesla_mock/winterdienst_webfinger.json"),
  753. headers: activitypub_object_headers()
  754. }}
  755. end
  756. def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
  757. {:ok,
  758. %Tesla.Env{
  759. status: 200,
  760. body: File.read!("test/fixtures/tesla_mock/status.alpicola.com_host_meta")
  761. }}
  762. end
  763. def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
  764. {:ok,
  765. %Tesla.Env{
  766. status: 200,
  767. body: File.read!("test/fixtures/tesla_mock/macgirvin.com_host_meta")
  768. }}
  769. end
  770. def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
  771. {:ok,
  772. %Tesla.Env{
  773. status: 200,
  774. body: File.read!("test/fixtures/tesla_mock/gerzilla.de_host_meta")
  775. }}
  776. end
  777. def get(
  778. "https://gerzilla.de/xrd/?uri=acct:kaniini@gerzilla.de",
  779. _,
  780. _,
  781. [{"accept", "application/xrd+xml,application/jrd+json"}]
  782. ) do
  783. {:ok,
  784. %Tesla.Env{
  785. status: 200,
  786. headers: [{"content-type", "application/jrd+json"}],
  787. body: File.read!("test/fixtures/tesla_mock/kaniini@gerzilla.de.json")
  788. }}
  789. end
  790. def get(
  791. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
  792. _,
  793. _,
  794. _
  795. ) do
  796. {:ok,
  797. %Tesla.Env{
  798. status: 200,
  799. body: File.read!("test/fixtures/tesla_mock/https___social.heldscal.la_user_23211.xml")
  800. }}
  801. end
  802. def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
  803. {:ok,
  804. %Tesla.Env{
  805. status: 200,
  806. body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
  807. }}
  808. end
  809. def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
  810. {:ok,
  811. %Tesla.Env{
  812. status: 200,
  813. body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
  814. }}
  815. end
  816. def get("https://mastodon.social/users/lambadalambda", _, _, _) do
  817. {:ok,
  818. %Tesla.Env{
  819. status: 200,
  820. body: File.read!("test/fixtures/lambadalambda.json"),
  821. headers: activitypub_object_headers()
  822. }}
  823. end
  824. def get("https://mastodon.social/users/lambadalambda/collections/featured", _, _, _) do
  825. {:ok,
  826. %Tesla.Env{
  827. status: 200,
  828. body:
  829. File.read!("test/fixtures/users_mock/masto_featured.json")
  830. |> String.replace("{{domain}}", "mastodon.social")
  831. |> String.replace("{{nickname}}", "lambadalambda"),
  832. headers: activitypub_object_headers()
  833. }}
  834. end
  835. def get("https://apfed.club/channel/indio", _, _, _) do
  836. {:ok,
  837. %Tesla.Env{
  838. status: 200,
  839. body: File.read!("test/fixtures/tesla_mock/osada-user-indio.json"),
  840. headers: activitypub_object_headers()
  841. }}
  842. end
  843. def get("https://social.heldscal.la/user/23211", _, _, [{"accept", "application/activity+json"}]) do
  844. {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
  845. end
  846. def get("http://example.com/ogp", _, _, _) do
  847. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  848. end
  849. def get("https://example.com/ogp", _, _, _) do
  850. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  851. end
  852. def get("https://pleroma.local/notice/9kCP7V", _, _, _) do
  853. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
  854. end
  855. def get("http://localhost:4001/users/masto_closed/followers", _, _, _) do
  856. {:ok,
  857. %Tesla.Env{
  858. status: 200,
  859. body: File.read!("test/fixtures/users_mock/masto_closed_followers.json"),
  860. headers: activitypub_object_headers()
  861. }}
  862. end
  863. def get("http://localhost:4001/users/masto_closed/followers?page=1", _, _, _) do
  864. {:ok,
  865. %Tesla.Env{
  866. status: 200,
  867. body: File.read!("test/fixtures/users_mock/masto_closed_followers_page.json"),
  868. headers: activitypub_object_headers()
  869. }}
  870. end
  871. def get("http://localhost:4001/users/masto_closed/following", _, _, _) do
  872. {:ok,
  873. %Tesla.Env{
  874. status: 200,
  875. body: File.read!("test/fixtures/users_mock/masto_closed_following.json"),
  876. headers: activitypub_object_headers()
  877. }}
  878. end
  879. def get("http://localhost:4001/users/masto_closed/following?page=1", _, _, _) do
  880. {:ok,
  881. %Tesla.Env{
  882. status: 200,
  883. body: File.read!("test/fixtures/users_mock/masto_closed_following_page.json"),
  884. headers: activitypub_object_headers()
  885. }}
  886. end
  887. def get("http://localhost:8080/followers/fuser3", _, _, _) do
  888. {:ok,
  889. %Tesla.Env{
  890. status: 200,
  891. body: File.read!("test/fixtures/users_mock/friendica_followers.json"),
  892. headers: activitypub_object_headers()
  893. }}
  894. end
  895. def get("http://localhost:8080/following/fuser3", _, _, _) do
  896. {:ok,
  897. %Tesla.Env{
  898. status: 200,
  899. body: File.read!("test/fixtures/users_mock/friendica_following.json"),
  900. headers: activitypub_object_headers()
  901. }}
  902. end
  903. def get("http://localhost:4001/users/fuser2/followers", _, _, _) do
  904. {:ok,
  905. %Tesla.Env{
  906. status: 200,
  907. body: File.read!("test/fixtures/users_mock/pleroma_followers.json"),
  908. headers: activitypub_object_headers()
  909. }}
  910. end
  911. def get("http://localhost:4001/users/fuser2/following", _, _, _) do
  912. {:ok,
  913. %Tesla.Env{
  914. status: 200,
  915. body: File.read!("test/fixtures/users_mock/pleroma_following.json"),
  916. headers: activitypub_object_headers()
  917. }}
  918. end
  919. def get("http://domain-with-errors:4001/users/fuser1/followers", _, _, _) do
  920. {:ok,
  921. %Tesla.Env{
  922. status: 504,
  923. body: ""
  924. }}
  925. end
  926. def get("http://domain-with-errors:4001/users/fuser1/following", _, _, _) do
  927. {:ok,
  928. %Tesla.Env{
  929. status: 504,
  930. body: ""
  931. }}
  932. end
  933. def get("http://example.com/ogp-missing-data", _, _, _) do
  934. {:ok,
  935. %Tesla.Env{
  936. status: 200,
  937. body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
  938. }}
  939. end
  940. def get("https://example.com/ogp-missing-data", _, _, _) do
  941. {:ok,
  942. %Tesla.Env{
  943. status: 200,
  944. body: File.read!("test/fixtures/rich_media/ogp-missing-data.html")
  945. }}
  946. end
  947. def get("http://example.com/malformed", _, _, _) do
  948. {:ok,
  949. %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/malformed-data.html")}}
  950. end
  951. def get("http://example.com/empty", _, _, _) do
  952. {:ok, %Tesla.Env{status: 200, body: "hello"}}
  953. end
  954. def get("http://404.site" <> _, _, _, _) do
  955. {:ok,
  956. %Tesla.Env{
  957. status: 404,
  958. body: ""
  959. }}
  960. end
  961. def get(
  962. "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:lain@zetsubou.xn--q9jyb4c",
  963. _,
  964. _,
  965. [{"accept", "application/xrd+xml,application/jrd+json"}]
  966. ) do
  967. {:ok,
  968. %Tesla.Env{
  969. status: 200,
  970. body: File.read!("test/fixtures/lain.xml"),
  971. headers: [{"content-type", "application/xrd+xml"}]
  972. }}
  973. end
  974. def get(
  975. "https://zetsubou.xn--q9jyb4c/.well-known/webfinger?resource=acct:https://zetsubou.xn--q9jyb4c/users/lain",
  976. _,
  977. _,
  978. [{"accept", "application/xrd+xml,application/jrd+json"}]
  979. ) do
  980. {:ok,
  981. %Tesla.Env{
  982. status: 200,
  983. body: File.read!("test/fixtures/lain.xml"),
  984. headers: [{"content-type", "application/xrd+xml"}]
  985. }}
  986. end
  987. def get("http://zetsubou.xn--q9jyb4c/.well-known/host-meta", _, _, _) do
  988. {:ok,
  989. %Tesla.Env{
  990. status: 200,
  991. body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
  992. }}
  993. end
  994. def get(
  995. "https://zetsubou.xn--q9jyb4c/.well-known/host-meta",
  996. _,
  997. _,
  998. _
  999. ) do
  1000. {:ok,
  1001. %Tesla.Env{
  1002. status: 200,
  1003. body: File.read!("test/fixtures/host-meta-zetsubou.xn--q9jyb4c.xml")
  1004. }}
  1005. end
  1006. def get("https://info.pleroma.site/activity.json", _, _, [
  1007. {"accept", "application/activity+json"}
  1008. ]) do
  1009. {:ok,
  1010. %Tesla.Env{
  1011. status: 200,
  1012. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity.json"),
  1013. headers: activitypub_object_headers()
  1014. }}
  1015. end
  1016. def get("https://info.pleroma.site/activity.json", _, _, _) do
  1017. {:ok, %Tesla.Env{status: 404, body: ""}}
  1018. end
  1019. def get("https://info.pleroma.site/activity2.json", _, _, [
  1020. {"accept", "application/activity+json"}
  1021. ]) do
  1022. {:ok,
  1023. %Tesla.Env{
  1024. status: 200,
  1025. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity2.json"),
  1026. headers: activitypub_object_headers()
  1027. }}
  1028. end
  1029. def get("https://info.pleroma.site/activity2.json", _, _, _) do
  1030. {:ok, %Tesla.Env{status: 404, body: ""}}
  1031. end
  1032. def get("https://info.pleroma.site/activity3.json", _, _, [
  1033. {"accept", "application/activity+json"}
  1034. ]) do
  1035. {:ok,
  1036. %Tesla.Env{
  1037. status: 200,
  1038. body: File.read!("test/fixtures/tesla_mock/https__info.pleroma.site_activity3.json"),
  1039. headers: activitypub_object_headers()
  1040. }}
  1041. end
  1042. def get("https://info.pleroma.site/activity3.json", _, _, _) do
  1043. {:ok, %Tesla.Env{status: 404, body: ""}}
  1044. end
  1045. def get("https://mstdn.jp/.well-known/webfinger?resource=acct:kpherox@mstdn.jp", _, _, _) do
  1046. {:ok,
  1047. %Tesla.Env{
  1048. status: 200,
  1049. body: File.read!("test/fixtures/tesla_mock/kpherox@mstdn.jp.xml"),
  1050. headers: [{"content-type", "application/xrd+xml"}]
  1051. }}
  1052. end
  1053. def get("https://10.111.10.1/notice/9kCP7V", _, _, _) do
  1054. {:ok, %Tesla.Env{status: 200, body: ""}}
  1055. end
  1056. def get("https://172.16.32.40/notice/9kCP7V", _, _, _) do
  1057. {:ok, %Tesla.Env{status: 200, body: ""}}
  1058. end
  1059. def get("https://192.168.10.40/notice/9kCP7V", _, _, _) do
  1060. {:ok, %Tesla.Env{status: 200, body: ""}}
  1061. end
  1062. def get("https://www.patreon.com/posts/mastodon-2-9-and-28121681", _, _, _) do
  1063. {:ok, %Tesla.Env{status: 200, body: ""}}
  1064. end
  1065. def get("http://mastodon.example.org/@admin/99541947525187367", _, _, _) do
  1066. {:ok,
  1067. %Tesla.Env{
  1068. status: 200,
  1069. body: File.read!("test/fixtures/mastodon-post-activity.json"),
  1070. headers: activitypub_object_headers()
  1071. }}
  1072. end
  1073. def get("https://info.pleroma.site/activity4.json", _, _, _) do
  1074. {:ok, %Tesla.Env{status: 500, body: "Error occurred"}}
  1075. end
  1076. def get("http://example.com/rel_me/anchor", _, _, _) do
  1077. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor.html")}}
  1078. end
  1079. def get("http://example.com/rel_me/anchor_nofollow", _, _, _) do
  1080. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_anchor_nofollow.html")}}
  1081. end
  1082. def get("http://example.com/rel_me/link", _, _, _) do
  1083. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_link.html")}}
  1084. end
  1085. def get("http://example.com/rel_me/null", _, _, _) do
  1086. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rel_me_null.html")}}
  1087. end
  1088. def get("https://skippers-bin.com/notes/7x9tmrp97i", _, _, _) do
  1089. {:ok,
  1090. %Tesla.Env{
  1091. status: 200,
  1092. body: File.read!("test/fixtures/tesla_mock/misskey_poll_no_end_date.json"),
  1093. headers: activitypub_object_headers()
  1094. }}
  1095. end
  1096. def get("https://example.org/emoji/firedfox.png", _, _, _) do
  1097. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/image.jpg")}}
  1098. end
  1099. def get("https://skippers-bin.com/users/7v1w1r8ce6", _, _, _) do
  1100. {:ok,
  1101. %Tesla.Env{
  1102. status: 200,
  1103. body: File.read!("test/fixtures/tesla_mock/sjw.json"),
  1104. headers: activitypub_object_headers()
  1105. }}
  1106. end
  1107. def get("https://patch.cx/users/rin", _, _, _) do
  1108. {:ok,
  1109. %Tesla.Env{
  1110. status: 200,
  1111. body: File.read!("test/fixtures/tesla_mock/rin.json"),
  1112. headers: activitypub_object_headers()
  1113. }}
  1114. end
  1115. def get(
  1116. "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871",
  1117. _,
  1118. _,
  1119. _
  1120. ) do
  1121. {:ok,
  1122. %Tesla.Env{
  1123. status: 200,
  1124. body: File.read!("test/fixtures/tesla_mock/funkwhale_audio.json"),
  1125. headers: activitypub_object_headers()
  1126. }}
  1127. end
  1128. def get("https://channels.tests.funkwhale.audio/federation/actors/compositions", _, _, _) do
  1129. {:ok,
  1130. %Tesla.Env{
  1131. status: 200,
  1132. body: File.read!("test/fixtures/tesla_mock/funkwhale_channel.json"),
  1133. headers: activitypub_object_headers()
  1134. }}
  1135. end
  1136. def get("http://example.com/rel_me/error", _, _, _) do
  1137. {:ok, %Tesla.Env{status: 404, body: ""}}
  1138. end
  1139. def get("https://relay.mastodon.host/actor", _, _, _) do
  1140. {:ok,
  1141. %Tesla.Env{
  1142. status: 200,
  1143. body: File.read!("test/fixtures/relay/relay.json"),
  1144. headers: activitypub_object_headers()
  1145. }}
  1146. end
  1147. def get("http://localhost:4001/", _, "", [{"accept", "text/html"}]) do
  1148. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/7369654.html")}}
  1149. end
  1150. def get("https://osada.macgirvin.com/", _, "", [{"accept", "text/html"}]) do
  1151. {:ok,
  1152. %Tesla.Env{
  1153. status: 200,
  1154. body: File.read!("test/fixtures/tesla_mock/https___osada.macgirvin.com.html")
  1155. }}
  1156. end
  1157. def get("https://patch.cx/objects/a399c28e-c821-4820-bc3e-4afeb044c16f", _, _, _) do
  1158. {:ok,
  1159. %Tesla.Env{
  1160. status: 200,
  1161. body: File.read!("test/fixtures/tesla_mock/emoji-in-summary.json"),
  1162. headers: activitypub_object_headers()
  1163. }}
  1164. end
  1165. def get(url, query, body, headers) do
  1166. {:error,
  1167. "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1168. inspect(headers)
  1169. }"}
  1170. end
  1171. # POST Requests
  1172. #
  1173. def post(url, query \\ [], body \\ [], headers \\ [])
  1174. def post("https://relay.mastodon.host/inbox", _, _, _) do
  1175. {:ok, %Tesla.Env{status: 200, body: ""}}
  1176. end
  1177. def post("http://example.org/needs_refresh", _, _, _) do
  1178. {:ok,
  1179. %Tesla.Env{
  1180. status: 200,
  1181. body: ""
  1182. }}
  1183. end
  1184. def post("http://mastodon.example.org/inbox", _, _, _) do
  1185. {:ok,
  1186. %Tesla.Env{
  1187. status: 200,
  1188. body: ""
  1189. }}
  1190. end
  1191. def post("https://hubzilla.example.org/inbox", _, _, _) do
  1192. {:ok,
  1193. %Tesla.Env{
  1194. status: 200,
  1195. body: ""
  1196. }}
  1197. end
  1198. def post("http://gs.example.org/index.php/main/salmon/user/1", _, _, _) do
  1199. {:ok,
  1200. %Tesla.Env{
  1201. status: 200,
  1202. body: ""
  1203. }}
  1204. end
  1205. def post("http://200.site" <> _, _, _, _) do
  1206. {:ok,
  1207. %Tesla.Env{
  1208. status: 200,
  1209. body: ""
  1210. }}
  1211. end
  1212. def post("http://connrefused.site" <> _, _, _, _) do
  1213. {:error, :connrefused}
  1214. end
  1215. def post("http://404.site" <> _, _, _, _) do
  1216. {:ok,
  1217. %Tesla.Env{
  1218. status: 404,
  1219. body: ""
  1220. }}
  1221. end
  1222. def post(url, query, body, headers) do
  1223. {:error,
  1224. "Mock response not implemented for POST #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1225. inspect(headers)
  1226. }"}
  1227. end
  1228. # Most of the rich media mocks are missing HEAD requests, so we just return 404.
  1229. @rich_media_mocks [
  1230. "https://example.com/ogp",
  1231. "https://example.com/ogp-missing-data",
  1232. "https://example.com/twitter-card"
  1233. ]
  1234. def head(url, _query, _body, _headers) when url in @rich_media_mocks do
  1235. {:ok, %Tesla.Env{status: 404, body: ""}}
  1236. end
  1237. def head(url, query, body, headers) do
  1238. {:error,
  1239. "Mock response not implemented for HEAD #{inspect(url)}, #{query}, #{inspect(body)}, #{
  1240. inspect(headers)
  1241. }"}
  1242. end
  1243. end