bookmarks_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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', :aggregate_failures do
  16. subject
  17. expect(response).to have_http_status(200)
  18. expect(user.account.bookmarked?(status)).to be true
  19. end
  20. it 'returns json with updated attributes' do
  21. subject
  22. expect(body_as_json).to match(
  23. a_hash_including(id: status.id.to_s, bookmarked: true)
  24. )
  25. end
  26. end
  27. context 'with private status of not-followed account' do
  28. let(:status) { Fabricate(:status, visibility: :private) }
  29. it 'returns http not found' do
  30. subject
  31. expect(response).to have_http_status(404)
  32. end
  33. end
  34. context 'with private status of followed account' do
  35. let(:status) { Fabricate(:status, visibility: :private) }
  36. before do
  37. user.account.follow!(status.account)
  38. end
  39. it 'bookmarks the status successfully', :aggregate_failures do
  40. subject
  41. expect(response).to have_http_status(200)
  42. expect(user.account.bookmarked?(status)).to be true
  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/bookmark', headers: headers
  48. expect(response).to have_http_status(404)
  49. end
  50. end
  51. context 'without an authorization header' do
  52. let(:headers) { {} }
  53. it 'returns http unauthorized' do
  54. subject
  55. expect(response).to have_http_status(401)
  56. end
  57. end
  58. end
  59. describe 'POST /api/v1/statuses/:status_id/unbookmark' do
  60. subject do
  61. post "/api/v1/statuses/#{status.id}/unbookmark", headers: headers
  62. end
  63. let(:status) { Fabricate(:status) }
  64. it_behaves_like 'forbidden for wrong scope', 'read'
  65. context 'with public status' do
  66. context 'when the status was previously bookmarked' do
  67. before do
  68. Bookmark.find_or_create_by!(account: user.account, status: status)
  69. end
  70. it 'unbookmarks the status successfully', :aggregate_failures do
  71. subject
  72. expect(response).to have_http_status(200)
  73. expect(user.account.bookmarked?(status)).to be false
  74. end
  75. it 'returns json with updated attributes' do
  76. subject
  77. expect(body_as_json).to match(
  78. a_hash_including(id: status.id.to_s, bookmarked: false)
  79. )
  80. end
  81. end
  82. context 'when the requesting user was blocked by the status author' do
  83. let(:status) { Fabricate(:status) }
  84. before do
  85. Bookmark.find_or_create_by!(account: user.account, status: status)
  86. status.account.block!(user.account)
  87. end
  88. it 'unbookmarks the status successfully', :aggregate_failures do
  89. subject
  90. expect(response).to have_http_status(200)
  91. expect(user.account.bookmarked?(status)).to be false
  92. end
  93. it 'returns json with updated attributes' do
  94. subject
  95. expect(body_as_json).to match(
  96. a_hash_including(id: status.id.to_s, bookmarked: false)
  97. )
  98. end
  99. end
  100. context 'when the status is not bookmarked' do
  101. it 'returns http success' do
  102. subject
  103. expect(response).to have_http_status(200)
  104. end
  105. end
  106. end
  107. context 'with private status that was not bookmarked' do
  108. let(:status) { Fabricate(:status, visibility: :private) }
  109. it 'returns http not found' do
  110. subject
  111. expect(response).to have_http_status(404)
  112. end
  113. end
  114. end
  115. end