1
0

suggestions_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Suggestions' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/suggestions' do
  9. subject do
  10. get '/api/v1/suggestions', headers: headers, params: params
  11. end
  12. let(:bob) { Fabricate(:account) }
  13. let(:jeff) { Fabricate(:account) }
  14. let(:params) { {} }
  15. before do
  16. PotentialFriendshipTracker.record(user.account_id, bob.id, :reblog)
  17. PotentialFriendshipTracker.record(user.account_id, jeff.id, :favourite)
  18. end
  19. it_behaves_like 'forbidden for wrong scope', 'write'
  20. it 'returns http success' do
  21. subject
  22. expect(response).to have_http_status(200)
  23. end
  24. it 'returns accounts' do
  25. subject
  26. body = body_as_json
  27. expect(body.size).to eq 2
  28. expect(body.pluck(:id)).to match_array([bob, jeff].map { |i| i.id.to_s })
  29. end
  30. context 'with limit param' do
  31. let(:params) { { limit: 1 } }
  32. it 'returns only the requested number of accounts' do
  33. subject
  34. expect(body_as_json.size).to eq 1
  35. end
  36. end
  37. context 'without an authorization header' do
  38. let(:headers) { {} }
  39. it 'returns http unauthorized' do
  40. subject
  41. expect(response).to have_http_status(401)
  42. end
  43. end
  44. end
  45. describe 'DELETE /api/v1/suggestions/:id' do
  46. subject do
  47. delete "/api/v1/suggestions/#{jeff.id}", headers: headers
  48. end
  49. let(:suggestions_source) { instance_double(AccountSuggestions::PastInteractionsSource, remove: nil) }
  50. let(:bob) { Fabricate(:account) }
  51. let(:jeff) { Fabricate(:account) }
  52. before do
  53. PotentialFriendshipTracker.record(user.account_id, bob.id, :reblog)
  54. PotentialFriendshipTracker.record(user.account_id, jeff.id, :favourite)
  55. allow(AccountSuggestions::PastInteractionsSource).to receive(:new).and_return(suggestions_source)
  56. end
  57. it_behaves_like 'forbidden for wrong scope', 'write'
  58. it 'returns http success' do
  59. subject
  60. expect(response).to have_http_status(200)
  61. end
  62. it 'removes the specified suggestion' do
  63. subject
  64. expect(suggestions_source).to have_received(:remove).with(user.account, jeff.id.to_s).once
  65. expect(suggestions_source).to_not have_received(:remove).with(user.account, bob.id.to_s)
  66. end
  67. context 'without an authorization header' do
  68. let(:headers) { {} }
  69. it 'returns http unauthorized' do
  70. subject
  71. expect(response).to have_http_status(401)
  72. end
  73. end
  74. end
  75. end