forked from googleads/google-ads-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth2_test.py
More file actions
117 lines (102 loc) · 4.23 KB
/
oauth2_test.py
File metadata and controls
117 lines (102 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the OAuth2 helper module."""
import mock
from unittest import TestCase
from google.ads.google_ads import oauth2
class OAuth2Tests(TestCase):
def setUp(self):
self.client_id = 'client_id_123456789'
self.client_secret = 'client_secret_987654321'
self.refresh_token = 'refresh'
self.path_to_private_key_file = '/path/to/file'
self.token_uri = oauth2._DEFAULT_TOKEN_URI
self.scopes = oauth2._SERVICE_ACCOUNT_SCOPES
def test_get_installed_app_credentials(self):
mock_credentials = mock.Mock()
mock_request = mock.Mock()
with mock.patch.object(
oauth2,
'InstalledAppCredentials',
return_value=mock_credentials
) as mock_initializer, mock.patch.object(
oauth2,
'Request',
return_value = mock_request
) as mock_request_class:
result = oauth2.get_installed_app_credentials(
self.client_id, self.client_secret, self.refresh_token)
mock_initializer.assert_called_once_with(
None,
client_id=self.client_id,
client_secret=self.client_secret,
refresh_token=self.refresh_token,
token_uri=self.token_uri)
mock_request_class.assert_called_once()
result.refresh.assert_called_once_with(mock_request)
def test_get_service_account_credentials(self):
mock_credentials = mock.Mock()
mock_request = mock.Mock()
with mock.patch.object(
oauth2.ServiceAccountCreds,
'from_service_account_file',
return_value=mock_credentials
) as mock_initializer, mock.patch.object(
oauth2,
'Request',
return_value = mock_request
) as mock_request_class:
result = oauth2.get_service_account_credentials(
self.path_to_private_key_file, self.subject)
mock_initializer.assert_called_once_with(
self.path_to_private_key_file,
subject=self.subject,
scopes=self.scopes)
mock_request_class.assert_called_once()
result.refresh.assert_called_once_with(mock_request)
def test_get_credentials_installed_application(self):
mock_config = {
'client_id': self.client_id,
'client_secret': self.client_secret,
'refresh_token': self.refresh_token}
with mock.patch.object(
oauth2,
'get_installed_app_credentials',
return_value=None
) as mock_initializer:
oauth2.get_credentials(mock_config)
mock_initializer.assert_called_once_with(
self.client_id, self.client_secret, self.refresh_token)
def test_get_credentials_installed_application_bad_config(self):
# using a config that is missing the refresh_token key
mock_config = {
'client_id': self.client_id,
'client_secret': self.client_secret}
self.assertRaises(
ValueError,
oauth2.get_credentials,
mock_config)
def test_get_credentials_installed_application(self):
mock_config = {
'path_to_private_key_file': self.path_to_private_key_file,
'delegated_account': self.subject}
with mock.patch.object(
oauth2,
'get_service_account_credentials',
return_value=None
) as mock_initializer:
oauth2.get_credentials(mock_config)
mock_initializer.assert_called_once_with(
self.path_to_private_key_file, self.subject)