Changeset View
Changeset View
Standalone View
Standalone View
profiles/tests/test_webhooks.py
| from typing import Dict, Union | from typing import Dict, Union | ||||
| from unittest.mock import patch | from unittest.mock import patch, Mock | ||||
| import hashlib | import hashlib | ||||
| import hmac | import hmac | ||||
| import json | import json | ||||
| import responses | import responses | ||||
| from django.contrib.auth.models import User, Group | from django.contrib.auth.models import User, Group | ||||
| from django.test import TestCase, override_settings | from django.test import TestCase, override_settings | ||||
| from django.urls import reverse | from django.urls import reverse | ||||
| Show All 17 Lines | |||||
| @override_settings( | @override_settings( | ||||
| BLENDER_ID={ | BLENDER_ID={ | ||||
| 'BASE_URL': BLENDER_ID_BASE_URL, | 'BASE_URL': BLENDER_ID_BASE_URL, | ||||
| 'OAUTH_CLIENT': 'testoauthclient', | 'OAUTH_CLIENT': 'testoauthclient', | ||||
| 'OAUTH_SECRET': 'testoathsecret', | 'OAUTH_SECRET': 'testoathsecret', | ||||
| 'WEBHOOK_USER_MODIFIED_SECRET': b'testsecret', | 'WEBHOOK_USER_MODIFIED_SECRET': b'testsecret', | ||||
| } | } | ||||
| ) | ) | ||||
| @patch('storages.backends.s3boto3.S3Boto3Storage.url', Mock(return_value='s3://file')) | |||||
| class WebhooksTest(TestCase): | class WebhooksTest(TestCase): | ||||
| webhook_payload = { | webhook_payload = { | ||||
| 'avatar_changed': False, | 'avatar_changed': False, | ||||
| 'email': 'newmail@example.com', | 'email': 'newmail@example.com', | ||||
| 'full_name': 'Иван Васильевич Doe', | 'full_name': 'Иван Васильевич Doe', | ||||
| 'id': 2, | 'id': 2, | ||||
| 'old_email': 'mail@example.com', | 'old_email': 'mail@example.com', | ||||
| 'roles': [], | 'roles': [], | ||||
| ▲ Show 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | def test_user_modified_logs_errors_when_blender_id_user_info_broken(self): | ||||
| with self.assertLogs('profiles.views.webhooks', level='ERROR') as logs: | with self.assertLogs('profiles.views.webhooks', level='ERROR') as logs: | ||||
| response = self.client.post( | response = self.client.post( | ||||
| self.url, body, content_type='application/json', **prepare_hmac_header(body) | self.url, body, content_type='application/json', **prepare_hmac_header(body) | ||||
| ) | ) | ||||
| self.assertRegex(logs.output[0], 'Unable to update username for Profile') | self.assertRegex(logs.output[0], 'Unable to update username for Profile') | ||||
| self.assertEquals(response.status_code, 204) | self.assertEquals(response.status_code, 204) | ||||
| @responses.activate | |||||
| def test_user_modified_avatar_changed(self): | |||||
| body = { | |||||
| **self.webhook_payload, | |||||
| 'avatar_changed': True, | |||||
| } | |||||
| with self.assertLogs('profiles.models', level='INFO') as logs: | |||||
| response = self.client.post( | |||||
| self.url, body, content_type='application/json', **prepare_hmac_header(body) | |||||
| ) | |||||
| self.assertRegex(logs.output[0], 'Profile image updated for Profile ⅉanedoe') | |||||
| self.assertEquals(response.status_code, 204) | |||||
| self.assertEquals(response.content, b'') | |||||
| profile = Profile.objects.get(user_id=self.user.pk) | |||||
| self.assertTrue(profile.image_url, 's3://file') | |||||