@@ -16,8 +16,95 @@ class HostsBulkActionsControllerTest < ActionController::TestCase
1616 organization : host . organization ,
1717 location : host . location )
1818 end
19+ let ( :environment ) { FactoryBot . create ( :environment , organizations : [ host . organization ] , locations : [ host . location ] ) }
20+ let ( :hostgroup_environment ) { FactoryBot . create ( :environment , organizations : [ host . organization ] , locations : [ host . location ] ) }
21+ let ( :hostgroup ) do
22+ FactoryBot . create ( :hostgroup , :with_puppet_enc ,
23+ environment : hostgroup_environment ,
24+ organizations : [ host . organization ] ,
25+ locations : [ host . location ] )
26+ end
1927 let ( :proxy ) { FactoryBot . create ( :puppet_and_ca_smart_proxy , organizations : [ host . organization ] , locations : [ host . location ] ) }
2028
29+ test 'changes puppet environment for selected hosts' do
30+ put :change_environment ,
31+ params : bulk_params . merge ( environment_id : environment . id )
32+
33+ assert_response :success
34+ assert_equal environment . id , host2 . reload . puppet . environment_id
35+ assert_equal environment . id , host . reload . puppet . environment_id
36+ end
37+
38+ test 'inherits puppet environment from hostgroups for selected hosts' do
39+ host . update! ( hostgroup : hostgroup )
40+ host2 . update! ( hostgroup : hostgroup )
41+
42+ put :change_environment ,
43+ params : bulk_params . merge ( environment_id : 'inherit' )
44+
45+ assert_response :success
46+ assert_equal hostgroup_environment . id , host2 . reload . puppet . environment_id
47+ assert_equal hostgroup_environment . id , host . reload . puppet . environment_id
48+ end
49+
50+ test 'clears puppet environment for selected hosts' do
51+ host . puppet . update! ( environment : environment )
52+ host2 . puppet . update! ( environment : environment )
53+
54+ put :change_environment ,
55+ params : bulk_params . merge ( environment_id : nil ) ,
56+ session : set_session_user
57+
58+ assert_response :success
59+ assert_nil host . reload . puppet . environment
60+ assert_nil host2 . reload . puppet . environment
61+ end
62+
63+ test 'returns error when puppet environment is missing' do
64+ missing_environment_id = 999_999
65+
66+ put :change_environment ,
67+ params : bulk_params . merge ( environment_id : missing_environment_id ) ,
68+ session : set_session_user
69+
70+ assert_response :unprocessable_entity
71+ response = JSON . parse ( @response . body )
72+ assert_equal "A Puppet environment with id #{ missing_environment_id } could not be found." ,
73+ response . dig ( 'error' , 'message' )
74+ end
75+
76+ test 'returns error when changing puppet environment fails for some hosts' do
77+ ::BulkHostsManager . any_instance . expects ( :change_puppet_environment )
78+ . with ( environment )
79+ . returns ( [ host2 . id ] )
80+
81+ put :change_environment ,
82+ params : bulk_params . merge ( environment_id : environment . id ) ,
83+ session : set_session_user
84+
85+ assert_response :unprocessable_entity
86+ response = JSON . parse ( @response . body )
87+ assert_equal 'Failed to change environment for 1 host' ,
88+ response . dig ( 'error' , 'message' )
89+ assert_equal [ host2 . id ] , response . dig ( 'error' , 'failed_host_ids' )
90+ end
91+
92+ test 'returns error when puppet environment is outside host taxonomies' do
93+ invalid_environment = FactoryBot . create ( :environment ,
94+ organizations : [ FactoryBot . create ( :organization ) ] ,
95+ locations : [ FactoryBot . create ( :location ) ] )
96+
97+ put :change_environment ,
98+ params : bulk_params . merge ( environment_id : invalid_environment . id ) ,
99+ session : set_session_user
100+
101+ assert_response :unprocessable_entity
102+ response = JSON . parse ( @response . body )
103+ assert_equal 'Selected Puppet environment is not assigned to the proper organization and/or location for all hosts.' ,
104+ response . dig ( 'error' , 'message' )
105+ assert_equal [ host . id , host2 . id ] . sort , response . dig ( 'error' , 'failed_host_ids' ) . sort
106+ end
107+
21108 test 'changes puppet proxy for selected hosts' do
22109 put :change_puppet_proxy ,
23110 params : bulk_params . merge ( proxy_id : proxy . id , ca_proxy : false )
0 commit comments