require "slidepay" require "spec_helper" describe SlidePay::Payment do describe "class instance variables" do it "should include an id_attribute" do expect(SlidePay::Payment.id_attribute).to eq("payment_id") end it "should include a root url" do expect(SlidePay::Payment.url_root).to eq("payment") end end it "should have an id_attribute" do p = SlidePay::Payment.new() expect(p.id_attribute).to eq("payment_id") end it "should have a root url" do p = SlidePay::Payment.new() expect(p.url_root).to eq("payment") end describe "url" do it "should not append the object id if no id is set" do p = SlidePay::Payment.new() expect(p.url()).to eq("payment") end it "should append the object id if set" do p = SlidePay::Payment.new("payment_id" => 2) expect(p.url()).to eq("payment/2") end end describe "id" do it "should return nil if the id is not set" do p = SlidePay::Payment.new() expect(p.id()).to eq(nil) end it "should return the id if it is present" do p = SlidePay::Payment.new("payment_id" => 2) expect(p.id()).to eq(2) end end describe "save" do it "should call the process method" do p = SlidePay::Payment.new() p.should_receive(:process) p.save() end end describe "destroy" do it "should call the refund method" do p = SlidePay::Payment.new() p.should_receive(:refund) p.destroy() end end describe "is_settled" do it "should return false for a payment whose provider_capture_state is not 'Captured'" do SlidePay.should_receive(:get).and_return(a_response_object(uncaptured_payment_response)) p = SlidePay::Payment.new({ "payment_id" => 1 }) p.retrieve() expect(p.is_settled?).to eq(false) end it "should return false for a payment with no settlement_transaction_token GUID" do SlidePay.should_receive(:get).and_return(a_response_object(unsettled_payment_response)) p = SlidePay::Payment.new({ "payment_id" => 1 }) p.retrieve() expect(p.is_settled?).to eq(false) end it "should return true if both of the above are false" do SlidePay.should_receive(:get).and_return(a_response_object(settled_payment_response)) p = SlidePay::Payment.new({ "payment_id" => 1 }) p.retrieve() expect(p.is_settled?).to eq(true) end it "should raise an error if the payment has not yet been retrieved, or is_new?" do # SlidePay.should_receive(:get).and_return(a_response_object(_payment_response)) p = SlidePay::Payment.new() expect { p.is_settled? }.to raise_error end end describe "process" do it "should set the payment_id and the order_master_id on success" do SlidePay.stub(:post) { a_response_object(successful_payment_response) } p = SlidePay::Payment.new() expect(p["order_master_id"]).to eq(nil) expect(p["payment_id"]).to eq(nil) p.process() expect(p["order_master_id"]).to eq("10") # Test data found in spec_helper expect(p["payment_id"]).to eq("12") end it "should raise an error on failure" do SlidePay.stub(:post) { a_response_object(failed_payment_response) } p = SlidePay::Payment.new() expect{ p.process() }.to raise_error("TEST_PAYMENT_FAILED_MESSAGE") end end describe "refund" do it "should return false for failure" do SlidePay.stub(:post) { a_response_object(failed_object_response) } p = SlidePay::Payment.new() expect(p.refund()).to eq(false) end it "should return true for successful refund" do SlidePay.stub(:post) { a_response_object(successful_object_response) } p = SlidePay::Payment.new() expect(p.refund()).to eq(true) end end describe "class methods" do describe "search" do it "should be represented by several instance methods" do public_methods = SlidePay::Payment.public_methods expect(public_methods.include?(:find_where)).to be_true expect(public_methods.include?(:find_greater_than)).to be_true expect(public_methods.include?(:find_less_than)).to be_true expect(public_methods.include?(:find_between)).to be_true end describe "find_where" do it "should make a put request to /payment" do SlidePay.should_receive(:put) .with(path: "payment", data: payment_where_sfa.to_json, token: nil, api_key: nil, endpoint: nil) .and_return(a_response_object(payment_search_response)) SlidePay::Payment.find_where(created: "12/13/2013") end end describe "find_less_than" do it "should make a put request to /payment" do SlidePay.should_receive(:put) .with(path: "payment", data: payment_less_than_sfa.to_json, token: nil, api_key: nil, endpoint: nil) .and_return(a_response_object(payment_search_response)) SlidePay::Payment.find_less_than(created: "10/08/2012") end end describe "find_greater_than" do it "should make a put request to /payment" do SlidePay.should_receive(:put) .with(path: "payment", data: payment_greater_than_sfa.to_json, token: nil, api_key: nil, endpoint: nil) .and_return(a_response_object(payment_search_response)) SlidePay::Payment.find_greater_than(created: "10/08/2013 23:28:40") end end describe "find_between" do it "should make a put request to /payment" do SlidePay.should_receive(:put) .with(path: "payment", data: payment_between_sfa.to_json, token: nil, api_key: nil, endpoint: nil) .and_return(a_response_object(payment_search_response)) SlidePay::Payment.find_between({:created => "10/08/2013 23:28"}, {:created => "10/08/2013 23:28:40"}) end end end end end