Skip to content

Commit

Permalink
Requiring admin to category functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
erickm32 committed Mar 1, 2017
1 parent e4b1b55 commit 3651fc9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CategoriesController < ApplicationController
before_action :require_admin, except: [:index, :show]

def index
@categories = Category.paginate(page: params[:page], per_page: 5)
end
Expand All @@ -24,4 +26,11 @@ def show
def category_params
params.require(:category).permit(:name)
end

def require_admin
if !logged_in? || (logged_in? && !current_user.admin?)
flash[:danger] = "Only admins can perform that action."
redirect_to categories_path
end
end
end
9 changes: 9 additions & 0 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CategoriesControllerTest < ActionController::TestCase

def setup
@category = Category.create(name: "sports")
@user = User.create(username: "John", email: "[email protected]", password: "password", admin: true)
end

test 'should get category index' do
Expand All @@ -12,6 +13,7 @@ def setup
end

test 'should get category new' do
session[:user_id] = @user.id
get :new
assert_response :success
end
Expand All @@ -20,4 +22,11 @@ def setup
get :show, params: { id: @category.id }
assert_response :success
end

test 'should redirect create when admin not logged in' do
assert_no_difference 'Category.count' do
post :create, params: { category: { name: "sports" } }
end
assert_redirected_to categories_path
end
end
7 changes: 7 additions & 0 deletions test/integration/create_categories_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
require 'test_helper'

class CreateCategoriesTest < ActionDispatch::IntegrationTest

def setup
@user = User.create(username: "John", email: "[email protected]", password: "password", admin: true)
end

test "get new category test and create category" do
sign_in_as(@user, "password")
get new_category_path
assert_template 'categories/new'
assert_difference 'Category.count', 1 do
Expand All @@ -14,6 +20,7 @@ class CreateCategoriesTest < ActionDispatch::IntegrationTest
end

test "invalid category submission results in failure" do
sign_in_as(@user, "password")
get new_category_path
assert_template 'categories/new'
assert_no_difference 'Category.count' do
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class ActiveSupport::TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
def sign_in_as(user, password)
post login_path, params: { session: { email: user.email, password: password } }
end

end

0 comments on commit 3651fc9

Please sign in to comment.