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.

680 line
18KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule HttpRequestMock do
  5. require Logger
  6. def request(
  7. %Tesla.Env{
  8. url: url,
  9. method: method,
  10. headers: headers,
  11. query: query,
  12. body: body
  13. } = _env
  14. ) do
  15. with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
  16. res
  17. else
  18. {_, _r} = error ->
  19. # Logger.warn(r)
  20. error
  21. end
  22. end
  23. # GET Requests
  24. #
  25. def get(url, query \\ [], body \\ [], headers \\ [])
  26. def get("https://osada.macgirvin.com/channel/mike", _, _, _) do
  27. {:ok,
  28. %Tesla.Env{
  29. status: 200,
  30. body:
  31. File.read!("test/fixtures/httpoison_mock/https___osada.macgirvin.com_channel_mike.json")
  32. }}
  33. end
  34. def get(
  35. "https://osada.macgirvin.com/.well-known/webfinger?resource=acct:mike@osada.macgirvin.com",
  36. _,
  37. _,
  38. Accept: "application/xrd+xml,application/jrd+json"
  39. ) do
  40. {:ok,
  41. %Tesla.Env{
  42. status: 200,
  43. body: File.read!("test/fixtures/httpoison_mock/mike@osada.macgirvin.com.json")
  44. }}
  45. end
  46. def get(
  47. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/29191",
  48. _,
  49. _,
  50. Accept: "application/xrd+xml,application/jrd+json"
  51. ) do
  52. {:ok,
  53. %Tesla.Env{
  54. status: 200,
  55. body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_29191.xml")
  56. }}
  57. end
  58. def get("https://pawoo.net/users/pekorino.atom", _, _, _) do
  59. {:ok,
  60. %Tesla.Env{
  61. status: 200,
  62. body: File.read!("test/fixtures/httpoison_mock/https___pawoo.net_users_pekorino.atom")
  63. }}
  64. end
  65. def get(
  66. "https://pawoo.net/.well-known/webfinger?resource=acct:https://pawoo.net/users/pekorino",
  67. _,
  68. _,
  69. Accept: "application/xrd+xml,application/jrd+json"
  70. ) do
  71. {:ok,
  72. %Tesla.Env{
  73. status: 200,
  74. body: File.read!("test/fixtures/httpoison_mock/https___pawoo.net_users_pekorino.xml")
  75. }}
  76. end
  77. def get(
  78. "https://social.stopwatchingus-heidelberg.de/api/statuses/user_timeline/18330.atom",
  79. _,
  80. _,
  81. _
  82. ) do
  83. {:ok,
  84. %Tesla.Env{
  85. status: 200,
  86. body: File.read!("test/fixtures/httpoison_mock/atarifrosch_feed.xml")
  87. }}
  88. end
  89. def get(
  90. "https://social.stopwatchingus-heidelberg.de/.well-known/webfinger?resource=acct:https://social.stopwatchingus-heidelberg.de/user/18330",
  91. _,
  92. _,
  93. Accept: "application/xrd+xml,application/jrd+json"
  94. ) do
  95. {:ok,
  96. %Tesla.Env{
  97. status: 200,
  98. body: File.read!("test/fixtures/httpoison_mock/atarifrosch_webfinger.xml")
  99. }}
  100. end
  101. def get("https://mamot.fr/users/Skruyb.atom", _, _, _) do
  102. {:ok,
  103. %Tesla.Env{
  104. status: 200,
  105. body: File.read!("test/fixtures/httpoison_mock/https___mamot.fr_users_Skruyb.atom")
  106. }}
  107. end
  108. def get(
  109. "https://mamot.fr/.well-known/webfinger?resource=acct:https://mamot.fr/users/Skruyb",
  110. _,
  111. _,
  112. Accept: "application/xrd+xml,application/jrd+json"
  113. ) do
  114. {:ok,
  115. %Tesla.Env{
  116. status: 200,
  117. body: File.read!("test/fixtures/httpoison_mock/skruyb@mamot.fr.atom")
  118. }}
  119. end
  120. def get(
  121. "https://social.heldscal.la/.well-known/webfinger?resource=nonexistant@social.heldscal.la",
  122. _,
  123. _,
  124. Accept: "application/xrd+xml,application/jrd+json"
  125. ) do
  126. {:ok,
  127. %Tesla.Env{
  128. status: 200,
  129. body: File.read!("test/fixtures/httpoison_mock/nonexistant@social.heldscal.la.xml")
  130. }}
  131. end
  132. def get("https://squeet.me/xrd/?uri=lain@squeet.me", _, _,
  133. Accept: "application/xrd+xml,application/jrd+json"
  134. ) do
  135. {:ok,
  136. %Tesla.Env{
  137. status: 200,
  138. body: File.read!("test/fixtures/httpoison_mock/lain_squeet.me_webfinger.xml")
  139. }}
  140. end
  141. def get("https://mst3k.interlinked.me/users/luciferMysticus", _, _,
  142. Accept: "application/activity+json"
  143. ) do
  144. {:ok,
  145. %Tesla.Env{
  146. status: 200,
  147. body: File.read!("test/fixtures/httpoison_mock/lucifermysticus.json")
  148. }}
  149. end
  150. def get("https://prismo.news/@mxb", _, _, _) do
  151. {:ok,
  152. %Tesla.Env{
  153. status: 200,
  154. body: File.read!("test/fixtures/httpoison_mock/https___prismo.news__mxb.json")
  155. }}
  156. end
  157. def get("https://hubzilla.example.org/channel/kaniini", _, _,
  158. Accept: "application/activity+json"
  159. ) do
  160. {:ok,
  161. %Tesla.Env{
  162. status: 200,
  163. body: File.read!("test/fixtures/httpoison_mock/kaniini@hubzilla.example.org.json")
  164. }}
  165. end
  166. def get("https://niu.moe/users/rye", _, _, Accept: "application/activity+json") do
  167. {:ok,
  168. %Tesla.Env{
  169. status: 200,
  170. body: File.read!("test/fixtures/httpoison_mock/rye.json")
  171. }}
  172. end
  173. def get("http://mastodon.example.org/users/admin/statuses/100787282858396771", _, _, _) do
  174. {:ok,
  175. %Tesla.Env{
  176. status: 200,
  177. body:
  178. File.read!(
  179. "test/fixtures/httpoison_mock/http___mastodon.example.org_users_admin_status_1234.json"
  180. )
  181. }}
  182. end
  183. def get("https://puckipedia.com/", _, _, Accept: "application/activity+json") do
  184. {:ok,
  185. %Tesla.Env{
  186. status: 200,
  187. body: File.read!("test/fixtures/httpoison_mock/puckipedia.com.json")
  188. }}
  189. end
  190. def get("https://peertube.moe/accounts/7even", _, _, _) do
  191. {:ok,
  192. %Tesla.Env{
  193. status: 200,
  194. body: File.read!("test/fixtures/httpoison_mock/7even.json")
  195. }}
  196. end
  197. def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3", _, _, _) do
  198. {:ok,
  199. %Tesla.Env{
  200. status: 200,
  201. body: File.read!("test/fixtures/httpoison_mock/peertube.moe-vid.json")
  202. }}
  203. end
  204. def get("https://baptiste.gelez.xyz/@/BaptisteGelez", _, _, _) do
  205. {:ok,
  206. %Tesla.Env{
  207. status: 200,
  208. body: File.read!("test/fixtures/httpoison_mock/baptiste.gelex.xyz-user.json")
  209. }}
  210. end
  211. def get("https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/", _, _, _) do
  212. {:ok,
  213. %Tesla.Env{
  214. status: 200,
  215. body: File.read!("test/fixtures/httpoison_mock/baptiste.gelex.xyz-article.json")
  216. }}
  217. end
  218. def get("http://mastodon.example.org/users/admin", _, _, Accept: "application/activity+json") do
  219. {:ok,
  220. %Tesla.Env{
  221. status: 200,
  222. body: File.read!("test/fixtures/httpoison_mock/admin@mastdon.example.org.json")
  223. }}
  224. end
  225. def get("http://mastodon.example.org/@admin/99541947525187367", _, _,
  226. Accept: "application/activity+json"
  227. ) do
  228. {:ok,
  229. %Tesla.Env{
  230. status: 200,
  231. body: File.read!("test/fixtures/mastodon-note-object.json")
  232. }}
  233. end
  234. def get("https://shitposter.club/notice/7369654", _, _, _) do
  235. {:ok,
  236. %Tesla.Env{
  237. status: 200,
  238. body: File.read!("test/fixtures/httpoison_mock/7369654.html")
  239. }}
  240. end
  241. def get("https://mstdn.io/users/mayuutann", _, _, Accept: "application/activity+json") do
  242. {:ok,
  243. %Tesla.Env{
  244. status: 200,
  245. body: File.read!("test/fixtures/httpoison_mock/mayumayu.json")
  246. }}
  247. end
  248. def get("https://mstdn.io/users/mayuutann/statuses/99568293732299394", _, _,
  249. Accept: "application/activity+json"
  250. ) do
  251. {:ok,
  252. %Tesla.Env{
  253. status: 200,
  254. body: File.read!("test/fixtures/httpoison_mock/mayumayupost.json")
  255. }}
  256. end
  257. def get("https://pleroma.soykaf.com/users/lain/feed.atom", _, _, _) do
  258. {:ok,
  259. %Tesla.Env{
  260. status: 200,
  261. body:
  262. File.read!(
  263. "test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain_feed.atom.xml"
  264. )
  265. }}
  266. end
  267. def get(url, _, _, Accept: "application/xrd+xml,application/jrd+json")
  268. when url in [
  269. "https://pleroma.soykaf.com/.well-known/webfinger?resource=acct:https://pleroma.soykaf.com/users/lain",
  270. "https://pleroma.soykaf.com/.well-known/webfinger?resource=https://pleroma.soykaf.com/users/lain"
  271. ] do
  272. {:ok,
  273. %Tesla.Env{
  274. status: 200,
  275. body: File.read!("test/fixtures/httpoison_mock/https___pleroma.soykaf.com_users_lain.xml")
  276. }}
  277. end
  278. def get("https://shitposter.club/api/statuses/user_timeline/1.atom", _, _, _) do
  279. {:ok,
  280. %Tesla.Env{
  281. status: 200,
  282. body:
  283. File.read!(
  284. "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_user_timeline_1.atom.xml"
  285. )
  286. }}
  287. end
  288. def get(
  289. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/1",
  290. _,
  291. _,
  292. Accept: "application/xrd+xml,application/jrd+json"
  293. ) do
  294. {:ok,
  295. %Tesla.Env{
  296. status: 200,
  297. body: File.read!("test/fixtures/httpoison_mock/https___shitposter.club_user_1.xml")
  298. }}
  299. end
  300. def get("https://shitposter.club/notice/2827873", _, _, _) do
  301. {:ok,
  302. %Tesla.Env{
  303. status: 200,
  304. body:
  305. File.read!("test/fixtures/httpoison_mock/https___shitposter.club_notice_2827873.html")
  306. }}
  307. end
  308. def get("https://shitposter.club/api/statuses/show/2827873.atom", _, _, _) do
  309. {:ok,
  310. %Tesla.Env{
  311. status: 200,
  312. body:
  313. File.read!(
  314. "test/fixtures/httpoison_mock/https___shitposter.club_api_statuses_show_2827873.atom.xml"
  315. )
  316. }}
  317. end
  318. def get("https://testing.pleroma.lol/objects/b319022a-4946-44c5-9de9-34801f95507b", _, _, _) do
  319. {:ok, %Tesla.Env{status: 200}}
  320. end
  321. def get("https://shitposter.club/api/statuses/user_timeline/5381.atom", _, _, _) do
  322. {:ok,
  323. %Tesla.Env{
  324. status: 200,
  325. body: File.read!("test/fixtures/httpoison_mock/spc_5381.atom")
  326. }}
  327. end
  328. def get(
  329. "https://shitposter.club/.well-known/webfinger?resource=https://shitposter.club/user/5381",
  330. _,
  331. _,
  332. Accept: "application/xrd+xml,application/jrd+json"
  333. ) do
  334. {:ok,
  335. %Tesla.Env{
  336. status: 200,
  337. body: File.read!("test/fixtures/httpoison_mock/spc_5381_xrd.xml")
  338. }}
  339. end
  340. def get("http://shitposter.club/.well-known/host-meta", _, _, _) do
  341. {:ok,
  342. %Tesla.Env{
  343. status: 200,
  344. body: File.read!("test/fixtures/httpoison_mock/shitposter.club_host_meta")
  345. }}
  346. end
  347. def get("https://shitposter.club/api/statuses/show/7369654.atom", _, _, _) do
  348. {:ok,
  349. %Tesla.Env{
  350. status: 200,
  351. body: File.read!("test/fixtures/httpoison_mock/7369654.atom")
  352. }}
  353. end
  354. def get("https://shitposter.club/notice/4027863", _, _, _) do
  355. {:ok,
  356. %Tesla.Env{
  357. status: 200,
  358. body: File.read!("test/fixtures/httpoison_mock/7369654.html")
  359. }}
  360. end
  361. def get("https://social.sakamoto.gq/users/eal/feed.atom", _, _, _) do
  362. {:ok,
  363. %Tesla.Env{
  364. status: 200,
  365. body: File.read!("test/fixtures/httpoison_mock/sakamoto_eal_feed.atom")
  366. }}
  367. end
  368. def get("http://social.sakamoto.gq/.well-known/host-meta", _, _, _) do
  369. {:ok,
  370. %Tesla.Env{
  371. status: 200,
  372. body: File.read!("test/fixtures/httpoison_mock/social.sakamoto.gq_host_meta")
  373. }}
  374. end
  375. def get(
  376. "https://social.sakamoto.gq/.well-known/webfinger?resource=https://social.sakamoto.gq/users/eal",
  377. _,
  378. _,
  379. Accept: "application/xrd+xml,application/jrd+json"
  380. ) do
  381. {:ok,
  382. %Tesla.Env{
  383. status: 200,
  384. body: File.read!("test/fixtures/httpoison_mock/eal_sakamoto.xml")
  385. }}
  386. end
  387. def get("https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056", _, _,
  388. Accept: "application/atom+xml"
  389. ) do
  390. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/sakamoto.atom")}}
  391. end
  392. def get("http://mastodon.social/.well-known/host-meta", _, _, _) do
  393. {:ok,
  394. %Tesla.Env{
  395. status: 200,
  396. body: File.read!("test/fixtures/httpoison_mock/mastodon.social_host_meta")
  397. }}
  398. end
  399. def get(
  400. "https://mastodon.social/.well-known/webfinger?resource=https://mastodon.social/users/lambadalambda",
  401. _,
  402. _,
  403. Accept: "application/xrd+xml,application/jrd+json"
  404. ) do
  405. {:ok,
  406. %Tesla.Env{
  407. status: 200,
  408. body:
  409. File.read!(
  410. "test/fixtures/httpoison_mock/https___mastodon.social_users_lambadalambda.xml"
  411. )
  412. }}
  413. end
  414. def get("http://gs.example.org/.well-known/host-meta", _, _, _) do
  415. {:ok,
  416. %Tesla.Env{
  417. status: 200,
  418. body: File.read!("test/fixtures/httpoison_mock/gs.example.org_host_meta")
  419. }}
  420. end
  421. def get(
  422. "http://gs.example.org/.well-known/webfinger?resource=http://gs.example.org:4040/index.php/user/1",
  423. _,
  424. _,
  425. Accept: "application/xrd+xml,application/jrd+json"
  426. ) do
  427. {:ok,
  428. %Tesla.Env{
  429. status: 200,
  430. body:
  431. File.read!(
  432. "test/fixtures/httpoison_mock/http___gs.example.org_4040_index.php_user_1.xml"
  433. )
  434. }}
  435. end
  436. def get("http://gs.example.org/index.php/api/statuses/user_timeline/1.atom", _, _, _) do
  437. {:ok,
  438. %Tesla.Env{
  439. status: 200,
  440. body:
  441. File.read!(
  442. "test/fixtures/httpoison_mock/http__gs.example.org_index.php_api_statuses_user_timeline_1.atom.xml"
  443. )
  444. }}
  445. end
  446. def get("https://social.heldscal.la/api/statuses/user_timeline/29191.atom", _, _, _) do
  447. {:ok,
  448. %Tesla.Env{
  449. status: 200,
  450. body:
  451. File.read!(
  452. "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_29191.atom.xml"
  453. )
  454. }}
  455. end
  456. def get("http://squeet.me/.well-known/host-meta", _, _, _) do
  457. {:ok,
  458. %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/squeet.me_host_meta")}}
  459. end
  460. def get("https://squeet.me/xrd?uri=lain@squeet.me", _, _,
  461. Accept: "application/xrd+xml,application/jrd+json"
  462. ) do
  463. {:ok,
  464. %Tesla.Env{
  465. status: 200,
  466. body: File.read!("test/fixtures/httpoison_mock/lain_squeet.me_webfinger.xml")
  467. }}
  468. end
  469. def get(
  470. "https://social.heldscal.la/.well-known/webfinger?resource=shp@social.heldscal.la",
  471. _,
  472. _,
  473. Accept: "application/xrd+xml,application/jrd+json"
  474. ) do
  475. {:ok,
  476. %Tesla.Env{
  477. status: 200,
  478. body: File.read!("test/fixtures/httpoison_mock/shp@social.heldscal.la.xml")
  479. }}
  480. end
  481. def get("http://framatube.org/.well-known/host-meta", _, _, _) do
  482. {:ok,
  483. %Tesla.Env{
  484. status: 200,
  485. body: File.read!("test/fixtures/httpoison_mock/framatube.org_host_meta")
  486. }}
  487. end
  488. def get("http://framatube.org/main/xrd?uri=framasoft@framatube.org", _, _,
  489. Accept: "application/xrd+xml,application/jrd+json"
  490. ) do
  491. {:ok,
  492. %Tesla.Env{
  493. status: 200,
  494. headers: [{"content-type", "application/json"}],
  495. body: File.read!("test/fixtures/httpoison_mock/framasoft@framatube.org.json")
  496. }}
  497. end
  498. def get("http://gnusocial.de/.well-known/host-meta", _, _, _) do
  499. {:ok,
  500. %Tesla.Env{
  501. status: 200,
  502. body: File.read!("test/fixtures/httpoison_mock/gnusocial.de_host_meta")
  503. }}
  504. end
  505. def get("http://gnusocial.de/main/xrd?uri=winterdienst@gnusocial.de", _, _,
  506. Accept: "application/xrd+xml,application/jrd+json"
  507. ) do
  508. {:ok,
  509. %Tesla.Env{
  510. status: 200,
  511. body: File.read!("test/fixtures/httpoison_mock/winterdienst_webfinger.json")
  512. }}
  513. end
  514. def get("http://status.alpicola.com/.well-known/host-meta", _, _, _) do
  515. {:ok,
  516. %Tesla.Env{
  517. status: 200,
  518. body: File.read!("test/fixtures/httpoison_mock/status.alpicola.com_host_meta")
  519. }}
  520. end
  521. def get("http://macgirvin.com/.well-known/host-meta", _, _, _) do
  522. {:ok,
  523. %Tesla.Env{
  524. status: 200,
  525. body: File.read!("test/fixtures/httpoison_mock/macgirvin.com_host_meta")
  526. }}
  527. end
  528. def get("http://gerzilla.de/.well-known/host-meta", _, _, _) do
  529. {:ok,
  530. %Tesla.Env{
  531. status: 200,
  532. body: File.read!("test/fixtures/httpoison_mock/gerzilla.de_host_meta")
  533. }}
  534. end
  535. def get("https://gerzilla.de/xrd/?uri=kaniini@gerzilla.de", _, _,
  536. Accept: "application/xrd+xml,application/jrd+json"
  537. ) do
  538. {:ok,
  539. %Tesla.Env{
  540. status: 200,
  541. headers: [{"content-type", "application/json"}],
  542. body: File.read!("test/fixtures/httpoison_mock/kaniini@gerzilla.de.json")
  543. }}
  544. end
  545. def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
  546. {:ok,
  547. %Tesla.Env{
  548. status: 200,
  549. body:
  550. File.read!(
  551. "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
  552. )
  553. }}
  554. end
  555. def get(
  556. "https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211",
  557. _,
  558. _,
  559. _
  560. ) do
  561. {:ok,
  562. %Tesla.Env{
  563. status: 200,
  564. body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")
  565. }}
  566. end
  567. def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
  568. {:ok,
  569. %Tesla.Env{
  570. status: 200,
  571. body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
  572. }}
  573. end
  574. def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
  575. {:ok,
  576. %Tesla.Env{
  577. status: 200,
  578. body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")
  579. }}
  580. end
  581. def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
  582. {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
  583. end
  584. def get("https://social.heldscal.la/user/23211", _, _, Accept: "application/activity+json") do
  585. {:ok, Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)}
  586. end
  587. def get(url, query, body, headers) do
  588. {:error,
  589. "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
  590. inspect(headers)
  591. }"}
  592. end
  593. # POST Requests
  594. #
  595. def post(url, query \\ [], body \\ [], headers \\ [])
  596. def post("http://example.org/needs_refresh", _, _, _) do
  597. {:ok,
  598. %Tesla.Env{
  599. status: 200,
  600. body: ""
  601. }}
  602. end
  603. def post(url, _query, _body, _headers) do
  604. {:error, "Not implemented the mock response for post #{inspect(url)}"}
  605. end
  606. end