@@ -24,7 +24,7 @@ class SpacesControllerTest < ActionController::TestCase
24
24
assert_not_empty assigns ( :spaces ) , 'spaces is empty'
25
25
end
26
26
27
- test 'admin should get index' do
27
+ test 'site admin should get index' do
28
28
sign_in users ( :admin )
29
29
get :index
30
30
assert_response :success
@@ -48,7 +48,16 @@ class SpacesControllerTest < ActionController::TestCase
48
48
assert_select 'a.btn[href=?]' , space_path ( @space ) , text : 'Delete' , count : 0
49
49
end
50
50
51
- test 'admin should show space' do
51
+ test 'space admin should show space' do
52
+ sign_in users ( :space_admin )
53
+ get :show , params : { id : @space }
54
+ assert_response :success
55
+ assert assigns ( :space )
56
+ assert_select 'a.btn[href=?]' , edit_space_path ( @space ) , count : 1
57
+ assert_select 'a.btn[href=?]' , space_path ( @space ) , text : 'Delete' , count : 0
58
+ end
59
+
60
+ test 'site admin should show space' do
52
61
sign_in users ( :admin )
53
62
get :show , params : { id : @space }
54
63
assert_response :success
@@ -63,12 +72,18 @@ class SpacesControllerTest < ActionController::TestCase
63
72
assert_redirected_to new_user_session_path
64
73
end
65
74
66
- test 'admin should get new' do
75
+ test 'site admin should get new' do
67
76
sign_in users ( :admin )
68
- get :new , params : { content_provider_id : content_providers ( :goblet ) }
77
+ get :new
69
78
assert_response :success
70
79
end
71
80
81
+ test 'space admin should not get new' do
82
+ sign_in users ( :space_admin )
83
+ get :new
84
+ assert_response :forbidden
85
+ end
86
+
72
87
# EDIT Tests
73
88
test 'public should not get edit' do
74
89
get :edit , params : { id : @space }
@@ -87,7 +102,19 @@ class SpacesControllerTest < ActionController::TestCase
87
102
assert_response :forbidden
88
103
end
89
104
90
- test 'admin should get edit' do
105
+ test 'space admin should get edit' do
106
+ sign_in users ( :space_admin )
107
+ get :edit , params : { id : @space }
108
+ assert_response :success
109
+ end
110
+
111
+ test 'space admin should not get edit for other space' do
112
+ sign_in users ( :space_admin )
113
+ get :edit , params : { id : spaces ( :other ) }
114
+ assert_response :forbidden
115
+ end
116
+
117
+ test 'site admin should get edit' do
91
118
sign_in users ( :admin )
92
119
get :edit , params : { id : @space }
93
120
assert_response :success
@@ -110,7 +137,16 @@ class SpacesControllerTest < ActionController::TestCase
110
137
assert_response :forbidden
111
138
end
112
139
113
- test 'admin can create space' do
140
+ test 'space admin cannot create new space' do
141
+ sign_in users ( :space_admin )
142
+ refute_permitted SpacePolicy , @space . user , :create? , Space
143
+ assert_no_difference 'Space.count' do
144
+ post :create , params : @space_params
145
+ end
146
+ assert_response :forbidden
147
+ end
148
+
149
+ test 'site admin can create space' do
114
150
sign_in users ( :admin )
115
151
assert_difference 'Space.count' , 1 do
116
152
post :create , params : @space_params
@@ -138,7 +174,15 @@ class SpacesControllerTest < ActionController::TestCase
138
174
assert_equal 'plants.mytess.training' , assigns ( :space ) . host , 'Non-admin user should not be able to modify host'
139
175
end
140
176
141
- test 'admin should update space' do
177
+ test 'space admin should update owned space' do
178
+ sign_in users ( :space_admin )
179
+ patch :update , params : { id : @space , space : { title : 'New Title' , host : 'newhost.mytess.golf' } }
180
+ assert_redirected_to space_path ( assigns ( :space ) )
181
+ assert_equal 'New Title' , assigns ( :space ) . title
182
+ assert_equal 'plants.mytess.training' , assigns ( :space ) . host , 'Non-admin user should not be able to modify host'
183
+ end
184
+
185
+ test 'site admin should update space' do
142
186
sign_in users ( :admin )
143
187
assert_no_difference 'Space.count' do
144
188
patch :update , params : { id : @space , space : { title : 'New Title' , host : 'newhost.mytess.golf' } }
@@ -164,12 +208,34 @@ class SpacesControllerTest < ActionController::TestCase
164
208
end
165
209
end
166
210
167
- test 'admin can destroy space' do
211
+ test 'space admin cannot destroy space' do
212
+ sign_in users ( :space_admin )
213
+ assert_no_difference 'Space.count' do
214
+ delete :destroy , params : { id : @space }
215
+ assert_response :forbidden
216
+ end
217
+ end
218
+
219
+ test 'site admin can destroy space' do
168
220
sign_in users ( :admin )
169
221
assert_difference 'Space.count' , -1 do
170
222
post :destroy , params : { id : @space }
171
223
assert_redirected_to spaces_path
172
224
assert_equal 'Space was successfully deleted.' , flash [ :notice ]
173
225
end
174
226
end
227
+
228
+ test 'space admin can assign new space admins' do
229
+ existing_admin = users ( :space_admin )
230
+ sign_in existing_admin
231
+ new_admin = users ( :regular_user )
232
+ assert_difference ( 'SpaceRole.count' , 1 ) do # space_admin is already an admin, so only increases by 1
233
+ patch :update , params : { id : @space , space : { title : 'New Title' , administrator_ids : [ existing_admin . id , new_admin . id ] } }
234
+ assert_redirected_to space_path ( assigns ( :space ) )
235
+ end
236
+
237
+ admins = assigns ( :space ) . administrators
238
+ assert_includes admins , existing_admin
239
+ assert_includes admins , new_admin
240
+ end
175
241
end
0 commit comments