require 'spec_helper' # Set up the VCR and record some stuff. VCR.configure do |c| c.cassette_library_dir = 'spec/fixtures/vcr_cassettes' c.hook_into :webmock c.allow_http_connections_when_no_cassette = true end describe 'CampMinder API' do let(:camp_id) { 0 } let(:token) { 'xxxxxxxx-yyyy-aaaa-bbbb-112233445566' } let(:household1) { { "Parent1" => { "FirstName" => 'John', "LastName" => "Doe", "LoginEmail" => "john.doe@test.com" }, "Phone" => "555-555-1212" } } let(:household2) { { "Parent1" => { "FirstName" => 'Jane', "LastName" => "Doe", "LoginEmail" => "jane.doe@test.com" } } } let(:camper) { "Needs example data" } let(:lead_date) { "Needs example data" } let(:lead_source) { "Google and Testing" } let(:email) { 'elliott@websitesthatdostuff.com' } let(:username) { 'test@testing.com' } let(:password) { 'alternatePW' } let!(:campminder) { CampMinder::CampMinder.new(camp_id, token) } it '#initialize' do expect(campminder.camp_id).to be(camp_id) expect(campminder.token).to be(token) end it '#return_api_key' do key = campminder.return_api_key expect(key).not_to be_nil end context 'authentication' do it '#login' do VCR.use_cassette('login', :match_requests_on => [:path]) do login = campminder.login(username, password) expect(login["Success"]).to equal(true) end end end context 'when adding a lead' do it '#add_new_lead' do VCR.use_cassette('add_new_lead_simple') do lead = campminder.add_new_lead(household1, {}, {}, "", lead_source) expect(lead["Success"]).to equal(true) end end it '#add_custom_field_data for lead' do VCR.use_cassette('add_custom_field_data') do custom_fields = campminder.add_custom_field_data(5423669, [{"FieldID" => 63021, "Value" => "Both"}]) expect(custom_fields.first["Success"]).to equal(true) end end end context 'when getting Telegraph reports' do it '#get_telegraph_reports' do VCR.use_cassette('get_telegraph_reports', :match_requests_on => [:path]) do reports = campminder.get_telegraph_reports expect(reports["Success"]).to equal(true) expect(reports["Result"].size).to be > 0 end end it '#get_telegraph_reports with invalid token' do VCR.use_cassette('get_telegraph_reports_invalid_token', :match_requests_on => [:path]) do campminder.token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' reports = campminder.get_telegraph_reports expect(reports).to equal(nil) end end end context 'when running reports' do it '#run_report' do VCR.use_cassette('run_report', :match_requests_on => [:path]) do report_data = campminder.run_report(101010) expect(report_data["Success"]).to equal(true) expect(report_data["Result"].length).to be > 0 end end it '#run_report for invalid report ID' do VCR.use_cassette('run_report_invalid_id', :match_requests_on => [:path]) do report_data = campminder.run_report(111000) expect(report_data["Success"]).to equal(false) end end it '#run_report with invalid token' do VCR.use_cassette('run_report_invalid_token', :match_requests_on => [:path]) do campminder.token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' report_data = campminder.run_report(101010) expect(report_data).to equal(nil) end end end context 'when retrieving a user by email' do it '#retrieve_person_id for a valid email' do VCR.use_cassette('retrieve_user_id', :match_requests_on => [:path]) do response = campminder.retrieve_person_id(email) expect(response["Success"]).to be true expect(response["Result"]).to eq 6825481 end end it '#retrieve_person_id for an invalid email' do VCR.use_cassette('retrieve_user_id_invalid_email', :match_requests_on => [:path]) do response = campminder.retrieve_person_id('nobody-at-this-address@gmail.com') expect(response["Success"]).to be true expect(response["Result"]).to be 0 end end it '#retrieve_person_id with an invalid token' do VCR.use_cassette('retrieve_user_id_invalid_token', :match_requests_on => [:path]) do campminder.token = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' response = campminder.retrieve_person_id(email) expect(response).to eq nil end end end end