This repository has been archived by the owner on Nov 12, 2022. It is now read-only.
forked from carpentries/amy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[carpentries#2062] Conditionally enable views based on feature flag
This will become useful for Instructor Recruitment views.
- Loading branch information
1 parent
34092fc
commit 7379a73
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |