Commit 43ba052009dc4dd6cf2d20f65f90511692723177

Authored by Rodrigo Souto
1 parent 403560ba

users-controller-tests: refactor and improving performance

Showing 1 changed file with 20 additions and 32 deletions   Show diff stats
test/functional/users_controller_test.rb
@@ -11,9 +11,6 @@ class UsersControllerTest < ActionController::TestCase @@ -11,9 +11,6 @@ class UsersControllerTest < ActionController::TestCase
11 @request = ActionController::TestRequest.new 11 @request = ActionController::TestRequest.new
12 @response = ActionController::TestResponse.new 12 @response = ActionController::TestResponse.new
13 13
14 -  
15 - Environment.delete_all  
16 - Environment.create(:name => 'some env', :is_default => true)  
17 admin_user = create_user_with_permission('adminuser', 'manage_environment_users', Environment.default) 14 admin_user = create_user_with_permission('adminuser', 'manage_environment_users', Environment.default)
18 login_as('adminuser') 15 login_as('adminuser')
19 end 16 end
@@ -26,9 +23,6 @@ class UsersControllerTest < ActionController::TestCase @@ -26,9 +23,6 @@ class UsersControllerTest < ActionController::TestCase
26 end 23 end
27 24
28 should 'grant access with right permission' do 25 should 'grant access with right permission' do
29 - admin_user = create_user_with_permission('admin_user', 'manage_environment_users', Environment.default)  
30 - login_as('admin_user')  
31 -  
32 get :index 26 get :index
33 assert_response :success 27 assert_response :success
34 end 28 end
@@ -39,10 +33,10 @@ class UsersControllerTest < ActionController::TestCase @@ -39,10 +33,10 @@ class UsersControllerTest < ActionController::TestCase
39 deactivated.person.visible = false 33 deactivated.person.visible = false
40 deactivated.save! 34 deactivated.save!
41 get :index, :q => '' 35 get :index, :q => ''
42 - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}}  
43 - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /deactivated/}} 36 + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}}
  37 + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /deactivated/}}
44 end 38 end
45 - 39 +
46 should 'blank search include all users' do 40 should 'blank search include all users' do
47 (1..5).each {|i| 41 (1..5).each {|i|
48 u = create_user('user'+i.to_s) 42 u = create_user('user'+i.to_s)
@@ -51,10 +45,10 @@ class UsersControllerTest < ActionController::TestCase @@ -51,10 +45,10 @@ class UsersControllerTest < ActionController::TestCase
51 assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}} 45 assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /adminuser/}}
52 (1..5).each {|i| 46 (1..5).each {|i|
53 u = 'user'+i.to_s 47 u = 'user'+i.to_s
54 - assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} 48 + assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}}
55 } 49 }
56 end 50 end
57 - 51 +
58 should 'search not include all users' do 52 should 'search not include all users' do
59 (1..5).each {|i| 53 (1..5).each {|i|
60 u = create_user('user'+i.to_s) 54 u = create_user('user'+i.to_s)
@@ -63,27 +57,27 @@ class UsersControllerTest < ActionController::TestCase @@ -63,27 +57,27 @@ class UsersControllerTest < ActionController::TestCase
63 assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /user5/}} 57 assert_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => /user5/}}
64 (1..4).each {|i| 58 (1..4).each {|i|
65 u = 'user'+i.to_s 59 u = 'user'+i.to_s
66 - assert_no_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}} 60 + assert_no_tag :tag => 'div', :attributes => { :id => /users-list/ }, :descendant => {:tag => 'a', :attributes => {:title => u}}
67 } 61 }
68 end 62 end
69 - 63 +
70 should 'set admin role' do 64 should 'set admin role' do
71 - u = create_user()  
72 - assert_equal false, u.person.is_admin?  
73 - post :set_admin_role, :id => u.person.id, :q => ''  
74 - u.reload  
75 - assert u.person.is_admin? 65 + person = create_user.person
  66 + assert_equal false, person.is_admin?
  67 + post :set_admin_role, :id => person.id, :q => ''
  68 + person.reload
  69 + assert person.is_admin?
76 end 70 end
77 71
78 should 'reset admin role' do 72 should 'reset admin role' do
79 - u = create_user()  
80 - e = Environment.default  
81 - e.add_admin(u.person)  
82 - u.reload  
83 - assert u.person.is_admin?  
84 - post :reset_admin_role, :id => u.person.id, :q => ''  
85 - u.reload  
86 - assert_equal false, u.person.is_admin? 73 + environment = Environment.default
  74 + person = create_user.person
  75 + environment.add_admin(person)
  76 + assert person.is_admin?
  77 +
  78 + post :reset_admin_role, :id => person.id, :q => ''
  79 + person.reload
  80 + assert !person.is_admin?
87 end 81 end
88 82
89 should 'activate user' do 83 should 'activate user' do
@@ -106,17 +100,11 @@ class UsersControllerTest < ActionController::TestCase @@ -106,17 +100,11 @@ class UsersControllerTest < ActionController::TestCase
106 end 100 end
107 101
108 should 'response as XML to export users' do 102 should 'response as XML to export users' do
109 - admin_user = create_user_with_permission('admin_user', 'manage_environment_users', Environment.default)  
110 - login_as('admin_user')  
111 -  
112 get :download, :format => 'xml' 103 get :download, :format => 'xml'
113 assert_equal 'text/xml', @response.content_type 104 assert_equal 'text/xml', @response.content_type
114 end 105 end
115 106
116 should 'response as CSV to export users' do 107 should 'response as CSV to export users' do
117 - admin_user = create_user_with_permission('admin_user', 'manage_environment_users', Environment.default)  
118 - login_as('admin_user')  
119 -  
120 get :download, :format => 'csv' 108 get :download, :format => 'csv'
121 assert_equal 'text/csv', @response.content_type 109 assert_equal 'text/csv', @response.content_type
122 assert_equal 'name;email', @response.body.split("\n")[0] 110 assert_equal 'name;email', @response.body.split("\n")[0]