Skip to content

Commit

Permalink
🐞 Space: SpacesController#show works with BYODomain (#1178)
Browse files Browse the repository at this point in the history
- #74

I introduced this bug in b65a233,
basically the `SpaceController#show` was not finding by domain, so it
would give an empty page.

This also adds support for using
[`assert_select`](http://api.rubyonrails.org/classes/ActionDispatch/IntegrationTest.html)
in request specs, so long as the `subject` is a `TestResponse`.
  • Loading branch information
zspencer authored Mar 3, 2023
1 parent 9bc9704 commit 5313039
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/controllers/spaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def space_params
policy_scope(Space).friendly.find(params[:id])
elsif params[:space]
policy_scope(Space).new(space_params)
elsif BrandedDomainConstraint.new(space_repository).space_for_request(request).present?
BrandedDomainConstraint.new(space_repository).space_for_request(request)
else
policy_scope(Space).new
end.tap do |space|
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
<%= render 'layouts/header' %>

<main>
<main id="<%= current_space.present? ? dom_id(current_space) : nil %>">
<%= yield %>
</main>

Expand Down
2 changes: 1 addition & 1 deletion app/views/spaces/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<%- if space.entrance.present? %>
<%= render space.entrance %>
<%- end %>
<%- end %>
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
# config.filter_gems_from_backtrace("gem name")

config.include(AuthHelpers, type: :request)
config.include(DomHelpers, type: :request)

config.include ViewComponent::TestHelpers, type: :component
end
Expand Down
29 changes: 25 additions & 4 deletions spec/requests/spaces_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,38 @@

require "swagger_helper"

RSpec.describe SpacesController, type: :request do
RSpec.describe SpacesController do
include ActiveJob::TestHelper

describe "#show" do
subject(:perform_request) do
get url
test_response
end

let(:space) { create(:space) }
let(:url) { polymorphic_url(space) }

it { is_expected.to be_ok }
specify { perform_request && assert_select("##{dom_id(space)}") }

context "with a branded domain" do
let(:space) { create(:space, branded_domain: "beta.example.com") }

it "redirects to the domain" do
get polymorphic_path(space)
context "when accessing via the neighborhood url" do
it { is_expected.to redirect_to "http://beta.example.com" }
end

context "when accessing via domain" do
before do
space
host! "beta.example.com"
end

let(:url) { "http://beta.example.com" }

expect(response).to redirect_to "http://beta.example.com"
it { is_expected.to be_ok }
specify { perform_request && assert_select("##{dom_id(space)}") }
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/support/dom_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DomHelpers
def record_identifier
ActionView::RecordIdentifier
end
delegate :dom_id, to: :record_identifier

def test_response
TestResponse.new(response)
end

class TestResponse < SimpleDelegator
def media_type?(expected_media_type)
media_type == Mime[expected_media_type]
end
end
end

0 comments on commit 5313039

Please sign in to comment.