# encoding: UTF-8 require 'test_helper' require 'adyen/api' require 'nokogiri' require 'rexml/document' Adyen.configuration.default_api_params = { :merchant_account => 'SuperShopper' } Adyen.configuration.api_username = 'SuperShopper' Adyen.configuration.api_password = 'secret' module Net class HTTP class Post attr_reader :header attr_reader :assigned_basic_auth alias old_basic_auth basic_auth def basic_auth(username, password) if Net::HTTP.stubbing_enabled @assigned_basic_auth = [username, password] else old_basic_auth(username, password) end end def soap_action header['soapaction'].first end end class << self attr_accessor :posted, :stubbed_response attr_reader :stubbing_enabled def stubbing_enabled=(enabled) reset! if @stubbing_enabled = enabled end def reset! @posted = nil @stubbed_response = nil end end def host @address end alias old_start start def start(&block) Net::HTTP.stubbing_enabled ? yield(self) : old_start(&block) end alias old_request request def request(request, body = nil, &block) if Net::HTTP.stubbing_enabled self.class.posted = [self, request] self.class.stubbed_response else old_request(request, body, &block) end end end end module Adyen module API class PaymentService public :authorise_payment_request_body, :authorise_recurring_payment_request_body, :authorise_one_click_payment_request_body, :capture_request_body, :refund_request_body, :cancel_or_refund_request_body, :cancel_request_body end class RecurringService public :list_request_body, :disable_request_body end end end module APISpecHelper def xpath(query, &block) node_for_current_method.xpath(query, &block) end def text(query) node_for_current_method.text(query) end def stub_net_http(response_body) Net::HTTP.stubbing_enabled = true response = Net::HTTPOK.new('1.1', '200', 'OK') response.stubs(:body).returns response_body Net::HTTP.stubbed_response = response end def self.included(klass) klass.extend ClassMethods end module ClassMethods def for_each_xml_backend(&block) backends = [Adyen::API::XMLQuerier::NokogiriBackend, Adyen::API::XMLQuerier::REXMLBackend] backends.each do |xml_backend| describe "with a #{xml_backend} backend" do before do Adyen::API::XMLQuerier.stubs(:default_backend).returns xml_backend.new end instance_eval(&block) end end end def it_should_return_params_for_each_xml_backend(params) for_each_xml_backend do it "returns a hash with parsed response details" do @object.send(@method).params.must_equal params end end end def it_should_validate_request_parameters(*params) params.each do |param| case param when Symbol it_should_validate_request_param(param) { @object.params[param] = '' } when Hash param.each do |name, attrs| it_should_validate_request_param(name) { @object.params[name] = nil } attrs.each do |attr| it_should_validate_request_param("#{name} => :#{attr}") { @object.params[name][attr] = nil } end end end end end def it_should_validate_request_param(name, &block) it "validates the `#{name}' request parameter" do instance_eval(&block) lambda { current_object_method_result }.must_raise(ArgumentError) end end def describe_response_from(method, response, soap_action = 'authorise', &block) describe(method) do before do stub_net_http(response) @method = method @object.params.merge!(:psp_reference => '9876543210987654') @response = @object.send(@method) @request, @post = Net::HTTP.posted end after do Net::HTTP.stubbing_enabled = false end it "posts the body generated for the given parameters" do @post.body.must_equal Adyen::API::SimpleSOAPClient::ENVELOPE % @object.send("#{@method}_request_body") end it "posts to the correct SOAP action" do @post.soap_action.must_equal soap_action end it "provides shortcut methods, on the response object, for all entries in the #params hash" do params = @response.params result = params.keys.map { |key| @response.send(key) } result.must_equal params.values end instance_eval(&block) end end def describe_request_body_of(method, xpath = nil, &block) method = "#{method}_request_body" describe(method) do prepare_request_body_specs(method, xpath) before { @method = method } instance_eval(&block) end end def describe_modification_request_body_of(method, camelized_method = nil, &block) xpath = "//payment:#{camelized_method || method}/payment:modificationRequest" method = "#{method}_request_body" describe method do prepare_request_body_specs(method, xpath) before do @method = method @payment.params[:psp_reference] = 'original-psp-reference' end it "includes the merchant account" do text('./payment:merchantAccount').must_equal 'SuperShopper' end it "includes the payment (PSP) reference of the payment to refund" do text('./payment:originalReference').must_equal 'original-psp-reference' end instance_eval(&block) if block_given? end end def prepare_request_body_specs(method, xpath) let(:current_object_method_result) { @object.send(method) } let(:node_for_current_object_and_method) do Adyen::API::XMLQuerier.xml(current_object_method_result) end if xpath let(:node_for_current_method) do node_for_current_object_and_method.xpath(xpath) end end end end end AUTHORISE_RESPONSE = < cardSummary 1111 1234 9876543210987654 Authorised EOS AUTHORISATION_DECLINED_RESPONSE = < 1234 9876543210987654 You need to actually own money. Refused EOS AUTHORISE_REQUEST_INVALID_RESPONSE = < soap:Server %s EOS AUTHORISE_REQUEST_REFUSED_RESPONSE = < You need to actually own money. Refused EOS LIST_RESPONSE = < 2009-10-27T11:26:22.203+01:00
12 2012 S. Hopper 1111 2009-10-27T11:50:12.178+01:00 RecurringDetailReference1 newAlias 123456 mc 123456789 bank-location-id AnyBank BBBBCCLLbbb NL NL69PSTB0001234567 S. Hopper 2009-10-27T11:26:22.216+01:00 RecurringDetailReference2 newAlias 123456 IDEAL S. Hopper 1234567890 Berlin 12345678 TestBank 2009-10-27T11:26:22.216+01:00 RecurringDetailReference3 newAlias 123456 elv
s.hopper@example.com user-id
EOS LIST_EMPTY_RESPONSE = <
EOS DISABLE_RESPONSE = < %s EOS REFUND_RESPONSE = < 8512865475512126 %s EOS CANCEL_OR_REFUND_RESPONSE = < 8512865521218306 %s EOS CANCEL_RESPONSE = < 8612865544848013 %s EOS CAPTURE_RESPONSE = < 8512867956198946 %s EOS BILLET_RECEIVED_RESPONSE = < boletobancario.barCodeReference03399.33335 33887.371731 37772.301026 8 69200000009314boletobancario.dataBQABAQA8M3ewKqDwrdIK+q9mDvlF+X0IszPCAG9Tg2jmOJn0/m6T+PABVpyI8/hFeY3xTN3B7yEffj6ehx0ADQh5sLT9ndkqu1UuHTZjlxBiJ9UcMOAl8rkPSLEJU1Dw1tLWGQ7PKRqB8bv3k/VbPGliXZVxEvzxAOZdZ1dpHeVZfA1XT8a9+ZQtJFPAHOhgYpPBmYkiVnavJfwbNMKddQY0CZxC1V2Hndx9yDl/S0IJ1hLgiwxr8eE6QkeDBYZ5uKKyiIVpPxKwDe1o3sq2v76s7cQvdfGn/mAr6jWiNl+rQU92/YlFuR4rHhZdOyUkNofTeTRcpuSaTsK1L9MS1UNqTAWbEMmlqUgNZ6B7HtiYP61sbMIAAECcYbKVpl+RN3hpP3HqHc0/xkOShfnE4bYOtDGVDMf4dt9kAM6+XDOyk8iu//dbHvrZnenLFsKhcdDEjiXRrLHMVxQI9BN/5Yy4hnGN7k/XI1mANMhNpKWLdPBCt94GzaiI830xKsh5KlgFrenU+4x2p0pbeAQUokTLKHEUUzmNmlplxhADBHmTAEwC0vbbGjWFh3PS0zGRwas2TpLNRtQznTmdfoI7j0dlSVHCQQ==boletobancario.dueDate2016-09-17boletobancario.urlhttps://test.adyen.com/hpp/generationBoleto.shtml?data=BQABAQA8M3ewKqDwrdIK%2Bq9mDvlF%2BX0IszPCAG9Tg2jmOJn0%2Fm6T%2BPABVpyI8%2FhFeY3xTN3B7yEffj6ehx0ADQh5sLT9ndkqu1UuHTZjlxBiJ9UcMOAl8rkPSLEJU1Dw1tLWGQ7PKRqB8bv3k%2FVbPGliXZVxEvzxAOZdZ1dpHeVZfA1XT8a9%2BZQtJFPAHOhgYpPBmYkiVnavJfwbNMKddQY0CZxC1V2Hndx9yDl%2FS0IJ1hLgiwxr8eE6QkeDBYZ5uKKyiIVpPxKwDe1o3sq2v76s7cQvdfGn%2FmAr6jWiNl%2BrQU92%2FYlFuR4rHhZdOyUkNofTeTRcpuSaTsK1L9MS1UNqTAWbEMmlqUgNZ6B7HtiYP61sbMIAAECcYbKVpl%2BRN3hpP3HqHc0%2FxkOShfnE4bYOtDGVDMf4dt9kAM6%2BXDOyk8iu%2F%2FdbHvrZnenLFsKhcdDEjiXRrLHMVxQI9BN%2F5Yy4hnGN7k%2FXI1mANMhNpKWLdPBCt94GzaiI830xKsh5KlgFrenU%2B4x2p0pbeAQUokTLKHEUUzmNmlplxhADBHmTAEwC0vbbGjWFh3PS0zGRwas2TpLNRtQznTmdfoI7j0dlSVHCQQ%3D%3Dboletobancario.expirationDate2016-10-028814737173377729Received EOS BILLET_REFUSED_RESPONSE = < 8514038928235061102 Unable to determine variantRefused EOS