Merge pull request #7104 from crohr/fix/improve-installation-uuid-generation

Add custom getter for Setting.installation_uuid

[ci skip]
pull/7106/head
Oliver Günther 6 years ago committed by GitHub
commit d79887a939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      app/models/setting.rb
  2. 7
      config/initializers/security_badge.rb
  3. 42
      spec/models/setting_spec.rb

@ -92,6 +92,7 @@ class Setting < ActiveRecord::Base
end
def self.create_setting_accessors(name)
return if [:installation_uuid].include?(name.to_sym)
# Defines getter and setter for each setting
# Then setting values can be read using: Setting.some_setting_name
# or set using Setting.some_setting_name = "some value"
@ -187,6 +188,28 @@ class Setting < ActiveRecord::Base
@@available_settings.has_key?(name)
end
def self.installation_uuid
if settings_table_exists_yet?
# we avoid the default getters and setters since the cache messes things up
setting = find_or_initialize_by(name: "installation_uuid")
if setting.value.blank?
setting.value = generate_installation_uuid
setting.save!
end
setting.value
else
"unknown"
end
end
def self.generate_installation_uuid
if Rails.env.test?
"test"
else
SecureRandom.uuid
end
end
[:emails_header, :emails_footer].each do |mail|
src = <<-END_SRC
def self.localized_#{mail}

@ -1,7 +0,0 @@
OpenProject::Application.configure do
config.after_initialize do
if Setting.settings_table_exists_yet?
Setting.installation_uuid ||= SecureRandom.uuid
end
end
end

@ -85,6 +85,48 @@ describe Setting, type: :model do
end
end
describe ".installation_uuid" do
after do
Setting.find_by(name: "installation_uuid")&.destroy
end
it "returns unknown if the settings table isn't available yet" do
Setting.stub(settings_table_exists_yet?: false)
expect(Setting.installation_uuid).to eq("unknown")
end
context "with settings table ready" do
it "resets the value if blank" do
Setting.create!(name: "installation_uuid", value: "")
expect(Setting.installation_uuid).not_to be_blank
end
it "returns the existing value if any", with_settings: { installation_uuid: "abcd1234" } do
expect(Setting.installation_uuid).to eq("abcd1234")
end
context "with no existing value" do
context "in test environment" do
before do
expect(Rails.env).to receive(:test?).and_return(true)
end
it "returns 'test' as the UUID" do
expect(Setting.installation_uuid).to eq("test")
end
end
it "returns a random UUID" do
expect(Rails.env).to receive(:test?).and_return(false)
installation_uuid = Setting.installation_uuid
expect(installation_uuid).not_to eq("test")
expect(installation_uuid.size).to eq(36)
expect(Setting.installation_uuid).to eq(installation_uuid)
end
end
end
end
# Check that when reading certain setting values that they get overwritten if needed.
describe "filter saved settings" do
before do

Loading…
Cancel
Save