aliases_controller_spec.rb 1.8 KB

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