accounts_controller_spec.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe AccountsController do
  4. render_views
  5. let(:account) { Fabricate(:account) }
  6. shared_examples 'cacheable response' do
  7. it 'does not set cookies' do
  8. expect(response.cookies).to be_empty
  9. expect(response.headers['Set-Cookies']).to be_nil
  10. end
  11. it 'does not set sessions' do
  12. expect(session).to be_empty
  13. end
  14. it 'returns Vary header' do
  15. expect(response.headers['Vary']).to include 'Accept'
  16. end
  17. it 'returns public Cache-Control header' do
  18. expect(response.headers['Cache-Control']).to include 'public'
  19. end
  20. end
  21. describe 'GET #show' do
  22. let(:format) { 'html' }
  23. let!(:status) { Fabricate(:status, account: account) }
  24. let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) }
  25. let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) }
  26. let!(:status_media) { Fabricate(:status, account: account) }
  27. let!(:status_pinned) { Fabricate(:status, account: account) }
  28. let!(:status_private) { Fabricate(:status, account: account, visibility: :private) }
  29. let!(:status_direct) { Fabricate(:status, account: account, visibility: :direct) }
  30. let!(:status_reblog) { Fabricate(:status, account: account, reblog: Fabricate(:status)) }
  31. before do
  32. status_media.media_attachments << Fabricate(:media_attachment, account: account, type: :image)
  33. account.pinned_statuses << status_pinned
  34. account.pinned_statuses << status_private
  35. end
  36. shared_examples 'preliminary checks' do
  37. context 'when account is not approved' do
  38. before do
  39. account.user.update(approved: false)
  40. end
  41. it 'returns http not found' do
  42. get :show, params: { username: account.username, format: format }
  43. expect(response).to have_http_status(404)
  44. end
  45. end
  46. end
  47. context 'with HTML' do
  48. let(:format) { 'html' }
  49. it_behaves_like 'preliminary checks'
  50. context 'when account is permanently suspended' do
  51. before do
  52. account.suspend!
  53. account.deletion_request.destroy
  54. end
  55. it 'returns http gone' do
  56. get :show, params: { username: account.username, format: format }
  57. expect(response).to have_http_status(410)
  58. end
  59. end
  60. context 'when account is temporarily suspended' do
  61. before do
  62. account.suspend!
  63. end
  64. it 'returns http forbidden' do
  65. get :show, params: { username: account.username, format: format }
  66. expect(response).to have_http_status(403)
  67. end
  68. end
  69. shared_examples 'common response characteristics' do
  70. it 'returns http success' do
  71. expect(response).to have_http_status(200)
  72. end
  73. it 'returns Link header' do
  74. expect(response.headers['Link'].to_s).to include ActivityPub::TagManager.instance.uri_for(account)
  75. end
  76. it 'renders show template' do
  77. expect(response).to render_template(:show)
  78. end
  79. end
  80. context 'with a normal account in an HTML request' do
  81. before do
  82. get :show, params: { username: account.username, format: format }
  83. end
  84. it_behaves_like 'common response characteristics'
  85. end
  86. context 'with replies' do
  87. before do
  88. allow(controller).to receive(:replies_requested?).and_return(true)
  89. get :show, params: { username: account.username, format: format }
  90. end
  91. it_behaves_like 'common response characteristics'
  92. end
  93. context 'with media' do
  94. before do
  95. allow(controller).to receive(:media_requested?).and_return(true)
  96. get :show, params: { username: account.username, format: format }
  97. end
  98. it_behaves_like 'common response characteristics'
  99. end
  100. context 'with tag' do
  101. let(:tag) { Fabricate(:tag) }
  102. let!(:status_tag) { Fabricate(:status, account: account) }
  103. before do
  104. allow(controller).to receive(:tag_requested?).and_return(true)
  105. status_tag.tags << tag
  106. get :show, params: { username: account.username, format: format, tag: tag.to_param }
  107. end
  108. it_behaves_like 'common response characteristics'
  109. end
  110. end
  111. context 'with JSON' do
  112. let(:authorized_fetch_mode) { false }
  113. let(:format) { 'json' }
  114. before do
  115. allow(controller).to receive(:authorized_fetch_mode?).and_return(authorized_fetch_mode)
  116. end
  117. it_behaves_like 'preliminary checks'
  118. context 'when account is suspended permanently' do
  119. before do
  120. account.suspend!
  121. account.deletion_request.destroy
  122. end
  123. it 'returns http gone' do
  124. get :show, params: { username: account.username, format: format }
  125. expect(response).to have_http_status(410)
  126. end
  127. end
  128. context 'when account is suspended temporarily' do
  129. before do
  130. account.suspend!
  131. end
  132. it 'returns http success' do
  133. get :show, params: { username: account.username, format: format }
  134. expect(response).to have_http_status(200)
  135. end
  136. end
  137. context 'with a normal account in a JSON request' do
  138. before do
  139. get :show, params: { username: account.username, format: format }
  140. end
  141. it 'returns http success' do
  142. expect(response).to have_http_status(200)
  143. end
  144. it 'returns application/activity+json' do
  145. expect(response.media_type).to eq 'application/activity+json'
  146. end
  147. it_behaves_like 'cacheable response'
  148. it 'renders account' do
  149. json = body_as_json
  150. expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  151. end
  152. context 'with authorized fetch mode' do
  153. let(:authorized_fetch_mode) { true }
  154. it 'returns http unauthorized' do
  155. expect(response).to have_http_status(401)
  156. end
  157. end
  158. end
  159. context 'when signed in' do
  160. let(:user) { Fabricate(:user) }
  161. before do
  162. sign_in(user)
  163. get :show, params: { username: account.username, format: format }
  164. end
  165. it 'returns http success' do
  166. expect(response).to have_http_status(200)
  167. end
  168. it 'returns application/activity+json' do
  169. expect(response.media_type).to eq 'application/activity+json'
  170. end
  171. it 'returns private Cache-Control header' do
  172. expect(response.headers['Cache-Control']).to include 'private'
  173. end
  174. it 'renders account' do
  175. json = body_as_json
  176. expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  177. end
  178. end
  179. context 'with signature' do
  180. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  181. before do
  182. allow(controller).to receive(:signed_request_actor).and_return(remote_account)
  183. get :show, params: { username: account.username, format: format }
  184. end
  185. it 'returns http success' do
  186. expect(response).to have_http_status(200)
  187. end
  188. it 'returns application/activity+json' do
  189. expect(response.media_type).to eq 'application/activity+json'
  190. end
  191. it_behaves_like 'cacheable response'
  192. it 'renders account' do
  193. json = body_as_json
  194. expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  195. end
  196. context 'with authorized fetch mode' do
  197. let(:authorized_fetch_mode) { true }
  198. it 'returns http success' do
  199. expect(response).to have_http_status(200)
  200. end
  201. it 'returns application/activity+json' do
  202. expect(response.media_type).to eq 'application/activity+json'
  203. end
  204. it 'returns private Cache-Control header' do
  205. expect(response.headers['Cache-Control']).to include 'private'
  206. end
  207. it 'returns Vary header with Signature' do
  208. expect(response.headers['Vary']).to include 'Signature'
  209. end
  210. it 'renders account' do
  211. json = body_as_json
  212. expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  213. end
  214. end
  215. end
  216. end
  217. context 'with RSS' do
  218. let(:format) { 'rss' }
  219. it_behaves_like 'preliminary checks'
  220. context 'when account is permanently suspended' do
  221. before do
  222. account.suspend!
  223. account.deletion_request.destroy
  224. end
  225. it 'returns http gone' do
  226. get :show, params: { username: account.username, format: format }
  227. expect(response).to have_http_status(410)
  228. end
  229. end
  230. context 'when account is temporarily suspended' do
  231. before do
  232. account.suspend!
  233. end
  234. it 'returns http forbidden' do
  235. get :show, params: { username: account.username, format: format }
  236. expect(response).to have_http_status(403)
  237. end
  238. end
  239. shared_examples 'common response characteristics' do
  240. it 'returns http success' do
  241. expect(response).to have_http_status(200)
  242. end
  243. it_behaves_like 'cacheable response'
  244. end
  245. context 'with a normal account in an RSS request' do
  246. before do
  247. get :show, params: { username: account.username, format: format }
  248. end
  249. it_behaves_like 'common response characteristics'
  250. it 'renders public status' do
  251. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status))
  252. end
  253. it 'renders self-reply' do
  254. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply))
  255. end
  256. it 'renders status with media' do
  257. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
  258. end
  259. it 'does not render reblog' do
  260. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
  261. end
  262. it 'does not render private status' do
  263. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
  264. end
  265. it 'does not render direct status' do
  266. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
  267. end
  268. it 'does not render reply to someone else' do
  269. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
  270. end
  271. end
  272. context 'with replies' do
  273. before do
  274. allow(controller).to receive(:replies_requested?).and_return(true)
  275. get :show, params: { username: account.username, format: format }
  276. end
  277. it_behaves_like 'common response characteristics'
  278. it 'renders public status' do
  279. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status))
  280. end
  281. it 'renders self-reply' do
  282. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply))
  283. end
  284. it 'renders status with media' do
  285. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
  286. end
  287. it 'does not render reblog' do
  288. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
  289. end
  290. it 'does not render private status' do
  291. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
  292. end
  293. it 'does not render direct status' do
  294. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
  295. end
  296. it 'renders reply to someone else' do
  297. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_reply))
  298. end
  299. end
  300. context 'with media' do
  301. before do
  302. allow(controller).to receive(:media_requested?).and_return(true)
  303. get :show, params: { username: account.username, format: format }
  304. end
  305. it_behaves_like 'common response characteristics'
  306. it 'does not render public status' do
  307. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status))
  308. end
  309. it 'does not render self-reply' do
  310. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply))
  311. end
  312. it 'renders status with media' do
  313. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
  314. end
  315. it 'does not render reblog' do
  316. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
  317. end
  318. it 'does not render private status' do
  319. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
  320. end
  321. it 'does not render direct status' do
  322. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
  323. end
  324. it 'does not render reply to someone else' do
  325. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
  326. end
  327. end
  328. context 'with tag' do
  329. let(:tag) { Fabricate(:tag) }
  330. let!(:status_tag) { Fabricate(:status, account: account) }
  331. before do
  332. allow(controller).to receive(:tag_requested?).and_return(true)
  333. status_tag.tags << tag
  334. get :show, params: { username: account.username, format: format, tag: tag.to_param }
  335. end
  336. it_behaves_like 'common response characteristics'
  337. it 'does not render public status' do
  338. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status))
  339. end
  340. it 'does not render self-reply' do
  341. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply))
  342. end
  343. it 'does not render status with media' do
  344. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_media))
  345. end
  346. it 'does not render reblog' do
  347. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
  348. end
  349. it 'does not render private status' do
  350. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
  351. end
  352. it 'does not render direct status' do
  353. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
  354. end
  355. it 'does not render reply to someone else' do
  356. expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
  357. end
  358. it 'renders status with tag' do
  359. expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_tag))
  360. end
  361. end
  362. end
  363. end
  364. end