require 'spec_helper' describe Medivo::Appointment do let(:lab_code) { '20060' } describe "finding appointments" do describe "with valid data" do before do @all_times = ["11/03/2011|08:30 AM", "11/04/2011|08:30 PM", "11/04/2011|08:30 AM"] end let(:body) { {'times'=> @all_times} } def returned_times(date, am_pm) Timecop.freeze(Time.strptime(date, "%m/%d/%Y")) stub_appointment_request(lab_code, date, am_pm, body) value = Medivo::Appointment.find(lab_code, date, am_pm) Timecop.return return value['times'] end it "returns times" do returned_times('11/01/2011', '').should == @all_times.sort end it "filters out past date times" do returned_times('11/04/2011', '').should == @all_times.slice(1, 2).sort end it "filters out past or pm times" do returned_times('11/04/2011', 'AM').should == ["11/04/2011|08:30 AM"] end it "filters out past or am times" do returned_times('11/04/2011', 'PM').should == ["11/04/2011|08:30 PM"] end it "filters out invalid time strings" do @all_times = ["11/02011|08:30 AM", "11/04/2011|11:30 AM"] returned_times('11/04/2011', '').should == ["11/04/2011|11:30 AM"] end it "orders the time strings" do returned_times('11/04/2011', '').should == ["11/04/2011|08:30 AM", "11/04/2011|08:30 PM"] end end describe "with invalid request returns error message" do let(:body) { {'message'=> "invalid data"} } it "for status 400" do date = '11/01/2011' stub_appointment_request(lab_code, date, '', body, 400) expect { Medivo::Appointment.find(lab_code, date) }.to raise_error end end end describe "making appointments" do let(:time) { Time.zone.parse("2011-11-17 09:30 AM") } let(:user) { {first_name: 'dude', last_name: 'man', date_of_birth: '12/28/1980', email_address: 'dudeman@dude.com', phone_number: '4155551212'} } let(:confirmation_code) { '123' } it "can make the appointment and returns nil" do body = {confirmation: confirmation_code} stub_appointment_confirmation(lab_code, time, user, body, 200) Medivo::Appointment.make(lab_code, time, user).should == confirmation_code end it " the appointment and returns nil" do body = {confirmation: nil} stub_appointment_confirmation(lab_code, time, user, body, 200) Medivo::Appointment.make(lab_code, time, user).should == nil end end end