From: Andy Allan Date: Wed, 31 May 2017 17:40:13 +0000 (+0100) Subject: Refactor the user list test to use factory-built users and explicit assertions. X-Git-Tag: live~3925 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/cea6f9c4cfc7dc1bd0dc9aa71f3a39feecb98113?ds=sidebyside Refactor the user list test to use factory-built users and explicit assertions. --- diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index 3232b9e2e..d3a654437 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -1348,44 +1348,55 @@ class UserControllerTest < ActionController::TestCase end def test_list_get + user = create(:user) + moderator_user = create(:moderator_user) + administrator_user = create(:administrator_user) + suspended_user = create(:user, :suspended) + ip_user = create(:user, :creation_ip => "1.2.3.4") + + # There are now 7 users - the five above, plus two extra "granters" for the + # moderator_user and administrator_user + assert_equal 7, User.count + # Shouldn't work when not logged in get :list assert_response :redirect assert_redirected_to :action => :login, :referer => users_path - session[:user] = create(:user).id + session[:user] = user.id # Shouldn't work when logged in as a normal user get :list assert_response :redirect assert_redirected_to :action => :login, :referer => users_path - session[:user] = create(:moderator_user).id + session[:user] = moderator_user.id # Shouldn't work when logged in as a moderator get :list assert_response :redirect assert_redirected_to :action => :login, :referer => users_path - session[:user] = create(:administrator_user).id + session[:user] = administrator_user.id + # Note there is a header row, so all row counts are users + 1 # Should work when logged in as an administrator get :list assert_response :success assert_template :list - assert_select "table#user_list tr", :count => User.count + 1 + assert_select "table#user_list tr", :count => 7 + 1 # Should be able to limit by status get :list, :status => "suspended" assert_response :success assert_template :list - assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1 + assert_select "table#user_list tr", :count => 1 + 1 # Should be able to limit by IP address get :list, :ip => "1.2.3.4" assert_response :success assert_template :list - assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1 + assert_select "table#user_list tr", :count => 1 + 1 end def test_list_get_paginated