Skip to content

Commit 10e789b

Browse files
committed
Changes to DomainContraint to prevent database call per request
- Switches logic to match on non event host domain set in ENV variable rather than website domains stored in DB - Moves config/initializers/domain_constraint.rb to app/constraints folder - Set Rails configuration events_hosts value from EVENTS_HOSTS env variable with fallback defaults for non production environments - Uses lvh.me as domain to more accurately test domain constraint logic in feature specs
1 parent 0771c3b commit 10e789b

File tree

11 files changed

+66
-41
lines changed

11 files changed

+66
-41
lines changed

app/constraints/domain_constraint.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class DomainConstraint
2+
def matches?(request)
3+
return false unless request.domain
4+
5+
!Rails.configuration.events_hosts.match?(request.domain)
6+
end
7+
end

app/controllers/application_controller.rb

-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ def require_event
116116
end
117117
end
118118

119-
def require_website
120-
redirect_to not_found_path and return unless current_website
121-
end
122-
123119
def require_proposal
124120
@proposal = @event.proposals.find_by!(uuid: params[:proposal_uuid] || params[:uuid])
125121
end

app/models/website.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Website < ApplicationRecord
2626
DEFAULT = 'default'.freeze
2727

2828
def self.domain_match(domain)
29-
where(arel_table[:domains].matches("%#{(domain)}"))
29+
where(arel_table[:domains].matches("%#{domain}%"))
3030
end
3131

3232
def manual_purge

config/application.rb

+4
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ class Application < Rails::Application
2626
config.active_record.time_zone_aware_types = [:datetime]
2727

2828
config.active_job.queue_adapter = :sidekiq
29+
30+
config.events_hosts = ENV.fetch('EVENTS_HOSTS') do
31+
'localhost,example.com,herokuapp.com'
32+
end
2933
end
3034
end

config/initializers/domain_constraint.rb

-5
This file was deleted.

spec/features/website/configuration_spec.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,35 @@
2020
website = create(:website, event: event)
2121
home_page = create(:page, website: website)
2222

23-
visit("/#{home_page.slug}")
23+
with_domain('lvh.me') do
24+
visit("/#{home_page.slug}")
2425

25-
expect(current_path).to eq(not_found_path)
26+
expect(current_path).to eq(not_found_path)
27+
end
2628

2729
login_as(organizer)
2830
visit event_path(website.event)
2931
within('.navbar') { click_on("Website") }
3032

3133
expect(page).to have_content("Edit Website")
3234

33-
fill_in('Domains', with: 'www.example.com')
35+
fill_in('Domains', with: 'lvh.me')
3436
fill_in('Navigation links', with: "Home\n")
3537
click_on("Save")
3638

3739
expect(page).to have_content("Website was successfully updated")
3840

3941
logout
4042

41-
visit("/#{home_page.slug}")
43+
with_domain('lvh.me') do
44+
visit("/#{home_page.slug}")
4245

43-
expect(page).to have_content(strip_tags(home_page.published_body))
46+
expect(page).to have_content(strip_tags(home_page.published_body))
4447

45-
click_on(home_page.name, match: :first)
48+
click_on(home_page.name, match: :first)
4649

47-
expect(current_path).to eq("/#{home_page.slug}")
50+
expect(current_path).to eq("/#{home_page.slug}")
51+
end
4852
end
4953

5054
scenario "Organizer fails to add font file correctly", :js do

spec/features/website/page_viewing_spec.rb

+27-23
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,41 @@
2020
expect(page).to have_content('Home Content')
2121
end
2222

23-
scenario 'Public views the landing page from custom domain' do
24-
website.update(domains: 'www.example.com')
25-
create(:page, published_body: 'Home Content', landing: true)
26-
visit root_path
23+
scenario 'Public views the landing page from custom domain', js: true do
24+
with_domain('lvh.me') do
25+
website.update(domains: 'www.lvh.me')
26+
create(:page, published_body: 'Home Content', landing: true)
27+
visit root_path
2728

28-
expect(page).to have_content('Home Content')
29+
expect(page).to have_content('Home Content')
30+
end
2931
end
3032

31-
scenario 'Public views the landing page for an older website on custom domain' do
32-
website.update(domains: 'www.example.com')
33-
old_home_page = create(:page, published_body: 'Old Website', landing: true)
34-
website.update(navigation_links: [old_home_page.slug])
33+
scenario 'Public views the landing page for an older website on custom domain', js: true do
34+
with_domain('lvh.me') do
35+
website.update(domains: 'www.lvh.me')
36+
old_home_page = create(:page, published_body: 'Old Website', landing: true)
37+
website.update(navigation_links: [old_home_page.slug])
3538

36-
new_website = create(:website, domains: 'www.example.com')
37-
new_home_page = create(:page,
38-
website: new_website,
39-
published_body: 'New Website',
40-
landing: true)
39+
new_website = create(:website, domains: 'www.lvh.me')
40+
new_home_page = create(:page,
41+
website: new_website,
42+
published_body: 'New Website',
43+
landing: true)
4144

42-
new_website.update(navigation_links: [new_home_page.slug])
43-
visit root_path
44-
expect(page).to have_content('New Website')
45+
new_website.update(navigation_links: [new_home_page.slug])
46+
visit root_path
47+
expect(page).to have_content('New Website')
4548

46-
click_on(new_home_page.name, match: :first)
47-
expect(page).to have_content('New Website')
49+
click_on(new_home_page.name, match: :first)
50+
expect(page).to have_content('New Website')
4851

49-
visit landing_path(slug: website.event.slug)
50-
expect(page).to have_content('Old Website')
52+
visit landing_path(slug: website.event.slug)
53+
expect(page).to have_content('Old Website')
5154

52-
click_on(old_home_page.name, match: :first)
53-
expect(page).to have_content('Old Website')
55+
click_on(old_home_page.name, match: :first)
56+
expect(page).to have_content('Old Website')
57+
end
5458
end
5559

5660
scenario 'Public gets not found message for wrong path on subdomain' do

spec/rails_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ def save_timestamped_screenshot(page)
9393

9494
page.save_screenshot(screenshot_path)
9595
end
96+
97+
Capybara.always_include_port = true

spec/support/helpers.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require 'support/helpers/session_helpers'
22
require 'support/helpers/form_helpers'
3+
require 'support/helpers/domain_helpers'
34

45
RSpec.configure do |config|
56
config.include Features::SessionHelpers, type: :feature
67
config.include Features::FormHelpers, type: :feature
8+
config.include Features::DomainHelpers, type: :feature
79
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Features
2+
module DomainHelpers
3+
def with_domain(host)
4+
original_host = Capybara.app_host
5+
Capybara.app_host = "http://#{host}"
6+
yield
7+
ensure
8+
Capybara.app_host = original_host
9+
end
10+
end
11+
end
12+

spec/support/helpers/session_helpers.rb

-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ def forgot_password(email)
2121
fill_in 'user_email', with: email
2222
click_button 'Send me reset password instructions'
2323
end
24-
2524
end
2625
end

0 commit comments

Comments
 (0)