statuses_spec.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'API V1 Trends Statuses' do
  4. describe 'GET /api/v1/trends/statuses' do
  5. context 'when trends are disabled' do
  6. before { Setting.trends = false }
  7. it 'returns http success' do
  8. get '/api/v1/trends/statuses'
  9. expect(response)
  10. .to have_http_status(200)
  11. .and not_have_http_link_header
  12. expect(response.content_type)
  13. .to start_with('application/json')
  14. end
  15. end
  16. context 'when trends are enabled' do
  17. before { Setting.trends = true }
  18. it 'returns http success' do
  19. prepare_trends
  20. stub_const('Api::BaseController::DEFAULT_STATUSES_LIMIT', 2)
  21. get '/api/v1/trends/statuses'
  22. expect(response)
  23. .to have_http_status(200)
  24. .and have_http_link_header(api_v1_trends_statuses_url(offset: 2)).for(rel: 'next')
  25. expect(response.content_type)
  26. .to start_with('application/json')
  27. end
  28. def prepare_trends
  29. Fabricate.times(3, :status, trendable: true, language: 'en').each do |status|
  30. 2.times { |i| Trends.statuses.add(status, i) }
  31. end
  32. Trends::Statuses.new(threshold: 1, decay_threshold: -1).refresh
  33. end
  34. context 'with a comically inflated external interactions count' do
  35. def prepare_fake_trends
  36. fake_remote_account = Fabricate(:account, domain: 'other.com')
  37. fake_status = Fabricate(:status, account: fake_remote_account, text: 'I am a big faker', trendable: true, language: 'en')
  38. fake_status.status_stat.tap do |status_stat|
  39. status_stat.reblogs_count = 0
  40. status_stat.favourites_count = 0
  41. status_stat.untrusted_reblogs_count = 1_000_000_000
  42. status_stat.untrusted_favourites_count = 1_000_000_000
  43. status_stat.save
  44. end
  45. real_remote_account = Fabricate(:account, domain: 'other.com')
  46. real_status = Fabricate(:status, account: real_remote_account, text: 'I make real friends online', trendable: true, language: 'en')
  47. real_status.status_stat.tap do |status_stat|
  48. status_stat.reblogs_count = 10
  49. status_stat.favourites_count = 10
  50. status_stat.untrusted_reblogs_count = 10
  51. status_stat.untrusted_favourites_count = 10
  52. status_stat.save
  53. end
  54. Trends.statuses.add(fake_status, 100)
  55. Trends.statuses.add(real_status, 101)
  56. Trends::Statuses.new(threshold: 1, decay_threshold: 1).refresh
  57. end
  58. it 'ignores the feeble attempts at deception' do
  59. prepare_fake_trends
  60. stub_const('Api::BaseController::DEFAULT_STATUSES_LIMIT', 10)
  61. get '/api/v1/trends/statuses'
  62. expect(response).to have_http_status(200)
  63. expect(response.parsed_body.length).to eq(1)
  64. expect(response.parsed_body[0]['content']).to eq('I make real friends online')
  65. end
  66. end
  67. end
  68. end
  69. end