Commit e22aa922dcfdeb7042f569910ffaa571043747b8

Authored by Braulio Bhavamitra
1 parent a2e3c514

Fix mock abused test

app/controllers/my_profile/enterprise_validation_controller.rb
1 class EnterpriseValidationController < MyProfileController 1 class EnterpriseValidationController < MyProfileController
2 2
3 protect 'validate_enterprise', :profile 3 protect 'validate_enterprise', :profile
4 - 4 +
5 def index 5 def index
6 @pending_validations = profile.pending_validations 6 @pending_validations = profile.pending_validations
7 end 7 end
@@ -27,7 +27,7 @@ class EnterpriseValidationController &lt; MyProfileController @@ -27,7 +27,7 @@ class EnterpriseValidationController &lt; MyProfileController
27 post_only :reject 27 post_only :reject
28 def reject 28 def reject
29 @pending = profile.find_pending_validation(params[:id]) 29 @pending = profile.find_pending_validation(params[:id])
30 - if @pending 30 + if @pending
31 @pending.reject_explanation = params[:reject_explanation] 31 @pending.reject_explanation = params[:reject_explanation]
32 begin 32 begin
33 @pending.reject 33 @pending.reject
app/models/validation_info.rb
@@ -2,9 +2,10 @@ class ValidationInfo &lt; ActiveRecord::Base @@ -2,9 +2,10 @@ class ValidationInfo &lt; ActiveRecord::Base
2 2
3 attr_accessible :validation_methodology, :restrictions, :organization 3 attr_accessible :validation_methodology, :restrictions, :organization
4 4
5 - validates_presence_of :validation_methodology  
6 -  
7 belongs_to :organization 5 belongs_to :organization
8 6
  7 + validates_presence_of :organization
  8 + validates_presence_of :validation_methodology
  9 +
9 xss_terminate :only => [ :validation_methodology, :restrictions ], :on => 'validation' 10 xss_terminate :only => [ :validation_methodology, :restrictions ], :on => 'validation'
10 end 11 end
test/functional/enterprise_validation_controller_test.rb
@@ -12,126 +12,109 @@ class EnterpriseValidationControllerTest &lt; ActionController::TestCase @@ -12,126 +12,109 @@ class EnterpriseValidationControllerTest &lt; ActionController::TestCase
12 @controller = EnterpriseValidationController.new 12 @controller = EnterpriseValidationController.new
13 @request = ActionController::TestRequest.new 13 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new 14 @response = ActionController::TestResponse.new
15 - 15 +
16 login_as 'ze' 16 login_as 'ze'
17 - @org = Organization.create!(:identifier => 'myorg', :name => "My Org") 17 + @user = Profile['ze']
  18 + @org = Organization.create!(identifier: 'myorg', name: "My Org")
18 give_permission('ze', 'validate_enterprise', @org) 19 give_permission('ze', 'validate_enterprise', @org)
19 - Profile.expects(:find_by_identifier).with('myorg').returns(@org).at_least_once  
20 end 20 end
21 21
22 should 'list pending validations on index' do 22 should 'list pending validations on index' do
23 - empty = []  
24 - @org.expects(:pending_validations).returns(empty)  
25 - get :index, :profile => 'myorg'  
26 - assert_equal empty, assigns(:pending_validations) 23 + get :index, profile: 'myorg'
  24 + assert_equal [], assigns(:pending_validations)
27 assert_template 'index' 25 assert_template 'index'
28 end 26 end
29 27
30 should 'display details and prompt for needed data when approving or rejecting enterprise' do 28 should 'display details and prompt for needed data when approving or rejecting enterprise' do
31 - validating = CreateEnterprise.new  
32 - @org.expects(:find_pending_validation).with('kakakaka').returns(validating)  
33 -  
34 - get :details, :profile => 'myorg', :id => 'kakakaka'  
35 - assert_same validating, assigns(:pending) 29 + code = 'kakakaka'
  30 + @org.validations.create! code: code, name: 'test', identifier: 'test', requestor: @user, target: @org
  31 + get :details, profile: 'myorg', id: code
  32 + assert_equal @org.find_pending_validation(code), assigns(:pending)
36 end 33 end
37 34
38 should 'refuse to validate unexisting request' do 35 should 'refuse to validate unexisting request' do
39 - @org.expects(:find_pending_validation).with('kakakaka').returns(nil)  
40 - get :details , :profile => 'myorg', :id => 'kakakaka' 36 + get :details, profile: 'myorg', id: 'kakakaka'
41 assert_response 404 37 assert_response 404
42 end 38 end
43 39
44 should 'be able to actually validate enterprise on request' do 40 should 'be able to actually validate enterprise on request' do
45 - validation = CreateEnterprise.new  
46 - @org.expects(:find_pending_validation).with('kakakaka').returns(validation)  
47 - validation.expects(:approve)  
48 - validation.expects(:code).returns('kakakaka')  
49 - post :approve, :profile => 'myorg', :id => 'kakakaka'  
50 - assert_redirected_to :action => 'view_processed', :id => 'kakakaka' 41 + code = 'kakakaka'
  42 + @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
  43 + post :approve, profile: 'myorg', id: code
  44 + assert_redirected_to action: 'view_processed', id: code
51 end 45 end
52 46
53 should 'be able to reject an enterprise' do 47 should 'be able to reject an enterprise' do
54 - validation = CreateEnterprise.new  
55 - @org.expects(:find_pending_validation).with('kakakaka').returns(validation)  
56 - validation.expects(:reject)  
57 - validation.expects(:code).returns('kakakaka')  
58 - post :reject, :profile => 'myorg', :id => 'kakakaka', :reject_explanation => 'this is not a solidarity economy enterprise'  
59 - assert_redirected_to :action => 'view_processed', :id => 'kakakaka' 48 + code = 'kakakaka'
  49 + @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
  50 + post :reject, profile: 'myorg', id: code, reject_explanation: 'this is not a solidarity economy enterprise'
  51 + assert_redirected_to action: 'view_processed', id: code
60 end 52 end
61 53
62 should 'require the user to fill in the explanation for an rejection' do 54 should 'require the user to fill in the explanation for an rejection' do
63 - validation = CreateEnterprise.new  
64 - validation.stubs(:environment).returns(Environment.default)  
65 - @org.expects(:find_pending_validation).with('kakakaka').returns(validation)  
66 -  
67 - # FIXME: this is not working, but should. Anyway the assert_response and  
68 - # assert_template below in some test some things we need. But the  
69 - # expectation below must be put to work.  
70 - #  
71 - #validation.expects(:reject).raises(ActiveRecord::RecordInvalid) 55 + code = 'kakakaka'
  56 + @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
72 57
73 - post :reject, :profile => 'myorg', :id => 'kakakaka' 58 + post :reject, profile: 'myorg', id: code
74 assert_response :success 59 assert_response :success
75 assert_template 'details' 60 assert_template 'details'
76 end 61 end
77 62
78 should 'list validations already processed' do 63 should 'list validations already processed' do
79 - processed_validations = [CreateEnterprise.new]  
80 - @org.expects(:processed_validations).returns(processed_validations)  
81 -  
82 - get :list_processed, :profile => 'myorg' 64 + v = @org.validations.create! code: 'kakakaka', name: 'test2', identifier: 'test2', requestor: @user, target: @org
  65 + v.perform
83 66
84 - assert_equal processed_validations, assigns(:processed_validations) 67 + get :list_processed, profile: 'myorg'
  68 +
  69 + assert_equal @org.processed_validations, assigns(:processed_validations)
85 70
86 assert_response :success 71 assert_response :success
87 assert_template 'list_processed' 72 assert_template 'list_processed'
88 end 73 end
89 - 74 +
90 should 'be able to display a validation that was already processed' do 75 should 'be able to display a validation that was already processed' do
91 - validation = CreateEnterprise.new  
92 - @org.expects(:find_processed_validation).with('kakakaka').returns(validation)  
93 - get :view_processed, :profile => 'myorg', :id => 'kakakaka'  
94 - assert_same validation, assigns(:processed) 76 + code = 'kakakaka'
  77 + v = @org.validations.create! code: code, name: 'test2', identifier: 'test2', requestor: @user, target: @org
  78 + v.perform
  79 +
  80 + get :view_processed, profile: 'myorg', id: code
  81 + assert_same @org.processed_validations.first, assigns(:processed)
95 end 82 end
96 83
97 should 'display a form for editing the validation info' do 84 should 'display a form for editing the validation info' do
98 - info = ValidationInfo.new(:validation_methodology => 'none')  
99 - @org.expects(:validation_info).returns(info)  
100 - get :edit_validation_info, :profile => 'myorg' 85 + info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
  86 + get :edit_validation_info, profile: 'myorg'
101 assert_response :success 87 assert_response :success
102 assert_equal info, assigns(:info) 88 assert_equal info, assigns(:info)
103 end 89 end
104 90
105 should 'save an alteration of the validation info' do 91 should 'save an alteration of the validation info' do
106 - info = ValidationInfo.new(:validation_methodology => 'none')  
107 - @org.expects(:validation_info).returns(info)  
108 - post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => 'new methodology'}  
109 - 92 + info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
  93 + post :edit_validation_info, profile: 'myorg', info: {validation_methodology: 'new methodology'}
  94 +
110 assert_response :redirect 95 assert_response :redirect
111 - assert_redirected_to :action => 'index'  
112 - assert_equal info, assigns(:info) 96 + assert_redirected_to action: 'index'
  97 + info.reload
  98 + assert_equal info.reload, assigns(:info)
113 end 99 end
114 100
115 should 'not save an empaty validation mthodology' do 101 should 'not save an empaty validation mthodology' do
116 - info = ValidationInfo.new(:validation_methodology => 'none')  
117 - @org.expects(:validation_info).returns(info)  
118 - post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => ''}  
119 - 102 + info = @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
  103 + post :edit_validation_info, profile: 'myorg', info: {validation_methodology: ''}
  104 +
120 assert_response :success 105 assert_response :success
121 assert_equal info, assigns(:info) 106 assert_equal info, assigns(:info)
122 end 107 end
123 108
124 should 'filter html from methodology of the validation info' do 109 should 'filter html from methodology of the validation info' do
125 - info = ValidationInfo.new(:validation_methodology => 'none')  
126 - @org.expects(:validation_info).returns(info)  
127 - post :edit_validation_info, :profile => 'myorg', :info => {:validation_methodology => 'new <b>methodology</b>'} 110 + @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
  111 + post :edit_validation_info, profile: 'myorg', info: {validation_methodology: 'new <b>methodology</b>'}
128 assert_sanitized assigns(:info).validation_methodology 112 assert_sanitized assigns(:info).validation_methodology
129 end 113 end
130 114
131 should 'filter html from restrictions of the validation info' do 115 should 'filter html from restrictions of the validation info' do
132 - info = ValidationInfo.new(:validation_methodology => 'none')  
133 - @org.expects(:validation_info).returns(info)  
134 - post :edit_validation_info, :profile => 'myorg', :info => {:restrictions => 'new <b>methodology</b>'} 116 + @org.validation_info = ValidationInfo.create! validation_methodology: 'none', organization: @org
  117 + post :edit_validation_info, profile: 'myorg', info: {restrictions: 'new <b>methodology</b>'}
135 assert_sanitized assigns(:info).restrictions 118 assert_sanitized assigns(:info).restrictions
136 end 119 end
137 120
test/integration/routing_test.rb
@@ -27,6 +27,10 @@ class RoutingTest &lt; ActionController::IntegrationTest @@ -27,6 +27,10 @@ class RoutingTest &lt; ActionController::IntegrationTest
27 assert_routing('/account/new_password/90dfhga7sadgd0as6saas', :controller => 'account', :action => 'new_password', :code => '90dfhga7sadgd0as6saas') 27 assert_routing('/account/new_password/90dfhga7sadgd0as6saas', :controller => 'account', :action => 'new_password', :code => '90dfhga7sadgd0as6saas')
28 end 28 end
29 29
  30 + should 'ignore case for profiles' do
  31 + assert_routing '/myprofile/ZE/cms', profile: 'ZE', controller: 'cms', action: 'index'
  32 + end
  33 +
30 def test_cms 34 def test_cms
31 assert_routing('/myprofile/ze/cms', :profile => 'ze', :controller => 'cms', :action => 'index') 35 assert_routing('/myprofile/ze/cms', :profile => 'ze', :controller => 'cms', :action => 'index')
32 end 36 end