relationships_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'GET /api/v1/accounts/relationships' do
  4. subject do
  5. get '/api/v1/accounts/relationships', headers: headers, params: params
  6. end
  7. let(:user) { Fabricate(:user) }
  8. let(:scopes) { 'read:follows' }
  9. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  10. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  11. let(:simon) { Fabricate(:account) }
  12. let(:lewis) { Fabricate(:account) }
  13. let(:bob) { Fabricate(:account, suspended: true) }
  14. before do
  15. user.account.follow!(simon)
  16. lewis.follow!(user.account)
  17. end
  18. context 'when provided only one ID' do
  19. let(:params) { { id: simon.id } }
  20. it 'returns JSON with correct data', :aggregate_failures do
  21. subject
  22. expect(response)
  23. .to have_http_status(200)
  24. expect(response.content_type)
  25. .to start_with('application/json')
  26. expect(response.parsed_body)
  27. .to be_an(Enumerable)
  28. .and contain_exactly(
  29. include(
  30. following: true,
  31. followed_by: false
  32. )
  33. )
  34. end
  35. end
  36. context 'when provided multiple IDs' do
  37. let(:params) { { id: [simon.id, lewis.id, bob.id] } }
  38. context 'when there is returned JSON data' do
  39. context 'with default parameters' do
  40. it 'returns an enumerable json with correct elements, excluding suspended accounts', :aggregate_failures do
  41. subject
  42. expect(response)
  43. .to have_http_status(200)
  44. expect(response.content_type)
  45. .to start_with('application/json')
  46. expect(response.parsed_body)
  47. .to be_an(Enumerable)
  48. .and have_attributes(
  49. size: 2
  50. )
  51. .and contain_exactly(
  52. include(simon_item),
  53. include(lewis_item)
  54. )
  55. end
  56. end
  57. context 'with `with_suspended` parameter' do
  58. let(:params) { { id: [simon.id, lewis.id, bob.id], with_suspended: true } }
  59. it 'returns an enumerable json with correct elements, including suspended accounts', :aggregate_failures do
  60. subject
  61. expect(response)
  62. .to have_http_status(200)
  63. expect(response.content_type)
  64. .to start_with('application/json')
  65. expect(response.parsed_body)
  66. .to be_an(Enumerable)
  67. .and have_attributes(
  68. size: 3
  69. )
  70. .and contain_exactly(
  71. include(simon_item),
  72. include(lewis_item),
  73. include(bob_item)
  74. )
  75. end
  76. end
  77. context 'when there are duplicate IDs in the params' do
  78. let(:params) { { id: [simon.id, lewis.id, lewis.id, lewis.id, simon.id] } }
  79. it 'removes duplicate account IDs from params' do
  80. subject
  81. expect(response.parsed_body)
  82. .to be_an(Enumerable)
  83. .and have_attributes(
  84. size: 2
  85. )
  86. .and contain_exactly(
  87. include(simon_item),
  88. include(lewis_item)
  89. )
  90. end
  91. end
  92. def simon_item
  93. {
  94. id: simon.id.to_s,
  95. following: true,
  96. showing_reblogs: true,
  97. followed_by: false,
  98. muting: false,
  99. requested: false,
  100. domain_blocking: false,
  101. }
  102. end
  103. def lewis_item
  104. {
  105. id: lewis.id.to_s,
  106. following: false,
  107. showing_reblogs: false,
  108. followed_by: true,
  109. muting: false,
  110. requested: false,
  111. domain_blocking: false,
  112. }
  113. end
  114. def bob_item
  115. {
  116. id: bob.id.to_s,
  117. following: false,
  118. showing_reblogs: false,
  119. followed_by: false,
  120. muting: false,
  121. requested: false,
  122. domain_blocking: false,
  123. }
  124. end
  125. end
  126. it 'returns JSON with correct data on previously cached requests' do
  127. # Initial request including multiple accounts in params
  128. get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id, lewis.id] }
  129. expect(response.parsed_body)
  130. .to have_attributes(size: 2)
  131. # Subsequent request with different id, should override cache from first request
  132. get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
  133. expect(response)
  134. .to have_http_status(200)
  135. expect(response.content_type)
  136. .to start_with('application/json')
  137. expect(response.parsed_body)
  138. .to be_an(Enumerable)
  139. .and have_attributes(
  140. size: 1
  141. )
  142. .and contain_exactly(
  143. include(
  144. following: true,
  145. showing_reblogs: true
  146. )
  147. )
  148. end
  149. it 'returns JSON with correct data after change too' do
  150. subject
  151. user.account.unfollow!(simon)
  152. get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
  153. expect(response)
  154. .to have_http_status(200)
  155. expect(response.content_type)
  156. .to start_with('application/json')
  157. expect(response.parsed_body)
  158. .to be_an(Enumerable)
  159. .and contain_exactly(
  160. include(
  161. following: false,
  162. showing_reblogs: false
  163. )
  164. )
  165. end
  166. end
  167. end