From f16222519573c6cd7bae593ed2bc2a09f69e28ca Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sun, 1 Feb 2015 11:16:50 +0100 Subject: [PATCH] Add authentication with devise, fix tests --- app/controllers/application_controller.rb | 1 + app/controllers/problems_controller.rb | 1 + app/controllers/translations_controller.rb | 1 + app/views/layouts/application.html.erb | 4 +++- test/controllers/problems_controller_test.rb | 2 ++ test/controllers/translations_controller_test.rb | 5 +++++ test/fixtures/admins.yml | 6 +++++- 7 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..407aa10 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + before_action :authenticate_admin! end diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index 43adad4..6bbc3fb 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -1,4 +1,5 @@ class ProblemsController < ApplicationController + skip_before_action :authenticate_admin!, only: [:index, :show] before_action :set_problem, only: [:show] def index diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 61aec87..7c3ea67 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -1,4 +1,5 @@ class TranslationsController < ApplicationController + skip_before_action :authenticate_admin!, only: [:new, :create] before_action :set_translation, only: :show before_action :set_problem, only: [:new, :create] diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 002277c..87f25b8 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -26,7 +26,9 @@ <%= horizontal do %> <%= nav class: 'navbar-left' do %> <%= link_to Problem.model_name.human(count: 2), problems_path %> - <%= link_to Translation.model_name.human(count: 2), translations_path %> + <% if admin_signed_in? %> + <%= link_to Translation.model_name.human(count: 2), translations_path %> + <% end %> <% end %> <%= nav class: 'navbar-right' do %> <% if admin_signed_in? %> diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb index 7f202d3..e553fa6 100644 --- a/test/controllers/problems_controller_test.rb +++ b/test/controllers/problems_controller_test.rb @@ -1,6 +1,8 @@ require 'test_helper' class ProblemsControllerTest < ActionController::TestCase + include Devise::TestHelpers + test "should get index" do get :index assert_response :success diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index df02bcc..f30faaa 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -1,15 +1,19 @@ require 'test_helper' class TranslationsControllerTest < ActionController::TestCase + include Devise::TestHelpers + setup do @translation = translations(:translation_one) @update = { title: 'New title', content: 'This is the new content', } + @admin = admins(:admin) end test "should get index" do + sign_in @admin get :index assert_response :success assert_not_nil assigns(:translations) @@ -29,6 +33,7 @@ class TranslationsControllerTest < ActionController::TestCase end test "should show translation" do + sign_in @admin get :show, id: @translation assert_response :success end diff --git a/test/fixtures/admins.yml b/test/fixtures/admins.yml index dfdafef..813e227 100644 --- a/test/fixtures/admins.yml +++ b/test/fixtures/admins.yml @@ -3,4 +3,8 @@ # This model initially had no columns defined. If you add columns to the # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below -# \ No newline at end of file +# + +admin: + email: admin@example.com + encrypted_password: <%= Devise.bcrypt(Admin, 'password') %> \ No newline at end of file