You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Api::V1::Statuses::FavouritesController do
  4. render_views
  5. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  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 private status that was not favourited' do
  65. let(:status) { Fabricate(:status, visibility: :private) }
  66. before do
  67. post :destroy, params: { status_id: status.id }
  68. end
  69. it 'returns http not found' do
  70. expect(response).to have_http_status(404)
  71. end
  72. end
  73. end
  74. end
  75. end