-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
229 lines (203 loc) · 6.71 KB
/
main.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import base64
import json
import os
import subprocess
import uuid
from typing import NamedTuple
class ApiResponse(NamedTuple):
body: dict
status_code: int
headers: dict
class ApiTester(object):
def __init__(
self,
container_bin: str,
sdk_language: str,
uploads_dir: str,
auth_type: str,
auth_key: str,
server: str,
):
self._container_bin = container_bin
self._sdk_language = sdk_language
self._uploads_dir = uploads_dir
self._auth_type = auth_type
self._auth_key = auth_key
self._server = server
def run(self, payload: dict) -> ApiResponse:
json_dump = json.dumps(payload)
base64_json = base64.b64encode(json_dump.encode('utf-8'))
base64_json_string = base64_json.decode('utf-8')
cmd = [
self._container_bin,
f'--sdk={self._sdk_language}',
f'--auth_type={self._auth_type}',
f'--auth_key={self._auth_key}',
f'--uploads_dir={self._uploads_dir}',
f'--server={self._server}',
f'--json={base64_json_string}',
]
response = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if response.returncode:
raise RuntimeError(
"Error running container return code:\n" +
response.stdout.decode('utf-8')
)
if response.stderr:
raise RuntimeError(
"Error running containe stderr:\n" +
response.stderr.decode('utf-8')
)
payload = json.loads(response.stdout.decode('utf-8'))
return ApiResponse(
body=payload['body'],
status_code=payload['status_code'],
headers=payload['headers'],
)
def test_create_account_success(tester: ApiTester):
email_address = f'signer{uuid.uuid4()}@hellosign.com'
json_data = {
"operationId": "accountCreate",
"parameters": {},
"data": {
"email_address": email_address,
},
"files": {},
}
response = tester.run(json_data)
print(f"\n\nResponse : test_create_account_success {response}")
assert response.status_code == 200
assert response.body['account']['email_address'] == email_address
def test_create_account_failure(tester: ApiTester):
email_address = '[email protected]'
json_data = {
"operationId": "accountCreate",
"parameters": {},
"data": {
"email_address": email_address,
},
"files": {},
}
response = tester.run(json_data)
print(f"\n\nResponse : test_create_account_failure {response}")
assert response.status_code == 400
assert 'email_address not valid' in response.body['error']['error_msg']
def test_signature_request_create_embedded(tester: ApiTester):
print(f"sdk langauage {sdk_language}")
print(f"url {server}")
json_data = {
"operationId": "signatureRequestCreateEmbedded",
"data": {
"allow_decline": True,
"allow_reassign": True,
"attachments": [
{
"name": "Attachment1",
"signer_index": 1,
"instructions": "Upload your Driver's License",
"required": True
}
],
"cc_email_addresses": [
],
"client_id": "efdbab0dd86acda1aaa3d56ea3ded8cf",
"custom_fields": [
{
"name": "Cost",
"value": "$20,000",
"editor": "0",
"required": True
}
],
"field_options": {
"date_format": "MM / DD / YYYY"
},
"form_fields_per_document": [
{
"document_index": 0,
"api_id": "uniqueIdHere_1",
"name": "",
"type": "text",
"x": 112,
"y": 328,
"width": 100,
"height": 16,
"required": True,
"signer": "0",
"page": 1,
"validation_type": "numbers_only",
}
],
"hide_text_tags": False,
"message": "Please sign this NDA and then we can discuss more. Let me know if you have any questions.",
"metadata": {
"field1": "value1"
},
"signers": [
{
"email_address": "[email protected]",
"name": "Jack",
"order": 0
},
{
"email_address": "[email protected]",
"name": "Jill",
"order": 1
}
],
"signing_options": {
"draw": True,
"type": True,
"upload": True,
"phone": False,
"default_type": "draw"
},
"subject": "The NDA we talked about",
"test_mode": True,
"title": "NDA with Acme Co.",
"use_text_tags": False
},
"files": {
"files": [
"pdf-sample.pdf"
]
},
"parameters": {}
}
response = tester.run(json_data)
print(f"\n\nResponse : test_signature_request_create_embedded {response.body}")
assert response.status_code == 200
if __name__ == '__main__':
dir_path = os.path.dirname(os.path.realpath(__file__))
print(f"dir path {dir_path}")
#
# Grab the following from config file, environment, or somewhere else
#
# One of "node", "php", "python", "ruby", "dotnet", "java"
sdk_language = os.environ['LANGUAGE']
# Uploads directory, containing PDFs you may want to upload to the API
uploads_dir = f'{dir_path}/../file_uploads'
# One of "apikey" or "oauth"
api_auth = 'apikey'
# The API key or OAuth bearer token to use for the request
api_key = os.environ['API_KEY']
# Change server, ie dev/qa/staging/prod
server = os.environ['SERVER']
container_bin = f'{dir_path}/../run'
tester = ApiTester(
container_bin,
sdk_language,
uploads_dir,
api_auth,
api_key,
server,
)
test_signature_request_create_embedded(tester)
test_create_account_success(tester)
test_create_account_failure(tester)