Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to DomainContraint to prevent database call per request #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
30 changes: 16 additions & 14 deletions spec/features/website/page_viewing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@
expect(page).to have_content('Home Content')
end

context 'when using a custom domain' do
scenario 'Public views the landing page from custom domain' do
website.update(domains: 'www.example.com')
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')
end
end

scenario 'Public views the landing page for an older website on custom domain' do
website.update(domains: 'www.example.com')
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_website = create(:website, domains: 'www.lvh.me')
new_home_page = create(:page,
website: new_website,
published_body: 'New Website',
landing: true)
website: new_website,
published_body: 'New Website',
landing: true)

new_website.update(navigation_links: [new_home_page.slug])
visit root_path
Expand All @@ -53,12 +55,12 @@
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
website.update(domains: 'www.example.com')
scenario 'Public gets not found message for wrong path on subdomain' do
website.update(domains: 'www.example.com')

visit landing_path(slug: website.event.slug)
expect(page).to have_content("Page Not Found")
end
visit landing_path(slug: website.event.slug)
expect(page).to have_content("Page Not Found")
end
end
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
Comment on lines +1 to +11
Copy link
Contributor

@codyjamesbrooks codyjamesbrooks Jul 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a good idea.


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