From acb01d5574f6db63edf830ed653a6b2bfb6e9cd5 Mon Sep 17 00:00:00 2001 From: Stephen Benjamin Date: Wed, 16 May 2018 13:37:01 -0400 Subject: [PATCH] fixes #23178 - seeded location should be in seeded org When creating a host in the seeded org and seeded loc, host saves and then organization becomes invalid: "Locations expecting locations used by hosts or inherited (check mismatches report)." --- db/seeds.d/050-taxonomies.rb | 10 ++++++++-- test/models/host_test.rb | 4 ++++ test/unit/tasks/seeds_test.rb | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/db/seeds.d/050-taxonomies.rb b/db/seeds.d/050-taxonomies.rb index ff758e683de..6936bd5070d 100644 --- a/db/seeds.d/050-taxonomies.rb +++ b/db/seeds.d/050-taxonomies.rb @@ -2,7 +2,7 @@ if SETTINGS[:organizations_enabled] && ENV['SEED_ORGANIZATION'] && !Organization.any? Organization.without_auditing do original_user, User.current = User.current, User.anonymous_admin - Organization.where(:name => ENV['SEED_ORGANIZATION']).first_or_create + @organization = Organization.where(:name => ENV['SEED_ORGANIZATION']).first_or_create User.current = original_user end end @@ -11,7 +11,13 @@ if SETTINGS[:locations_enabled] && ENV['SEED_LOCATION'] && !Location.any? Location.without_auditing do original_user, User.current = User.current, User.anonymous_admin - Location.where(:name => ENV['SEED_LOCATION']).first_or_create! + @location = Location.where(:name => ENV['SEED_LOCATION']).first_or_create! User.current = original_user end end + +# Add the initial location to the initial organization to prevent mismatches +# when a host is created that uses them +if @organization && @location && @organization.locations.exclude?(@location) + @organization.locations << @location +end diff --git a/test/models/host_test.rb b/test/models/host_test.rb index 514582f2511..cab3501d408 100644 --- a/test/models/host_test.rb +++ b/test/models/host_test.rb @@ -748,6 +748,8 @@ def teardown @host = FactoryBot.create(:host, :managed => false) @location = Location.create :name => "New York" @organization = Organization.create :name => "Hosting client 1" + + @organization.locations << @location end test "assign a host to a location" do @@ -778,6 +780,8 @@ def teardown @host.organization_id = @organization.id assert @host.save! + assert @host.organization.valid? + assert @host.location.valid? end end diff --git a/test/unit/tasks/seeds_test.rb b/test/unit/tasks/seeds_test.rb index 9208b4a5994..a316565d056 100644 --- a/test/unit/tasks/seeds_test.rb +++ b/test/unit/tasks/seeds_test.rb @@ -161,6 +161,23 @@ def seed refute Location.unscoped.find_by_name('seed_test') end + test "seeded organization contains seeded location" do + Location.stubs(:any?).returns(false) + Organization.stubs(:any?).returns(false) + + org_name = 'seed_org' + loc_name = 'seed_loc' + + with_env('SEED_ORGANIZATION' => org_name, 'SEED_LOCATION' => loc_name) do + seed + end + + org = Organization.unscoped.find_by_name(org_name) + loc = Location.unscoped.find_by_name(loc_name) + + assert org.locations.include?(loc) + end + test "all access permissions are created by permissions seed" do seed access_permissions = Foreman::AccessControl.permissions.reject(&:public?).reject(&:plugin?).map(&:name).map(&:to_s)