Skip to content

Commit

Permalink
Merge pull request #522 from ZeusWPI/feature/521/dummy-dev-login
Browse files Browse the repository at this point in the history
Feature/521/dummy dev login
  • Loading branch information
TomNaessens committed Nov 23, 2015
2 parents d206a59 + 44fd500 commit 5ce18c6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
17 changes: 17 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class UsersController < ApplicationController
before_filter :restrict_to_development

def login
u = User.find_or_create_by(username: "tnnaesse", admin: true)
u.clubs = Club.where(internal_name: 'zeus')
sign_in(u)

redirect_to '/'
end

protected
# this method should be placed in ApplicationController
def restrict_to_development
head(:bad_request) unless Rails.env.development?
end
end
22 changes: 22 additions & 0 deletions app/views/layouts/_login.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="navbar-form <%= classes %>">
<div class="form-group">
<% if user_signed_in? %>
<div class="form-group">
<%= button_to "Logout", destroy_user_session_path(current_user), class: "btn btn-default form-control", method: :delete %>
</div>
<% else %>
<div class="form-group">
<div class="button-group">
<%= button_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
</div>
</div>
<% if Rails.env.development? %>
<div class="form-group">
<div class="button-group">
<%= button_to "Login (dev)", dev_login_path, class: "btn form-control" %>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
22 changes: 3 additions & 19 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
<%= render partial: "layouts/ga" if Rails.env.production? %>
<%= csrf_meta_tags %>
<% if content_for?(:og_title) %>
<meta property="og:title" content="<%= yield(:og_title) %>" />
<% end %>
Expand Down Expand Up @@ -40,15 +40,7 @@
<li><%= link_to "Events", events_path %></li>
</ul>

<div class="hidden-xs navbar-form navbar-right">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path(current_user), class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= button_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<% end %>
</div>
</div>
<%= render partial: "layouts/login", locals: {classes: "hidden-xs navbar-right"} %>

<ul class="nav navbar-nav navbar-right">
<li><%= mail_to "gandalf@zeus.ugent.be", "Send feedback" %></li>
Expand All @@ -67,15 +59,7 @@
<% end %>
</ul>

<div class="visible-xs navbar-form">
<div class="form-group">
<% if user_signed_in? %>
<%= button_to "Logout", destroy_user_session_path(current_user), class: "btn btn-default form-control", method: :delete %>
<% else %>
<%= button_to "Login", new_user_session_path, class: "btn btn-success form-control" %>
<% end %>
</div>
</div>
<%= render partial: "layouts/login", locals: {classes: 'visible-xs'} %>

</div><!-- /.navbar-collapse -->
</div><!-- /.navbar-collapse -->
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@

patch "events/:event_id/access_level/:access_level_id/set_zones", to: "access_levels#set_zones", as: "set_zones_for_access_level"

# Development backdoor
if Rails.env.development?
post "dev_login", to: "users#login"
end

# Example resource route with options:
# resources :products do
# member do
Expand Down
15 changes: 15 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
test "developer backdoor doesnt work in production" do
stub_env "production" do
assert Rails.env.production?
# Not actually a decent assert as the route didn't exist
# before the environment change as the routes gets also
# skipped in the current test environment ¯\_(ツ)_/¯
assert_raises(ActionController::UrlGenerationError) do
post :dev_login
end
end
end
end
12 changes: 11 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@

I18n.enforce_available_locales = false

class Minitest::Test
def stub_env(new_env, &block)
original_env = Rails.env
Rails.instance_variable_set("@_env", new_env.inquiry)
block.call
ensure
Rails.instance_variable_set("@_env", original_env.inquiry)
end
end

class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!

Expand All @@ -37,8 +47,8 @@ def self.verify_fixtures(clazz)
clazz.all.map { |o| assert o.valid?, o.inspect.to_s + "\n" + o.errors.full_messages.join("\n") }
end
end

end

class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
#
Expand Down

0 comments on commit 5ce18c6

Please sign in to comment.