diff --git a/app/controllers/admin/application_controller.rb b/app/controllers/admin/application_controller.rb index 13beeec..16e0b3f 100644 --- a/app/controllers/admin/application_controller.rb +++ b/app/controllers/admin/application_controller.rb @@ -9,9 +9,20 @@ module Admin before_filter :authenticate_admin def authenticate_admin - ok = current_user != nil - ok = current_user.email == "torsten@villataika.fi" if ok - redirect_to "/" unless ok + user = current_user + if(user) + redirect_to "/" unless user.admin? + end + end + end + class AdminController < ApplicationController + before_filter :authenticate_admin + + def authenticate_admin + user = current_user + if(user) + redirect_to "/" unless user.admin? + end end end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 43795c3..326803f 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,19 +1,4 @@ module Admin - class UsersController < Admin::ApplicationController - # To customize the behavior of this controller, - # simply overwrite any of the RESTful actions. For example: - # - # def index - # super - # @resources = User.all.paginate(10, params[:page]) - # end - - # Define a custom finder by overriding the `find_resource` method: - # def find_resource(param) - # User.find_by!(slug: param) - # end - - # See https://administrate-docs.herokuapp.com/customizing_controller_actions - # for more information + class UsersController < Admin::ApplicationController #AdminController end end diff --git a/spec/features/admin_users/users_spec.rb b/spec/features/admin_users/users_spec.rb new file mode 100644 index 0000000..3f949a1 --- /dev/null +++ b/spec/features/admin_users/users_spec.rb @@ -0,0 +1,12 @@ +describe User do + before(:each) do + sign_admin_in + end + it "lists users" do + visit_path admin_users_path + end + it "shows a user" do + user = create(:user) + visit_path admin_user_path(user) + end +end diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index 37579cc..a2cde38 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -3,7 +3,7 @@ describe UserPolicy do let (:current_user) { FactoryGirl.build_stubbed :user } let (:other_user) { FactoryGirl.build_stubbed :user } - let (:admin) { FactoryGirl.build_stubbed :user, :admin } + let (:admin) { FactoryGirl.build_stubbed :admin } permissions :index? do it "denies access if not an admin" do diff --git a/spec/support/request_helper.rb b/spec/support/request_helper.rb new file mode 100644 index 0000000..0caa480 --- /dev/null +++ b/spec/support/request_helper.rb @@ -0,0 +1,29 @@ +module RequestHelper + def ensure_path path + expect(page.current_path).to eq path + end + def visit_path path + visit path + expect(status_code).to be 200 + expect(page).not_to have_css(".translation_missing") + ensure_path path + end + + def ensure_admin + admin = User.where(:role => :admin).first + admin = create :admin unless admin + expect(admin).not_to be nil + admin + end + + def sign_admin_in + admin = ensure_admin + signin(admin.email, admin.password , ".footer-sign") + expect(page).to have_content I18n.t 'devise.sessions.signed_in' + admin + end +end + +RSpec.configure do |config| + config.include RequestHelper +end