statuses_spec.rb 9.7 KB

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