enterprise_controller_test.rb
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require File.dirname(__FILE__) + '/../test_helper'
require 'enterprise_controller'
# Re-raise errors caught by the controller.
class EnterpriseController; def rescue_action(e) raise e end; end
class EnterpriseControllerTest < Test::Unit::TestCase
all_fixtures
def setup
@controller = EnterpriseController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
# FIXME: this should be generic
def get(action, params = {})
super(action, {:profile => 'ze'}.merge(params))
end
# FIXME: this should be generic
def post(action, params = {})
super(action, {:profile => 'ze'}.merge(params))
end
def test_logged_with_one_enterprise_index
login_as 'ze'
get :index, :profile => 'ze'
assert_response :redirect
assert_redirected_to :action => 'show'
assert_kind_of Array, assigns(:my_pending_enterprises)
end
def test_logged_with_two_enterprises_index
login_as 'johndoe'
get :index
assert_response :redirect
assert_redirected_to :action => 'list'
assert_kind_of Array, assigns(:my_pending_enterprises)
end
def test_not_logged_index
get :index
assert_response :redirect
assert_redirected_to :controller => 'account'
end
def test_my_enterprises
login_as 'ze'
get :index
assert_not_nil assigns(:my_enterprises)
assert_kind_of Array, assigns(:my_enterprises)
end
def test_enterprise_listing
login_as 'ze'
get :list
assert_not_nil assigns(:enterprises)
assert Array, assigns(:enterprises)
end
def test_enterprise_showing
login_as 'ze'
get :show, :id => 5
assert_not_nil assigns(:enterprise)
assert_kind_of Enterprise, assigns(:enterprise)
end
def test_register_form
login_as 'ze'
get :register_form
assert_response :success
end
def test_register
login_as 'ze'
post :register, :enterprise => {:name => 'register_test', :identifier => 'register_test'}
assert_not_nil assigns(:enterprise)
assert_response :redirect
assert_redirected_to :action => 'index'
end
def test_fail_register
login_as 'ze'
post :register, :enterprise => {:name => ''}
assert_response :success
assert !assigns(:enterprise).valid?
end
def test_enterprise_editing
login_as 'ze'
e = create_enterprise
get :edit, :id => e
assert_not_nil assigns(:enterprise)
assert_kind_of Enterprise, assigns(:enterprise)
end
def test_enterprise_updating
login_as 'ze'
e = create_enterprise
post :update, :id => e, :enterprise => {:name => 'colivre'}
assert_not_nil assigns(:enterprise)
assert_kind_of Enterprise, assigns(:enterprise)
assert_response :redirect
assert_redirected_to :action => 'index'
end
def test_enterprise_updating_wrong
login_as 'ze'
e = create_enterprise
post :update, :id => e, :enterprise => {:name => ''} # name can't be blank
assert_not_nil assigns(:enterprise)
assert_kind_of Enterprise, assigns(:enterprise)
assert_response :success
assert_template 'edit'
end
def test_affiliate
login_as 'ze'
e = create_enterprise(:owner => 'johndoe', :user => 'ze')
post :affiliate, :id => e
assert assigns(:enterprise)
assert assigns(:enterprise).people.include?(assigns(:person))
assert assigns(:person).enterprises.include?(assigns(:enterprise))
end
def test_destroy
login_as 'ze'
e = create_enterprise
c = Enterprise.count
assert_nothing_raised { Enterprise.find(e) }
post :destroy, :id => e
assert assigns(:enterprise)
assert_raise ActiveRecord::RecordNotFound do
Enterprise.find(e)
end
assert_equal c - 1, Enterprise.count
end
def test_search
login_as 'ze'
e = create_enterprise(:tag_list => 'bla')
get :search, :query => 'bla'
assert assigns(:tagged_enterprises)
assert_kind_of Array, assigns(:tagged_enterprises)
end
def test_activate
login_as 'ze'
e = create_enterprise
post :activate, :id => e
assert assigns(:enterprise)
assert_kind_of Enterprise, assigns(:enterprise)
assert assigns(:enterprise).active
end
def test_approve
login_as 'ze'
e = create_enterprise
post :approve, :id => e
assert assigns(:enterprise)
end
protected
def create_enterprise(options = {})
owner = options.delete(:owner)
user = options.delete(:user)
login_as owner if owner
post :register, :enterprise => {:identifier => 'enterprise_a', :name => 'Enterprise A'}.merge(options)
id = assigns(:enterprise).id
login_as user if user
id
end
end