Skip to content

Commit

Permalink
Merge pull request #303 from rubyforgood/home-link-fix
Browse files Browse the repository at this point in the history
fix subdomain spec stuff and home page links
  • Loading branch information
mark100net authored Apr 23, 2020
2 parents e47a2d8 + 7b9a954 commit 5bfd6ed
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 73 deletions.
8 changes: 5 additions & 3 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def create
@category = authorize Category.new(category_params)

if @category.save
redirect_to @category, notice: 'Category was successfully created.'
redirect_to category_url(@category, subdomain: current_tenant.subdomain),
notice: 'Category was successfully created.'
else
render :new
end
Expand All @@ -37,7 +38,8 @@ def update
authorize @category

if @category.update(category_params)
redirect_to @category, notice: 'Category was successfully updated.'
redirect_to category_url(@category, subdomain: current_tenant.subdomain),
notice: 'Category was successfully updated.'
else
render :edit
end
Expand All @@ -48,7 +50,7 @@ def destroy
authorize @category

if @category.destroy
redirect_to categories_url, alert: 'Category was successfully destroyed.'
redirect_to categories_url(subdomain: current_tenant.subdomain), alert: 'Category was successfully destroyed.'
end
end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class HomeController < ApplicationController
skip_before_action :authenticate_user!, :redirect_notenant

def index
@domain = [request.subdomains.last, request.domain].reject(&:blank?).join('.')
@organizations = Organization.nonadmin.order(:name)
end
end
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Welcome to Babywearing. Please click the link to one of our sites below:

<ul>
<% @organizations.each do |org| %>
<li><%= link_to org.name, root_url(subdomain: org.subdomain) %></li>
<li><%= link_to org.name, root_url(domain: @domain, subdomain: org.subdomain) %></li>
<% end %>
</ul>
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'mailcatcher', port: 1025 }

config.hosts << "midatlantic.lvh.me" << "acme.lvh.me" << "admin.lvh.me" << "lvh.me" << "midatlantic.stage.lvh.me"
config.hosts << /^([a-zA-Z0-9]*\.)*(lvh\.me|babywearing\.exchange)$/
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

resources :photos, only: :destroy

constraints subdomain: "admin" do
constraints subdomain: %w(admin admin.stage) do
root :to => "categories#index", :as=> :subdomain_root
resources :categories
end
Expand Down
2 changes: 1 addition & 1 deletion deploy/deploy_hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
placeholder
placeholder
8 changes: 4 additions & 4 deletions spec/features/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

it "allows user to create a category" do
visit categories_url
visit categories_url(subdomain: "admin")
find_link("+ New").click
fill_in "Name", with: "pineapple"
fill_in "Description", with: "sweet"
Expand All @@ -22,7 +22,7 @@
end

it "allows user to update a category" do
visit categories_url
visit categories_url(subdomain: "admin")
click_link category.name
expect(page).to have_content category.name
find_link("Edit").click
Expand All @@ -33,14 +33,14 @@
end

it "allows user to delete a category" do
visit categories_url
visit categories_url(subdomain: "admin")
expect(page).to have_content category.name
find_link("Destroy").click
expect(page).to have_content "Category was successfully destroyed."
end

it 'expands the subcategories table' do
visit(categories_url)
visit(categories_url(subdomain: "admin"))
expect(page).to have_content(category.name)
expect(page).not_to have_content(category_child.name)
find_link('+').click
Expand Down
10 changes: 10 additions & 0 deletions spec/requests/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@
expect(response).to redirect_to(new_user_session_url)
end
end

describe "admin organization redirect" do
context "with no session" do
it "redirects to sign in" do
get root_url(domain: "stage.example.com", subdomain: "admin")

expect(response).to redirect_to(new_user_session_url)
end
end
end
end
120 changes: 58 additions & 62 deletions spec/requests/categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,91 @@
RSpec.describe "Categories", type: :request do
let!(:category) { categories(:category_parent) }

within_subdomain("admin") do
describe 'GET /categories' do
it 'has http status 200' do
sign_in users(:admin_org)
get categories_url
describe 'GET /categories' do
it 'has http status 200' do
sign_in users(:admin_org)
get categories_url(subdomain: "admin")

expect(response).to be_successful
expect(response.body).to match(/category parent/)
end
expect(response).to be_successful
expect(response.body).to match(/category parent/)
end
end

describe 'POST /categories' do
let(:valid_attr) { { name: 'Test', description: 'Test content' } }
let(:invalid_attr) { { name: '' } }
describe 'POST /categories' do
let(:valid_attr) { { name: 'Test', description: 'Test content' } }
let(:invalid_attr) { { name: '' } }

context 'with valid attributes' do
it 'creates a category' do
sign_in users(:admin_org)
post categories_url, params: { category: valid_attr }
context 'with valid attributes' do
it 'creates a category' do
sign_in users(:admin_org)
post categories_url(subdomain: "admin"), params: { category: valid_attr }

expect(response).to redirect_to(category_url(assigns(:category)))
expect(flash[:notice]).to eq('Category was successfully created.')
end
expect(response).to redirect_to(category_url(assigns(:category), subdomain: "admin"))
expect(flash[:notice]).to eq('Category was successfully created.')
end
end

context 'with invalid attributes' do
it "doen't create a category" do
sign_in users(:admin_org)
send :post, categories_url, params: { category: invalid_attr }
context 'with invalid attributes' do
it "doesn't create a category" do
sign_in users(:admin_org)
send :post, categories_url(subdomain: "admin"), params: { category: invalid_attr }

expect(response.body).to match(/prohibited this/)
end
expect(response.body).to match(/prohibited this/)
end
end
end

describe 'GET /categories/:id' do
it 'has have_http_status 200' do
sign_in users(:admin_org)
get categories_url(category)
describe 'GET /categories/:id' do
it 'has have_http_status 200' do
sign_in users(:admin_org)
get categories_url(category, subdomain: "admin")

expect(response).to be_successful
expect(response.body).to match(/category parent/)
end
expect(response).to be_successful
expect(response.body).to match(/category parent/)
end
end

describe 'PUT /category/:id' do
let(:valid_attr) { { name: 'New agreement', description: 'New content' } }
let(:invalid_attr) { { name: '' } }
describe 'PUT /category/:id' do
let(:valid_attr) { { name: 'New category', description: 'New content' } }
let(:invalid_attr) { { name: '' } }

context 'with valid attributes' do
it 'updates the category' do
sign_in users(:admin_org)
send :put, category_url(category), params: { category: valid_attr }
context 'with valid attributes' do
it 'updates the category' do
sign_in users(:admin_org)
put category_url(category, subdomain: "admin"), params: { category: valid_attr }

expect(flash[:notice]).to eq('Category was successfully updated.')
end
expect(flash[:notice]).to eq('Category was successfully updated.')
end
end

context 'with invalid attributes' do
it "doesn't update the category" do
sign_in users(:admin_org)
send :put, category_url(category), params: { category: invalid_attr }
context 'with invalid attributes' do
it "doesn't update the category" do
sign_in users(:admin_org)
put category_url(category, subdomain: "admin"), params: { category: invalid_attr }

expect(response.body).to match(/prohibited this/)
end
expect(response.body).to match(/prohibited this/)
end
end
end

describe 'DELETE /category/:id' do
let(:category) { Category.create(name: 'Test', description: 'Test') }
describe 'DELETE /category/:id' do
let(:category) { Category.create(name: 'Test', description: 'Test') }

it 'destroys the category' do
sign_in users(:admin_org)
send :delete, category_url(category)
it 'destroys the category' do
sign_in users(:admin_org)
delete category_url(category, subdomain: "admin")

expect(response).to have_http_status(:found)
expect(flash[:alert]).to eq('Category was successfully destroyed.')
end
expect(response).to have_http_status(:found)
expect(flash[:alert]).to eq('Category was successfully destroyed.')
end
end

within_subdomain("midatlantic") do
describe 'GET /categories' do
it 'has a routing error' do
sign_in users(:member)
expect do
get "http://midatlantic.example.com/categories"
end.to raise_error(ActionController::RoutingError)
end
describe 'GET /categories' do
it 'has a routing error' do
sign_in users(:member)
expect do
get "http://midatlantic.example.com/categories"
end.to raise_error(ActionController::RoutingError)
end
end
end

0 comments on commit 5bfd6ed

Please sign in to comment.