From d75a88bf6e7370d7ca7f909e51bb99a5b12d8995 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 4 Feb 2019 13:58:20 +0100 Subject: [PATCH] Fix seed data, improve admin area testing --- app/controllers/admin_controller.rb | 2 +- db/seeds.rb | 33 +++++++++++-------- .../admin/dashboard_controller_test.rb | 10 +++++- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 34852f6..71752e9 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -3,6 +3,6 @@ class AdminController < ApplicationController def authenticate! authenticate_user! - raise SecurityError unless current_user.admin? + throw(:warden) unless current_user.admin? end end \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index 0986997..6a36b07 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -8,7 +8,7 @@ Translation.delete_all Problem.delete_all -Admin.delete_all +User.delete_all for i in 1..20 do @@ -17,27 +17,32 @@ end for i in 1..10 do translation = Translation.create!( - problem: Problem.find(i), - title: "Problem Nummer #{i}", - content: %Q(

Das hier ist der Inhalt von Problem #{i}. -
Hier ist die zweite Zeile.

-

Es können auch Formeln im Text (z.B. $1+2=3$) oder als eigene Zeile genutzt werden.

- \\[ a^2 + b^2 = c^2 \\] - ) + problem: Problem.find(i), + title: "Problem Nummer #{i}", + content: %Q(

Das hier ist der Inhalt von Problem #{i}. +
Hier ist die zweite Zeile.

+

Es können auch Formeln im Text (z.B. $1+2=3$) oder als eigene Zeile genutzt werden.

+\\[ a^2 + b^2 = c^2 \\]) ) problem = Problem.find(i) problem.set_translation(translation) - problem.save! end -Admin.create!( - email: 'admin@example.com', - password: 'password', - password_confirmation: 'password' +User.create!( + provider: :developer, + uid: "admin", + name: "admin", + role: 1 +) + +User.create!( + provider: :developer, + uid: "translator", + name: "translator" ) p "Created #{Problem.count} problems" p "Created #{Translation.count} translations" -p "Created #{Admin.count} admins" +p "Created #{User.count} users" diff --git a/test/controllers/admin/dashboard_controller_test.rb b/test/controllers/admin/dashboard_controller_test.rb index 586211f..fa89bbd 100644 --- a/test/controllers/admin/dashboard_controller_test.rb +++ b/test/controllers/admin/dashboard_controller_test.rb @@ -4,21 +4,29 @@ class Admin::DashboardControllerTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers setup do - login_admin + end + + test "should not get index if not admin" do + login_translator + get admin_dashboard_index_url + assert_redirected_to root_url end test "should get index" do + login_admin get admin_dashboard_index_url assert_response :success end test "should post new problem count" do + login_admin post admin_dashboard_update_problem_count_url(problem_count: 15) assert_redirected_to admin_dashboard_index_url assert_equal 15, Problem.count end test "should fail incorrect problem count" do + login_admin post admin_dashboard_update_problem_count_url(problem_count: 2) assert_redirected_to admin_dashboard_index_url assert_equal 3, Problem.count