google4r-checkout 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/CHANGES +40 -0
  2. data/LICENSE +22 -0
  3. data/README +76 -0
  4. data/lib/google4r/checkout.rb +33 -0
  5. data/lib/google4r/checkout/commands.rb +260 -0
  6. data/lib/google4r/checkout/frontend.rb +108 -0
  7. data/lib/google4r/checkout/notifications.rb +549 -0
  8. data/lib/google4r/checkout/shared.rb +541 -0
  9. data/lib/google4r/checkout/xml_generation.rb +313 -0
  10. data/test/integration/checkout_command_test.rb +103 -0
  11. data/test/unit/address_test.rb +131 -0
  12. data/test/unit/area_test.rb +44 -0
  13. data/test/unit/checkout_command_test.rb +116 -0
  14. data/test/unit/checkout_command_xml_generator_test.rb +203 -0
  15. data/test/unit/command_test.rb +115 -0
  16. data/test/unit/flat_rate_shipping_test.rb +114 -0
  17. data/test/unit/frontend_test.rb +63 -0
  18. data/test/unit/item_test.rb +159 -0
  19. data/test/unit/marketing_preferences_test.rb +65 -0
  20. data/test/unit/merchant_code_test.rb +122 -0
  21. data/test/unit/new_order_notification_test.rb +115 -0
  22. data/test/unit/notification_acknowledgement_test.rb +43 -0
  23. data/test/unit/notification_handler_test.rb +93 -0
  24. data/test/unit/order_adjustment_test.rb +119 -0
  25. data/test/unit/order_state_change_notification_test.rb +159 -0
  26. data/test/unit/pickup_shipping_test.rb +70 -0
  27. data/test/unit/postal_area_test.rb +71 -0
  28. data/test/unit/private_data_parser_test.rb +68 -0
  29. data/test/unit/shipping_adjustment_test.rb +100 -0
  30. data/test/unit/shipping_method_test.rb +41 -0
  31. data/test/unit/shopping_cart_test.rb +146 -0
  32. data/test/unit/tax_rule_test.rb +70 -0
  33. data/test/unit/tax_table_test.rb +82 -0
  34. data/test/unit/us_country_area_test.rb +76 -0
  35. data/test/unit/us_state_area_test.rb +70 -0
  36. data/test/unit/us_zip_area_test.rb +66 -0
  37. data/test/unit/world_area_test.rb +48 -0
  38. data/var/cacert.pem +7815 -0
  39. metadata +92 -0
@@ -0,0 +1,115 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/new_order_confirmation_test.rb
4
+ # Author: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Copyright: (c) 2007 by Manuel Holtgrewe
6
+ # License: MIT License as follows:
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ # persons to whom the Software is furnished to do so, subject to the
14
+ # following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included
17
+ # in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class NewOrderConfirmation.
35
+ class Google4R::Checkout::NewOrderNotificationTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @xml_str = %q{<?xml version="1.0" encoding="UTF-8" ?>
40
+ <new-order-notification xmlns="http://checkout.google.com/schema/2" serial-number="8571632143">
41
+ <google-order-number>6014423719</google-order-number>
42
+ <buyer-billing-address />
43
+ <buyer-id>294873009217523</buyer-id>
44
+ <buyer-marketing-preferences />
45
+ <buyer-shipping-address />
46
+ <financial-order-state>REVIEWING</financial-order-state>
47
+ <fulfillment-order-state>NEW</fulfillment-order-state>
48
+ <google-order-number>6014423719</google-order-number>
49
+ <order-adjustment />
50
+ <order-total currency="USD">321.98</order-total>
51
+ <shopping-cart />
52
+ <timestamp>2006-03-18T17:32:11</timestamp>
53
+ </new-order-notification>}
54
+
55
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
56
+ @frontend.tax_table_factory = TestTaxTableFactory.new
57
+ end
58
+
59
+ def test_responds_correctly
60
+ assert_respond_to NewOrderNotification, :create_from_element
61
+
62
+ notification = NewOrderNotification.new(@frontend)
63
+
64
+ [ :serial_number, :google_order_number, :buyer_billing_address, :buyer_shipping_address,
65
+ :buyer_id, :marketing_preferences, :financial_order_state, :fulfillment_order_state,
66
+ :google_order_number, :order_adjustment, :order_total, :shopping_cart, :timestamp,
67
+ :serial_number=, :google_order_number=, :buyer_billing_address=, :buyer_shipping_address=,
68
+ :buyer_id=, :marketing_preferences=, :financial_order_state=, :fulfillment_order_state=,
69
+ :google_order_number=, :order_adjustment=, :order_total=, :shopping_cart=, :timestamp=,
70
+ ].each do |symbol|
71
+ assert_respond_to notification, symbol
72
+ end
73
+ end
74
+
75
+ def test_create_from_element_works_correctly
76
+ # Build up some Mocha expectations.
77
+ expect = Address.stubs(:create_from_element)
78
+ expect.times(1).returns(:buyer_billing_address)
79
+ expect.with{ |element| element.name == 'buyer-billing-address' }
80
+
81
+ expect = Address.stubs(:create_from_element)
82
+ expect.times(1).returns(:buyer_shipping_address)
83
+ expect.with{ |element| element.name == 'buyer-shipping-address' }
84
+
85
+ expect = MarketingPreferences.stubs(:create_from_element)
86
+ expect.times(1).returns(:marketing_preferences)
87
+ expect.with{ |element| element.name == 'buyer-marketing-preferences' }
88
+
89
+ expect = OrderAdjustment.stubs(:create_from_element)
90
+ expect.times(1).returns(:order_adjustment)
91
+ expect.with{ |element| element.name == 'order-adjustment' }
92
+
93
+ expect = ShoppingCart.stubs(:create_from_element)
94
+ expect.times(1).returns(:shopping_cart)
95
+ expect.with { |element, owner| element.name == 'shopping-cart' and owner.kind_of?(NewOrderNotification) }
96
+
97
+ # Create the new notification.
98
+ notification = NewOrderNotification.create_from_element(REXML::Document.new(@xml_str).root, @frontend)
99
+
100
+ # perform the assertions
101
+ assert_equal "8571632143", notification.serial_number
102
+ assert_equal "6014423719", notification.google_order_number
103
+ assert_equal :buyer_billing_address, notification.buyer_billing_address
104
+ assert_equal :buyer_shipping_address, notification.buyer_shipping_address
105
+ assert_equal "294873009217523", notification.buyer_id
106
+ assert_equal :marketing_preferences, notification.marketing_preferences
107
+ assert_equal "REVIEWING", notification.financial_order_state
108
+ assert_equal "NEW", notification.fulfillment_order_state
109
+ assert_equal "6014423719", notification.google_order_number
110
+ assert_equal :order_adjustment, notification.order_adjustment
111
+ assert_equal Money.new(32198, "USD"), notification.order_total
112
+ assert_equal :shopping_cart, notification.shopping_cart
113
+ assert_equal Time.parse("2006-03-18T17:32:11"), notification.timestamp
114
+ end
115
+ end
@@ -0,0 +1,43 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/notification_acknowledgement_test.rb
4
+ # Author: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Copyright: (c) 2007 by Manuel Holtgrewe
6
+ # License: MIT License as follows:
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ # persons to whom the Software is furnished to do so, subject to the
14
+ # following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included
17
+ # in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class NotificationAcknowledgement.
35
+ class Google4R::Checkout::NotificationAcknowledgementTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def test_to_xml_works_as_expected
39
+ ack = NotificationAcknowledgement.new
40
+ str = %q{<?xml version="1.0" encoding="UTF-8"?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2"/>}
41
+ assert_equal str, ack.to_xml
42
+ end
43
+ end
@@ -0,0 +1,93 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/notification_handler_test.rb
4
+ # Author: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Copyright: (c) 2007 by Manuel Holtgrewe
6
+ # License: MIT License as follows:
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ # persons to whom the Software is furnished to do so, subject to the
14
+ # following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included
17
+ # in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class NotificationHandler.
35
+ class Google4R::Checkout::NotificationHandlerTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
40
+ @frontend.tax_table_factory = TestTaxTableFactory.new
41
+ @notification_handler = @frontend.create_notification_handler
42
+
43
+ @xmls_with_known_tags =
44
+ [
45
+ [
46
+ '<new-order-notification xmlns="http://checkout.google.com/schema/2" />',
47
+ 'new-order-notification',
48
+ NewOrderNotification
49
+ ],
50
+ [
51
+ '<order-state-change-notification xmlns="http://checkout.google.com/schema/2" />',
52
+ 'order-state-change-notification',
53
+ OrderStateChangeNotification
54
+ ]
55
+ ]
56
+
57
+ @xmls_with_unknown_tags =
58
+ [
59
+ '<unknown-notification />',
60
+ %q{<risk-information-notification xmlns="http://checkout.google.com/schema/2" />},
61
+ %q{<charge-amount-notification xmlns="http://checkout.google.com/schema/2" />},
62
+ %q{<refund-amount-notification xmlns="http://checkout.google.com/schema/2" />},
63
+ %q{<chargeback-amount-notification xmlns="http://checkout.google.com/schema/2" />},
64
+ %q{<authorization-amount-notification xmlns="http://checkout.google.com/schema/2" />}
65
+ ]
66
+ end
67
+
68
+ def test_handler_gets_initialized_correctly
69
+ assert_equal @frontend, @notification_handler.frontend
70
+ end
71
+
72
+ def test_returns_correct_notification_class_for_known_notifications
73
+ @xmls_with_known_tags.each do |triple|
74
+ xml_str, tag_name, klass = triple
75
+
76
+ expect = klass.stubs(:create_from_element)
77
+ expect.times(1).returns(:foo)
78
+ expect.with { |element, frontend| element.name == tag_name and frontend == @frontend }
79
+
80
+ result = nil
81
+ assert_nothing_raised {
82
+ result = @notification_handler.handle(xml_str)
83
+ }
84
+ assert_equal :foo, result
85
+ end
86
+ end
87
+
88
+ def test_raises_exception_on_unknown_notifications
89
+ @xmls_with_unknown_tags.each do |xml_str|
90
+ assert_raises(UnknownNotificationType) { @notification_handler.handle(xml_str) }
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,119 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/order_adjustment_test.rb
4
+ # Author: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Copyright: (c) 2007 by Manuel Holtgrewe
6
+ # License: MIT License as follows:
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ # persons to whom the Software is furnished to do so, subject to the
14
+ # following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included
17
+ # in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class OrderAdjustment.
35
+ class Google4R::Checkout::OrderAdjustmentTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @xml_str = %q{<?xml version="1.0" encoding="UTF-8" ?>
40
+ <order-adjustment>
41
+ <merchant-calculation-successful>true</merchant-calculation-successful>
42
+ <merchant-codes>
43
+ <dummy-adjustment-one />
44
+ <dummy-adjustment-two />
45
+ </merchant-codes>
46
+ <total-tax currency="USD">11.05</total-tax>
47
+ <adjustment-total currency="USD">12.05</adjustment-total>
48
+ <shipping>
49
+ <%s />
50
+ </shipping>
51
+ </order-adjustment>}
52
+ @shipping_adjustments = [ 'flat-rate-shipping-adjustment', 'pickup-shipping-adjustment' ]
53
+ end
54
+
55
+ def test_reponds_correctly
56
+ assert_respond_to OrderAdjustment, :create_from_element
57
+
58
+ adjustment = OrderAdjustment.new
59
+
60
+ [ :adjustment_total, :adjustment_total=, :merchant_calculation_successful,
61
+ :merchant_calculation_successful, :merchant_codes, :merchant_codes=,
62
+ :shipping, :shipping=, :total_tax, :total_tax=
63
+ ].each do |symbol|
64
+ assert_respond_to adjustment, symbol
65
+ end
66
+ end
67
+
68
+ def test_create_from_xml_works_correctly
69
+ @shipping_adjustments.each do |adjustment_name|
70
+ # Build Mocha Expectations
71
+ expect = MerchantCode.stubs(:create_from_element)
72
+ expect.with { |element| element.name == 'dummy-adjustment-one' }
73
+ expect.times(1).returns(:dummy_adjustment1)
74
+
75
+ expect = MerchantCode.stubs(:create_from_element)
76
+ expect.with { |element| element.name == 'dummy-adjustment-two' }
77
+ expect.times(1).returns(:dummy_adjustment2)
78
+
79
+ expect = ShippingAdjustment.stubs(:create_from_element)
80
+ expect.with { |element| element.name == adjustment_name }
81
+ expect.times(1).returns(adjustment_name.to_sym)
82
+
83
+ # Create the adjustment
84
+ element = REXML::Document.new(@xml_str % [ adjustment_name ]).root
85
+ adjustment = OrderAdjustment.create_from_element(element)
86
+
87
+ # Assert!
88
+ assert_equal Money.new(1205, 'USD'), adjustment.adjustment_total
89
+ assert_equal true, adjustment.merchant_calculation_successful
90
+ assert_equal [ :dummy_adjustment1, :dummy_adjustment2 ], adjustment.merchant_codes
91
+ assert_equal adjustment_name.to_sym, adjustment.shipping
92
+ assert_equal Money.new(1105, 'USD'), adjustment.total_tax
93
+ end
94
+ end
95
+
96
+ def test_create_from_xml_works_correctly_without_shiping_tag
97
+ # Build Mocha Expectations
98
+ expect = MerchantCode.stubs(:create_from_element)
99
+ expect.with { |element| element.name == 'dummy-adjustment-one' }
100
+ expect.times(1).returns(:dummy_adjustment1)
101
+
102
+ expect = MerchantCode.stubs(:create_from_element)
103
+ expect.with { |element| element.name == 'dummy-adjustment-two' }
104
+ expect.times(1).returns(:dummy_adjustment2)
105
+
106
+ expect = ShippingAdjustment.expects(:create_from_element).never
107
+
108
+ # Create the adjustment
109
+ element = REXML::Document.new(@xml_str.gsub(%r{<shipping>.*<\/shipping>}m, '')).root
110
+ adjustment = OrderAdjustment.create_from_element(element)
111
+
112
+ # Assert!
113
+ assert_equal Money.new(1205, 'USD'), adjustment.adjustment_total
114
+ assert_equal true, adjustment.merchant_calculation_successful
115
+ assert_equal [ :dummy_adjustment1, :dummy_adjustment2 ], adjustment.merchant_codes
116
+ assert_equal nil, adjustment.shipping
117
+ assert_equal Money.new(1105, 'USD'), adjustment.total_tax
118
+ end
119
+ end
@@ -0,0 +1,159 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/order_state_change_notification_test.rb
4
+ # Author: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Copyright: (c) 2007 by Manuel Holtgrewe
6
+ # License: MIT License as follows:
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
13
+ # persons to whom the Software is furnished to do so, subject to the
14
+ # following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included
17
+ # in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class Area.
35
+ class Google4R::Checkout::OrderStateChangeNotificationTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @data = Hash.new
40
+ @data[:serial_number] = "c821426e-7caa-4d51-9b2e-48ef7ecd6423"
41
+ @data[:google_order_number] = "841171949013218"
42
+ @data[:new_financial_order_state] = "CHARGING"
43
+ @data[:previous_fulfillment_order_state] = "NEW"
44
+ @data[:new_financial_order_state] = "CHARGEABLE"
45
+ @data[:previous_fulfillment_order_state] = "NEW"
46
+ @data[:reason] = "charge_customer"
47
+ @data[:timestamp] = Time.new
48
+
49
+ @xml_template = %q{<?xml version="1.0" encoding="UTF-8"?>
50
+ <order-state-change-notification xmlns="http://checkout.google.com/schema/2"
51
+ serial-number="%s">
52
+ <google-order-number>%s</google-order-number>
53
+ <new-financial-order-state>%s</new-financial-order-state>
54
+ <new-fulfillment-order-state>%s</new-fulfillment-order-state>
55
+ <previous-financial-order-state>%s</previous-financial-order-state>
56
+ <previous-fulfillment-order-state>%s</previous-fulfillment-order-state>
57
+ <reason>%s</reason>
58
+ <timestamp>%s</timestamp>
59
+ </order-state-change-notification>
60
+ }
61
+
62
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
63
+ @frontend.tax_table_factory = TestTaxTableFactory.new
64
+ end
65
+
66
+ def test_order_state_change_notification_responds_correctly
67
+ assert_respond_to OrderStateChangeNotification, :create_from_element
68
+
69
+ notification = OrderStateChangeNotification.new(@frontend)
70
+
71
+ [ :serial_number, :google_order_number, :new_financial_order_state, :previous_fulfillment_order_state,
72
+ :new_financial_order_state, :previous_fulfillment_order_state, :reason, :timestamp,
73
+ :serial_number=, :google_order_number=, :new_financial_order_state=, :previous_fulfillment_order_state=,
74
+ :new_financial_order_state=, :previous_fulfillment_order_state=, :reason=, :timestamp
75
+ ].each do |sym|
76
+ assert_respond_to notification, sym
77
+ end
78
+ end
79
+
80
+ def test_financial_order_state_defines_correct_constants
81
+ assert defined?(FinancialOrderState::REVIEWING)
82
+ assert defined?(FinancialOrderState::CHARGEABLE)
83
+ assert defined?(FinancialOrderState::CHARGING)
84
+ assert defined?(FinancialOrderState::CHARGED)
85
+ assert defined?(FinancialOrderState::PAYMENT_DECLINED)
86
+ assert defined?(FinancialOrderState::CANCELLED)
87
+ assert defined?(FinancialOrderState::CANCELLED_BY_GOOGLE)
88
+
89
+ assert_equal "REVIEWING", FinancialOrderState::REVIEWING
90
+ assert_equal "CHARGEABLE", FinancialOrderState::CHARGEABLE
91
+ assert_equal "CHARGING", FinancialOrderState::CHARGING
92
+ assert_equal "CHARGED", FinancialOrderState::CHARGED
93
+ assert_equal "PAYMENT_DECLINED", FinancialOrderState::PAYMENT_DECLINED
94
+ assert_equal "CANCELLED", FinancialOrderState::CANCELLED
95
+ assert_equal "CANCELLED_BY_GOOGLE", FinancialOrderState::CANCELLED_BY_GOOGLE
96
+ end
97
+
98
+ def test_fulfillment_order_state_defines_correct_constants
99
+ assert defined?(FulfillmentOrderState::NEW)
100
+ assert defined?(FulfillmentOrderState::PROCESSING)
101
+ assert defined?(FulfillmentOrderState::DELIVERED)
102
+ assert defined?(FulfillmentOrderState::WILL_NOT_DELIVER)
103
+
104
+ assert_equal "NEW", FulfillmentOrderState::NEW
105
+ assert_equal "PROCESSING", FulfillmentOrderState::PROCESSING
106
+ assert_equal "DELIVERED", FulfillmentOrderState::DELIVERED
107
+ assert_equal "WILL_NOT_DELIVER", FulfillmentOrderState::WILL_NOT_DELIVER
108
+ end
109
+
110
+ # <reason> in notification
111
+ def test_create_from_xml_should_correctly_create_order_state_change_notification_with_reason
112
+ [ true, false ].each do |skip_reason|
113
+ xml_str = @xml_template %
114
+ [
115
+ @data[:serial_number], @data[:google_order_number], @data[:new_financial_order_state],
116
+ @data[:new_fulfillment_order_state], @data[:previous_financial_order_state],
117
+ @data[:previous_fulfillment_order_state], @data[:reason], @data[:timestamp].iso8601
118
+ ]
119
+
120
+ xml_str = xml_str.gsub(/<reason>.*?<\/reason>/, '') if skip_reason
121
+
122
+ notification = OrderStateChangeNotification.create_from_element(REXML::Document.new(xml_str).root, @frontend)
123
+
124
+ @data.each do |key, value|
125
+ next if skip_reason and key == :reason
126
+
127
+ if key == :timestamp then
128
+ assert_in_delta @data[:timestamp].to_f, notification.timestamp.to_f, 1
129
+ else
130
+ assert_equal @data[key], notification.send(key), "#{key}"
131
+ end
132
+ end
133
+
134
+ assert_nil notification.reason if skip_reason
135
+ end
136
+ end
137
+
138
+ def test_create_from_xml_should_raise_exception_with_missing_tags
139
+ # Remove all required tags from the XML and expect the XPath query in
140
+ # OrderStateChangeNotification#create_from_xml to evaluate to nil and thus
141
+ # raise a NoMethodError.
142
+ [ 'google-order-number', 'new-financial-order-state', 'new-fulfillment-order-state',
143
+ 'previous-financial-order-state', 'previous-fulfillment-order-state', 'timestamp' ].each do |tag_name|
144
+ xml_str = @xml_template %
145
+ [
146
+ @data[:serial_number], @data[:google_order_number], @data[:new_financial_order_state],
147
+ @data[:new_fulfillment_order_state], @data[:previous_financial_order_state],
148
+ @data[:previous_fulfillment_order_state], @data[:reason], @data[:timestamp].iso8601
149
+ ]
150
+
151
+ xml_str = xml_str.gsub(%r{<#{tag_name}>.*?</#{tag_name}>}, '')
152
+ doc = REXML::Document.new(xml_str)
153
+
154
+ assert_raises(NoMethodError, "Removed tag: #{tag_name} - #{xml_str}") {
155
+ notification = OrderStateChangeNotification.create_from_element(doc.root, @frontend)
156
+ }
157
+ end
158
+ end
159
+ end