forked from theforeman/foreman_rh_cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_request_controller_test.rb
104 lines (85 loc) · 3.4 KB
/
cloud_request_controller_test.rb
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
require 'json'
require 'test_plugin_helper'
module InsightsCloud::Api
class CloudRequestControllerTest < ActionController::TestCase
tests Api::V2::RhCloud::CloudRequestController
setup do
@test_org = FactoryBot.create(:organization)
end
test 'Fails for unknown directives' do
request_params = run_playbook_request
request_params['directive'] = 'not-valid'
post :update, params: request_params
assert_response :bad_request
end
test 'Starts playbook run for correct directive' do
host1 = FactoryBot.create(:host, :with_insights_hits)
host1.insights.uuid = 'TEST_UUID1'
host1.insights.save!
host2 = FactoryBot.create(:host, :with_insights_hits)
host2.insights.uuid = 'TEST_UUID2'
host2.insights.save!
mock_composer = mock('composer')
::JobInvocationComposer.expects(:for_feature).with do |feature, host_ids, params|
feature == :rh_cloud_connector_run_playbook &&
host_ids.first == host1.id &&
host_ids.last == host2.id
end.returns(mock_composer)
mock_composer.expects(:trigger!)
mock_composer.expects(:job_invocation)
post :update, params: run_playbook_request
assert_response :success
end
test 'Starts playbook run for correct directive with capitalized keys' do
host1 = FactoryBot.create(:host, :with_insights_hits)
host1.insights.uuid = 'TEST_UUID1'
host1.insights.save!
host2 = FactoryBot.create(:host, :with_insights_hits)
host2.insights.uuid = 'TEST_UUID2'
host2.insights.save!
mock_composer = mock('composer')
::JobInvocationComposer.expects(:for_feature).with do |feature, host_ids, params|
feature == :rh_cloud_connector_run_playbook &&
host_ids.first == host1.id &&
host_ids.last == host2.id
end.returns(mock_composer)
mock_composer.expects(:trigger!)
mock_composer.expects(:job_invocation)
params = run_playbook_request
params['Directive'] = params.delete('directive')
params['Metadata'] = params.delete('metadata')
params['Content'] = params.delete('content')
post :update, params: run_playbook_request
assert_response :success
end
private
def run_playbook_request
request_json = <<-REQUEST
{
"type": "data",
"message_id": "a6a7d866-7de0-409a-84e0-3c56c4171bb7",
"version": 1,
"sent": "2021-01-12T15:30:08+00:00",
"directive": "playbook-sat",
"metadata": {
"operation": "run",
"return_url": "https://cloud.redhat.com/api/v1/ingres/upload",
"correlation_id": "6684b9dd-0d16-42c1-b13a-9f45be59e3b6",
"playbook_run_name": "Human-readable playbook run name",
"playbook_run_url": "https://console.redhat.com/insights/remediations/1234",
"sat_id": "aa3b1faa-56f3-4d14-8258-615d11e20060",
"sat_org_id": "#{FactoryBot.create(:organization).id}",
"initiator_user_id": "4efca34c6d9ae05ef7c3d7a7424e6370d198159a841ae005084888a9a4529e27",
"hosts": "TEST_UUID1,TEST_UUID2",
"response_interval": "30",
"response_full": "false"
},
"content": ""
}
REQUEST
request = JSON.parse(request_json)
request['content'] = "\"#{Base64.encode64('https://cloud.redhat.com/api/v1/remediations/1234/playbook')}\""
request
end
end
end