|
| 1 | +require 'test_helper' |
| 2 | +require 'registration/registration_commands_api' |
| 3 | + |
| 4 | +class RegistrationCommandsApiTest < Test::Unit::TestCase |
| 5 | + include Rack::Test::Methods |
| 6 | + |
| 7 | + def app |
| 8 | + Proxy::Registration::CommandsApi.new |
| 9 | + end |
| 10 | + |
| 11 | + def setup |
| 12 | + @foreman_url = 'http://foreman.example.com' |
| 13 | + Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url) |
| 14 | + end |
| 15 | + |
| 16 | + def test_registration_command |
| 17 | + expected_body = '{"registration_command":"curl ... | bash"}' |
| 18 | + stub_request(:post, "#{@foreman_url}/api/registration_commands") |
| 19 | + .to_return(body: expected_body, headers: { 'Content-Type' => 'application/json' }) |
| 20 | + |
| 21 | + post '/' |
| 22 | + assert last_response.ok? |
| 23 | + assert_equal expected_body, last_response.body |
| 24 | + end |
| 25 | + |
| 26 | + def test_registration_command_with_json_body |
| 27 | + request_body = '{"registration_command":{"organization_id":1,"hostgroup_id":2}}' |
| 28 | + expected_body = '{"registration_command":"curl ... | bash"}' |
| 29 | + stub_request(:post, "#{@foreman_url}/api/registration_commands") |
| 30 | + .with(body: request_body) |
| 31 | + .to_return(body: expected_body, headers: { 'Content-Type' => 'application/json' }) |
| 32 | + |
| 33 | + post '/', request_body, { 'CONTENT_TYPE' => 'application/json' } |
| 34 | + assert last_response.ok? |
| 35 | + assert_equal expected_body, last_response.body |
| 36 | + end |
| 37 | + |
| 38 | + def test_registration_command_401 |
| 39 | + stub_request(:post, "#{@foreman_url}/api/registration_commands") |
| 40 | + .to_return(body: 'Unauthorized', status: 401) |
| 41 | + |
| 42 | + post '/' |
| 43 | + assert last_response.unauthorized? |
| 44 | + end |
| 45 | + |
| 46 | + def test_registration_command_500 |
| 47 | + Rack::NullLogger.any_instance.stubs(:exception) |
| 48 | + stub_request(:post, "#{@foreman_url}/api/registration_commands").to_timeout |
| 49 | + |
| 50 | + post '/' |
| 51 | + assert last_response.server_error? |
| 52 | + end |
| 53 | +end |
0 commit comments