favourites_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Favourites', :inline_jobs do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'write:favourites' }
  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/favourite' do
  9. subject do
  10. post "/api/v1/statuses/#{status.id}/favourite", headers: headers
  11. end
  12. let(:status) { Fabricate(:status) }
  13. it_behaves_like 'forbidden for wrong scope', 'read read:favourites'
  14. context 'with public status' do
  15. it 'favourites 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.favourited?(status)).to be true
  21. expect(response.parsed_body).to match(
  22. a_hash_including(id: status.id.to_s, favourites_count: 1, favourited: 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 'favourites 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.favourited?(status)).to be true
  46. end
  47. end
  48. context 'without an authorization header' do
  49. let(:headers) { {} }
  50. it 'returns http unauthorized' do
  51. subject
  52. expect(response).to have_http_status(401)
  53. expect(response.content_type)
  54. .to start_with('application/json')
  55. end
  56. end
  57. end
  58. describe 'POST /api/v1/statuses/:status_id/unfavourite' do
  59. subject do
  60. post "/api/v1/statuses/#{status.id}/unfavourite", headers: headers
  61. end
  62. let(:status) { Fabricate(:status) }
  63. it_behaves_like 'forbidden for wrong scope', 'read read:favourites'
  64. context 'with public status' do
  65. before do
  66. FavouriteService.new.call(user.account, status)
  67. end
  68. it 'unfavourites the status successfully and includes updated json', :aggregate_failures do
  69. subject
  70. expect(response).to have_http_status(200)
  71. expect(response.content_type)
  72. .to start_with('application/json')
  73. expect(user.account.favourited?(status)).to be false
  74. expect(response.parsed_body).to match(
  75. a_hash_including(id: status.id.to_s, favourites_count: 0, favourited: false)
  76. )
  77. end
  78. end
  79. context 'when the requesting user was blocked by the status author' do
  80. before do
  81. FavouriteService.new.call(user.account, status)
  82. status.account.block!(user.account)
  83. end
  84. it 'unfavourites the status successfully and includes updated json', :aggregate_failures do
  85. subject
  86. expect(response).to have_http_status(200)
  87. expect(response.content_type)
  88. .to start_with('application/json')
  89. expect(user.account.favourited?(status)).to be false
  90. expect(response.parsed_body).to match(
  91. a_hash_including(id: status.id.to_s, favourites_count: 0, favourited: false)
  92. )
  93. end
  94. end
  95. context 'when status is not favourited' do
  96. it 'returns http success' do
  97. subject
  98. expect(response).to have_http_status(200)
  99. expect(response.content_type)
  100. .to start_with('application/json')
  101. end
  102. end
  103. context 'with private status that was not favourited' do
  104. let(:status) { Fabricate(:status, visibility: :private) }
  105. it 'returns http not found' do
  106. subject
  107. expect(response).to have_http_status(404)
  108. expect(response.content_type)
  109. .to start_with('application/json')
  110. end
  111. end
  112. end
  113. end