Skip to content

Commit

Permalink
Updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Aug 16, 2021
1 parent bb29a38 commit 0e3ff61
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
8 changes: 4 additions & 4 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class CommunityBaseSettings(Settings):
SERVER_EMAIL = DEFAULT_FROM_EMAIL
SUPPORT_EMAIL = None
SUPPORT_FORM_ENDPOINT = None
FRONT_TOKEN = None
FRONT_API_SECRET = None
FRONTAPP_TOKEN = None
FRONTAPP_API_SECRET = None

# Sessions
SESSION_COOKIE_DOMAIN = 'readthedocs.org'
Expand All @@ -80,7 +80,7 @@ class CommunityBaseSettings(Settings):
def SESSION_COOKIE_SAMESITE(self):
"""
Cookie used in cross-origin API requests from *.rtd.io to rtd.org/api/v2/sustainability/.
"""
"""
if self.USE_PROMOS:
return None
# This is django's default.
Expand Down Expand Up @@ -625,7 +625,7 @@ def DOCKER_LIMITS(self):
DEFAULT_VERSION_PRIVACY_LEVEL = 'public'
GROK_API_HOST = 'https://api.grokthedocs.com'
ALLOW_ADMIN = True
ADMIN_URL = None
ADMIN_URL = 'https://admin.ops.readthedocs.org:10000/admin'
RTD_ALLOW_ORGANIZATIONS = False

# Elasticsearch settings.
Expand Down
31 changes: 0 additions & 31 deletions readthedocs/support/front.py

This file was deleted.

16 changes: 8 additions & 8 deletions readthedocs/support/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
from django.urls import reverse
from django_dynamic_fixture import get

from readthedocs.support.views import FrontWebhookBase
from readthedocs.support.views import FrontAppWebhookBase


@override_settings(
ADMIN_URL='https://readthedocs.org/admin',
FRONT_API_SECRET='1234',
FRONT_TOKEN='1234',
FRONTAPP_API_SECRET='1234',
FRONTAPP_TOKEN='1234',
)
class TestFrontWebhook(TestCase):
class TestFrontAppWebhook(TestCase):

def setUp(self):
self.user = get(User, email='test@example.com', username='test')
self.url = reverse('front_webhook')
self.url = reverse('frontapp_webhook')

def test_invalid_payload(self):
resp = self.client.post(
Expand All @@ -29,7 +29,7 @@ def test_invalid_payload(self):
self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.data['detail'], 'Invalid payload')

@mock.patch.object(FrontWebhookBase, '_is_payload_valid')
@mock.patch.object(FrontAppWebhookBase, '_is_payload_valid')
def test_invalid_event(self, is_payload_valid):
is_payload_valid.return_value = True
resp = self.client.post(
Expand All @@ -41,7 +41,7 @@ def test_invalid_event(self, is_payload_valid):
self.assertEqual(resp.data['detail'], 'Skipping outbound event')

@requests_mock.Mocker(kw='mock_request')
@mock.patch.object(FrontWebhookBase, '_is_payload_valid')
@mock.patch.object(FrontAppWebhookBase, '_is_payload_valid')
def test_inbound_event(self, is_payload_valid, mock_request):
is_payload_valid.return_value = True
self._mock_request(mock_request)
Expand All @@ -63,7 +63,7 @@ def test_inbound_event(self, is_payload_valid, mock_request):
self.assertEqual(custom_fields[field], 'Do not change this')

@requests_mock.Mocker(kw='mock_request')
@mock.patch.object(FrontWebhookBase, '_is_payload_valid')
@mock.patch.object(FrontAppWebhookBase, '_is_payload_valid')
def test_inbound_event_unknow_email(self, is_payload_valid, mock_request):
self.user.email = 'unknown@example.com'
self.user.save()
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/support/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url

from readthedocs.support.views import FrontWebhook
from readthedocs.support.views import FrontAppWebhook

urlpatterns = [
url(r'^front-webhook/$', FrontWebhook.as_view(), name='front_webhook'),
url(r'^frontapp-webhook/$', FrontAppWebhook.as_view(), name='frontapp_webhook'),
]
47 changes: 39 additions & 8 deletions readthedocs/support/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hmac
import logging

import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models import Q
Expand All @@ -16,12 +17,38 @@
from rest_framework.views import APIView

from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.support.front import FrontClient

log = logging.getLogger(__name__)


class FrontWebhookBase(APIView):
class FrontAppClient:

"""Wrapper around Front's API."""

BASE_URL = 'https://api2.frontapp.com'

def __init__(self, token):
self.token = token

@property
def _headers(self):
headers = {
"Authorization": f"Bearer {self.token}",
}
return headers

def _get_url(self, path):
return f'{self.BASE_URL}{path}'

def get(self, path, **kwargs):
kwargs.setdefault('headers', {}).update(self._headers)
return requests.get(self._get_url(path), **kwargs)

def patch(self, path, **kwargs):
kwargs.setdefault('headers', {}).update(self._headers)
return requests.patch(self._get_url(path), **kwargs)

class FrontAppWebhookBase(APIView):

"""
Front's webhook handler.
Expand Down Expand Up @@ -51,9 +78,11 @@ def _update_contact_information(self, data):
Update contact information using Front's API.
The webhook event give us the conversation_id,
we use that to
we use that to retrieve the email from the user that originated
the conversation, and finally we use the email to update
the contact information (three API requests!).
"""
client = FrontClient(token=settings.FRONT_TOKEN)
client = FrontAppClient(token=settings.FRONTAPP_TOKEN)

# Retrieve the user from the email from the conversation.
conversation_id = data.get('conversation', {}).get('id')
Expand All @@ -71,7 +100,9 @@ def _update_contact_information(self, data):
.first()
)
if not user:
return Response({'detail': f'User with email {email} not found in our database'})
msg = f'User with email {email} not found in our database'
log.info(msg)
return Response({'detail': msg})

# Get current custom fields, and update them.
try:
Expand Down Expand Up @@ -131,7 +162,7 @@ def _is_payload_valid(self):

def _get_digest(self):
"""Get a HMAC digest of the request using Front's API secret."""
secret = settings.FRONT_API_SECRET
secret = settings.FRONTAPP_API_SECRET
digest = hmac.new(
secret.encode(),
msg=self.request.body,
Expand All @@ -140,5 +171,5 @@ def _get_digest(self):
return base64.b64encode(digest.digest())


class FrontWebhook(SettingsOverrideObject):
_default_class = FrontWebhookBase
class FrontAppWebhook(SettingsOverrideObject):
_default_class = FrontAppWebhookBase

0 comments on commit 0e3ff61

Please sign in to comment.