set_system_information_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Service::System::SetSystemInformation do
  4. let(:service) { described_class.new(data: variables) }
  5. let(:required_variables) do
  6. {
  7. url: 'http://example.com',
  8. organization: 'Sample'
  9. }
  10. end
  11. describe 'setting locale' do
  12. context 'when locale is given' do
  13. let(:variables) { required_variables.merge(locale_default: 'lt') }
  14. it 'sets locale' do
  15. expect { service.execute }
  16. .to change { Setting.get('locale_default') }
  17. .to('lt')
  18. end
  19. it 'does return updated settings' do
  20. result = service.execute
  21. expect(result).to include(
  22. locale_default: 'lt',
  23. organization: 'Sample',
  24. http_type: 'http',
  25. fqdn: 'example.com'
  26. )
  27. end
  28. end
  29. context 'when locale is given but another parameter is invalid' do
  30. let(:variables) { required_variables.merge(locale_default: 'lt').tap { _1.delete(:url) } }
  31. it 'does not set locale' do
  32. expect { service.execute }.to raise_error(Exceptions::InvalidAttribute)
  33. end
  34. end
  35. context 'when locale is not given' do
  36. let(:variables) { required_variables }
  37. it 'does not change locale' do
  38. expect { service.execute }.not_to change { Setting.get('locale_default') }
  39. end
  40. end
  41. end
  42. describe 'setting timezone' do
  43. context 'when timezone is given' do
  44. let(:variables) { required_variables.merge(timezone_default: 'Europe/Vilnius') }
  45. it 'sets timezone' do
  46. expect { service.execute }.to change { Setting.get('timezone_default') }.to('Europe/Vilnius')
  47. end
  48. end
  49. context 'when timezone is not given' do
  50. let(:variables) { required_variables }
  51. it 'does not change timezone' do
  52. expect { service.execute }.not_to change { Setting.get('timezone_default') }
  53. end
  54. end
  55. end
  56. describe 'setting organization name' do
  57. context 'when organization name is given' do
  58. let(:variables) { required_variables }
  59. it 'sets organization name' do
  60. expect { service.execute }.to change { Setting.get('organization') }.to('Sample')
  61. end
  62. end
  63. context 'when organization name is given but another parameter is invalid' do
  64. let(:variables) { required_variables.tap { _1.delete(:url) } }
  65. it 'does not set organization name' do
  66. expect { service.execute }.to raise_error(Exceptions::InvalidAttribute)
  67. end
  68. end
  69. context 'when organization name is not valid' do
  70. let(:variables) { required_variables.merge(organization: ' ') }
  71. it 'returns an error' do
  72. expect { service.execute }.to raise_error(Exceptions::MissingAttribute)
  73. end
  74. end
  75. context 'when organization name is not given' do
  76. let(:variables) { required_variables.tap { _1.delete(:organization) } }
  77. it 'returns an error' do
  78. expect { service.execute }.to raise_error(Exceptions::MissingAttribute)
  79. end
  80. end
  81. end
  82. describe 'setting http type & FQDN' do
  83. context 'when url is given' do
  84. let(:variables) { required_variables }
  85. it 'sets service name' do
  86. expect { service.execute }
  87. .to change { [Setting.get('http_type'), Setting.get('fqdn')] }
  88. .to(['http', 'example.com'])
  89. end
  90. context 'when system is online service' do
  91. before { Setting.set('system_online_service', true) }
  92. it 'does not set http type & FQDN' do
  93. expect { service.execute }
  94. .not_to change { [Setting.get('http_type'), Setting.get('fqdn')] }
  95. end
  96. end
  97. end
  98. context 'when url is given but another parameter is invalid' do
  99. let(:variables) { required_variables.tap { _1.delete(:organization) } }
  100. it 'does not set http type & FQDN' do
  101. expect { service.execute }.to raise_error(Exceptions::MissingAttribute)
  102. end
  103. end
  104. context 'when url is not valid' do
  105. let(:variables) { required_variables.merge(url: 'meh') }
  106. it 'returns an error' do
  107. expect { service.execute }.to raise_error(Exceptions::InvalidAttribute)
  108. end
  109. end
  110. context 'when url is not given' do
  111. let(:variables) { required_variables.tap { _1.delete(:url) } }
  112. it 'returns an error' do
  113. expect { service.execute }.to raise_error(Exceptions::InvalidAttribute)
  114. end
  115. end
  116. end
  117. describe 'setting logo' do
  118. let(:image_data) { Rails.root.join('test/data/image/1000x1000.png').binread }
  119. before do
  120. freeze_time
  121. allow(Service::SystemAssets::ProductLogo)
  122. .to receive(:store_one)
  123. .and_call_original
  124. end
  125. context 'when logo is given' do
  126. let(:variables) { required_variables.merge(logo: image_data) }
  127. it 'sets updates logo and sets logo timestamp' do
  128. expect { service.execute }
  129. .to change { Setting.get('product_logo') }
  130. .to(Time.current.to_i)
  131. end
  132. it 'stores both original and resized logos' do
  133. service.execute
  134. expect(Service::SystemAssets::ProductLogo).to have_received(:store_one).twice
  135. end
  136. end
  137. context 'when logo is given but another parameter is invalid' do
  138. let(:variables) { required_variables.merge(logo: image_data).tap { _1.delete(:url) } }
  139. it 'does not store logo to storage', :aggregate_failures do
  140. expect { service.execute }.to raise_error(Exceptions::InvalidAttribute)
  141. expect(Service::SystemAssets::ProductLogo).not_to have_received(:store_one)
  142. end
  143. end
  144. context 'when logo is not given' do
  145. let(:variables) { required_variables }
  146. it 'does not set logo timestamp' do
  147. expect { service.execute }
  148. .not_to change { Setting.get('product_logo') }
  149. end
  150. it 'does not store logo to storage' do
  151. service.execute
  152. expect(Service::SystemAssets::ProductLogo).not_to have_received(:store_one)
  153. end
  154. end
  155. end
  156. end