relationships_controller_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Api::V1::Accounts::RelationshipsController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:follows') }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. describe 'GET #index' do
  11. let(:simon) { Fabricate(:account) }
  12. let(:lewis) { Fabricate(:account) }
  13. before do
  14. user.account.follow!(simon)
  15. lewis.follow!(user.account)
  16. end
  17. context 'when provided only one ID' do
  18. before do
  19. get :index, params: { id: simon.id }
  20. end
  21. it 'returns http success' do
  22. expect(response).to have_http_status(200)
  23. end
  24. it 'returns JSON with correct data' do
  25. json = body_as_json
  26. expect(json).to be_a Enumerable
  27. expect(json.first[:following]).to be true
  28. expect(json.first[:followed_by]).to be false
  29. end
  30. end
  31. context 'when provided multiple IDs' do
  32. before do
  33. get :index, params: { id: [simon.id, lewis.id] }
  34. end
  35. it 'returns http success' do
  36. expect(response).to have_http_status(200)
  37. end
  38. context 'when there is returned JSON data' do
  39. let(:json) { body_as_json }
  40. it 'returns an enumerable json' do
  41. expect(json).to be_a Enumerable
  42. end
  43. it 'returns a correct first element' do
  44. expect(json.first[:id]).to eq simon.id.to_s
  45. expect(json.first[:following]).to be true
  46. expect(json.first[:showing_reblogs]).to be true
  47. expect(json.first[:followed_by]).to be false
  48. expect(json.first[:muting]).to be false
  49. expect(json.first[:requested]).to be false
  50. expect(json.first[:domain_blocking]).to be false
  51. end
  52. it 'returns a correct second element' do
  53. expect(json.second[:id]).to eq lewis.id.to_s
  54. expect(json.second[:following]).to be false
  55. expect(json.second[:showing_reblogs]).to be false
  56. expect(json.second[:followed_by]).to be true
  57. expect(json.second[:muting]).to be false
  58. expect(json.second[:requested]).to be false
  59. expect(json.second[:domain_blocking]).to be false
  60. end
  61. end
  62. it 'returns JSON with correct data on cached requests too' do
  63. get :index, params: { id: [simon.id] }
  64. json = body_as_json
  65. expect(json).to be_a Enumerable
  66. expect(json.first[:following]).to be true
  67. expect(json.first[:showing_reblogs]).to be true
  68. end
  69. it 'returns JSON with correct data after change too' do
  70. user.account.unfollow!(simon)
  71. get :index, params: { id: [simon.id] }
  72. json = body_as_json
  73. expect(json).to be_a Enumerable
  74. expect(json.first[:following]).to be false
  75. expect(json.first[:showing_reblogs]).to be false
  76. end
  77. end
  78. end
  79. end