mirror of
https://github.com/google/earthengine-api.git
synced 2025-12-08 19:26:12 +00:00
59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
#!/usr/bin/env python
|
|
"""Test for the ee.oauth module."""
|
|
|
|
|
|
import json
|
|
from unittest import mock
|
|
from six.moves.urllib import parse
|
|
|
|
import tempfile
|
|
import unittest
|
|
|
|
import ee
|
|
|
|
|
|
class OAuthTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.test_tmpdir = tempfile.mkdtemp()
|
|
|
|
def testRequestToken(self):
|
|
|
|
class MockResponse(object):
|
|
|
|
def __init__(self, code):
|
|
self.code = code.decode()
|
|
|
|
def read(self):
|
|
return ('{"refresh_token": "' + self.code + '456"}').encode()
|
|
|
|
def mock_urlopen(unused_url, param):
|
|
parsed = parse.parse_qs(param)
|
|
self.assertEqual('xyz', parsed[b'code_verifier'][0].decode())
|
|
return MockResponse(parsed[b'code'][0])
|
|
|
|
with mock.patch('six.moves.urllib.request.urlopen', new=mock_urlopen):
|
|
auth_code = '123'
|
|
verifier = 'xyz'
|
|
refresh_token = ee.oauth.request_token(auth_code, verifier)
|
|
self.assertEqual('123456', refresh_token)
|
|
|
|
def testWriteToken(self):
|
|
|
|
def mock_credentials_path():
|
|
return self.test_tmpdir+'/tempfile'
|
|
|
|
oauth_pkg = 'ee.oauth'
|
|
with mock.patch(oauth_pkg+'.get_credentials_path',
|
|
new=mock_credentials_path):
|
|
client_info = dict(refresh_token='123')
|
|
ee.oauth.write_private_json(ee.oauth.get_credentials_path(), client_info)
|
|
|
|
with open(mock_credentials_path()) as f:
|
|
token = json.load(f)
|
|
self.assertEqual({'refresh_token': '123'}, token)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|