Skip to content

Commit

Permalink
Changes to DomainContraint to prevent database call per request
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jonsgreen committed Jul 7, 2022
1 parent 0771c3b commit 10e789b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 41 deletions.
7 changes: 7 additions & 0 deletions app/constraints/domain_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class DomainConstraint
def matches?(request)
return false unless request.domain

!Rails.configuration.events_hosts.match?(request.domain)
end
end
4 changes: 0 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ def require_event
end
end

def require_website
redirect_to not_found_path and return unless current_website
end

def require_proposal
@proposal = @event.proposals.find_by!(uuid: params[:proposal_uuid] || params[:uuid])
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Website < ApplicationRecord
DEFAULT = 'default'.freeze

def self.domain_match(domain)
where(arel_table[:domains].matches("%#{(domain)}"))
where(arel_table[:domains].matches("%#{domain}%"))
end

def manual_purge
Expand Down
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ class Application < Rails::Application
config.active_record.time_zone_aware_types = [:datetime]

config.active_job.queue_adapter = :sidekiq

config.events_hosts = ENV.fetch('EVENTS_HOSTS') do
'localhost,example.com,herokuapp.com'
end
end
end
5 changes: 0 additions & 5 deletions config/initializers/domain_constraint.rb

This file was deleted.

18 changes: 11 additions & 7 deletions spec/features/website/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,35 @@
website = create(:website, event: event)
home_page = create(:page, website: website)

visit("/#{home_page.slug}")
with_domain('lvh.me') do
visit("/#{home_page.slug}")

expect(current_path).to eq(not_found_path)
expect(current_path).to eq(not_found_path)
end

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

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

fill_in('Domains', with: 'www.example.com')
fill_in('Domains', with: 'lvh.me')
fill_in('Navigation links', with: "Home\n")
click_on("Save")

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

logout

visit("/#{home_page.slug}")
with_domain('lvh.me') do
visit("/#{home_page.slug}")

expect(page).to have_content(strip_tags(home_page.published_body))
expect(page).to have_content(strip_tags(home_page.published_body))

click_on(home_page.name, match: :first)
click_on(home_page.name, match: :first)

expect(current_path).to eq("/#{home_page.slug}")
expect(current_path).to eq("/#{home_page.slug}")
end
end

scenario "Organizer fails to add font file correctly", :js do
Expand Down
50 changes: 27 additions & 23 deletions spec/features/website/page_viewing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,41 @@
expect(page).to have_content('Home Content')
end

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

expect(page).to have_content('Home Content')
expect(page).to have_content('Home Content')
end
end

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

new_website = create(:website, domains: 'www.example.com')
new_home_page = create(:page,
website: new_website,
published_body: 'New Website',
landing: true)
new_website = create(:website, domains: 'www.lvh.me')
new_home_page = create(:page,
website: new_website,
published_body: 'New Website',
landing: true)

new_website.update(navigation_links: [new_home_page.slug])
visit root_path
expect(page).to have_content('New Website')
new_website.update(navigation_links: [new_home_page.slug])
visit root_path
expect(page).to have_content('New Website')

click_on(new_home_page.name, match: :first)
expect(page).to have_content('New Website')
click_on(new_home_page.name, match: :first)
expect(page).to have_content('New Website')

visit landing_path(slug: website.event.slug)
expect(page).to have_content('Old Website')
visit landing_path(slug: website.event.slug)
expect(page).to have_content('Old Website')

click_on(old_home_page.name, match: :first)
expect(page).to have_content('Old Website')
click_on(old_home_page.name, match: :first)
expect(page).to have_content('Old Website')
end
end

scenario 'Public gets not found message for wrong path on subdomain' do
Expand Down
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ def save_timestamped_screenshot(page)

page.save_screenshot(screenshot_path)
end

Capybara.always_include_port = true
2 changes: 2 additions & 0 deletions spec/support/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'support/helpers/session_helpers'
require 'support/helpers/form_helpers'
require 'support/helpers/domain_helpers'

RSpec.configure do |config|
config.include Features::SessionHelpers, type: :feature
config.include Features::FormHelpers, type: :feature
config.include Features::DomainHelpers, type: :feature
end
12 changes: 12 additions & 0 deletions spec/support/helpers/domain_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Features
module DomainHelpers
def with_domain(host)
original_host = Capybara.app_host
Capybara.app_host = "http://#{host}"
yield
ensure
Capybara.app_host = original_host
end
end
end

1 change: 0 additions & 1 deletion spec/support/helpers/session_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ def forgot_password(email)
fill_in 'user_email', with: email
click_button 'Send me reset password instructions'
end

end
end

0 comments on commit 10e789b

Please sign in to comment.