Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Add support to webhook subscription API (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
anapaulagomes authored and anneFly committed Feb 15, 2019
1 parent 9094a97 commit a90a245
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 3 deletions.
35 changes: 35 additions & 0 deletions closeio/closeio.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,38 @@ def get_event_logs(self, **kwargs):
self._api.event.get,
**kwargs
)

@parse_response
@handle_errors
def get_webhooks(self):
return paginate(
self._api.webhook.get,
)

@parse_response
@handle_errors
def get_webhook(self, webhook_id):
return self._api.webhook(webhook_id).get()

@parse_response
@handle_errors
def create_webhook(self, data):
return self._api.webhook.post(data)

@parse_response
@handle_errors
def update_webhook(self, webhook_id, data):
"""
Update a webhook status.
:param webhook_id:
:param data (dict): { status: active or paused }
:return:
"""
status = convert(data)
return self._api.webhook(webhook_id).put(status)

@parse_response
@handle_errors
def delete_webhook(self, webhook_id):
return self._api.webhook(webhook_id).delete()
50 changes: 50 additions & 0 deletions closeio/contrib/testing_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,53 @@ def api_key(self):
'key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
}],
})

@parse_response
def get_webhooks(self):
return self._data('webhooks', [])

@parse_response
def get_webhook(self, webhook_id):
webhooks = self._data('webhooks', [])

for webhook in webhooks:
if webhook['id'] == webhook_id:
return webhook

raise CloseIOError()

@parse_response
def create_webhook(self, data):
webhooks = self._data('webhooks', [])

new_webhook = {
'status': 'active',
'updated_by': 'user_{}'.format(uuid.uuid4().hex),
'url': data['url'],
'created_by': 'user_{}'.format(uuid.uuid4().hex),
'signature_key': 'xxxxxx',
'date_created': datetime.utcnow().isoformat(),
'date_updated': datetime.utcnow().isoformat(),
'verify_ssl': True,
'events': data['events'],
'id': 'whsub_{}'.format(uuid.uuid4().hex)
}
webhooks.append(new_webhook)
return new_webhook

@parse_response
def update_webhook(self, webhook_id, status):
webhook = self.get_webhook(webhook_id)
webhook['status'] = status['status']
return webhook

@parse_response
def delete_webhook(self, webhook_id):
webhooks = self._data('webhooks', [])

for index, webhook in enumerate(webhooks):
if webhook['id'] == webhook_id:
webhooks.pop(index)
return True
else:
raise CloseIOError()
77 changes: 74 additions & 3 deletions tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ def wait_for_index(lead_id):
def export(self, client, random_string):
return client.create_lead_export(random_string)

@pytest.fixture
def webhook(self, client):
data = {
'url': 'https://example.thermondo.de/notes/closeio/',
'events': [{
'object_type': 'activity.note',
'action': 'created'
}]
}
new_webhook = client.create_webhook(data)

yield new_webhook

client.delete_webhook(new_webhook['id'])

def test_create_lead(self, client):
with open(os.path.join(FIXTURE_DIR, 'lead.json')) as f:
lead = utils.parse(json.load(f))
Expand All @@ -103,7 +118,7 @@ def test_get_leads(self, client, lead):

def test_update_lead(self, client, lead):
fields = {
"description": "Best show ever canceled. Sad."
'description': 'Best show ever canceled. Sad.'
}
response = client.update_lead(lead['id'], fields)
assert fields['description'] == response['description']
Expand All @@ -125,7 +140,7 @@ def test_create_task(self, client, lead, _date):
@pytest.mark.flaky(reruns=3)
def test_update_task(self, client, task):
fields = {
"is_complete": True
'is_complete': True
}
response = client.update_task(task['id'], fields)
assert response['is_complete'], dict(response)
Expand Down Expand Up @@ -153,7 +168,7 @@ def test_create_opportunity(self, client, lead):

def test_update_opportunity(self, client, opportunity):
fields = {
"confidence": 75
'confidence': 75
}
response = client.update_opportunity(opportunity['id'], fields)
assert response['confidence'] == 75, dict(response)
Expand Down Expand Up @@ -246,3 +261,59 @@ def test_get_export(self, client, export):

def test_api_key(self, client):
assert client.api_key()

def test_get_webhooks(self, client, webhook):
existent_webhooks = next(client.get_webhooks())

assert existent_webhooks['id']
assert existent_webhooks['status']
assert existent_webhooks['url']
assert existent_webhooks['events']

def test_create_webhook(self, client):
data = {
'url': 'https://example.thermondo.de/notes/closeio/',
'events': [{
'object_type': 'activity.note',
'action': 'created'
}]
}
new_webhook = client.create_webhook(data)

assert new_webhook['id']
assert new_webhook['status']
assert new_webhook['url']
assert new_webhook['events']

client.delete_webhook(new_webhook['id'])

def test_get_webhook(self, client, webhook):
retrieved_webhook = client.get_webhook(webhook['id'])

assert retrieved_webhook['id']
assert retrieved_webhook['status']
assert retrieved_webhook['url']
assert retrieved_webhook['events']

def test_update_webhook(self, client, webhook):
data = {'status': 'paused'}
updated_webhook = client.update_webhook(webhook['id'], data)

assert updated_webhook['id']
assert updated_webhook['status']
assert updated_webhook['url']
assert updated_webhook['events']

def test_delete_webhook(self, client):
data = {
'url': 'https://example.thermondo.de/notes/closeio/',
'events': [{
'object_type': 'activity.note',
'action': 'created'
}]
}
new_webhook = client.create_webhook(data)

deleted_webhook = client.delete_webhook(new_webhook['id'])

assert isinstance(deleted_webhook, bool)
84 changes: 84 additions & 0 deletions tests/test_testing_stub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime

import pytest

from closeio import CloseIOError
from closeio.contrib.testing_stub import CloseIOStub


Expand Down Expand Up @@ -91,3 +94,84 @@ def test_create_activity_email(self):
}
response = client.create_activity_email(**kwargs)
assert response['id'].startswith('acti_')


class TestWebhooksSubscription:
def test_get_webhooks(self):
client = CloseIOStub()

response = client.get_webhooks()

assert response == []

data = {
'url': 'aweso.me',
'events': [
{
'object_type': 'activity.note',
'action': 'created'
}
]
}
client.create_webhook(data)

response = client.get_webhooks()

assert len(response) == 1
assert response[0]['id']
assert response[0]['status']
assert response[0]['url']
assert response[0]['events']

def test_create_webhook(self):
client = CloseIOStub()

data = {
'url': 'aweso.me',
'events': [
{
'object_type': 'activity.note',
'action': 'created'
}
]
}
new_webhook = client.create_webhook(data)
assert new_webhook['id']
assert new_webhook['status']
assert new_webhook['url']
assert new_webhook['events']

def test_update_webhook(self):
client = CloseIOStub()

data = {
'url': 'aweso.me',
'events': [
{
'object_type': 'activity.note',
'action': 'created'
}
]
}
new_webhook = client.create_webhook(data)

data = {'status': 'paused'}
updated_webhook = client.update_webhook(new_webhook['id'], data)

assert updated_webhook['status'] == data['status']

def test_delete_webhook(self):
client = CloseIOStub()
data = {
'url': 'https://example.com/notes/closeio/',
'events': [{
'object_type': 'activity.note',
'action': 'created'
}]

}
new_webhook = client.create_webhook(data)

assert client.delete_webhook(new_webhook['id']) is True
with pytest.raises(CloseIOError):
client.get_webhook(new_webhook['id'])

0 comments on commit a90a245

Please sign in to comment.