google-ads-common 0.14.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,96 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests the array replies from services.
20
-
21
- require 'test/unit'
22
-
23
- require 'ads_common/config'
24
-
25
- class TestConfig < Test::Unit::TestCase
26
- DEFAULT_CONFIG_HASH = {
27
- :service => {:use_ruby_names => false,
28
- :environment => 'sandbox'},
29
- :authentication => {:method => 'ClientLogin',
30
- :email => 'root@example.com'}
31
- }
32
-
33
- DEFAULT_CONFIG_FILENAME = 'test/test_config.yml'
34
-
35
- # Test initializer with no arguments.
36
- def test_initialize_nil
37
- assert_nothing_raised do
38
- config = AdsCommon::Config.new
39
- assert_nil(config.read('service.use_ruby_names'))
40
- end
41
- end
42
-
43
- # Test initializer with hash argument.
44
- def test_initialize_hash
45
- config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
46
- assert_equal(false, config.read('service.use_ruby_names'))
47
- assert_equal('sandbox', config.read('service.environment'))
48
- assert_equal('ClientLogin', config.read('authentication.method'))
49
- assert_equal('root@example.com', config.read('authentication.email'))
50
- assert_nil(config.read('unexisting.entry'))
51
- end
52
-
53
- # Test initializer with filename argument.
54
- def test_initialize_filename_correct
55
- config = AdsCommon::Config.new(DEFAULT_CONFIG_FILENAME)
56
- assert_equal(false, config.read('service.use_ruby_names'))
57
- assert_equal('sandbox', config.read('service.environment'))
58
- assert_equal('ClientLogin', config.read('authentication.method'))
59
- assert_equal('root@example.com', config.read('authentication.email'))
60
- assert_nil(config.read('unexisting.entry'))
61
- end
62
-
63
- # Test initializer with an incorrect existing file.
64
- def test_initialize_filename_incorrect
65
- assert_raises (AdsCommon::Errors::Error) do
66
- AdsCommon::Config.new('/dev/null')
67
- end
68
- end
69
-
70
- # Test default result.
71
- def test_read_default_result
72
- config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
73
- assert_nil(config.read('unexisting.entry'))
74
- assert_equal('default', config.read('unexisting.entry', 'default'))
75
- assert_equal('sandbox', config.read('service.environment', 'production'))
76
- end
77
-
78
- # Test setter.
79
- def test_set
80
- config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
81
- assert_equal('sandbox', config.read('service.environment'))
82
- assert_nil(config.read('unexisting.entry'))
83
- config.set('unexisting.entry', 'foobar')
84
- assert_equal('sandbox', config.read('service.environment'))
85
- assert_equal('foobar', config.read('unexisting.entry'))
86
- end
87
-
88
- # Test subhash.
89
- def test_get_hash
90
- config = AdsCommon::Config.new(DEFAULT_CONFIG_HASH)
91
- result = config.read('service')
92
- assert_instance_of(Hash, result)
93
- assert_equal('sandbox', result[:environment])
94
- assert_equal(false, result[:use_ruby_names])
95
- end
96
- end
@@ -1,11 +0,0 @@
1
- ---
2
- # This is a test configuration file for Ads Common library.
3
- :authentication:
4
- :method: ClientLogin
5
- :application_name: ruby_test_suit
6
- :password: mySecretPassword
7
- :email: root@example.com
8
- :network_code: 1234567
9
- :service:
10
- :environment: sandbox
11
- :use_ruby_names: false
@@ -1,125 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2012, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests credential handler methods.
20
-
21
- require 'logger'
22
- require 'minitest/mock'
23
- require 'test/unit'
24
-
25
- require 'ads_common/config'
26
- require 'ads_common/credential_handler'
27
-
28
- class TestCredentialHandler < Test::Unit::TestCase
29
-
30
- def setup()
31
- logger = Logger.new(STDERR)
32
- @default_credentials = {:client_customer_id => '1234567890', :foo => 'bar'}
33
- config = AdsCommon::Config.new({
34
- :library => {:logger => logger},
35
- :authentication => @default_credentials
36
- })
37
- @handler = AdsCommon::CredentialHandler.new(config)
38
- end
39
-
40
- def test_credentials_simple()
41
- credentials = @handler.credentials()
42
- assert_equal(@default_credentials, credentials)
43
- assert_not_same(@default_credentials, credentials)
44
- end
45
-
46
- def test_credentials_override()
47
- @override = {:client_customer_id => 42}
48
- credentials = @handler.credentials(@override)
49
- assert_not_equal(@default_credentials, credentials)
50
- assert_not_same(@default_credentials, credentials)
51
- assert_equal(42, credentials[:client_customer_id])
52
- assert_equal('bar', credentials[:foo])
53
- end
54
-
55
- def test_generate_user_agent_simple()
56
- result1 = @handler.generate_user_agent()
57
- assert_kind_of(String, result1)
58
- end
59
-
60
- def test_generate_user_agent_chained()
61
- test_str = 'Tester/0.2.0'
62
- result1 = @handler.generate_user_agent([test_str])
63
- assert_kind_of(String, result1)
64
- assert_match(/#{Regexp.escape(test_str)}/, result1)
65
- end
66
-
67
- def test_generate_user_agent_include_simple()
68
- test_str = 'Tester'
69
- @handler.include_in_user_agent(test_str)
70
- result1 = @handler.generate_user_agent()
71
- assert_kind_of(String, result1)
72
- assert_match(/#{Regexp.escape(test_str)}/, result1)
73
- result2 = @handler.generate_user_agent()
74
- assert_kind_of(String, result2)
75
- assert_no_match(/#{Regexp.escape(test_str)}/, result2)
76
- end
77
-
78
- def test_generate_user_agent_include_version()
79
- test_str = 'Tester/0.2.0'
80
- argument1 = 'Tester'
81
- argument2 = '0.2.0'
82
- @handler.include_in_user_agent(argument1, argument2)
83
- result1 = @handler.generate_user_agent()
84
- assert_kind_of(String, result1)
85
- assert_match(/#{Regexp.escape(test_str)}/, result1)
86
- result2 = @handler.generate_user_agent()
87
- assert_kind_of(String, result2)
88
- assert_no_match(/#{Regexp.escape(test_str)}/, result2)
89
- end
90
-
91
- def test_auth_handler_callback_once()
92
- mock = Minitest::Mock.new()
93
- mock.expect(:property_changed, nil, [:foo, 'bar'])
94
- @handler.set_auth_handler(mock)
95
- @handler.set_credential(:foo, 'bar')
96
- assert(mock.verify)
97
- end
98
-
99
- def test_auth_handler_callback_compare()
100
- credentials = @handler.credentials
101
-
102
- credentials[:foo] = 'bar'
103
- credentials[:baz] = 42
104
- mock1 = Minitest::Mock.new()
105
- mock1.expect(:property_changed, nil, [:baz, 42])
106
- @handler.set_auth_handler(mock1)
107
- @handler.credentials = credentials
108
- assert(mock1.verify)
109
-
110
- credentials.delete(:baz)
111
- mock2 = Minitest::Mock.new()
112
- mock2.expect(:property_changed, nil, [:baz, nil])
113
- @handler.set_auth_handler(mock2)
114
- @handler.credentials = credentials
115
- assert(mock2.verify)
116
-
117
- credentials[:foo] = nil
118
- mock3 = Minitest::Mock.new()
119
- mock3.expect(:property_changed, nil, [:foo, nil])
120
- mock3.expect(:property_changed, nil, [:baz, nil])
121
- @handler.set_auth_handler(mock3)
122
- @handler.credentials = credentials
123
- assert(mock3.verify)
124
- end
125
- end
@@ -1,43 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2016, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests environment is correct.
20
-
21
- require 'openssl'
22
- require 'test/unit'
23
-
24
- class TestEnv < Test::Unit::TestCase
25
-
26
- # Output ruby interpreter version to the tests log.
27
- def test_ruby_version
28
- puts "\nRunning tests with %s-%s-%s.\n" %
29
- [RUBY_ENGINE, RUBY_VERSION, RUBY_PATCHLEVEL]
30
- end
31
-
32
- # Output SSL version to the tests log. There is a known issue with this
33
- # required property not defined in some JRuby implementations.
34
- def test_openssl_version
35
- puts "\nUsing OpenSSL %s with %d methods.\n" %
36
- [OpenSSL::OPENSSL_VERSION, OpenSSL::SSL::SSLContext::METHODS.count]
37
- end
38
-
39
- # Output rake version to the tests log.
40
- def test_rake_version
41
- puts "\nRunning with rake %s.\n" % Rake::VERSION if defined?(Rake)
42
- end
43
- end
@@ -1,84 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2015, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Test OAuth2 authentication.
20
-
21
- require 'time'
22
-
23
- require 'ads_common/auth/oauth2_handler'
24
- require 'ads_common/config'
25
- require 'webmock/test_unit'
26
-
27
- module AdsCommon
28
- module Auth
29
- class OAuth2Handler
30
- attr_reader :scopes
31
-
32
- def client()
33
- @client
34
- end
35
-
36
- # Overrides to ensure @client stores issued_at as a string, to test
37
- # converting it back.
38
- def setup_client()
39
- @client = Signet::OAuth2::Client.new({
40
- :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
41
- :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
42
- :client_id => 'client_id123',
43
- :client_secret => 'client_secret123',
44
- :scope => 'https://www.googleapis.com/auth/adwords'
45
- })
46
- end
47
- end
48
- end
49
- end
50
-
51
- class TestOAuth < Test::Unit::TestCase
52
- def setup()
53
- stub_request(:post, 'https://accounts.google.com/o/oauth2/auth').to_return(
54
- :status => 200,
55
- :body => '{"access_token":"access_token123",' +
56
- '"token_type":"Bearer","expires_in":"3600"}\n',
57
- :headers => {}
58
- )
59
- end
60
-
61
- def test_string_issued_at()
62
- handler = AdsCommon::Auth::OAuth2Handler.new(AdsCommon::Config.new(), nil)
63
-
64
- # Modify @client in the handler to get around a full setup.
65
- handler.setup_client()
66
- assert_not_nil(handler.client)
67
- handler.client.issued_at = Time.now.to_s
68
- assert_equal(Time, handler.client.issued_at.class)
69
-
70
- # Make sure that we are still able to refresh the token.
71
- assert_nothing_raised do
72
- handler.refresh_token!();
73
- end
74
- end
75
-
76
- def test_additional_scopes()
77
- config = AdsCommon::Config.new()
78
- config.set('authentication.oauth2_extra_scopes', ['extra-scope'])
79
- handler = AdsCommon::Auth::OAuth2Handler.new(config, 'base-scope')
80
- scopes = handler.scopes
81
- assert(scopes.include?('base-scope'), 'Missing base scope.')
82
- assert(scopes.include?('extra-scope'), 'Missing extra scope.')
83
- end
84
- end
@@ -1,61 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2016, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Test OAuth2 service account authentication.
20
-
21
- require 'tempfile'
22
-
23
- require 'ads_common/auth/oauth2_service_account_handler'
24
- require 'ads_common/config'
25
-
26
- module AdsCommon
27
- module Auth
28
- class OAuth2ServiceAccountHandler
29
- public :validate_credentials
30
- end
31
- end
32
- end
33
-
34
- class TestOAuthServiceAccount < Test::Unit::TestCase
35
- def test_file_extension_check()
36
- assert_nothing_raised do
37
- validate_credentials(['test', '.json'])
38
- end
39
- assert_raise(AdsCommon::Errors::AuthError) do
40
- validate_credentials(['test', '.p12'])
41
- end
42
- assert_raises(AdsCommon::Errors::AuthError) do
43
- validate_credentials(['test', '.other'])
44
- end
45
- end
46
-
47
- def validate_credentials(filename)
48
- file = Tempfile.new(filename)
49
- credentials = {
50
- :method => 'OAUTH2_SERVICE_ACCOUNT',
51
- :oauth2_keyfile => file.path,
52
- :oauth2_issuer => 'issuer',
53
- :oauth2_secret => 'secret'
54
- }
55
- handler = AdsCommon::Auth::OAuth2ServiceAccountHandler.new(
56
- AdsCommon::Config.new(), 'https://www.googleapis.com/auth/adwords')
57
- handler.validate_credentials(credentials)
58
- file.close
59
- file.unlink
60
- end
61
- end
@@ -1,153 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: utf-8
3
- #
4
- # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
5
- #
6
- # License:: Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15
- # implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
18
- #
19
- # Tests validator methods.
20
-
21
- require 'test/unit'
22
-
23
- require 'ads_common/errors'
24
- require 'ads_common/parameters_validator'
25
-
26
- module AdsCommon
27
- class ParametersValidator
28
- public :deep_copy, :add_attribute, :array_from_named_list
29
- public :check_required_argument_present, :arrayize
30
- end
31
- end
32
-
33
- class TestParametersValidator < Test::Unit::TestCase
34
- def setup
35
- @validator = AdsCommon::ParametersValidator.new(nil)
36
- end
37
-
38
- def test_deep_copy_simple
39
- result1 = @validator.deep_copy(42)
40
- assert_equal(42, result1)
41
-
42
- result2 = @validator.deep_copy('Hello World')
43
- assert_equal('Hello World', result2)
44
-
45
- result3 = @validator.deep_copy(nil)
46
- assert_nil(result3)
47
-
48
- result4 = @validator.deep_copy([])
49
- assert_equal([], result4)
50
- assert_not_same([], result4)
51
-
52
- result5 = @validator.deep_copy({})
53
- assert_equal({}, result5)
54
- assert_not_same({}, result5)
55
- end
56
-
57
- def test_deep_copy_complex
58
- data = {:ab => 'ab', :cd => ['cd', 'de', 'ef']}
59
-
60
- result1 = @validator.deep_copy(data)
61
- assert_equal(data, result1)
62
- assert_not_same(data, result1)
63
-
64
- result2 = @validator.deep_copy(data)
65
- assert_equal(result2, result1)
66
- assert_not_same(result2, result1)
67
-
68
- result2[:cd] = nil
69
- assert_not_equal(data, result2)
70
- assert_equal(data, result1)
71
- end
72
-
73
- def test_add_attribute
74
- node = {}
75
-
76
- key, name, value1, value2, value3 = 'key', 'name', 'Lorem', 'ipsum', 'dolor'
77
-
78
- @validator.add_attribute(node, key, name, value1)
79
- assert_kind_of(Hash, node)
80
- assert_kind_of(Hash, node[:attributes!])
81
- assert_kind_of(Hash, node[:attributes!][key])
82
- assert_equal(value1, node[:attributes!][key][name])
83
-
84
- @validator.add_attribute(node, key, name, value2)
85
- assert_kind_of(Hash, node)
86
- assert_kind_of(Hash, node[:attributes!])
87
- assert_kind_of(Hash, node[:attributes!][key])
88
- assert_kind_of(Array, node[:attributes!][key][name])
89
- assert_equal(value1, node[:attributes!][key][name][0])
90
- assert_equal(value2, node[:attributes!][key][name][1])
91
-
92
- @validator.add_attribute(node, key, name, value3)
93
- assert_equal(value1, node[:attributes!][key][name][0])
94
- assert_equal(value2, node[:attributes!][key][name][1])
95
- assert_equal(value3, node[:attributes!][key][name][2])
96
- end
97
-
98
- def test_array_from_named_list
99
- src = [{:name => 'foo'}, {:name => 'bar', :bar => :baz}, {:name => 'ipsum'}]
100
- result = @validator.array_from_named_list(src)
101
- assert_equal(['foo', 'bar', 'ipsum'], result)
102
- end
103
-
104
- def test_check_required_argument_present
105
- field1 = {:min_occurs => 1, :max_occurs => 1,
106
- :name => 'field1', :type => 'type1'}
107
- assert_raises(AdsCommon::Errors::MissingPropertyError) do
108
- @validator.check_required_argument_present(nil, field1)
109
- end
110
- assert_raises(AdsCommon::Errors::TypeMismatchError) do
111
- @validator.check_required_argument_present([], field1)
112
- end
113
- assert_nothing_raised do
114
- @validator.check_required_argument_present({}, field1)
115
- @validator.check_required_argument_present('foobar', field1)
116
- @validator.check_required_argument_present(42, field1)
117
- end
118
-
119
- field2 = {:min_occurs => 0, :max_occurs => :unbounded,
120
- :name => 'field2', :type => 'type2'}
121
- assert_raises(AdsCommon::Errors::TypeMismatchError) do
122
- @validator.check_required_argument_present({}, field2)
123
- end
124
- assert_nothing_raised do
125
- @validator.check_required_argument_present(nil, field2)
126
- @validator.check_required_argument_present([], field2)
127
- @validator.check_required_argument_present([field1, field2], field2)
128
- end
129
- end
130
-
131
- def test_arrayize_empty
132
- result1 = @validator.arrayize(nil)
133
- assert_instance_of(Array, result1, 'returned object is not an Array')
134
- assert_equal(0, result1.size, 'array is not empty')
135
-
136
- result2 = @validator.arrayize([])
137
- assert_instance_of(Array, result2, 'returned object is not an Array')
138
- assert_equal(0, result2.size, 'array is not empty')
139
- end
140
-
141
- def test_arrayize_on_array
142
- result1 = @validator.arrayize([nil])
143
- assert_instance_of(Array, result1, 'returned object is not an Array')
144
- assert_equal(1, result1.size, 'array changed size')
145
- assert_equal(nil, result1[0], 'array changed data')
146
-
147
- result2 = @validator.arrayize(['a', 'b'])
148
- assert_instance_of(Array, result2, 'returned object is not an Array')
149
- assert_equal(2, result2.size, 'array changed size')
150
- assert_equal('a', result2[0], 'array changed data')
151
- assert_equal('b', result2[1], 'array changed data')
152
- end
153
- end