require 'spec_helper' describe "Product", "converting to xml" do before do @product_a = Caren::Product.new( :name => "Washing", :price => 100.euros, :in_store => true ) @product_b = Caren::Product.new( :name => "Bedpan", :price => 100.euros, :in_store => false ) end it "should be able to convert a product to valid xml" do @product_a.should convert_to_valid_caren_xml end it "should be able to convert an array of products to valid xml" do [@product_a,@product_b].should convert_to_valid_caren_array_xml end end describe "Product", "REST methods" do before do product = File.read("spec/fixtures/caren_product.xml") products = File.read("spec/fixtures/caren_products.xml") product_search = File.read("spec/fixtures/caren_products_search.xml") product_url = Caren::Api.session.url_for( Caren::Product.resource_url(1) ) products_url = Caren::Api.session.url_for( Caren::Product.resource_url ) search_url = Caren::Api.session.url_for( "#{Caren::Product.resource_url}?key=name&value=bedpan" ) timestamp = DateTime.now.to_i FakeWeb.register_uri(:post, products_url, :timestamp => timestamp, :signature => Caren::Api.session.sign(timestamp) ) FakeWeb.register_uri(:put, product_url, :timestamp => timestamp, :signature => Caren::Api.session.sign(timestamp) ) FakeWeb.register_uri(:get, products_url, :body => products, :timestamp => timestamp, :signature => Caren::Api.session.sign(timestamp,products) ) FakeWeb.register_uri(:get, product_url, :body => product, :timestamp => timestamp, :signature => Caren::Api.session.sign(timestamp,product) ) FakeWeb.register_uri(:get, search_url, :body => product_search, :timestamp => timestamp, :signature => Caren::Api.session.sign(timestamp,product_search) ) end it "should be able to update a product" do lambda{ Caren::Product.new( :id => 1, :name => "Bedpan" ).update( Caren::Api.session ) }.should_not raise_error end it "should be able to update the photo for a product" do lambda{ Caren::Product.new( :id => 1 ).update_photo( "spec/fixtures/bacon.jpg", Caren::Api.session ) }.should_not raise_error end it "should be able to find a specific product" do product = Caren::Product.find 1, Caren::Api.session product.name.should == "Dishwashing" end it "should be able to search for a specific product" do products = Caren::Product.search :name, "bedpan", Caren::Api.session products.should have(1).things products.first.name.should == "bedpan" end it "should be able to find all products" do products = Caren::Product.all Caren::Api.session products.should have(2).things products.first.name.should == "Dishwashing" end end