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.

83 lines
2.5 KiB

  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. describe 'for a record that doesnt exist' do
  17. around do |example|
  18. before = Setting.site_extended_description
  19. Setting.site_extended_description = nil
  20. example.run
  21. Setting.site_extended_description = before
  22. Setting.new_setting_key = nil
  23. end
  24. it 'cannot create a setting value for a non-admin key' do
  25. expect(Setting.new_setting_key).to be_blank
  26. patch :update, params: { form_admin_settings: { new_setting_key: 'New key value' } }
  27. expect(response).to redirect_to(edit_admin_settings_path)
  28. expect(Setting.new_setting_key).to be_nil
  29. end
  30. it 'creates a settings value that didnt exist before for eligible key' do
  31. expect(Setting.site_extended_description).to be_blank
  32. patch :update, params: { form_admin_settings: { site_extended_description: 'New key value' } }
  33. expect(response).to redirect_to(edit_admin_settings_path)
  34. expect(Setting.site_extended_description).to eq 'New key value'
  35. end
  36. end
  37. context do
  38. around do |example|
  39. site_title = Setting.site_title
  40. example.run
  41. Setting.site_title = site_title
  42. end
  43. it 'updates a settings value' do
  44. Setting.site_title = 'Original'
  45. patch :update, params: { form_admin_settings: { site_title: 'New title' } }
  46. expect(response).to redirect_to(edit_admin_settings_path)
  47. expect(Setting.site_title).to eq 'New title'
  48. end
  49. end
  50. context do
  51. around do |example|
  52. open_registrations = Setting.open_registrations
  53. example.run
  54. Setting.open_registrations = open_registrations
  55. end
  56. it 'typecasts open_registrations to boolean' do
  57. Setting.open_registrations = false
  58. patch :update, params: { form_admin_settings: { open_registrations: '1' } }
  59. expect(response).to redirect_to(edit_admin_settings_path)
  60. expect(Setting.open_registrations).to eq true
  61. end
  62. end
  63. end
  64. end
  65. end