Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create webhooks #42

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ about future releases, check `milestones`_ and :doc:`/about/vision`.
0.14 (unreleased)
-----------------

- Nothing changed yet.
- Create adobesign webhooks


0.13 (2023-09-29)
Expand Down
13 changes: 10 additions & 3 deletions django_adobesign/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ def get_adobesign_participants(self, signature):
jsonified_signers.append(signer)
return jsonified_signers

def create_signature(self, signature, post_sign_redirect_url=None,
post_sign_redirect_delay=0,
send_mail=True, **extra_data):
def create_signature(self, signature, webhook_handler_url,
post_sign_redirect_url=None,
post_sign_redirect_delay=0, send_mail=True,
**extra_data):
"""Register ``signature`` in AdobeSign service, return updated object.
This method calls ``save()`` on ``signature`` and ``signer``.

Expand All @@ -62,6 +63,12 @@ def create_signature(self, signature, post_sign_redirect_url=None,

# Update signers instance with external id
self.map_adobe_signer_to_signer(signature)

# Create webhook for the new agreement
self.adobesign_client.post_webhooks(
agreement_id=signature.signature_backend_id,
webhook_handler_url=webhook_handler_url,
)
return signature

def map_adobe_signer_to_signer(self, signature):
Expand Down
23 changes: 23 additions & 0 deletions django_adobesign/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,26 @@ def get_events(self, agreement_id):
def rebuild_with_token(self, access_token):
return AdobeSignClient(self.root_url, access_token, self.api_user,
self.on_behalf_of_user)

@handle_adobe_exception
def post_webhooks(self, agreement_id, webhook_handler_url):
"""
Create a a new webhook
"""
url = self.build_url('webhooks')
data = {
"name": "APIv2 Agreement webhook",
"scope": "RESOURCE",
"state": "ACTIVE",
"resourceType": "AGREEMENT",
"resourceId": agreement_id,
"webhookSubscriptionEvents": ["AGREEMENT_ALL"],
"webhookUrlInfo": {"url": webhook_handler_url},
}
response = requests.post(
url,
headers=self.get_headers(),
json=data, timeout=self.timeout
)
response.raise_for_status()
return response.json()
11 changes: 10 additions & 1 deletion django_adobesign/tests/test_adobe_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def test_create_signature(mocker, minimal_signature, adobe_sign_backend):
'id': 'barid',
'status': 'CANCELLED',
'order': 1}]})

mocked_post_webhooks = mocker.patch.object(
AdobeSignClient, 'post_webhooks')

# Signers
signer1 = Signer(full_name='Poney poney', email='poney@plop.com',
Expand All @@ -99,7 +102,8 @@ def test_create_signature(mocker, minimal_signature, adobe_sign_backend):

assert minimal_signature.signature_backend_id == ""

adobe_sign_backend.create_signature(minimal_signature)
adobe_sign_backend.create_signature(
minimal_signature, 'https://test.com/handler')

minimal_signature.refresh_from_db()
assert minimal_signature.signature_backend_id == 'test_agreement_id'
Expand All @@ -109,6 +113,11 @@ def test_create_signature(mocker, minimal_signature, adobe_sign_backend):
assert signer1.signature_backend_id == "barid"
assert signer2.signature_backend_id == "fooid"

mocked_post_webhooks.assert_called_once_with(
agreement_id=minimal_signature.signature_backend_id,
webhook_handler_url='https://test.com/handler',
)


def test_get_next_signer_urls(mocker, adobe_sign_backend):
mocker.patch.object(AdobeSignClient, 'get_signing_url',
Expand Down
26 changes: 26 additions & 0 deletions django_adobesign/tests/test_adobe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,29 @@ def test_update_signer_client_or_server_error(

assert expected_json_reply_error['code'] in str(e.value)
assert expected_json_reply_error['message'] in str(e.value)


def test_post_webhooks(mocker, adobe_sign_client, expected_headers):
mocked_post = mocker.patch('requests.post')
adobe_sign_client.post_webhooks(
agreement_id='test-id',
webhook_handler_url='https://test.com/handler'
)

mandatory_parameters = mocked_post.call_args[0]
assert mandatory_parameters == ('http://test/api/rest/v6/webhooks',)

kwargs_parameters = mocked_post.call_args[1]
assert kwargs_parameters == {
'headers': expected_headers,
'json': {
'name': 'APIv2 Agreement webhook',
'scope': 'RESOURCE',
'state': 'ACTIVE',
'resourceType': 'AGREEMENT',
'resourceId': 'test-id',
'webhookSubscriptionEvents': ['AGREEMENT_ALL'],
'webhookUrlInfo': {'url': 'https://test.com/handler'}
},
'timeout': 15
}
Loading