Skip to content

Commit 0cda55c

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 7ae6eaf commit 0cda55c

File tree

10 files changed

+54
-31
lines changed

10 files changed

+54
-31
lines changed

app/constraints/domain_constraint.rb

Lines changed: 7 additions & 0 deletions
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

Lines changed: 0 additions & 4 deletions
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

config/application.rb

Lines changed: 4 additions & 0 deletions
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

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/features/website/configuration_spec.rb

Lines changed: 11 additions & 7 deletions
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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@
2020
expect(page).to have_content('Home Content')
2121
end
2222

23-
context 'when using a custom domain' do
24-
scenario 'Public views the landing page from custom domain' do
25-
website.update(domains: 'www.example.com')
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')
2626
create(:page, published_body: 'Home Content', landing: true)
2727
visit root_path
2828

2929
expect(page).to have_content('Home Content')
3030
end
31+
end
3132

32-
scenario 'Public views the landing page for an older website on custom domain' do
33-
website.update(domains: 'www.example.com')
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')
3436
old_home_page = create(:page, published_body: 'Old Website', landing: true)
3537
website.update(navigation_links: [old_home_page.slug])
3638

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

4345
new_website.update(navigation_links: [new_home_page.slug])
4446
visit root_path
@@ -53,12 +55,12 @@
5355
click_on(old_home_page.name, match: :first)
5456
expect(page).to have_content('Old Website')
5557
end
58+
end
5659

57-
scenario 'Public gets not found message for wrong path on subdomain' do
58-
website.update(domains: 'www.example.com')
60+
scenario 'Public gets not found message for wrong path on subdomain' do
61+
website.update(domains: 'www.example.com')
5962

60-
visit landing_path(slug: website.event.slug)
61-
expect(page).to have_content("Page Not Found")
62-
end
63+
visit landing_path(slug: website.event.slug)
64+
expect(page).to have_content("Page Not Found")
6365
end
6466
end

spec/rails_helper.rb

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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
Lines changed: 12 additions & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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)