statuses_controller_spec.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe StatusesController do
  4. render_views
  5. shared_examples 'cacheable response' do
  6. it 'does not set cookies' do
  7. expect(response.cookies).to be_empty
  8. expect(response.headers['Set-Cookies']).to be_nil
  9. end
  10. it 'does not set sessions' do
  11. expect(session).to be_empty
  12. end
  13. it 'returns Vary header' do
  14. expect(response.headers['Vary']).to include 'Accept, Accept-Language, Cookie'
  15. end
  16. it 'returns public Cache-Control header' do
  17. expect(response.headers['Cache-Control']).to include 'public'
  18. end
  19. end
  20. describe 'GET #show' do
  21. let(:account) { Fabricate(:account) }
  22. let(:status) { Fabricate(:status, account: account) }
  23. context 'when account is permanently suspended' do
  24. before do
  25. account.suspend!
  26. account.deletion_request.destroy
  27. get :show, params: { account_username: account.username, id: status.id }
  28. end
  29. it 'returns http gone' do
  30. expect(response).to have_http_status(410)
  31. end
  32. end
  33. context 'when account is temporarily suspended' do
  34. before do
  35. account.suspend!
  36. get :show, params: { account_username: account.username, id: status.id }
  37. end
  38. it 'returns http forbidden' do
  39. expect(response).to have_http_status(403)
  40. end
  41. end
  42. context 'when status is a reblog' do
  43. let(:original_account) { Fabricate(:account, domain: 'example.com') }
  44. let(:original_status) { Fabricate(:status, account: original_account, url: 'https://example.com/123') }
  45. let(:status) { Fabricate(:status, account: account, reblog: original_status) }
  46. before do
  47. get :show, params: { account_username: status.account.username, id: status.id }
  48. end
  49. it 'redirects to the original status' do
  50. expect(response).to redirect_to(original_status.url)
  51. end
  52. end
  53. context 'when status is public' do
  54. before do
  55. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  56. end
  57. context 'with HTML' do
  58. let(:format) { 'html' }
  59. it 'renders status successfully', :aggregate_failures do
  60. expect(response).to have_http_status(200)
  61. expect(response.headers['Link'].to_s).to include 'activity+json'
  62. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  63. expect(response.headers['Cache-Control']).to include 'public'
  64. expect(response).to render_template(:show)
  65. expect(response.body).to include status.text
  66. end
  67. end
  68. context 'with JSON' do
  69. let(:format) { 'json' }
  70. it_behaves_like 'cacheable response'
  71. it 'renders ActivityPub Note object successfully', :aggregate_failures do
  72. expect(response).to have_http_status(200)
  73. expect(response.headers['Link'].to_s).to include 'activity+json'
  74. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  75. expect(response.headers['Content-Type']).to include 'application/activity+json'
  76. json = body_as_json
  77. expect(json[:content]).to include status.text
  78. end
  79. end
  80. end
  81. context 'when status is private' do
  82. let(:status) { Fabricate(:status, account: account, visibility: :private) }
  83. before do
  84. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  85. end
  86. context 'with JSON' do
  87. let(:format) { 'json' }
  88. it 'returns http not found' do
  89. expect(response).to have_http_status(404)
  90. end
  91. end
  92. context 'with HTML' do
  93. let(:format) { 'html' }
  94. it 'returns http not found' do
  95. expect(response).to have_http_status(404)
  96. end
  97. end
  98. end
  99. context 'when status is direct' do
  100. let(:status) { Fabricate(:status, account: account, visibility: :direct) }
  101. before do
  102. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  103. end
  104. context 'with JSON' do
  105. let(:format) { 'json' }
  106. it 'returns http not found' do
  107. expect(response).to have_http_status(404)
  108. end
  109. end
  110. context 'with HTML' do
  111. let(:format) { 'html' }
  112. it 'returns http not found' do
  113. expect(response).to have_http_status(404)
  114. end
  115. end
  116. end
  117. context 'when signed-in' do
  118. let(:user) { Fabricate(:user) }
  119. before do
  120. sign_in(user)
  121. end
  122. context 'when account blocks user' do
  123. before do
  124. account.block!(user.account)
  125. get :show, params: { account_username: status.account.username, id: status.id }
  126. end
  127. it 'returns http not found' do
  128. expect(response).to have_http_status(404)
  129. end
  130. end
  131. context 'when status is public' do
  132. before do
  133. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  134. end
  135. context 'with HTML' do
  136. let(:format) { 'html' }
  137. it 'renders status successfully', :aggregate_failures do
  138. expect(response).to have_http_status(200)
  139. expect(response.headers['Link'].to_s).to include 'activity+json'
  140. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  141. expect(response.headers['Cache-Control']).to include 'private'
  142. expect(response).to render_template(:show)
  143. expect(response.body).to include status.text
  144. end
  145. end
  146. context 'with JSON' do
  147. let(:format) { 'json' }
  148. it 'renders ActivityPub Note object successfully', :aggregate_failures do
  149. expect(response).to have_http_status(200)
  150. expect(response.headers['Link'].to_s).to include 'activity+json'
  151. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  152. expect(response.headers['Cache-Control']).to include 'private'
  153. expect(response.headers['Content-Type']).to include 'application/activity+json'
  154. json = body_as_json
  155. expect(json[:content]).to include status.text
  156. end
  157. end
  158. end
  159. context 'when status is private' do
  160. let(:status) { Fabricate(:status, account: account, visibility: :private) }
  161. context 'when user is authorized to see it' do
  162. before do
  163. user.account.follow!(account)
  164. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  165. end
  166. context 'with HTML' do
  167. let(:format) { 'html' }
  168. it 'renders status successfully', :aggregate_failures do
  169. expect(response).to have_http_status(200)
  170. expect(response.headers['Link'].to_s).to include 'activity+json'
  171. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  172. expect(response.headers['Cache-Control']).to include 'private'
  173. expect(response).to render_template(:show)
  174. expect(response.body).to include status.text
  175. end
  176. end
  177. context 'with JSON' do
  178. let(:format) { 'json' }
  179. it 'renders ActivityPub Note object successfully', :aggregate_failures do
  180. expect(response).to have_http_status(200)
  181. expect(response.headers['Link'].to_s).to include 'activity+json'
  182. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  183. expect(response.headers['Cache-Control']).to include 'private'
  184. expect(response.headers['Content-Type']).to include 'application/activity+json'
  185. json = body_as_json
  186. expect(json[:content]).to include status.text
  187. end
  188. end
  189. end
  190. context 'when user is not authorized to see it' do
  191. before do
  192. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  193. end
  194. context 'with JSON' do
  195. let(:format) { 'json' }
  196. it 'returns http not found' do
  197. expect(response).to have_http_status(404)
  198. end
  199. end
  200. context 'with HTML' do
  201. let(:format) { 'html' }
  202. it 'returns http not found' do
  203. expect(response).to have_http_status(404)
  204. end
  205. end
  206. end
  207. end
  208. context 'when status is direct' do
  209. let(:status) { Fabricate(:status, account: account, visibility: :direct) }
  210. context 'when user is authorized to see it' do
  211. before do
  212. Fabricate(:mention, account: user.account, status: status)
  213. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  214. end
  215. context 'with HTML' do
  216. let(:format) { 'html' }
  217. it 'renders status successfully', :aggregate_failures do
  218. expect(response).to have_http_status(200)
  219. expect(response.headers['Link'].to_s).to include 'activity+json'
  220. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  221. expect(response.headers['Cache-Control']).to include 'private'
  222. expect(response).to render_template(:show)
  223. expect(response.body).to include status.text
  224. end
  225. end
  226. context 'with JSON' do
  227. let(:format) { 'json' }
  228. it 'renders ActivityPub Note object successfully' do
  229. expect(response).to have_http_status(200)
  230. expect(response.headers['Link'].to_s).to include 'activity+json'
  231. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  232. expect(response.headers['Cache-Control']).to include 'private'
  233. expect(response.headers['Content-Type']).to include 'application/activity+json'
  234. json = body_as_json
  235. expect(json[:content]).to include status.text
  236. end
  237. end
  238. end
  239. context 'when user is not authorized to see it' do
  240. before do
  241. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  242. end
  243. context 'with JSON' do
  244. let(:format) { 'json' }
  245. it 'returns http not found' do
  246. expect(response).to have_http_status(404)
  247. end
  248. end
  249. context 'with HTML' do
  250. let(:format) { 'html' }
  251. it 'returns http not found' do
  252. expect(response).to have_http_status(404)
  253. end
  254. end
  255. end
  256. end
  257. end
  258. context 'with signature' do
  259. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  260. before do
  261. allow(controller).to receive(:signed_request_actor).and_return(remote_account)
  262. end
  263. context 'when account blocks account' do
  264. before do
  265. account.block!(remote_account)
  266. get :show, params: { account_username: status.account.username, id: status.id }
  267. end
  268. it 'returns http not found' do
  269. expect(response).to have_http_status(404)
  270. end
  271. end
  272. context 'when account domain blocks account' do
  273. before do
  274. account.block_domain!(remote_account.domain)
  275. get :show, params: { account_username: status.account.username, id: status.id }
  276. end
  277. it 'returns http not found' do
  278. expect(response).to have_http_status(404)
  279. end
  280. end
  281. context 'when status is public' do
  282. before do
  283. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  284. end
  285. context 'with HTML' do
  286. let(:format) { 'html' }
  287. it 'renders status successfully', :aggregate_failures do
  288. expect(response).to have_http_status(200)
  289. expect(response.headers['Link'].to_s).to include 'activity+json'
  290. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  291. expect(response.headers['Cache-Control']).to include 'private'
  292. expect(response).to render_template(:show)
  293. expect(response.body).to include status.text
  294. end
  295. end
  296. context 'with JSON' do
  297. let(:format) { 'json' }
  298. it_behaves_like 'cacheable response'
  299. it 'renders ActivityPub Note object successfully', :aggregate_failures do
  300. expect(response).to have_http_status(200)
  301. expect(response.headers['Link'].to_s).to include 'activity+json'
  302. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  303. expect(response.headers['Content-Type']).to include 'application/activity+json'
  304. json = body_as_json
  305. expect(json[:content]).to include status.text
  306. end
  307. end
  308. end
  309. context 'when status is private' do
  310. let(:status) { Fabricate(:status, account: account, visibility: :private) }
  311. context 'when user is authorized to see it' do
  312. before do
  313. remote_account.follow!(account)
  314. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  315. end
  316. context 'with HTML' do
  317. let(:format) { 'html' }
  318. it 'renders status successfully', :aggregate_failures do
  319. expect(response).to have_http_status(200)
  320. expect(response.headers['Link'].to_s).to include 'activity+json'
  321. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  322. expect(response.headers['Cache-Control']).to include 'private'
  323. expect(response).to render_template(:show)
  324. expect(response.body).to include status.text
  325. end
  326. end
  327. context 'with JSON' do
  328. let(:format) { 'json' }
  329. it 'renders ActivityPub Note object successfully' do
  330. expect(response).to have_http_status(200)
  331. expect(response.headers['Link'].to_s).to include 'activity+json'
  332. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  333. expect(response.headers['Cache-Control']).to include 'private'
  334. expect(response.headers['Content-Type']).to include 'application/activity+json'
  335. json = body_as_json
  336. expect(json[:content]).to include status.text
  337. end
  338. end
  339. end
  340. context 'when user is not authorized to see it' do
  341. before do
  342. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  343. end
  344. context 'with JSON' do
  345. let(:format) { 'json' }
  346. it 'returns http not found' do
  347. expect(response).to have_http_status(404)
  348. end
  349. end
  350. context 'with HTML' do
  351. let(:format) { 'html' }
  352. it 'returns http not found' do
  353. expect(response).to have_http_status(404)
  354. end
  355. end
  356. end
  357. end
  358. context 'when status is direct' do
  359. let(:status) { Fabricate(:status, account: account, visibility: :direct) }
  360. context 'when user is authorized to see it' do
  361. before do
  362. Fabricate(:mention, account: remote_account, status: status)
  363. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  364. end
  365. context 'with HTML' do
  366. let(:format) { 'html' }
  367. it 'renders status successfully', :aggregate_failures do
  368. expect(response).to have_http_status(200)
  369. expect(response.headers['Link'].to_s).to include 'activity+json'
  370. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  371. expect(response.headers['Cache-Control']).to include 'private'
  372. expect(response).to render_template(:show)
  373. expect(response.body).to include status.text
  374. end
  375. end
  376. context 'with JSON' do
  377. let(:format) { 'json' }
  378. it 'renders ActivityPub Note object', :aggregate_failures do
  379. expect(response).to have_http_status(200)
  380. expect(response.headers['Link'].to_s).to include 'activity+json'
  381. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  382. expect(response.headers['Cache-Control']).to include 'private'
  383. expect(response.headers['Content-Type']).to include 'application/activity+json'
  384. json = body_as_json
  385. expect(json[:content]).to include status.text
  386. end
  387. end
  388. end
  389. context 'when user is not authorized to see it' do
  390. before do
  391. get :show, params: { account_username: status.account.username, id: status.id, format: format }
  392. end
  393. context 'with JSON' do
  394. let(:format) { 'json' }
  395. it 'returns http not found' do
  396. expect(response).to have_http_status(404)
  397. end
  398. end
  399. context 'with HTML' do
  400. let(:format) { 'html' }
  401. it 'returns http not found' do
  402. expect(response).to have_http_status(404)
  403. end
  404. end
  405. end
  406. end
  407. end
  408. end
  409. describe 'GET #activity' do
  410. let(:account) { Fabricate(:account) }
  411. let(:status) { Fabricate(:status, account: account) }
  412. context 'when account is permanently suspended' do
  413. before do
  414. account.suspend!
  415. account.deletion_request.destroy
  416. get :activity, params: { account_username: account.username, id: status.id }
  417. end
  418. it 'returns http gone' do
  419. expect(response).to have_http_status(410)
  420. end
  421. end
  422. context 'when account is temporarily suspended' do
  423. before do
  424. account.suspend!
  425. get :activity, params: { account_username: account.username, id: status.id }
  426. end
  427. it 'returns http forbidden' do
  428. expect(response).to have_http_status(403)
  429. end
  430. end
  431. context 'when status is public' do
  432. before do
  433. status.update(visibility: :public)
  434. get :activity, params: { account_username: account.username, id: status.id }
  435. end
  436. it 'returns http success' do
  437. expect(response).to have_http_status(:success)
  438. end
  439. end
  440. context 'when status is private' do
  441. before do
  442. status.update(visibility: :private)
  443. get :activity, params: { account_username: account.username, id: status.id }
  444. end
  445. it 'returns http not_found' do
  446. expect(response).to have_http_status(404)
  447. end
  448. end
  449. context 'when status is direct' do
  450. before do
  451. status.update(visibility: :direct)
  452. get :activity, params: { account_username: account.username, id: status.id }
  453. end
  454. it 'returns http not_found' do
  455. expect(response).to have_http_status(404)
  456. end
  457. end
  458. context 'when signed-in' do
  459. let(:user) { Fabricate(:user) }
  460. before do
  461. sign_in(user)
  462. end
  463. context 'when status is public' do
  464. before do
  465. status.update(visibility: :public)
  466. get :activity, params: { account_username: account.username, id: status.id }
  467. end
  468. it 'returns http success' do
  469. expect(response).to have_http_status(:success)
  470. end
  471. end
  472. context 'when status is private' do
  473. before do
  474. status.update(visibility: :private)
  475. end
  476. context 'when user is authorized to see it' do
  477. before do
  478. user.account.follow!(account)
  479. get :activity, params: { account_username: account.username, id: status.id }
  480. end
  481. it 'returns http success' do
  482. expect(response).to have_http_status(200)
  483. end
  484. end
  485. context 'when user is not authorized to see it' do
  486. before do
  487. get :activity, params: { account_username: account.username, id: status.id }
  488. end
  489. it 'returns http not_found' do
  490. expect(response).to have_http_status(404)
  491. end
  492. end
  493. end
  494. context 'when status is direct' do
  495. before do
  496. status.update(visibility: :direct)
  497. end
  498. context 'when user is authorized to see it' do
  499. before do
  500. Fabricate(:mention, account: user.account, status: status)
  501. get :activity, params: { account_username: account.username, id: status.id }
  502. end
  503. it 'returns http success' do
  504. expect(response).to have_http_status(200)
  505. end
  506. end
  507. context 'when user is not authorized to see it' do
  508. before do
  509. get :activity, params: { account_username: account.username, id: status.id }
  510. end
  511. it 'returns http not_found' do
  512. expect(response).to have_http_status(404)
  513. end
  514. end
  515. end
  516. end
  517. context 'with signature' do
  518. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  519. before do
  520. allow(controller).to receive(:signed_request_actor).and_return(remote_account)
  521. end
  522. context 'when status is public' do
  523. before do
  524. status.update(visibility: :public)
  525. get :activity, params: { account_username: account.username, id: status.id }
  526. end
  527. it 'returns http success' do
  528. expect(response).to have_http_status(:success)
  529. end
  530. end
  531. context 'when status is private' do
  532. before do
  533. status.update(visibility: :private)
  534. end
  535. context 'when user is authorized to see it' do
  536. before do
  537. remote_account.follow!(account)
  538. get :activity, params: { account_username: account.username, id: status.id }
  539. end
  540. it 'returns http success' do
  541. expect(response).to have_http_status(200)
  542. end
  543. end
  544. context 'when user is not authorized to see it' do
  545. before do
  546. get :activity, params: { account_username: account.username, id: status.id }
  547. end
  548. it 'returns http not_found' do
  549. expect(response).to have_http_status(404)
  550. end
  551. end
  552. end
  553. context 'when status is direct' do
  554. before do
  555. status.update(visibility: :direct)
  556. end
  557. context 'when user is authorized to see it' do
  558. before do
  559. Fabricate(:mention, account: remote_account, status: status)
  560. get :activity, params: { account_username: account.username, id: status.id }
  561. end
  562. it 'returns http success' do
  563. expect(response).to have_http_status(200)
  564. end
  565. end
  566. context 'when user is not authorized to see it' do
  567. before do
  568. get :activity, params: { account_username: account.username, id: status.id }
  569. end
  570. it 'returns http not_found' do
  571. expect(response).to have_http_status(404)
  572. end
  573. end
  574. end
  575. end
  576. end
  577. describe 'GET #embed' do
  578. let(:account) { Fabricate(:account) }
  579. let(:status) { Fabricate(:status, account: account) }
  580. context 'when account is suspended' do
  581. let(:account) { Fabricate(:account, suspended: true) }
  582. before do
  583. get :embed, params: { account_username: account.username, id: status.id }
  584. end
  585. it 'returns http gone' do
  586. expect(response).to have_http_status(410)
  587. end
  588. end
  589. context 'when status is a reblog' do
  590. let(:original_account) { Fabricate(:account, domain: 'example.com') }
  591. let(:original_status) { Fabricate(:status, account: original_account, url: 'https://example.com/123') }
  592. let(:status) { Fabricate(:status, account: account, reblog: original_status) }
  593. before do
  594. get :embed, params: { account_username: status.account.username, id: status.id }
  595. end
  596. it 'returns http not found' do
  597. expect(response).to have_http_status(404)
  598. end
  599. end
  600. context 'when status is public' do
  601. before do
  602. get :embed, params: { account_username: status.account.username, id: status.id }
  603. end
  604. it 'renders status successfully', :aggregate_failures do
  605. expect(response).to have_http_status(200)
  606. expect(response.headers['Link'].to_s).to include 'activity+json'
  607. expect(response.headers['Vary']).to eq 'Accept, Accept-Language, Cookie'
  608. expect(response.headers['Cache-Control']).to include 'public'
  609. expect(response).to render_template(:embed)
  610. expect(response.body).to include status.text
  611. end
  612. end
  613. context 'when status is private' do
  614. let(:status) { Fabricate(:status, account: account, visibility: :private) }
  615. before do
  616. get :embed, params: { account_username: status.account.username, id: status.id }
  617. end
  618. it 'returns http not found' do
  619. expect(response).to have_http_status(404)
  620. end
  621. end
  622. context 'when status is direct' do
  623. let(:status) { Fabricate(:status, account: account, visibility: :direct) }
  624. before do
  625. get :embed, params: { account_username: status.account.username, id: status.id }
  626. end
  627. it 'returns http not found' do
  628. expect(response).to have_http_status(404)
  629. end
  630. end
  631. end
  632. end