Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
[carpentries#2062] Conditionally enable views based on feature flag
Browse files Browse the repository at this point in the history
This will become useful for Instructor Recruitment views.
  • Loading branch information
pbanaszkiewicz committed Dec 5, 2021
1 parent 34092fc commit 7379a73
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions amy/workshops/base_views.py
@@ -1,4 +1,5 @@
from smtplib import SMTPException
from typing import Optional

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -430,3 +431,18 @@ def get(self, request, *args, **kwargs):
requested_person_id = self.kwargs.get(self.person_url_kwarg)
assign(request, self.object, requested_person_id)
return super().get(request, *args, **kwargs)


class ConditionallyEnabledMixin:
"""Mixin for enabling views based on feature flag."""

view_enabled: Optional[bool] = None

def get_view_enabled(self) -> bool:
return self.view_enabled is True

def dispatch(self, request, *args, **kwargs):
if self.get_view_enabled() is not True:
raise Http404("Page not found")

return super().dispatch(request, *args, **kwargs)
73 changes: 73 additions & 0 deletions amy/workshops/tests/test_base_views.py
@@ -0,0 +1,73 @@
from unittest.mock import patch

from django.http.response import Http404
from django.test import TestCase

from workshops.base_views import ConditionallyEnabledMixin


class FakeView:
"""Used in the tests below."""

def dispatch(self, request, *args, **kwargs):
pass


class TestConditionallyEnabledView(TestCase):
def test_disabled_view_through_parameter(self):
# Arrange
class View(ConditionallyEnabledMixin, FakeView):
pass

view = View()
view.view_enabled = False

# Act & Assert
with patch.object(FakeView, "dispatch") as mock_dispatch:
with self.assertRaises(Http404):
view.dispatch(None)
mock_dispatch.assert_not_called()

def test_disabled_view_through_method(self):
# Arrange
class View(ConditionallyEnabledMixin, FakeView):
def get_view_enabled(self) -> bool:
return False

view = View()

# Act & Assert
with patch.object(FakeView, "dispatch") as mock_dispatch:
with self.assertRaises(Http404):
view.dispatch(None)
mock_dispatch.assert_not_called()

def test_enabled_view_through_parameter(self):
# Arrange
class View(ConditionallyEnabledMixin, FakeView):
pass

view = View()
view.view_enabled = True

with patch.object(FakeView, "dispatch") as mock_dispatch:
# Act
view.dispatch(None)

# Assert
mock_dispatch.assert_called_once()

def test_enabled_view_through_method(self):
# Arrange
class View(ConditionallyEnabledMixin, FakeView):
def get_view_enabled(self) -> bool:
return True

view = View()

with patch.object(FakeView, "dispatch") as mock_dispatch:
# Act
view.dispatch(None)

# Assert
mock_dispatch.assert_called_once()

0 comments on commit 7379a73

Please sign in to comment.