favourites_controller_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Api::V1::Statuses::FavouritesController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'write:favourites', application: app) }
  8. context 'with an oauth token' do
  9. before do
  10. allow(controller).to receive(:doorkeeper_token) { token }
  11. end
  12. describe 'POST #create' do
  13. let(:status) { Fabricate(:status, account: user.account) }
  14. before do
  15. post :create, params: { status_id: status.id }
  16. end
  17. context 'with public status' do
  18. it 'returns http success' do
  19. expect(response).to have_http_status(200)
  20. end
  21. it 'updates the favourites count' do
  22. expect(status.favourites.count).to eq 1
  23. end
  24. it 'updates the favourited attribute' do
  25. expect(user.account.favourited?(status)).to be true
  26. end
  27. it 'returns json with updated attributes' do
  28. hash_body = body_as_json
  29. expect(hash_body[:id]).to eq status.id.to_s
  30. expect(hash_body[:favourites_count]).to eq 1
  31. expect(hash_body[:favourited]).to be true
  32. end
  33. end
  34. context 'with private status of not-followed account' do
  35. let(:status) { Fabricate(:status, visibility: :private) }
  36. it 'returns http not found' do
  37. expect(response).to have_http_status(404)
  38. end
  39. end
  40. end
  41. describe 'POST #destroy' do
  42. context 'with public status' do
  43. let(:status) { Fabricate(:status, account: user.account) }
  44. before do
  45. FavouriteService.new.call(user.account, status)
  46. post :destroy, params: { status_id: status.id }
  47. end
  48. it 'returns http success' do
  49. expect(response).to have_http_status(200)
  50. end
  51. it 'updates the favourites count' do
  52. expect(status.favourites.count).to eq 0
  53. end
  54. it 'updates the favourited attribute' do
  55. expect(user.account.favourited?(status)).to be false
  56. end
  57. it 'returns json with updated attributes' do
  58. hash_body = body_as_json
  59. expect(hash_body[:id]).to eq status.id.to_s
  60. expect(hash_body[:favourites_count]).to eq 0
  61. expect(hash_body[:favourited]).to be false
  62. end
  63. end
  64. context 'with public status when blocked by its author' do
  65. let(:status) { Fabricate(:status) }
  66. before do
  67. FavouriteService.new.call(user.account, status)
  68. status.account.block!(user.account)
  69. post :destroy, params: { status_id: status.id }
  70. end
  71. it 'returns http success' do
  72. expect(response).to have_http_status(200)
  73. end
  74. it 'updates the favourite attribute' do
  75. expect(user.account.favourited?(status)).to be false
  76. end
  77. it 'returns json with updated attributes' do
  78. hash_body = body_as_json
  79. expect(hash_body[:id]).to eq status.id.to_s
  80. expect(hash_body[:favourited]).to be false
  81. end
  82. end
  83. context 'with private status that was not favourited' do
  84. let(:status) { Fabricate(:status, visibility: :private) }
  85. before do
  86. post :destroy, params: { status_id: status.id }
  87. end
  88. it 'returns http not found' do
  89. expect(response).to have_http_status(404)
  90. end
  91. end
  92. end
  93. end
  94. end