diff --git a/lib/cronitor/monitor.rb b/lib/cronitor/monitor.rb index c029d91..4c530e0 100644 --- a/lib/cronitor/monitor.rb +++ b/lib/cronitor/monitor.rb @@ -201,7 +201,16 @@ def fetch return end - HTTParty.get(monitor_api_url, timeout: Cronitor.timeout, headers: Cronitor::Monitor::Headers::JSON, format: :json) + HTTParty.get( + monitor_api_url, + basic_auth: { + username: api_key, + password: '' + }, + timeout: Cronitor.timeout, + headers: Cronitor::Monitor::Headers::JSON, + format: :json + ) end def clean_params(params) diff --git a/spec/cronitor_spec.rb b/spec/cronitor_spec.rb index 351e1f7..09fa0e2 100644 --- a/spec/cronitor_spec.rb +++ b/spec/cronitor_spec.rb @@ -242,6 +242,25 @@ end end end + + context '#data' do + it 'should fetch the monitor with cronitor api key' do + expect(HTTParty).to receive(:get).with("https://cronitor.io/api/monitors", hash_including(basic_auth: {username: Cronitor.api_key, password: ''})).and_return( + instance_double(HTTParty::Response, code: 200, body: MONITOR.to_json) + ) + Cronitor::Monitor.new('test-job').data + end + + context 'when monitor api key is provided' do + it 'should fetch the monitor with monitor api key' do + api_key = 'monitor_api_key' + expect(HTTParty).to receive(:get).with("https://cronitor.io/api/monitors", hash_including(basic_auth: {username: api_key, password: ''})).and_return( + instance_double(HTTParty::Response, code: 200, body: MONITOR.to_json) + ) + Cronitor::Monitor.new('test-job', api_key: api_key).data + end + end + end end describe 'functional tests - ', type: 'functional' do