accounts_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Accounts show response' do
  4. let(:account) { Fabricate(:account) }
  5. context 'with an unapproved account' do
  6. before { account.user.update(approved: false) }
  7. it 'returns http not found' do
  8. %w(html json rss).each do |format|
  9. get short_account_path(username: account.username), as: format
  10. expect(response).to have_http_status(404)
  11. end
  12. end
  13. end
  14. context 'with a permanently suspended account' do
  15. before do
  16. account.suspend!
  17. account.deletion_request.destroy
  18. end
  19. it 'returns http gone' do
  20. %w(html json rss).each do |format|
  21. get short_account_path(username: account.username), as: format
  22. expect(response).to have_http_status(410)
  23. end
  24. end
  25. end
  26. context 'with a temporarily suspended account' do
  27. before { account.suspend! }
  28. it 'returns appropriate http response code' do
  29. { html: 403, json: 200, rss: 403 }.each do |format, code|
  30. get short_account_path(username: account.username), as: format
  31. expect(response).to have_http_status(code)
  32. end
  33. end
  34. end
  35. describe 'GET to short username paths' do
  36. context 'with existing statuses' do
  37. context 'with HTML' do
  38. let(:format) { 'html' }
  39. shared_examples 'common HTML response' do
  40. it 'returns a standard HTML response', :aggregate_failures do
  41. expect(response)
  42. .to have_http_status(200)
  43. .and render_template(:show)
  44. .and have_http_link_header(ActivityPub::TagManager.instance.uri_for(account)).for(rel: 'alternate')
  45. end
  46. end
  47. context 'with a normal account in an HTML request' do
  48. before do
  49. get short_account_path(username: account.username), as: format
  50. end
  51. it_behaves_like 'common HTML response'
  52. end
  53. context 'with replies' do
  54. before do
  55. get short_account_with_replies_path(username: account.username), as: format
  56. end
  57. it_behaves_like 'common HTML response'
  58. end
  59. context 'with media' do
  60. before do
  61. get short_account_media_path(username: account.username), as: format
  62. end
  63. it_behaves_like 'common HTML response'
  64. end
  65. context 'with tag' do
  66. let(:tag) { Fabricate(:tag) }
  67. let!(:status_tag) { Fabricate(:status, account: account) }
  68. before do
  69. status_tag.tags << tag
  70. get short_account_tag_path(username: account.username, tag: tag), as: format
  71. end
  72. it_behaves_like 'common HTML response'
  73. end
  74. end
  75. context 'with JSON' do
  76. let(:authorized_fetch_mode) { false }
  77. let(:headers) { { 'ACCEPT' => 'application/json' } }
  78. around do |example|
  79. ClimateControl.modify AUTHORIZED_FETCH: authorized_fetch_mode.to_s do
  80. example.run
  81. end
  82. end
  83. context 'with a normal account in a JSON request' do
  84. before do
  85. get short_account_path(username: account.username), headers: headers
  86. end
  87. it 'returns a JSON version of the account', :aggregate_failures do
  88. expect(response)
  89. .to have_http_status(200)
  90. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  91. .and have_attributes(
  92. media_type: eq('application/activity+json')
  93. )
  94. expect(response.parsed_body).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  95. end
  96. context 'with authorized fetch mode' do
  97. let(:authorized_fetch_mode) { true }
  98. it 'returns http unauthorized' do
  99. expect(response).to have_http_status(401)
  100. end
  101. end
  102. end
  103. context 'when signed in' do
  104. let(:user) { Fabricate(:user) }
  105. before do
  106. sign_in(user)
  107. get short_account_path(username: account.username), headers: headers.merge({ 'Cookie' => '123' })
  108. end
  109. it 'returns a private JSON version of the account', :aggregate_failures do
  110. expect(response)
  111. .to have_http_status(200)
  112. .and have_attributes(
  113. media_type: eq('application/activity+json')
  114. )
  115. expect(response.headers['Cache-Control']).to include 'private'
  116. expect(response.parsed_body).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  117. end
  118. end
  119. context 'with signature' do
  120. let(:remote_account) { Fabricate(:account, domain: 'example.com') }
  121. before do
  122. get short_account_path(username: account.username), headers: headers, sign_with: remote_account
  123. end
  124. it 'returns a JSON version of the account', :aggregate_failures do
  125. expect(response)
  126. .to have_http_status(200)
  127. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  128. .and have_attributes(
  129. media_type: eq('application/activity+json')
  130. )
  131. expect(response.parsed_body).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  132. end
  133. context 'with authorized fetch mode' do
  134. let(:authorized_fetch_mode) { true }
  135. it 'returns a private signature JSON version of the account', :aggregate_failures do
  136. expect(response)
  137. .to have_http_status(200)
  138. .and have_attributes(
  139. media_type: eq('application/activity+json')
  140. )
  141. expect(response.headers['Cache-Control']).to include 'private'
  142. expect(response.headers['Vary']).to include 'Signature'
  143. expect(response.parsed_body).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
  144. end
  145. end
  146. end
  147. end
  148. context 'with RSS' do
  149. let(:format) { 'rss' }
  150. let!(:status) { Fabricate(:status, account: account) }
  151. let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) }
  152. let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) }
  153. let!(:status_media) { Fabricate(:status, account: account) }
  154. let!(:status_pinned) { Fabricate(:status, account: account) }
  155. let!(:status_private) { Fabricate(:status, account: account, visibility: :private) }
  156. let!(:status_direct) { Fabricate(:status, account: account, visibility: :direct) }
  157. let!(:status_reblog) { Fabricate(:status, account: account, reblog: Fabricate(:status)) }
  158. before do
  159. status_media.media_attachments << Fabricate(:media_attachment, account: account, type: :image)
  160. account.pinned_statuses << status_pinned
  161. account.pinned_statuses << status_private
  162. end
  163. context 'with a normal account in an RSS request' do
  164. before do
  165. get short_account_path(username: account.username, format: format)
  166. end
  167. it 'responds with correct statuses', :aggregate_failures do
  168. expect(response)
  169. .to have_http_status(200)
  170. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  171. expect(response.body).to include(status_tag_for(status_media))
  172. expect(response.body).to include(status_tag_for(status_self_reply))
  173. expect(response.body).to include(status_tag_for(status))
  174. expect(response.body).to_not include(status_tag_for(status_direct))
  175. expect(response.body).to_not include(status_tag_for(status_private))
  176. expect(response.body).to_not include(status_tag_for(status_reblog.reblog))
  177. expect(response.body).to_not include(status_tag_for(status_reply))
  178. end
  179. end
  180. context 'with replies' do
  181. before do
  182. get short_account_with_replies_path(username: account.username, format: format)
  183. end
  184. it 'responds with correct statuses with replies', :aggregate_failures do
  185. expect(response)
  186. .to have_http_status(200)
  187. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  188. expect(response.body).to include(status_tag_for(status_media))
  189. expect(response.body).to include(status_tag_for(status_reply))
  190. expect(response.body).to include(status_tag_for(status_self_reply))
  191. expect(response.body).to include(status_tag_for(status))
  192. expect(response.body).to_not include(status_tag_for(status_direct))
  193. expect(response.body).to_not include(status_tag_for(status_private))
  194. expect(response.body).to_not include(status_tag_for(status_reblog.reblog))
  195. end
  196. end
  197. context 'with media' do
  198. before do
  199. get short_account_media_path(username: account.username, format: format)
  200. end
  201. it 'responds with correct statuses with media', :aggregate_failures do
  202. expect(response)
  203. .to have_http_status(200)
  204. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  205. expect(response.body).to include(status_tag_for(status_media))
  206. expect(response.body).to_not include(status_tag_for(status_direct))
  207. expect(response.body).to_not include(status_tag_for(status_private))
  208. expect(response.body).to_not include(status_tag_for(status_reblog.reblog))
  209. expect(response.body).to_not include(status_tag_for(status_reply))
  210. expect(response.body).to_not include(status_tag_for(status_self_reply))
  211. expect(response.body).to_not include(status_tag_for(status))
  212. end
  213. end
  214. context 'with tag' do
  215. let(:tag) { Fabricate(:tag) }
  216. let!(:status_tag) { Fabricate(:status, account: account) }
  217. before do
  218. status_tag.tags << tag
  219. get short_account_tag_path(username: account.username, tag: tag, format: format)
  220. end
  221. it 'responds with correct statuses with a tag', :aggregate_failures do
  222. expect(response)
  223. .to have_http_status(200)
  224. .and have_cacheable_headers.with_vary('Accept, Accept-Language, Cookie')
  225. expect(response.body).to include(status_tag_for(status_tag))
  226. expect(response.body).to_not include(status_tag_for(status_direct))
  227. expect(response.body).to_not include(status_tag_for(status_media))
  228. expect(response.body).to_not include(status_tag_for(status_private))
  229. expect(response.body).to_not include(status_tag_for(status_reblog.reblog))
  230. expect(response.body).to_not include(status_tag_for(status_reply))
  231. expect(response.body).to_not include(status_tag_for(status_self_reply))
  232. expect(response.body).to_not include(status_tag_for(status))
  233. end
  234. end
  235. end
  236. end
  237. end
  238. def status_tag_for(status)
  239. ActivityPub::TagManager.instance.url_for(status)
  240. end
  241. end