pins_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Pins' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'write:accounts' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'POST /api/v1/statuses/:status_id/pin' do
  9. subject do
  10. post "/api/v1/statuses/#{status.id}/pin", headers: headers
  11. end
  12. let(:status) { Fabricate(:status, account: user.account) }
  13. it_behaves_like 'forbidden for wrong scope', 'read read:accounts'
  14. context 'when the status is public' do
  15. it 'pins the status successfully and returns updated json', :aggregate_failures do
  16. subject
  17. expect(response).to have_http_status(200)
  18. expect(response.content_type)
  19. .to start_with('application/json')
  20. expect(user.account.pinned?(status)).to be true
  21. expect(response.parsed_body).to match(
  22. a_hash_including(id: status.id.to_s, pinned: true)
  23. )
  24. end
  25. end
  26. context 'when the status is private' do
  27. let(:status) { Fabricate(:status, account: user.account, visibility: :private) }
  28. it 'pins the status successfully', :aggregate_failures do
  29. subject
  30. expect(response).to have_http_status(200)
  31. expect(response.content_type)
  32. .to start_with('application/json')
  33. expect(user.account.pinned?(status)).to be true
  34. end
  35. end
  36. context 'when the status belongs to somebody else' do
  37. let(:status) { Fabricate(:status) }
  38. it 'returns http unprocessable entity' do
  39. subject
  40. expect(response).to have_http_status(422)
  41. expect(response.content_type)
  42. .to start_with('application/json')
  43. end
  44. end
  45. context 'when the status does not exist' do
  46. it 'returns http not found' do
  47. post '/api/v1/statuses/-1/pin', headers: headers
  48. expect(response).to have_http_status(404)
  49. expect(response.content_type)
  50. .to start_with('application/json')
  51. end
  52. end
  53. context 'without an authorization header' do
  54. let(:headers) { {} }
  55. it 'returns http unauthorized' do
  56. subject
  57. expect(response).to have_http_status(401)
  58. expect(response.content_type)
  59. .to start_with('application/json')
  60. end
  61. end
  62. end
  63. describe 'POST /api/v1/statuses/:status_id/unpin' do
  64. subject do
  65. post "/api/v1/statuses/#{status.id}/unpin", headers: headers
  66. end
  67. let(:status) { Fabricate(:status, account: user.account) }
  68. context 'when the status is pinned' do
  69. before do
  70. Fabricate(:status_pin, status: status, account: user.account)
  71. end
  72. it 'unpins the status successfully and includes updated json', :aggregate_failures do
  73. subject
  74. expect(response).to have_http_status(200)
  75. expect(response.content_type)
  76. .to start_with('application/json')
  77. expect(user.account.pinned?(status)).to be false
  78. expect(response.parsed_body).to match(
  79. a_hash_including(id: status.id.to_s, pinned: false)
  80. )
  81. end
  82. end
  83. context 'when the status is not pinned' do
  84. it 'returns http success' do
  85. subject
  86. expect(response).to have_http_status(200)
  87. expect(response.content_type)
  88. .to start_with('application/json')
  89. end
  90. end
  91. context 'when the status does not exist' do
  92. it 'returns http not found' do
  93. post '/api/v1/statuses/-1/unpin', headers: headers
  94. expect(response).to have_http_status(404)
  95. expect(response.content_type)
  96. .to start_with('application/json')
  97. end
  98. end
  99. context 'without an authorization header' do
  100. let(:headers) { {} }
  101. it 'returns http unauthorized' do
  102. subject
  103. expect(response).to have_http_status(401)
  104. expect(response.content_type)
  105. .to start_with('application/json')
  106. end
  107. end
  108. end
  109. end