-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathtest_oauth.py
More file actions
110 lines (90 loc) · 5.48 KB
/
test_oauth.py
File metadata and controls
110 lines (90 loc) · 5.48 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
# coding: utf-8
"""
DocuSign REST API
The DocuSign REST API provides you with a powerful, convenient, and simple Web services API for interacting with DocuSign.
OpenAPI spec version: v2
Generated by: https://github.com/swagger-api/swagger-codegen.git
"""
from __future__ import absolute_import
import base64
import os
import unittest
from dotenv import load_dotenv
from docusign_esign.client.api_client import ApiClient
from docusign_esign.client.auth.oauth import OAuthToken
load_dotenv()
class TestConfig(object):
def __init__(self, user_name=None, client_secret =None, user_id=None, integrator_key=None, host=None, recipient_email=None,
recipient_name=None, template_role_name=None, template_id=None, return_url=None, redirect_uri=None):
self.user_name = user_name if user_name else os.environ.get("USER_NAME")
self.client_secret = client_secret if client_secret else os.environ.get("CLIENT_SECRET")
self.integrator_key = integrator_key if integrator_key else os.environ.get("INTEGRATOR_KEY_JWT")
self.host = host if host else "https://demo.docusign.net/restapi"
self.recipient_email = recipient_email if recipient_email else os.environ.get("USER_NAME")
self.recipient_name = recipient_name if recipient_name else os.environ.get("USER_NAME")
self.template_role_name = template_role_name if template_role_name else os.environ.get("USER_NAME")
self.template_id = template_id if template_id else os.environ.get("TEMPLATE_ID")
self.return_url = return_url if return_url else os.environ.get("REDIRECT_URI")
self.user_id = user_id if user_id else os.environ.get("USER_ID")
self.redirect_uri = redirect_uri if redirect_uri else os.environ.get("REDIRECT_URI")
self.oauth_host_name = "account-d.docusign.com"
self.private_key_bytes = os.environ.get("PRIVATE_KEY")
self.expires_in = 3600
class TestOauth(unittest.TestCase):
""" AccountBillingPlan unit test stubs """
def setUp(self):
self.test_config = TestConfig()
self.api_client = ApiClient(oauth_host_name=self.test_config.oauth_host_name)
self.api_client.set_base_path("https://demo.docusign.net")
self.api_client.set_oauth_host_name(self.test_config.oauth_host_name)
def test_oauth_uri(self):
self.api_client.get_oauth_host_name()
uri = self.api_client.get_authorization_uri(client_id=self.test_config.integrator_key,
redirect_uri=self.test_config.redirect_uri,
scopes=["signature", "impersonation"],
response_type='code')
self.assertTrue(isinstance(uri, str))
self.api_client.rest_client.pool_manager.clear()
def test_jwt_application(self):
token_obj = self.api_client.request_jwt_application_token(client_id=self.test_config.integrator_key,
oauth_host_name=self.test_config.oauth_host_name,
private_key_bytes=self.test_config.private_key_bytes,
expires_in=self.test_config.expires_in)
self.assertTrue(isinstance(token_obj, OAuthToken))
self.api_client.rest_client.pool_manager.clear()
def test_jwt_user(self):
token_obj = self.api_client.request_jwt_user_token(client_id=self.test_config.integrator_key,
user_id=self.test_config.user_id,
oauth_host_name=self.api_client.get_oauth_host_name(),
private_key_bytes=self.test_config.private_key_bytes,
expires_in=self.test_config.expires_in
)
self.assertTrue(isinstance(token_obj, OAuthToken))
self.api_client.rest_client.pool_manager.clear()
def test_authorization_code_login(self):
self.api_client.get_oauth_host_name()
uri = self.api_client.get_authorization_uri(client_id=self.test_config.integrator_key,
redirect_uri=self.test_config.redirect_uri,
scopes=["signature"],
response_type='code')
self.assertTrue(isinstance(uri, str))
# import webbrowser
# from docusign_esign.client.auth.oauth.user_info import UserInfo
# webbrowser.open(uri)
#
# # IMPORTANT: after the login, DocuSign will send back a fresh
# # authorization code as a query param of the redirect URI.
# # You should set up a route that handles the redirect call to get
# # that code and pass it to token endpoint as shown in the next
# # lines:
# #
# code = "code"
# token_obj = self.api_client.generate_access_token(self.test_config.integrator_key,self.test_config.client_secret, code)
# self.assertTrue(isinstance(token_obj, OAuthToken))
#
# self.api_client.set_access_token(token_obj)
# user_info = self.api_client.get_user_info(token_obj.access_token)
# self.assertTrue(isinstance(user_info, UserInfo))
self.api_client.rest_client.pool_manager.clear()
if __name__ == '__main__':
unittest.main()