search_controller_spec.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V2::SearchController do
  4. render_views
  5. context 'with token' do
  6. let(:user) { Fabricate(:user) }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:search') }
  8. before do
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. describe 'GET #index' do
  12. let!(:bob) { Fabricate(:account, username: 'bob_test') }
  13. let!(:ana) { Fabricate(:account, username: 'ana_test') }
  14. let!(:tom) { Fabricate(:account, username: 'tom_test') }
  15. let(:params) { { q: 'test' } }
  16. it 'returns http success' do
  17. get :index, params: params
  18. expect(response).to have_http_status(200)
  19. end
  20. context 'when searching accounts' do
  21. let(:params) { { q: 'test', type: 'accounts' } }
  22. it 'returns all matching accounts' do
  23. get :index, params: params
  24. expect(body_as_json[:accounts].pluck(:id)).to contain_exactly(bob.id.to_s, ana.id.to_s, tom.id.to_s)
  25. end
  26. context 'with following=true' do
  27. let(:params) { { q: 'test', type: 'accounts', following: 'true' } }
  28. before do
  29. user.account.follow!(ana)
  30. end
  31. it 'returns only the followed accounts' do
  32. get :index, params: params
  33. expect(body_as_json[:accounts].pluck(:id)).to contain_exactly(ana.id.to_s)
  34. end
  35. end
  36. end
  37. end
  38. end
  39. context 'without token' do
  40. describe 'GET #index' do
  41. let(:search_params) {}
  42. before do
  43. get :index, params: search_params
  44. end
  45. context 'with a `q` shorter than 5 characters' do
  46. let(:search_params) { { q: 'test' } }
  47. it 'returns http success' do
  48. expect(response).to have_http_status(200)
  49. end
  50. end
  51. context 'with a `q` equal to or longer than 5 characters' do
  52. let(:search_params) { { q: 'test1' } }
  53. it 'returns http success' do
  54. expect(response).to have_http_status(200)
  55. end
  56. context 'with truthy `resolve`' do
  57. let(:search_params) { { q: 'test1', resolve: '1' } }
  58. it 'returns http unauthorized' do
  59. expect(response).to have_http_status(401)
  60. end
  61. end
  62. context 'with `offset`' do
  63. let(:search_params) { { q: 'test1', offset: 1 } }
  64. it 'returns http unauthorized' do
  65. expect(response).to have_http_status(401)
  66. end
  67. end
  68. end
  69. end
  70. end
  71. end