statuses_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe '/api/v1/statuses' do
  4. context 'with an oauth token' do
  5. let(:user) { Fabricate(:user) }
  6. let(:client_app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, application: client_app, scopes: scopes) }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'GET /api/v1/statuses?id[]=:id' do
  10. let(:status) { Fabricate(:status) }
  11. let(:other_status) { Fabricate(:status) }
  12. let(:scopes) { 'read:statuses' }
  13. it 'returns expected response' do
  14. get '/api/v1/statuses', headers: headers, params: { id: [status.id, other_status.id, 123_123] }
  15. expect(response).to have_http_status(200)
  16. expect(response.content_type)
  17. .to start_with('application/json')
  18. expect(response.parsed_body).to contain_exactly(
  19. hash_including(id: status.id.to_s),
  20. hash_including(id: other_status.id.to_s)
  21. )
  22. end
  23. end
  24. describe 'GET /api/v1/statuses/:id' do
  25. subject do
  26. get "/api/v1/statuses/#{status.id}", headers: headers
  27. end
  28. let(:scopes) { 'read:statuses' }
  29. let(:status) { Fabricate(:status, account: user.account) }
  30. it_behaves_like 'forbidden for wrong scope', 'write write:statuses'
  31. it 'returns http success' do
  32. subject
  33. expect(response).to have_http_status(200)
  34. expect(response.content_type)
  35. .to start_with('application/json')
  36. end
  37. context 'when post includes filtered terms' do
  38. let(:status) { Fabricate(:status, text: 'this toot is about that banned word') }
  39. before do
  40. user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  41. end
  42. it 'returns filter information', :aggregate_failures do
  43. subject
  44. expect(response).to have_http_status(200)
  45. expect(response.content_type)
  46. .to start_with('application/json')
  47. expect(response.parsed_body[:filtered][0]).to include({
  48. filter: a_hash_including({
  49. id: user.account.custom_filters.first.id.to_s,
  50. title: 'filter1',
  51. filter_action: 'hide',
  52. }),
  53. keyword_matches: ['banned'],
  54. })
  55. end
  56. end
  57. context 'when post is explicitly filtered' do
  58. let(:status) { Fabricate(:status, text: 'hello world') }
  59. before do
  60. filter = user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide)
  61. filter.statuses.create!(status_id: status.id)
  62. end
  63. it 'returns filter information', :aggregate_failures do
  64. subject
  65. expect(response).to have_http_status(200)
  66. expect(response.content_type)
  67. .to start_with('application/json')
  68. expect(response.parsed_body[:filtered][0]).to include({
  69. filter: a_hash_including({
  70. id: user.account.custom_filters.first.id.to_s,
  71. title: 'filter1',
  72. filter_action: 'hide',
  73. }),
  74. status_matches: [status.id.to_s],
  75. })
  76. end
  77. end
  78. context 'when reblog includes filtered terms' do
  79. let(:status) { Fabricate(:status, reblog: Fabricate(:status, text: 'this toot is about that banned word')) }
  80. before do
  81. user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
  82. end
  83. it 'returns filter information', :aggregate_failures do
  84. subject
  85. expect(response).to have_http_status(200)
  86. expect(response.content_type)
  87. .to start_with('application/json')
  88. expect(response.parsed_body[:reblog][:filtered][0]).to include({
  89. filter: a_hash_including({
  90. id: user.account.custom_filters.first.id.to_s,
  91. title: 'filter1',
  92. filter_action: 'hide',
  93. }),
  94. keyword_matches: ['banned'],
  95. })
  96. end
  97. end
  98. end
  99. describe 'GET /api/v1/statuses/:id/context' do
  100. let(:scopes) { 'read:statuses' }
  101. let(:status) { Fabricate(:status, account: user.account) }
  102. before do
  103. Fabricate(:status, account: user.account, thread: status)
  104. end
  105. it 'returns http success' do
  106. get "/api/v1/statuses/#{status.id}/context", headers: headers
  107. expect(response).to have_http_status(200)
  108. expect(response.content_type)
  109. .to start_with('application/json')
  110. end
  111. end
  112. describe 'POST /api/v1/statuses' do
  113. subject do
  114. post '/api/v1/statuses', headers: headers, params: params
  115. end
  116. let(:scopes) { 'write:statuses' }
  117. let(:params) { { status: 'Hello world' } }
  118. it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
  119. context 'with a basic status body' do
  120. it 'returns rate limit headers', :aggregate_failures do
  121. subject
  122. expect(response).to have_http_status(200)
  123. expect(response.content_type)
  124. .to start_with('application/json')
  125. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  126. expect(response.headers['X-RateLimit-Remaining']).to eq (RateLimiter::FAMILIES[:statuses][:limit] - 1).to_s
  127. end
  128. end
  129. context 'with a safeguard' do
  130. let!(:alice) { Fabricate(:account, username: 'alice') }
  131. let!(:bob) { Fabricate(:account, username: 'bob') }
  132. let(:params) { { status: '@alice hm, @bob is really annoying lately', allowed_mentions: [alice.id] } }
  133. it 'returns serialized extra accounts in body', :aggregate_failures do
  134. subject
  135. expect(response).to have_http_status(422)
  136. expect(response.content_type)
  137. .to start_with('application/json')
  138. expect(response.parsed_body[:unexpected_accounts].map { |a| a.slice(:id, :acct) }).to match [{ id: bob.id.to_s, acct: bob.acct }]
  139. end
  140. end
  141. context 'with missing parameters' do
  142. let(:params) { {} }
  143. it 'returns rate limit headers', :aggregate_failures do
  144. subject
  145. expect(response).to have_http_status(422)
  146. expect(response.content_type)
  147. .to start_with('application/json')
  148. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  149. end
  150. end
  151. context 'when exceeding rate limit' do
  152. before do
  153. rate_limiter = RateLimiter.new(user.account, family: :statuses)
  154. RateLimiter::FAMILIES[:statuses][:limit].times { rate_limiter.record! }
  155. end
  156. it 'returns rate limit headers', :aggregate_failures do
  157. subject
  158. expect(response).to have_http_status(429)
  159. expect(response.content_type)
  160. .to start_with('application/json')
  161. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  162. expect(response.headers['X-RateLimit-Remaining']).to eq '0'
  163. end
  164. end
  165. context 'with missing thread' do
  166. let(:params) { { status: 'Hello world', in_reply_to_id: 0 } }
  167. it 'returns http not found' do
  168. subject
  169. expect(response).to have_http_status(404)
  170. expect(response.content_type)
  171. .to start_with('application/json')
  172. end
  173. end
  174. context 'when scheduling a status' do
  175. let(:params) { { status: 'Hello world', scheduled_at: 10.minutes.from_now } }
  176. let(:account) { user.account }
  177. it 'returns HTTP 200' do
  178. subject
  179. expect(response).to have_http_status(200)
  180. expect(response.content_type)
  181. .to start_with('application/json')
  182. end
  183. it 'creates a scheduled status' do
  184. expect { subject }.to change { account.scheduled_statuses.count }.from(0).to(1)
  185. end
  186. context 'when the scheduling time is less than 5 minutes' do
  187. let(:params) { { status: 'Hello world', scheduled_at: 4.minutes.from_now } }
  188. it 'does not create a scheduled status', :aggregate_failures do
  189. subject
  190. expect(response).to have_http_status(422)
  191. expect(response.content_type)
  192. .to start_with('application/json')
  193. expect(account.scheduled_statuses).to be_empty
  194. end
  195. end
  196. end
  197. end
  198. describe 'DELETE /api/v1/statuses/:id' do
  199. subject do
  200. delete "/api/v1/statuses/#{status.id}", headers: headers
  201. end
  202. let(:scopes) { 'write:statuses' }
  203. let(:status) { Fabricate(:status, account: user.account) }
  204. it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
  205. it 'removes the status', :aggregate_failures do
  206. subject
  207. expect(response).to have_http_status(200)
  208. expect(response.content_type)
  209. .to start_with('application/json')
  210. expect(Status.find_by(id: status.id)).to be_nil
  211. end
  212. end
  213. describe 'PUT /api/v1/statuses/:id' do
  214. subject do
  215. put "/api/v1/statuses/#{status.id}", headers: headers, params: { status: 'I am updated' }
  216. end
  217. let(:scopes) { 'write:statuses' }
  218. let(:status) { Fabricate(:status, account: user.account) }
  219. it_behaves_like 'forbidden for wrong scope', 'read read:statuses'
  220. it 'updates the status', :aggregate_failures do
  221. subject
  222. expect(response).to have_http_status(200)
  223. expect(response.content_type)
  224. .to start_with('application/json')
  225. expect(status.reload.text).to eq 'I am updated'
  226. end
  227. end
  228. end
  229. context 'without an oauth token' do
  230. context 'with a private status' do
  231. let(:status) { Fabricate(:status, visibility: :private) }
  232. describe 'GET /api/v1/statuses/:id' do
  233. it 'returns http unauthorized' do
  234. get "/api/v1/statuses/#{status.id}"
  235. expect(response).to have_http_status(404)
  236. expect(response.content_type)
  237. .to start_with('application/json')
  238. end
  239. end
  240. describe 'GET /api/v1/statuses/:id/context' do
  241. before do
  242. Fabricate(:status, thread: status)
  243. end
  244. it 'returns http unauthorized' do
  245. get "/api/v1/statuses/#{status.id}/context"
  246. expect(response).to have_http_status(404)
  247. expect(response.content_type)
  248. .to start_with('application/json')
  249. end
  250. end
  251. end
  252. context 'with a public status' do
  253. let(:status) { Fabricate(:status, visibility: :public) }
  254. describe 'GET /api/v1/statuses/:id' do
  255. it 'returns http success' do
  256. get "/api/v1/statuses/#{status.id}"
  257. expect(response).to have_http_status(200)
  258. expect(response.content_type)
  259. .to start_with('application/json')
  260. end
  261. end
  262. describe 'GET /api/v1/statuses/:id/context' do
  263. before do
  264. Fabricate(:status, thread: status)
  265. end
  266. it 'returns http success' do
  267. get "/api/v1/statuses/#{status.id}/context"
  268. expect(response).to have_http_status(200)
  269. expect(response.content_type)
  270. .to start_with('application/json')
  271. end
  272. end
  273. end
  274. end
  275. end