aliases_controller_spec.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe Settings::AliasesController do
  4. render_views
  5. let!(:user) { Fabricate(:user) }
  6. let(:account) { user.account }
  7. before do
  8. sign_in user, scope: :user
  9. end
  10. describe 'GET #index' do
  11. before do
  12. get :index
  13. end
  14. it 'returns http success' do
  15. expect(response).to have_http_status(200)
  16. end
  17. it 'returns private cache control headers' do
  18. expect(response.headers['Cache-Control']).to include('private, no-store')
  19. end
  20. end
  21. describe 'POST #create' do
  22. context 'with valid alias' do
  23. before { stub_resolver }
  24. it 'creates an alias for the user' do
  25. expect do
  26. post :create, params: { account_alias: { acct: 'new@example.com' } }
  27. end.to change(AccountAlias, :count).by(1)
  28. expect(response).to redirect_to(settings_aliases_path)
  29. end
  30. end
  31. context 'with invalid alias' do
  32. it 'does not create an alias for the user' do
  33. expect do
  34. post :create, params: { account_alias: { acct: 'format-wrong' } }
  35. end.to_not change(AccountAlias, :count)
  36. expect(response).to have_http_status(200)
  37. end
  38. end
  39. end
  40. describe 'DELETE #destroy' do
  41. let(:account_alias) do
  42. AccountAlias.new(account: user.account, acct: 'new@example.com').tap do |account_alias|
  43. account_alias.save(validate: false)
  44. end
  45. end
  46. it 'removes an alias' do
  47. delete :destroy, params: { id: account_alias.id }
  48. expect(response).to redirect_to(settings_aliases_path)
  49. expect { account_alias.reload }.to raise_error(ActiveRecord::RecordNotFound)
  50. end
  51. end
  52. private
  53. def stub_resolver
  54. resolver = instance_double(ResolveAccountService, call: Fabricate(:account))
  55. allow(ResolveAccountService).to receive(:new).and_return(resolver)
  56. end
  57. end