Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
handle non-admin user access; tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Nov 23, 2024
1 parent 33e4a50 commit bb0bb92
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApplicationController < BaseApplicationController

def ensure_admin
unless Current.user&.admin?
redirect_to root_path
redirect_to main_app.root_path, alert: "You are not authorized to access this page."
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StaticController < ApplicationController
allow_unauthenticated_access

def index
end
end
11 changes: 11 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
</head>

<body>
<%= link_to 'Home', root_path %>
<%= link_to 'Avo', Avo.configuration.root_path %>
<% if authenticated? %>
<%= Current.user.email_address %>
<%= button_to 'Sign out', session_path, method: :delete %>
<% else %>
<%= link_to 'Sign in', new_session_path %>
<% end %>
<%= notice %>
<%= alert %>
<hr>
<%= yield %>
</body>
</html>
2 changes: 2 additions & 0 deletions app/views/static/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Static#index</h1>
<p>Find me in app/views/static/index.html.erb</p>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker

# Defines the root path route ("/")
root "sessions#new"
root "static#index"
end
22 changes: 22 additions & 0 deletions test/controllers/static_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "test_helper"

class StaticControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get root_url
assert_response :success

get Avo.configuration.root_path
assert_redirected_to '/session/new'

Check failure on line 9 in test/controllers/static_controller_test.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

post '/session', params: { email_address: users(:one).email_address, password: 'password' }

Check failure on line 11 in test/controllers/static_controller_test.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 11 in test/controllers/static_controller_test.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
# assert_redirected_to Avo.configuration.root_path
get Avo.configuration.root_path
assert_redirected_to '/'

Check failure on line 14 in test/controllers/static_controller_test.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
follow_redirect!
assert_response :success

users(:one).update(admin: true)
get '/avo/resources/users'

Check failure on line 19 in test/controllers/static_controller_test.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
assert_response :success
end
end

0 comments on commit bb0bb92

Please sign in to comment.