bookmarks_spec.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Bookmarks' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'write:bookmarks' }
  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/bookmark' do
  9. subject do
  10. post "/api/v1/statuses/#{status.id}/bookmark", headers: headers
  11. end
  12. let(:status) { Fabricate(:status) }
  13. it_behaves_like 'forbidden for wrong scope', 'read'
  14. context 'with public status' do
  15. it 'bookmarks the status successfully and includes 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.bookmarked?(status)).to be true
  21. expect(response.parsed_body).to match(
  22. a_hash_including(id: status.id.to_s, bookmarked: true)
  23. )
  24. end
  25. end
  26. context 'with private status of not-followed account' do
  27. let(:status) { Fabricate(:status, visibility: :private) }
  28. it 'returns http not found' do
  29. subject
  30. expect(response).to have_http_status(404)
  31. expect(response.content_type)
  32. .to start_with('application/json')
  33. end
  34. end
  35. context 'with private status of followed account' do
  36. let(:status) { Fabricate(:status, visibility: :private) }
  37. before do
  38. user.account.follow!(status.account)
  39. end
  40. it 'bookmarks the status successfully', :aggregate_failures do
  41. subject
  42. expect(response).to have_http_status(200)
  43. expect(response.content_type)
  44. .to start_with('application/json')
  45. expect(user.account.bookmarked?(status)).to be true
  46. end
  47. end
  48. context 'when the status does not exist' do
  49. it 'returns http not found' do
  50. post '/api/v1/statuses/-1/bookmark', headers: headers
  51. expect(response).to have_http_status(404)
  52. expect(response.content_type)
  53. .to start_with('application/json')
  54. end
  55. end
  56. context 'without an authorization header' do
  57. let(:headers) { {} }
  58. it 'returns http unauthorized' do
  59. subject
  60. expect(response).to have_http_status(401)
  61. expect(response.content_type)
  62. .to start_with('application/json')
  63. end
  64. end
  65. end
  66. describe 'POST /api/v1/statuses/:status_id/unbookmark' do
  67. subject do
  68. post "/api/v1/statuses/#{status.id}/unbookmark", headers: headers
  69. end
  70. let(:status) { Fabricate(:status) }
  71. it_behaves_like 'forbidden for wrong scope', 'read'
  72. context 'with public status' do
  73. context 'when the status was previously bookmarked' do
  74. before do
  75. Bookmark.find_or_create_by!(account: user.account, status: status)
  76. end
  77. it 'unbookmarks the status successfully and includes updated json', :aggregate_failures do
  78. subject
  79. expect(response).to have_http_status(200)
  80. expect(response.content_type)
  81. .to start_with('application/json')
  82. expect(user.account.bookmarked?(status)).to be false
  83. expect(response.parsed_body).to match(
  84. a_hash_including(id: status.id.to_s, bookmarked: false)
  85. )
  86. end
  87. end
  88. context 'when the requesting user was blocked by the status author' do
  89. let(:status) { Fabricate(:status) }
  90. before do
  91. Bookmark.find_or_create_by!(account: user.account, status: status)
  92. status.account.block!(user.account)
  93. end
  94. it 'unbookmarks the status successfully and includes updated json', :aggregate_failures do
  95. subject
  96. expect(response).to have_http_status(200)
  97. expect(response.content_type)
  98. .to start_with('application/json')
  99. expect(user.account.bookmarked?(status)).to be false
  100. expect(response.parsed_body).to match(
  101. a_hash_including(id: status.id.to_s, bookmarked: false)
  102. )
  103. end
  104. end
  105. context 'when the status is not bookmarked' do
  106. it 'returns http success' do
  107. subject
  108. expect(response).to have_http_status(200)
  109. expect(response.content_type)
  110. .to start_with('application/json')
  111. end
  112. end
  113. end
  114. context 'with private status that was not bookmarked' do
  115. let(:status) { Fabricate(:status, visibility: :private) }
  116. it 'returns http not found' do
  117. subject
  118. expect(response).to have_http_status(404)
  119. expect(response.content_type)
  120. .to start_with('application/json')
  121. end
  122. end
  123. end
  124. end