You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.5 KiB

  1. require 'rails_helper'
  2. RSpec.describe Api::V1::StatusesController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, application: app, scopes: scopes) }
  7. context 'with an oauth token' do
  8. before do
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. describe 'GET #show' do
  12. let(:scopes) { 'read:statuses' }
  13. let(:status) { Fabricate(:status, account: user.account) }
  14. it 'returns http success' do
  15. get :show, params: { id: status.id }
  16. expect(response).to have_http_status(200)
  17. end
  18. end
  19. describe 'GET #context' do
  20. let(:scopes) { 'read:statuses' }
  21. let(:status) { Fabricate(:status, account: user.account) }
  22. before do
  23. Fabricate(:status, account: user.account, thread: status)
  24. end
  25. it 'returns http success' do
  26. get :context, params: { id: status.id }
  27. expect(response).to have_http_status(200)
  28. end
  29. end
  30. describe 'POST #create' do
  31. let(:scopes) { 'write:statuses' }
  32. context do
  33. before do
  34. post :create, params: { status: 'Hello world' }
  35. end
  36. it 'returns http success' do
  37. expect(response).to have_http_status(200)
  38. end
  39. it 'returns rate limit headers' do
  40. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  41. expect(response.headers['X-RateLimit-Remaining']).to eq (RateLimiter::FAMILIES[:statuses][:limit] - 1).to_s
  42. end
  43. end
  44. context 'with missing parameters' do
  45. before do
  46. post :create, params: {}
  47. end
  48. it 'returns http unprocessable entity' do
  49. expect(response).to have_http_status(422)
  50. end
  51. it 'returns rate limit headers' do
  52. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  53. end
  54. end
  55. context 'when exceeding rate limit' do
  56. before do
  57. rate_limiter = RateLimiter.new(user.account, family: :statuses)
  58. 300.times { rate_limiter.record! }
  59. post :create, params: { status: 'Hello world' }
  60. end
  61. it 'returns http too many requests' do
  62. expect(response).to have_http_status(429)
  63. end
  64. it 'returns rate limit headers' do
  65. expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
  66. expect(response.headers['X-RateLimit-Remaining']).to eq '0'
  67. end
  68. end
  69. end
  70. describe 'DELETE #destroy' do
  71. let(:scopes) { 'write:statuses' }
  72. let(:status) { Fabricate(:status, account: user.account) }
  73. before do
  74. post :destroy, params: { id: status.id }
  75. end
  76. it 'returns http success' do
  77. expect(response).to have_http_status(200)
  78. end
  79. it 'removes the status' do
  80. expect(Status.find_by(id: status.id)).to be nil
  81. end
  82. end
  83. end
  84. context 'without an oauth token' do
  85. before do
  86. allow(controller).to receive(:doorkeeper_token) { nil }
  87. end
  88. context 'with a private status' do
  89. let(:status) { Fabricate(:status, account: user.account, visibility: :private) }
  90. describe 'GET #show' do
  91. it 'returns http unautharized' do
  92. get :show, params: { id: status.id }
  93. expect(response).to have_http_status(404)
  94. end
  95. end
  96. describe 'GET #context' do
  97. before do
  98. Fabricate(:status, account: user.account, thread: status)
  99. end
  100. it 'returns http unautharized' do
  101. get :context, params: { id: status.id }
  102. expect(response).to have_http_status(404)
  103. end
  104. end
  105. end
  106. context 'with a public status' do
  107. let(:status) { Fabricate(:status, account: user.account, visibility: :public) }
  108. describe 'GET #show' do
  109. it 'returns http success' do
  110. get :show, params: { id: status.id }
  111. expect(response).to have_http_status(200)
  112. end
  113. end
  114. describe 'GET #context' do
  115. before do
  116. Fabricate(:status, account: user.account, thread: status)
  117. end
  118. it 'returns http success' do
  119. get :context, params: { id: status.id }
  120. expect(response).to have_http_status(200)
  121. end
  122. end
  123. end
  124. end
  125. end