settings_controller_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::SettingsController, type: :controller do
  4. render_views
  5. describe 'When signed in as an admin' do
  6. before do
  7. sign_in Fabricate(:user, admin: true), scope: :user
  8. end
  9. describe 'GET #edit' do
  10. it 'returns http success' do
  11. get :edit
  12. expect(response).to have_http_status(200)
  13. end
  14. end
  15. describe 'PUT #update' do
  16. before do
  17. allow_any_instance_of(Form::AdminSettings).to receive(:valid?).and_return(true)
  18. end
  19. describe 'for a record that doesnt exist' do
  20. around do |example|
  21. before = Setting.site_extended_description
  22. Setting.site_extended_description = nil
  23. example.run
  24. Setting.site_extended_description = before
  25. Setting.new_setting_key = nil
  26. end
  27. it 'cannot create a setting value for a non-admin key' do
  28. expect(Setting.new_setting_key).to be_blank
  29. patch :update, params: { form_admin_settings: { new_setting_key: 'New key value' } }
  30. expect(response).to redirect_to(edit_admin_settings_path)
  31. expect(Setting.new_setting_key).to be_nil
  32. end
  33. it 'creates a settings value that didnt exist before for eligible key' do
  34. expect(Setting.site_extended_description).to be_blank
  35. patch :update, params: { form_admin_settings: { site_extended_description: 'New key value' } }
  36. expect(response).to redirect_to(edit_admin_settings_path)
  37. expect(Setting.site_extended_description).to eq 'New key value'
  38. end
  39. end
  40. context do
  41. around do |example|
  42. site_title = Setting.site_title
  43. example.run
  44. Setting.site_title = site_title
  45. end
  46. it 'updates a settings value' do
  47. Setting.site_title = 'Original'
  48. patch :update, params: { form_admin_settings: { site_title: 'New title' } }
  49. expect(response).to redirect_to(edit_admin_settings_path)
  50. expect(Setting.site_title).to eq 'New title'
  51. end
  52. end
  53. end
  54. end
  55. end