spec/uptimerobot_spec.rb in uptimerobot-0.1.6 vs spec/uptimerobot_spec.rb in uptimerobot-0.2.0

- old
+ new

@@ -9,150 +9,152 @@ "pausedMonitors"=>"11"}} end it do client = uptime_robot do |stub| - stub.get('getAccountDetails') do |env| - expect(env.params).to eq DEFAULT_PARAMS - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getAccountDetails') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end + expect(client.getAccountDetails).to eq response end end describe '#getMonitors' do let(:params) do { :logs => 1, - :alertContacts => 1, - :responseTimes => 1, - :responseTimesAverage => 180, + :alert_contacts => 1, + :response_times => 1, + :response_times_average => 180, :monitors => '15830-32696' } end let(:response) do {"stat"=>"ok", - "offset"=>"0", - "limit"=>"50", - "total"=>"2", + "pagination"=>{ + "offset"=>"0", + "limit"=>"50", + "total"=>"2" + }, "monitors"=> - {"monitor"=> - [{"id"=>"128795", - "friendlyname"=>"Yahoo", - "url"=>"http://www.yahoo.com/", - "type"=>"1", - "subtype"=>"", - "keywordtype"=>"0", - "keywordvalue"=>"", - "httpusername"=>"", - "httppassword"=>"", - "port"=>"", - "interval"=>"300", - "status"=>"2", - "alltimeuptimeratio"=>"99.98", - "customuptimeratio"=>"100.00", - "alertcontact"=> - [{"id"=>"4631", "type"=>"2", "value"=>"uptime@webresourcesdepot.com"}, - {"id"=>"2420", "type"=>"3", "value"=>"umutm"}], - "log"=> - [{"type"=>"2", - "datetime"=>"09/25/2011 16:12:44", - "alertcontact"=> - [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, - {"type"=>"3", "value"=>"umutm"}]}, - {"type"=>"1", - "datetime"=>"09/25/2011 16:11:44", - "alertcontact"=> - [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, - {"type"=>"3", "value"=>"umutm"}]}], - "responsetime"=> - [{"datetime"=>"02/04/2014 11:30:41", "value"=>"405"}, - {"datetime"=>"02/04/2014 12:00:41", "value"=>"516"}, - {"datetime"=>"02/04/2014 12:30:41", "value"=>"780"}]}, - {"id"=>"128796", - "friendlyname"=>"WebResourcesDepot", - "url"=>"http://www.webresourcesdepot.com/", - "type"=>"1", - "subtype"=>"", - "keywordtype"=>"0", - "keywordvalue"=>"", - "httpusername"=>"", - "httppassword"=>"", - "port"=>"", - "interval"=>"300", - "status"=>"2", - "alltimeuptimeratio"=>"99.94", - "customtimeuptimeratio"=>"89.51", - "alertcontact"=>[{"id"=>"2420", "type"=>"3", "value"=>"umutm"}], - "log"=> - [{"type"=>"2", - "datetime"=>"08/30/2011 16:11:15", - "alertcontact"=> - [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, - {"type"=>"3", "value"=>"umutm"}]}, - {"type"=>"1", - "datetime"=>"08/30/2011 16:09:30", - "alertcontact"=> - [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, - {"type"=>"3", "value"=>"umutm"}]}], - "responsetime"=> - [{"datetime"=>"02/04/2014 11:48:41", "value"=>"405"}, - {"datetime"=>"02/04/2014 12:18:41", "value"=>"516"}, - {"datetime"=>"02/04/2014 12:48:41", "value"=>"780"}]}]}} + [{"id"=>"128795", + "friendlyname"=>"Yahoo", + "url"=>"http://www.yahoo.com/", + "type"=>"1", + "subtype"=>"", + "keywordtype"=>"0", + "keywordvalue"=>"", + "httpusername"=>"", + "httppassword"=>"", + "port"=>"", + "interval"=>"300", + "status"=>"2", + "alltimeuptimeratio"=>"99.98", + "customuptimeratio"=>"100.00", + "alertcontact"=> + [{"id"=>"4631", "type"=>"2", "value"=>"uptime@webresourcesdepot.com"}, + {"id"=>"2420", "type"=>"3", "value"=>"umutm"}], + "log"=> + [{"type"=>"2", + "datetime"=>"09/25/2011 16:12:44", + "alertcontact"=> + [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, + {"type"=>"3", "value"=>"umutm"}]}, + {"type"=>"1", + "datetime"=>"09/25/2011 16:11:44", + "alertcontact"=> + [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, + {"type"=>"3", "value"=>"umutm"}]}], + "responsetime"=> + [{"datetime"=>"02/04/2014 11:30:41", "value"=>"405"}, + {"datetime"=>"02/04/2014 12:00:41", "value"=>"516"}, + {"datetime"=>"02/04/2014 12:30:41", "value"=>"780"}]}, + {"id"=>"128796", + "friendlyname"=>"WebResourcesDepot", + "url"=>"http://www.webresourcesdepot.com/", + "type"=>"1", + "subtype"=>"", + "keywordtype"=>"0", + "keywordvalue"=>"", + "httpusername"=>"", + "httppassword"=>"", + "port"=>"", + "interval"=>"300", + "status"=>"2", + "alltimeuptimeratio"=>"99.94", + "customtimeuptimeratio"=>"89.51", + "alertcontact"=>[{"id"=>"2420", "type"=>"3", "value"=>"umutm"}], + "log"=> + [{"type"=>"2", + "datetime"=>"08/30/2011 16:11:15", + "alertcontact"=> + [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, + {"type"=>"3", "value"=>"umutm"}]}, + {"type"=>"1", + "datetime"=>"08/30/2011 16:09:30", + "alertcontact"=> + [{"type"=>"0", "value"=>"uptime@webresourcesdepot.com"}, + {"type"=>"3", "value"=>"umutm"}]}], + "responsetime"=> + [{"datetime"=>"02/04/2014 11:48:41", "value"=>"405"}, + {"datetime"=>"02/04/2014 12:18:41", "value"=>"516"}, + {"datetime"=>"02/04/2014 12:48:41", "value"=>"780"}]}]} end it do client = uptime_robot do |stub| - stub.get('getMonitors') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getMonitors') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.getMonitors(params)).to eq response end context 'when include escaped string' do let(:response_with_escaped_string) do res = response.dup - monitor = res['monitors']['monitor'][0] - monitor['friendlyname'] = 'http monitor (basic auth)' - monitor['keywordvalue'] = '(keywordvalue)' - monitor['httpusername'] = '(httpusername)' - monitor['httppassword'] = '(httppassword)' + monitor = res['monitors'][0] + monitor['friendly_name'] = 'http monitor (basic auth)' + monitor['keyword_value'] = '(keyword_value)' + monitor['http_username'] = '(http_username)' + monitor['http_password'] = '(http_password)' res end let(:response_with_unescaped_string) do res = response.dup - monitor = res['monitors']['monitor'][0] - monitor['friendlyname'] = 'http monitor (basic auth)' - monitor['keywordvalue'] = '(keywordvalue)' - monitor['httpusername'] = '(httpusername)' - monitor['httppassword'] = '(httppassword)' + monitor = res['monitors'][0] + monitor['friendly_name'] = 'http monitor (basic auth)' + monitor['keyword_value'] = '(keyword_value)' + monitor['http_username'] = '(http_username)' + monitor['http_password'] = '(http_password)' res end it do client = uptime_robot do |stub| - stub.get('getMonitors') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response_with_escaped_string)] + stub.post('/v2/getMonitors') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response_with_escaped_string)] end end expect(client.getMonitors(params)).to eq response_with_unescaped_string end it do client = uptime_robot(:skip_unescape_monitor => true) do |stub| - stub.get('getMonitors') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response_with_escaped_string)] + stub.post('/v2/getMonitors') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response_with_escaped_string)] end end expect(client.getMonitors(params)).to eq response_with_escaped_string end @@ -160,50 +162,50 @@ end describe '#newMonitor' do let(:params) do { - :monitorFriendlyName => 'Google', - :monitorURL => 'http://www.google.com', - :monitorType => UptimeRobot::Monitor::Type::HTTP, - :monitorAlertContacts => '448-716' + :friendly_name => 'Google', + :url => 'http://www.google.com', + :type => UptimeRobot::Monitor::Type::HTTP, + :alert_contacts => '448-716' } end let(:response) do {"stat"=>"ok", "monitor"=>{"id"=>"128798"}} end it do client = uptime_robot do |stub| - stub.get('newMonitor') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/newMonitor') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.newMonitor(params)).to eq response end end describe '#editMonitor' do let(:params) do { - :monitorID => 128798, - :monitorFriendlyName => 'GoogleHomepage' + :id => 128798, + :friendly_name => 'GoogleHomepage' } end let(:response) do {"stat"=>"ok", "monitor"=>{"id"=>"128798"}} end it do client = uptime_robot do |stub| - stub.get('editMonitor') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/editMonitor') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.editMonitor(params)).to eq response end @@ -220,13 +222,13 @@ {"stat"=>"ok", "monitor"=>{"id"=>"128798"}} end it do client = uptime_robot do |stub| - stub.get('deleteMonitor') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/deleteMonitor') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.deleteMonitor(params)).to eq response end @@ -252,13 +254,13 @@ "status"=>"2"}]}} end it do client = uptime_robot do |stub| - stub.get('getAlertContacts') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getAlertContacts') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.getAlertContacts(params)).to eq response end @@ -276,13 +278,13 @@ {"stat"=>"ok", "alertcontact"=>{"id"=>"4561", "status"=>"0"}} end it do client = uptime_robot do |stub| - stub.get('newAlertContact') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/newAlertContact') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.newAlertContact(params)).to eq response end @@ -299,30 +301,30 @@ {"stat"=>"ok", "alertcontact"=>{"id"=>"236"}} end it do client = uptime_robot do |stub| - stub.get('deleteAlertContact') do |env| - expect(env.params).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/deleteAlertContact') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS.merge(stringify_hash(params)) + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.deleteAlertContact(params)).to eq response end end context 'when stat is fail' do let(:response) do - {"stat"=>"fail", "id"=>"101", "message"=>"apiKey is wrong"} + {"stat"=>"fail", "id"=>"101", "message"=>"api_key is wrong"} end it do client = uptime_robot do |stub| - stub.get('getAccountDetails') do |env| - expect(env.params).to eq DEFAULT_PARAMS - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getAccountDetails') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect { client.getAccountDetails @@ -331,12 +333,12 @@ end context 'when status is not 200' do it do client = uptime_robot do |stub| - stub.get('getAccountDetails') do |env| - expect(env.params).to eq DEFAULT_PARAMS + stub.post('/v2/getAccountDetails') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS [500, {}, 'An error occurred on the server when processing the URL'] end end expect { @@ -363,44 +365,44 @@ client.getAccountDetails(0) }.to raise_error(ArgumentError, 'invalid argument: [0]') end end - context 'when apiKey is not passed' do + context 'when api_key is not passed' do it do expect { - uptime_robot(:apiKey => nil) - }.to raise_error(ArgumentError, ':apiKey is required') + uptime_robot(:api_key => nil) + }.to raise_error(ArgumentError, ':api_key is required') end end context 'when account has no monitors' do let(:response) { {"stat"=>"fail", "id"=>"212", "message"=>"The account has no monitors"} } it do client = uptime_robot do |stub| - stub.get('getAccountDetails') do |env| - expect(env.params).to eq DEFAULT_PARAMS - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getAccountDetails') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect(client.getAccountDetails).to eq( {"stat"=>"fail", "id"=>"212", "message"=>"The account has no monitors", "total"=>"0", - "monitors"=>{"monitor"=>[]}} + "monitors"=>[]} ) end it do client = uptime_robot(:raise_no_monitors_error => true) do |stub| - stub.get('getAccountDetails') do |env| - expect(env.params).to eq DEFAULT_PARAMS - [200, {'Content-Type' => 'json'}, JSON.dump(response)] + stub.post('/v2/getAccountDetails') do |env| + expect(decoded_request_body(env.body)).to eq DEFAULT_PARAMS + [200, {'Content-Type' => 'application/json'}, JSON.dump(response)] end end expect { client.getAccountDetails