@@ -24,7 +24,7 @@ class SpacesControllerTest < ActionController::TestCase
2424 assert_not_empty assigns ( :spaces ) , 'spaces is empty'
2525 end
2626
27- test 'admin should get index' do
27+ test 'site admin should get index' do
2828 sign_in users ( :admin )
2929 get :index
3030 assert_response :success
@@ -48,7 +48,16 @@ class SpacesControllerTest < ActionController::TestCase
4848 assert_select 'a.btn[href=?]' , space_path ( @space ) , text : 'Delete' , count : 0
4949 end
5050
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
5261 sign_in users ( :admin )
5362 get :show , params : { id : @space }
5463 assert_response :success
@@ -63,12 +72,18 @@ class SpacesControllerTest < ActionController::TestCase
6372 assert_redirected_to new_user_session_path
6473 end
6574
66- test 'admin should get new' do
75+ test 'site admin should get new' do
6776 sign_in users ( :admin )
68- get :new , params : { content_provider_id : content_providers ( :goblet ) }
77+ get :new
6978 assert_response :success
7079 end
7180
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+
7287 # EDIT Tests
7388 test 'public should not get edit' do
7489 get :edit , params : { id : @space }
@@ -87,7 +102,19 @@ class SpacesControllerTest < ActionController::TestCase
87102 assert_response :forbidden
88103 end
89104
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
91118 sign_in users ( :admin )
92119 get :edit , params : { id : @space }
93120 assert_response :success
@@ -110,7 +137,16 @@ class SpacesControllerTest < ActionController::TestCase
110137 assert_response :forbidden
111138 end
112139
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
114150 sign_in users ( :admin )
115151 assert_difference 'Space.count' , 1 do
116152 post :create , params : @space_params
@@ -138,7 +174,15 @@ class SpacesControllerTest < ActionController::TestCase
138174 assert_equal 'plants.mytess.training' , assigns ( :space ) . host , 'Non-admin user should not be able to modify host'
139175 end
140176
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
142186 sign_in users ( :admin )
143187 assert_no_difference 'Space.count' do
144188 patch :update , params : { id : @space , space : { title : 'New Title' , host : 'newhost.mytess.golf' } }
@@ -164,12 +208,34 @@ class SpacesControllerTest < ActionController::TestCase
164208 end
165209 end
166210
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
168220 sign_in users ( :admin )
169221 assert_difference 'Space.count' , -1 do
170222 post :destroy , params : { id : @space }
171223 assert_redirected_to spaces_path
172224 assert_equal 'Space was successfully deleted.' , flash [ :notice ]
173225 end
174226 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
175241end
0 commit comments