Repository: tough-dev-school/education-backend

Issue:
Выпилить интеграцию с mailjet (она давно не используется)



Discussion:

Resolving patch:
diff --git a/src/app/integrations/mailjet.py b/src/app/integrations/mailjet.py
deleted file mode 100644
--- a/src/app/integrations/mailjet.py
+++ /dev/null
@@ -1,38 +0,0 @@
-import mailjet_rest as mailjet
-from django.conf import settings
-from django.utils.functional import cached_property
-
-from users.models import User
-
-
-class AppMailjetWrongResponseException(Exception):
-    pass
-
-
-class AppMailjet:
-    def __init__(self):
-        pass
-
-    @cached_property
-    def client(self):
-        return mailjet.Client(
-            auth=(settings.MAILJET_API_KEY, settings.MAILJET_SECRET_KEY),
-            version='v3',
-        )
-
-    def subscribe(self, user: User):
-        response = self.client.contactslist_managecontact.create(
-            id=settings.MAILJET_CONTACT_LIST_ID,
-            data={
-                'Action': 'addnoforce',
-                'Email': user.email,
-                'Properties': {
-                    'name': str(user),
-                    'firstname': user.first_name,
-                    'lastname': user.last_name,
-                },
-            },
-        )
-
-        if response.status_code != 201:
-            raise AppMailjetWrongResponseException(f'Wrong response from mailjet: {response.status_code}. Content: {response.content}')
diff --git a/src/app/settings.py b/src/app/settings.py
--- a/src/app/settings.py
+++ b/src/app/settings.py
@@ -207,10 +207,6 @@
 
 EMAIL_BACKEND = env('EMAIL_BACKEND', default='django.core.mail.backends.console.EmailBackend')
 
-MAILJET_API_KEY = env('MAILJET_API_KEY', default='')
-MAILJET_SECRET_KEY = env('MAILJET_SECRET_KEY', default='')
-MAILJET_CONTACT_LIST_ID = env('MAILJET_CONTACT_LIST_ID', cast=int, default=None)
-
 MAILCHIMP_API_KEY = env('MAILCHIMP_API_KEY', default='')
 MAILCHIMP_CONTACT_LIST_ID = env('MAILCHIMP_CONTACT_LIST_ID', cast=str, default=None)
 
diff --git a/src/app/tasks.py b/src/app/tasks.py
--- a/src/app/tasks.py
+++ b/src/app/tasks.py
@@ -9,7 +9,6 @@
 from app.integrations import tg
 from app.integrations.clickmeeting import ClickMeetingClient, ClickMeetingHTTPException
 from app.integrations.mailchimp import AppMailchimp, MailchimpException
-from app.integrations.mailjet import AppMailjet, AppMailjetWrongResponseException
 from app.integrations.zoomus import ZoomusClient, ZoomusHTTPException
 from app.mail.owl import TemplOwl
 
@@ -57,23 +56,6 @@ def invite_to_zoomus(webinar_id: str, user_id: int):
     client.invite(webinar_id, user)
 
 
-@celery.task(
-    autoretry_for=[RequestException, AppMailjetWrongResponseException],
-    retry_kwargs={
-        'max_retries': 10,
-        'countdown': 5,
-    },
-)
-def subscribe_to_mailjet(user_id: int):
-    if not all(getattr(settings, x) for x in ['MAILJET_API_KEY', 'MAILJET_SECRET_KEY', 'MAILJET_CONTACT_LIST_ID']):
-        return
-
-    user = apps.get_model('users.User').objects.get(pk=user_id)
-    mailjet = AppMailjet()
-
-    mailjet.subscribe(user)
-
-
 @celery.task(
     autoretry_for=[RequestException, MailchimpException],
     retry_kwargs={


Test patch:
diff --git a/src/app/tests/mailjet/conftest.py b/src/app/tests/mailjet/conftest.py
deleted file mode 100644
--- a/src/app/tests/mailjet/conftest.py
+++ /dev/null
@@ -1,22 +0,0 @@
-import pytest
-
-from app.integrations.mailjet import AppMailjet
-
-pytestmark = [pytest.mark.django_db]
-
-
-@pytest.fixture(autouse=True)
-def set_mailjet_credentials(settings):
-    settings.MAILJET_API_KEY = 'key'
-    settings.MAILJET_SECRET_KEY = 'secret'
-    settings.MAILJET_CONTACT_LIST_ID = 100500
-
-
-@pytest.fixture
-def mailjet():
-    return AppMailjet()
-
-
-@pytest.fixture
-def user(mixer):
-    return mixer.blend('users.User', email='test@e.mail', first_name='Rulon', last_name='Oboev')
diff --git a/src/app/tests/mailjet/tests_mailjet_subscribing.py b/src/app/tests/mailjet/tests_mailjet_subscribing.py
deleted file mode 100644
--- a/src/app/tests/mailjet/tests_mailjet_subscribing.py
+++ /dev/null
@@ -1,43 +0,0 @@
-import pytest
-import requests_mock
-
-pytestmark = [pytest.mark.django_db]
-
-
-@pytest.fixture
-def mailjet(mailjet):
-    with requests_mock.Mocker() as m:
-        mailjet.http_mock = m
-
-        yield mailjet
-
-
-def test_ok(mailjet, user):
-    def assertion(req, res):
-        query = req.json()
-
-        assert query['Email'] == 'test@e.mail'
-        assert query['Properties']['firstname'] == 'Rulon'
-        assert query['Properties']['lastname'] == 'Oboev'
-        assert query['Properties']['name'] == 'Rulon Oboev'
-
-        res.status_code = 201
-        return {'ok': 'mocked'}
-
-    mailjet.http_mock.post('https://api.eu.mailjet.com/v3/REST/contactslist/100500/managecontact', json=assertion)
-
-    mailjet.subscribe(user)
-
-
-@pytest.mark.xfail(reason='Just to check if above works')
-def test_fail(mailjet, user):
-    def assertion(req, res):
-        query = req.json()
-
-        assert query['Email'] == 'WRONG!1'
-
-        res.status_code = 201
-        return {'ok': 'mocked'}
-
-    mailjet.http_mock.post('https://api.eu.mailjet.com/v3/REST/contactslist/100500/managecontact', json=assertion)
-    mailjet.subscribe(user)
diff --git a/src/app/tests/mailjet/tests_mailjet_subscribing_task.py b/src/app/tests/mailjet/tests_mailjet_subscribing_task.py
deleted file mode 100644
--- a/src/app/tests/mailjet/tests_mailjet_subscribing_task.py
+++ /dev/null
@@ -1,29 +0,0 @@
-import pytest
-
-from app.tasks import subscribe_to_mailjet
-
-pytestmark = [pytest.mark.django_db]
-
-
-@pytest.fixture(autouse=True)
-def subscribe(mocker):
-    return mocker.patch('app.integrations.mailjet.AppMailjet.subscribe')
-
-
-def test(subscribe, user):
-    subscribe_to_mailjet(user.id)
-
-    subscribe.assert_called_once_with(user)
-
-
-@pytest.mark.parametrize('setting', [
-    'MAILJET_API_KEY',
-    'MAILJET_SECRET_KEY',
-    'MAILJET_CONTACT_LIST_ID',
-])
-def test_subscription_is_disabled_if_any_single_setting_is_disabled(subscribe, user, setting, settings):
-    setattr(settings, setting, '')
-
-    subscribe_to_mailjet(user.id)
-
-    subscribe.assert_not_called()