Skip to content

Commit

Permalink
Add USE_ORGANIZATIONS context variablea (#10592)
Browse files Browse the repository at this point in the history
* Add USE_ORGANIZATIONS context variable

* Format

* Make prospector happy

* Fix test fixture data

* Black on all of test_notifications
  • Loading branch information
agjohnson authored Aug 2, 2023
1 parent 7d65c16 commit 689e5c6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 98 deletions.
23 changes: 11 additions & 12 deletions readthedocs/core/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@

"""Template context processors for core app."""

from django.conf import settings


def readthedocs_processor(request):
# pylint: disable=unused-argument
exports = {
'PUBLIC_DOMAIN': settings.PUBLIC_DOMAIN,
'PRODUCTION_DOMAIN': settings.PRODUCTION_DOMAIN,
'USE_SUBDOMAIN': settings.USE_SUBDOMAIN,
'GLOBAL_ANALYTICS_CODE': settings.GLOBAL_ANALYTICS_CODE,
'DASHBOARD_ANALYTICS_CODE': settings.DASHBOARD_ANALYTICS_CODE,
'SITE_ROOT': settings.SITE_ROOT + '/',
'TEMPLATE_ROOT': settings.TEMPLATE_ROOT + '/',
'DO_NOT_TRACK_ENABLED': settings.DO_NOT_TRACK_ENABLED,
'USE_PROMOS': settings.USE_PROMOS,
'SUPPORT_EMAIL': settings.SUPPORT_EMAIL,
"PUBLIC_DOMAIN": settings.PUBLIC_DOMAIN,
"PRODUCTION_DOMAIN": settings.PRODUCTION_DOMAIN,
"USE_SUBDOMAIN": settings.USE_SUBDOMAIN,
"GLOBAL_ANALYTICS_CODE": settings.GLOBAL_ANALYTICS_CODE,
"DASHBOARD_ANALYTICS_CODE": settings.DASHBOARD_ANALYTICS_CODE,
"SITE_ROOT": settings.SITE_ROOT + "/",
"TEMPLATE_ROOT": settings.TEMPLATE_ROOT + "/",
"DO_NOT_TRACK_ENABLED": settings.DO_NOT_TRACK_ENABLED,
"USE_PROMOS": settings.USE_PROMOS,
"USE_ORGANIZATIONS": settings.RTD_ALLOW_ORGANIZATIONS,
"SUPPORT_EMAIL": settings.SUPPORT_EMAIL,
}
return exports
172 changes: 86 additions & 86 deletions readthedocs/rtd_tests/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,78 @@

@override_settings(
NOTIFICATION_BACKENDS=[
'readthedocs.notifications.backends.EmailBackend',
'readthedocs.notifications.backends.SiteBackend',
"readthedocs.notifications.backends.EmailBackend",
"readthedocs.notifications.backends.SiteBackend",
],
PRODUCTION_DOMAIN='readthedocs.org',
SUPPORT_EMAIL='[email protected]',
PRODUCTION_DOMAIN="readthedocs.org",
SUPPORT_EMAIL="[email protected]",
)
@mock.patch('readthedocs.notifications.notification.render_to_string')
@mock.patch.object(Notification, 'send')
@mock.patch("readthedocs.notifications.notification.render_to_string")
@mock.patch.object(Notification, "send")
class NotificationTests(TestCase):

def test_notification_custom(self, send, render_to_string):
render_to_string.return_value = 'Test'
render_to_string.return_value = "Test"

class TestNotification(Notification):
name = 'foo'
subject = 'This is {{ foo.id }}'
context_object_name = 'foo'
name = "foo"
subject = "This is {{ foo.id }}"
context_object_name = "foo"

build = fixture.get(Build)
req = mock.MagicMock()
notify = TestNotification(context_object=build, request=req)

self.assertEqual(
notify.get_template_names('email'),
['builds/notifications/foo_email.html'],
notify.get_template_names("email"),
["builds/notifications/foo_email.html"],
)
self.assertEqual(
notify.get_template_names('site'),
['builds/notifications/foo_site.html'],
notify.get_template_names("site"),
["builds/notifications/foo_site.html"],
)
self.assertEqual(
notify.get_subject(),
'This is {}'.format(build.id),
"This is {}".format(build.id),
)
self.assertEqual(
notify.get_context_data(),
{
'foo': build,
'production_uri': 'https://readthedocs.org',
'request': req,

"foo": build,
"production_uri": "https://readthedocs.org",
"request": req,
# readthedocs_processor context
'DASHBOARD_ANALYTICS_CODE': mock.ANY,
'DO_NOT_TRACK_ENABLED': mock.ANY,
'GLOBAL_ANALYTICS_CODE': mock.ANY,
'PRODUCTION_DOMAIN': 'readthedocs.org',
'PUBLIC_DOMAIN': mock.ANY,
'SITE_ROOT': mock.ANY,
'SUPPORT_EMAIL': '[email protected]',
'TEMPLATE_ROOT': mock.ANY,
'USE_PROMOS': mock.ANY,
'USE_SUBDOMAIN': mock.ANY,
"DASHBOARD_ANALYTICS_CODE": mock.ANY,
"DO_NOT_TRACK_ENABLED": mock.ANY,
"GLOBAL_ANALYTICS_CODE": mock.ANY,
"PRODUCTION_DOMAIN": "readthedocs.org",
"PUBLIC_DOMAIN": mock.ANY,
"SITE_ROOT": mock.ANY,
"SUPPORT_EMAIL": "[email protected]",
"TEMPLATE_ROOT": mock.ANY,
"USE_PROMOS": mock.ANY,
"USE_SUBDOMAIN": mock.ANY,
"USE_ORGANIZATIONS": mock.ANY,
},
)

notify.render('site')
render_to_string.assert_has_calls([
mock.call(
context=mock.ANY,
template_name=['builds/notifications/foo_site.html'],
),
])
notify.render("site")
render_to_string.assert_has_calls(
[
mock.call(
context=mock.ANY,
template_name=["builds/notifications/foo_site.html"],
),
]
)


class NotificationBackendTests(TestCase):

@mock.patch('readthedocs.notifications.backends.send_email')
@mock.patch("readthedocs.notifications.backends.send_email")
def test_email_backend(self, send_email):
class TestNotification(Notification):
name = 'foo'
subject = 'This is {{ foo.id }}'
context_object_name = 'foo'
name = "foo"
subject = "This is {{ foo.id }}"
context_object_name = "foo"
level = ERROR

build = fixture.get(Build)
Expand All @@ -117,12 +117,12 @@ class TestNotification(Notification):

@mock.patch("readthedocs.notifications.notification.render_to_string")
def test_message_backend(self, render_to_string):
render_to_string.return_value = 'Test'
render_to_string.return_value = "Test"

class TestNotification(Notification):
name = 'foo'
subject = 'This is {{ foo.id }}'
context_object_name = 'foo'
name = "foo"
subject = "This is {{ foo.id }}"
context_object_name = "foo"

build = fixture.get(Build)
user = fixture.get(User)
Expand All @@ -140,12 +140,12 @@ class TestNotification(Notification):
@mock.patch("readthedocs.notifications.notification.render_to_string")
def test_message_anonymous_user(self, render_to_string):
"""Anonymous user still throwns exception on persistent messages."""
render_to_string.return_value = 'Test'
render_to_string.return_value = "Test"

class TestNotification(Notification):
name = 'foo'
subject = 'This is {{ foo.id }}'
context_object_name = 'foo'
name = "foo"
subject = "This is {{ foo.id }}"
context_object_name = "foo"

build = fixture.get(Build)
user = AnonymousUser()
Expand All @@ -163,11 +163,11 @@ class TestNotification(Notification):
self.assertEqual(render_to_string.call_count, 1)
self.assertEqual(PersistentMessage.objects.count(), 0)

@mock.patch('readthedocs.notifications.backends.send_email')
@mock.patch("readthedocs.notifications.backends.send_email")
def test_non_persistent_message(self, send_email):
class TestNotification(SiteNotification):
name = 'foo'
success_message = 'Test success message'
name = "foo"
success_message = "Test success message"
success_level = INFO_NON_PERSISTENT

user = fixture.get(User)
Expand All @@ -185,39 +185,38 @@ class TestNotification(SiteNotification):
self.assertEqual(PersistentMessage.objects.filter(read=False).count(), 1)

self.client.force_login(user)
response = self.client.get('/dashboard/')
self.assertContains(response, 'Test success message')
response = self.client.get("/dashboard/")
self.assertContains(response, "Test success message")
self.assertEqual(PersistentMessage.objects.count(), 1)
self.assertEqual(PersistentMessage.objects.filter(read=True).count(), 1)

response = self.client.get('/dashboard/')
self.assertNotContains(response, 'Test success message')
response = self.client.get("/dashboard/")
self.assertNotContains(response, "Test success message")


@override_settings(
PRODUCTION_DOMAIN='readthedocs.org',
SUPPORT_EMAIL='[email protected]',
PRODUCTION_DOMAIN="readthedocs.org",
SUPPORT_EMAIL="[email protected]",
)
class SiteNotificationTests(TestCase):

class TestSiteNotification(SiteNotification):
name = 'foo'
success_message = 'simple success message'
name = "foo"
success_message = "simple success message"
failure_message = {
1: 'simple failure message',
2: '{{ object.name }} object name',
'three': '{{ object.name }} and {{ other.name }} render',
1: "simple failure message",
2: "{{ object.name }} object name",
"three": "{{ object.name }} and {{ other.name }} render",
}
success_level = INFO_NON_PERSISTENT
failure_level = WARNING_NON_PERSISTENT

def setUp(self):
self.user = fixture.get(User)
self.context = {'other': {'name': 'other name'}}
self.context = {"other": {"name": "other name"}}
self.n = self.TestSiteNotification(
self.user,
True,
context_object={'name': 'object name'},
context_object={"name": "object name"},
extra_context=self.context,
)

Expand All @@ -228,16 +227,17 @@ def test_context_data(self):
"production_uri": "https://readthedocs.org",
"other": {"name": "other name"},
# readthedocs_processor context
'DASHBOARD_ANALYTICS_CODE': mock.ANY,
'DO_NOT_TRACK_ENABLED': mock.ANY,
'GLOBAL_ANALYTICS_CODE': mock.ANY,
'PRODUCTION_DOMAIN': 'readthedocs.org',
'PUBLIC_DOMAIN': mock.ANY,
'SITE_ROOT': mock.ANY,
'SUPPORT_EMAIL': '[email protected]',
'TEMPLATE_ROOT': mock.ANY,
'USE_PROMOS': mock.ANY,
'USE_SUBDOMAIN': mock.ANY,
"DASHBOARD_ANALYTICS_CODE": mock.ANY,
"DO_NOT_TRACK_ENABLED": mock.ANY,
"GLOBAL_ANALYTICS_CODE": mock.ANY,
"PRODUCTION_DOMAIN": "readthedocs.org",
"PUBLIC_DOMAIN": mock.ANY,
"SITE_ROOT": mock.ANY,
"SUPPORT_EMAIL": "[email protected]",
"TEMPLATE_ROOT": mock.ANY,
"USE_PROMOS": mock.ANY,
"USE_SUBDOMAIN": mock.ANY,
"USE_ORGANIZATIONS": mock.ANY,
}
self.assertEqual(self.n.get_context_data(), context)

Expand All @@ -250,19 +250,19 @@ def test_message_level(self):

def test_message(self):
self.n.reason = 1
self.assertEqual(self.n.get_message(True), 'simple success message')
self.n.reason = 'three'
self.assertEqual(self.n.get_message(True), 'simple success message')
self.assertEqual(self.n.get_message(True), "simple success message")
self.n.reason = "three"
self.assertEqual(self.n.get_message(True), "simple success message")

self.n.reason = 1
self.assertEqual(self.n.get_message(False), 'simple failure message')
self.assertEqual(self.n.get_message(False), "simple failure message")
self.n.reason = 2
self.assertEqual(self.n.get_message(False), 'object name object name')
self.n.reason = 'three'
self.assertEqual(self.n.get_message(False), 'object name and other name render')
self.assertEqual(self.n.get_message(False), "object name object name")
self.n.reason = "three"
self.assertEqual(self.n.get_message(False), "object name and other name render")

# Invalid reason
self.n.reason = None
with mock.patch('readthedocs.notifications.notification.log') as mock_log:
self.assertEqual(self.n.get_message(False), '')
with mock.patch("readthedocs.notifications.notification.log") as mock_log:
self.assertEqual(self.n.get_message(False), "")
mock_log.error.assert_called_once()

0 comments on commit 689e5c6

Please sign in to comment.