You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.0 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe RemoteFollowController do
  4. render_views
  5. describe '#new' do
  6. it 'returns success when session is empty' do
  7. account = Fabricate(:account)
  8. get :new, params: { account_username: account.to_param }
  9. expect(response).to have_http_status(:success)
  10. expect(response).to render_template(:new)
  11. expect(assigns(:remote_follow).acct).to be_nil
  12. end
  13. it 'populates the remote follow with session data when session exists' do
  14. session[:remote_follow] = 'user@example.com'
  15. account = Fabricate(:account)
  16. get :new, params: { account_username: account.to_param }
  17. expect(response).to have_http_status(:success)
  18. expect(response).to render_template(:new)
  19. expect(assigns(:remote_follow).acct).to eq 'user@example.com'
  20. end
  21. end
  22. describe '#create' do
  23. before do
  24. @account = Fabricate(:account, username: 'test_user')
  25. end
  26. context 'with a valid acct' do
  27. context 'when webfinger values are wrong' do
  28. it 'renders new when redirect url is nil' do
  29. resource_with_nil_link = double(link: nil)
  30. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_return(resource_with_nil_link)
  31. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  32. expect(response).to render_template(:new)
  33. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  34. end
  35. it 'renders new when template is nil' do
  36. link_with_nil_template = double(template: nil)
  37. resource_with_link = double(link: link_with_nil_template)
  38. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_return(resource_with_link)
  39. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  40. expect(response).to render_template(:new)
  41. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  42. end
  43. end
  44. context 'when webfinger values are good' do
  45. before do
  46. link_with_template = double(template: 'http://example.com/follow_me?acct={uri}')
  47. resource_with_link = double(link: link_with_template)
  48. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_return(resource_with_link)
  49. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  50. end
  51. it 'saves the session' do
  52. expect(session[:remote_follow]).to eq 'user@example.com'
  53. end
  54. it 'redirects to the remote location' do
  55. address = "http://example.com/follow_me?acct=test_user%40#{Rails.configuration.x.local_domain}"
  56. expect(response).to redirect_to(address)
  57. end
  58. end
  59. end
  60. context 'with an invalid acct' do
  61. it 'renders new when acct is missing' do
  62. post :create, params: { account_username: @account.to_param, remote_follow: { acct: '' } }
  63. expect(response).to render_template(:new)
  64. end
  65. it 'renders new with error when goldfinger fails' do
  66. allow(Goldfinger).to receive(:finger).with('acct:user@example.com').and_raise(Goldfinger::Error)
  67. post :create, params: { account_username: @account.to_param, remote_follow: { acct: 'user@example.com' } }
  68. expect(response).to render_template(:new)
  69. expect(response.body).to include(I18n.t('remote_follow.missing_resource'))
  70. end
  71. end
  72. end
  73. describe 'with a suspended account' do
  74. before do
  75. @account = Fabricate(:account, suspended: true)
  76. end
  77. it 'returns 410 gone on GET to #new' do
  78. get :new, params: { account_username: @account.to_param }
  79. expect(response).to have_http_status(:gone)
  80. end
  81. it 'returns 410 gone on POST to #create' do
  82. post :create, params: { account_username: @account.to_param }
  83. expect(response).to have_http_status(:gone)
  84. end
  85. end
  86. end