google4r 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGES +5 -0
  2. data/LICENSE +22 -0
  3. data/README +75 -0
  4. data/lib/google4r/checkout.rb +36 -0
  5. data/lib/google4r/checkout/commands.rb +267 -0
  6. data/lib/google4r/checkout/frontend.rb +100 -0
  7. data/lib/google4r/checkout/notifications.rb +533 -0
  8. data/lib/google4r/checkout/shared.rb +501 -0
  9. data/lib/google4r/checkout/xml_generation.rb +271 -0
  10. data/lib/google4r/maps.rb +174 -0
  11. data/test/checkout/integration/checkout_command_test.rb +103 -0
  12. data/test/checkout/unit/address_test.rb +131 -0
  13. data/test/checkout/unit/area_test.rb +41 -0
  14. data/test/checkout/unit/checkout_command_test.rb +112 -0
  15. data/test/checkout/unit/checkout_command_xml_generator_test.rb +187 -0
  16. data/test/checkout/unit/command_test.rb +126 -0
  17. data/test/checkout/unit/flat_rate_shipping_test.rb +114 -0
  18. data/test/checkout/unit/frontend_test.rb +63 -0
  19. data/test/checkout/unit/item_test.rb +159 -0
  20. data/test/checkout/unit/marketing_preferences_test.rb +65 -0
  21. data/test/checkout/unit/merchant_code_test.rb +122 -0
  22. data/test/checkout/unit/new_order_notification_test.rb +115 -0
  23. data/test/checkout/unit/notification_acknowledgement_test.rb +43 -0
  24. data/test/checkout/unit/notification_handler_test.rb +93 -0
  25. data/test/checkout/unit/order_adjustment_test.rb +95 -0
  26. data/test/checkout/unit/order_state_change_notification_test.rb +159 -0
  27. data/test/checkout/unit/pickup_shipping_test.rb +70 -0
  28. data/test/checkout/unit/private_data_parser_test.rb +68 -0
  29. data/test/checkout/unit/shipping_adjustment_test.rb +100 -0
  30. data/test/checkout/unit/shipping_method_test.rb +41 -0
  31. data/test/checkout/unit/shopping_cart_test.rb +146 -0
  32. data/test/checkout/unit/tax_rule_test.rb +65 -0
  33. data/test/checkout/unit/tax_table_test.rb +82 -0
  34. data/test/checkout/unit/us_country_area_test.rb +76 -0
  35. data/test/checkout/unit/us_state_area_test.rb +70 -0
  36. data/test/checkout/unit/us_zip_area_test.rb +66 -0
  37. data/test/maps/geocoder_test.rb +143 -0
  38. data/var/cacert.pem +7815 -0
  39. metadata +100 -0
@@ -0,0 +1,131 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/address_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 'test/checkout/frontend_configuration'
31
+
32
+ require 'google4r/checkout'
33
+
34
+ # Test for the class Address.
35
+ class Google4R::Checkout::AddressTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @data =
40
+ {
41
+ :contact_name => 'John Smith',
42
+ :email => 'johnsmith@example.com',
43
+ :fax => '+01123456789',
44
+ :phone => '+01123456789',
45
+ :address1 => '10 Example Road',
46
+ :address2 => 'Bar',
47
+ :city => 'Sampleville',
48
+ :company_name => 'Company Name',
49
+ :region => 'CA',
50
+ :postal_code => '94141',
51
+ :country_code => 'US',
52
+ }
53
+
54
+ @optional_fields = [ :fax, :phone, :address2, :company_name, :contact_name ]
55
+
56
+ @xml_str = %q{<?xml version="1.0" encoding="UTF-8" ?>
57
+ <root>
58
+ <some-address>
59
+ <contact-name>%s</contact-name>
60
+ <email>%s</email>
61
+ <fax>%s</fax>
62
+ <phone>%s</phone>
63
+ <address1>%s</address1>
64
+ <address2>%s</address2>
65
+ <city>%s</city>
66
+ <company-name>%s</company-name>
67
+ <region>%s</region>
68
+ <postal-code>%s</postal-code>
69
+ <country-code>%s</country-code>
70
+ </some-address>
71
+ </root>}
72
+ end
73
+
74
+ def test_responds_correctly
75
+ @address = Address.new
76
+
77
+ @data.each do |key, value|
78
+ assert_respond_to @address, key, "key == #{key}"
79
+ assert_respond_to @address, "#{key}=".to_sym, "key == #{key}"
80
+ end
81
+ end
82
+
83
+ def test_creating_address_with_full_fields_works
84
+ xml_str = @xml_str %
85
+ [
86
+ @data[:contact_name], @data[:email], @data[:fax], @data[:phone], @data[:address1],
87
+ @data[:address2], @data[:city], @data[:company_name], @data[:region],
88
+ @data[:postal_code], @data[:country_code]
89
+ ]
90
+
91
+ document = REXML::Document.new(xml_str)
92
+
93
+ the_element = document.elements['/root/some-address']
94
+
95
+ address = Address.create_from_element(the_element)
96
+
97
+ @data.each do |key, value|
98
+ assert_equal value, address.send(key)
99
+ end
100
+ end
101
+
102
+ def test_creating_address_works_when_removing_optional_fields
103
+ @optional_fields.power.each do |optional_symbols|
104
+ xml_str = @xml_str %
105
+ [
106
+ @data[:contact_name], @data[:email], @data[:fax], @data[:phone], @data[:address1],
107
+ @data[:address2], @data[:city], @data[:company_name], @data[:region],
108
+ @data[:postal_code], @data[:country_code]
109
+ ]
110
+
111
+ # Remove all optional symbols in this run.
112
+ optional_symbols.each do |symbol|
113
+ xml_str = xml_str.gsub(%r{<#{symbol.to_s.gsub('_', '-')}>.*?</#{symbol.to_s.gsub('_', '-')}>}, '')
114
+ end
115
+
116
+ document = REXML::Document.new(xml_str)
117
+
118
+ the_element = document.elements['/root/some-address']
119
+
120
+ address = Address.create_from_element(the_element)
121
+
122
+ @data.each do |key, value|
123
+ if optional_symbols.include?(key) then
124
+ assert_nil address.send(key)
125
+ else
126
+ assert_equal value, address.send(key)
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/area_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 'test/checkout/frontend_configuration'
31
+
32
+ require 'google4r/checkout'
33
+
34
+ # Test for the class Area.
35
+ class Google4R::Checkout::AreaTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def test_instanciating_area_raises_runtime_error
39
+ assert_raises(RuntimeError) { Area.new }
40
+ end
41
+ end
@@ -0,0 +1,112 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/checkout_command_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 'test/checkout/frontend_configuration'
31
+
32
+ require 'google4r/checkout'
33
+
34
+ # Tests for the CheckoutCommand class.
35
+ class Google4R::Checkout::CheckoutCommandTest < 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
+ @command = @frontend.create_checkout_command
42
+ end
43
+
44
+ def test_tax_table_factory_works_correctly
45
+ TestTaxTableFactory.any_instance.stubs(:effective_tax_tables_at).returns([:tax_table])
46
+
47
+ frontend = Frontend.new(FRONTEND_CONFIGURATION)
48
+ frontend.tax_table_factory = TestTaxTableFactory.new
49
+ command = frontend.create_checkout_command
50
+
51
+ assert_equal [:tax_table], command.tax_tables
52
+ end
53
+
54
+ def test_initialized_correctly
55
+ assert_equal @frontend, @command.frontend
56
+ assert_kind_of ShoppingCart, @command.cart
57
+ assert_equal [], @command.shipping_methods
58
+ assert_nil @command.edit_cart_url
59
+ assert_nil @command.continue_shopping_url
60
+ assert_nil @command.request_buyer_phone_number
61
+ end
62
+
63
+ def test_command_behaves_correctly
64
+ [ :frontend, :cart, :tax_tables, :shipping_methods, :create_shipping_method,
65
+ :edit_cart_url, :edit_cart_url=, :continue_shopping_url, :continue_shopping_url=,
66
+ :request_buyer_phone_number, :request_buyer_phone_number=
67
+ ].each do |symbol|
68
+ assert_respond_to @command, symbol
69
+ end
70
+ end
71
+
72
+ def test_accessors_work_correctly
73
+ @command.edit_cart_url = "edit cart url"
74
+ assert_equal "edit cart url", @command.edit_cart_url
75
+
76
+ @command.continue_shopping_url = "continue shopping url"
77
+ assert_equal "continue shopping url", @command.continue_shopping_url
78
+
79
+ @command.request_buyer_phone_number = true
80
+ assert_equal true, @command.request_buyer_phone_number
81
+ end
82
+
83
+ def test_create_shipping_method_works_correctly_with_block
84
+ [ PickupShipping, FlatRateShipping ].each do |clazz|
85
+
86
+ shipping = nil
87
+
88
+ res =
89
+ @command.create_shipping_method(clazz) do |obj|
90
+ shipping = obj
91
+ assert_kind_of clazz, obj
92
+ end
93
+
94
+ assert_equal res, shipping
95
+ assert @command.shipping_methods.include?(shipping)
96
+ end
97
+ end
98
+
99
+ def test_create_shipping_method_works_correctly_without_block
100
+ [ PickupShipping, FlatRateShipping ].each do |clazz|
101
+ obj = @command.create_shipping_method(clazz)
102
+
103
+ assert_kind_of clazz, obj
104
+
105
+ assert @command.shipping_methods.include?(obj)
106
+ end
107
+ end
108
+
109
+ def test_to_xml_does_not_raise_exception
110
+ assert_nothing_raised { @command.to_xml }
111
+ end
112
+ end
@@ -0,0 +1,187 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/checkout_command_xml_generator_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 'test/checkout/frontend_configuration'
31
+
32
+ require 'google4r/checkout'
33
+
34
+ require 'tempfile'
35
+ require 'open3'
36
+ require 'rexml/document'
37
+
38
+ # Test for the class UsZipArea.
39
+ #
40
+ # TODO:
41
+ #
42
+ # * Make the tests querying Google Checkout's diagnose service and the
43
+ # XSD validation optional.
44
+ class Google4R::Checkout::CheckoutCommandXmlGeneratorTest < Test::Unit::TestCase
45
+ include Google4R::Checkout
46
+
47
+ def setup
48
+ @schema_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'xml', 'apiv2.xsd'))
49
+ @expected_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'xml', 'test_check_persisting_works_expected.xml'))
50
+
51
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
52
+ @frontend.tax_table_factory = TestTaxTableFactory.new
53
+ @command = @frontend.create_checkout_command
54
+ @generator = CheckoutCommandXmlGenerator.new(@command)
55
+ end
56
+
57
+ # Note that this is not really a good test. We do not actually compare anything.
58
+ # Instead, we simply generate some XML, validate it using XSD via xmllint and then
59
+ # compare it to the output that we validated by reading it earlier.
60
+ def test_check_persisting_works
61
+ tax_tables = Array.new
62
+ table = TaxTable.new(false)
63
+ table.name = 'Default Table'
64
+ table.create_rule do |rule|
65
+ rule.rate = 0.04
66
+ rule.area = UsCountryArea.new
67
+ rule.area.area = UsCountryArea::ALL
68
+ end
69
+ tax_tables << table
70
+ nondefault_table = TaxTable.new(false)
71
+ nondefault_table.name = 'Special Table'
72
+ nondefault_table.create_rule do |rule|
73
+ rule.rate = 0.02
74
+ rule.area = UsCountryArea.new
75
+ rule.area.area = UsCountryArea::ALL
76
+ end
77
+ tax_tables << nondefault_table
78
+
79
+ TestTaxTableFactory.any_instance.stubs(:effective_tax_tables_at).returns(tax_tables)
80
+ @frontend.tax_table_factory = TestTaxTableFactory.new
81
+ @command = @frontend.create_checkout_command
82
+
83
+ @command.continue_shopping_url = 'http://wwww.example.com/continue_shopping'
84
+ @command.edit_cart_url = 'http://wwww.example.com/edit_cart'
85
+ @command.request_buyer_phone_number = false
86
+ @generator = CheckoutCommandXmlGenerator.new(@command)
87
+
88
+ @command.cart.expires_at = Time.parse('2007-11-29T15:33:20+01:00')
89
+
90
+ @command.create_shipping_method(PickupShipping) do |shipping|
91
+ shipping.name = 'Pickup Test Shipping'
92
+ shipping.price = Money.new(100, 'USD')
93
+ end
94
+
95
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
96
+ shipping.name = 'State Test Shipping'
97
+ shipping.price = Money.new(100, 'USD')
98
+
99
+ shipping.create_allowed_area(UsStateArea) { |area| area.state = 'CA' }
100
+ shipping.create_excluded_area(UsStateArea) { |area| area.state = 'TX' }
101
+ end
102
+
103
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
104
+ shipping.name = 'Country Area Test Shipping'
105
+ shipping.price = Money.new(100, 'USD')
106
+
107
+ shipping.create_allowed_area(UsCountryArea) { |area| area.area = UsCountryArea::FULL_50_STATES }
108
+ shipping.create_excluded_area(UsCountryArea) { |area| area.area = UsCountryArea::CONTINENTAL_48 }
109
+ end
110
+
111
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
112
+ shipping.name = 'Zip Test Shipping'
113
+ shipping.price = Money.new(100, 'USD')
114
+
115
+ shipping.create_allowed_area(UsZipArea) { |area| area.pattern = '1*' }
116
+ shipping.create_excluded_area(UsZipArea) { |area| area.pattern = '12*' }
117
+ end
118
+
119
+ @command.cart.private_data = { 'we' => 'can pass in some data here'.split }
120
+
121
+ 1.upto(10) do |i|
122
+ @command.cart.create_item do |item|
123
+ item.name = "Item Name #{i}"
124
+ item.description = "Description #{i}"
125
+ item.unit_price = Money.new(i * 1000, 'USD')
126
+ item.quantity = i * 2
127
+ item.id = "Merchant ID #{i}"
128
+ if i == 1 then
129
+ item.private_data = { 'some' => { 'data' => 'Yeah, Yeah!' }, 'bars' => { 'bar' => [ 1, 2 ] } }
130
+ end
131
+
132
+ item.tax_table = nondefault_table if i % 2 == 1
133
+ end
134
+ end
135
+
136
+ xml_str = @generator.generate
137
+ assert_xml_validates_against_xml_schema(@schema_path, xml_str)
138
+ assert_string_equals_file_contents(@expected_path, xml_str)
139
+ assert_google_checkout_diagnose_returns_no_warnings(xml_str)
140
+ end
141
+
142
+ protected
143
+
144
+ def assert_google_checkout_diagnose_returns_no_warnings(xml_str)
145
+ tmpfile = Tempfile.new('xml_output')
146
+ tmpfile << xml_str
147
+ tmpfile.flush
148
+
149
+ url = "https://%s:%s@sandbox.google.com/checkout/cws/v2/Merchant/%s/request/diagnose" %
150
+ [ FRONTEND_CONFIGURATION[:merchant_id], FRONTEND_CONFIGURATION[:merchant_key],
151
+ FRONTEND_CONFIGURATION[:merchant_id] ]
152
+
153
+ stdin, stdout, stderr = Open3.popen3("curl", "-d", "@#{tmpfile.path}", url)
154
+ outstr = stdout.read
155
+ errstr = stderr.read
156
+
157
+ # Check that there is no <warnings> tag in the XML.
158
+ xml_document = REXML::Document.new(outstr)
159
+ assert 0, xml_document.root.elements.to_a('//warnings').size
160
+
161
+ tmpfile.close!
162
+ end
163
+
164
+ def assert_xml_validates_against_xml_schema(schema_path, xml_str)
165
+ tmpfile = Tempfile.new('xml_output')
166
+ tmpfile << xml_str
167
+ tmpfile.flush
168
+
169
+ #puts "---\n#{xml_str}\n---"
170
+
171
+ stdin, stdout, stderr = Open3.popen3("xmllint", "--schema", schema_path, tmpfile.path)
172
+ outstr = stdout.read
173
+ errstr = stderr.read
174
+ if errstr !~ /validates$/ then
175
+ assert false, "The document did not validate: ---\nOUT:#{outstr}\nERR:#{errstr}\n---"
176
+ else
177
+ assert true
178
+ end
179
+
180
+ tmpfile.close!
181
+ end
182
+
183
+ def assert_string_equals_file_contents(expected_path, xml_str)
184
+ file_contents = File.open(expected_path, 'r') { |f| f.read }
185
+ assert_equal file_contents, xml_str
186
+ end
187
+ end