# encoding: utf-8 require 'spec_helper' describe Highrise::Person do subject { Highrise::Person.new(:id => 1) } it { should be_a_kind_of Highrise::Subject } it_should_behave_like "a paginated class" it_should_behave_like "a taggable class" it_should_behave_like "a searchable class" describe "#company" do it "returns nil when it doesn't have a company" do subject.should_receive(:company_id).and_return(nil) subject.company.should be_nil end it "delegate to Highrise::Company when have company_id" do subject.should_receive(:company_id).at_least(2).times.and_return(1) Highrise::Company.should_receive(:find).with(1).and_return("company") subject.company.should == "company" end end it "#name" do subject.should_receive(:first_name).and_return("Marcos") subject.should_receive(:last_name).and_return("Tapajós ") subject.name.should == "Marcos Tapajós" end describe "#email_addresses" do it "returns an empty array when there are none set" do subject.email_addresses.should == [] end it "returns all email_addresses as string in an array" do subject = Highrise::Person.new(:id => 1, :contact_data => { :email_addresses => [{ :email_address => { :address => "important@person.com" } }] }) subject.email_addresses.should == ["important@person.com"] end end describe "#phone_numbers" do it "returns an empty array when there is none set" do subject.phone_numbers.should== [] end it "returns all phone numbers as a string aray" do subject = Highrise::Person.new(:id => 1, :contact_data => { :phone_numbers => [{ :phone_number => { :number => "123456789" } }] }) subject.phone_numbers.should== [ "123456789" ] end end describe "#tags" do let(:person_tags) { [ {'id' => "414578", 'name' => "cliente"}, {'id' => "414580", 'name' => "ged"}, {'id' => "414579", 'name' => "iepc"} ] } let(:person_john_doe) { { :id => 1, :first_name => "John", :last_name => "Doe" } } let(:person_john_doe_request){ ActiveResource::Request.new(:get, '/people/1.xml', nil, {"Authorization"=>"Bearer OAUTH_TOKEN", "Accept"=>"application/xml"}) } let(:person_john_doe_request_pair){ {person_john_doe_request => ActiveResource::Response.new(person_john_doe.to_xml, 200, {})} } it "should return the tags as a Highrise::Tag" do person_john_doe[:tags] = person_tags ActiveResource::HttpMock.respond_to(person_john_doe_request_pair) tags = person_tags.collect {|tag| Highrise::Tag.new(tag)} subject.tags.should == tags end it "should return an empty collection when there are no tags" do ActiveResource::HttpMock.respond_to(person_john_doe_request_pair) subject.tags.should == [] end end describe "#tags" do before(:each) do (@tags = []).tap do @tags << {'id' => "414578", 'name' => "cliente"} @tags << {'id' => "414580", 'name' => "ged"} @tags << {'id' => "414579", 'name' => "iepc"} end subject.attributes["tags"] = @tags end it { subject.tags.should == @tags } end describe "Custom fields" do before (:each) do @fruit_person = Highrise::Person.new({ :person => { :id => 1, :first_name => "John", :last_name => "Doe", :subject_datas => [{ :subject_field_label => "Fruit Banana", :value => "Yellow" }, { :subject_field_label => "Fruit Grape", :value => "Green" }] } }) @subject_field_blueberry = Highrise::SubjectField.new ({:id => 1, :label => "Fruit Blueberry"}) @subject_field_papaya = Highrise::SubjectField.new ({:id => 2, :label => "Fruit Papaya"}) end it "Can get the value of a custom field via the field method" do @fruit_person.field("Fruit Banana").should== "Yellow" end it "Can get the value of a custom field using a custom method call" do @fruit_person.fruit_grape.should== "Green" end it "Will raise an exception on an unknown field" do expect {@fruit_person.unknown_fruit}.to raise_exception(NoMethodError) end it "Can set the value of a custom field via the field method" do @fruit_person.set_field_value("Fruit Grape", "Red") @fruit_person.field("Fruit Grape").should== "Red" end it "Can set the value of a custom field" do @fruit_person.fruit_grape= "Red" @fruit_person.fruit_grape.should== "Red" end it "Assignment just returns the arguments (like ActiveResource base does)" do Highrise::SubjectField.should_receive(:find).with(:all).and_return [] (@fruit_person.unknown_fruit = 10).should== 10 end it "Can deal with the find returning nil (which is a bit ugly in the ActiveResource API)" do Highrise::SubjectField.should_receive(:find).with(:all).and_return nil (@fruit_person.unknown_fruit = 10).should== 10 end it "Can set the value of a custom field that wasn't there via the field method, but that was defined (happens on new Person)" do Highrise::SubjectField.should_receive(:find).with(:all).and_return([@subject_field_papaya, @subject_field_blueberry]) @fruit_person.set_field_value("Fruit Blueberry", "Purple") @fruit_person.field("Fruit Blueberry").should== "Purple" @fruit_person.attributes["subject_datas"][2].subject_field_id.should == 1 end it "Can still set and read the usual way of reading attrivutes" do @fruit_person.first_name = "Jacob" @fruit_person.first_name.should== "Jacob" end end it { subject.label.should == 'Party' } end