diff --git a/conntrackt/tests/test_views.py b/conntrackt/tests/test_views.py --- a/conntrackt/tests/test_views.py +++ b/conntrackt/tests/test_views.py @@ -23,7 +23,10 @@ from conntrackt.views import EntityView, from conntrackt.views import InterfaceCreateView, InterfaceUpdateView, InterfaceDeleteView from conntrackt.views import CommunicationCreateView, CommunicationUpdateView, CommunicationDeleteView -from helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages +# Test imports. +from .forms import FormWithWidgetCSSClassFormMixin, FormWithPlaceholderFormMixin +from .helpers import PermissionTestMixin, create_get_request, generate_get_response, FakeMessages +from .views import RedirectToNextMixinView class IndexViewTest(PermissionTestMixin, TestCase): @@ -1382,3 +1385,48 @@ class ProjectDiagramTest(PermissionTestM self.assertContains(response, '"-//W3C//DTD SVG 1.1//EN"') self.assertContains(response, "Test Project 1") + + +class RedirectToNextMixinTest(TestCase): + + def test_request_with_next(self): + """ + Test if the get_success_url returns correct URL if "next" is present in + request's GET parameters. + """ + + # Generate the request. + request = RequestFactory().post("/fake-path?next=/next") + + # Initialise the pseudo-view. + view = RedirectToNextMixinView(request) + + self.assertEqual("/next", view.get_success_url()) + + def test_request_without_next(self): + """ + Test if the get_success_url returns correct URL if "next" is not present + in request's GET parameters. + """ + + # Generate the request. + request = RequestFactory().post("/fake-path") + + # Initialise the pseudo-view. + view = RedirectToNextMixinView(request) + + self.assertEqual("/STATIC", view.get_success_url()) + + def test_request_custom_parameter_name(self): + """ + Test if the mixin honours the custom parameter name. + """ + + # Generate the request. + request = RequestFactory().post("/fake-path?custom=/next") + + # Initialise the pseudo-view. + view = RedirectToNextMixinView(request) + view.next_parameter = "custom" + + self.assertEqual("/next", view.get_success_url()) diff --git a/conntrackt/tests/views.py b/conntrackt/tests/views.py new file mode 100644 --- /dev/null +++ b/conntrackt/tests/views.py @@ -0,0 +1,27 @@ +# Application imports. +from conntrackt.views import RedirectToNextMixin + + +class StaticSuccessUrlFakeMixin(object): + """ + Helper view for testing the RedirectToNextMixinView mixin. + """ + + def get_success_url(self): + return self.success_url + + +class RedirectToNextMixinView(RedirectToNextMixin, StaticSuccessUrlFakeMixin): + """ + Helper view for testing the RedirectToNextMixinView mixin. StaticSuccessUrl + is there just to provide default for get_success_url(). + """ + + success_url = "/STATIC" + + def __init__(self, request): + """ + Initialise the request to provided value. + """ + + self.request = request diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -19,6 +19,28 @@ from .models import Project, Entity, Loc from .utils import generate_entity_iptables, generate_project_diagram +class RedirectToNextMixin(object): + """ + View mixin that can be used for redirecting the user to URL defined through + a GET parameter. The mixin is usable with Create/Update/Delete views that + utilise the get_success_url() call. + + The mixin accepts the following class options: + + next_parameter - Name of the GET parameter that contains the redirect + URL. Defaults to "next". + """ + + next_parameter = "next" + + def get_success_url(self): + """ + Returns the success URL to which the user will be redirected. + """ + + return self.request.GET.get(self.next_parameter, super(RedirectToNextMixin, self).get_success_url()) + + class IndexView(MultiplePermissionsRequiredMixin, TemplateView): """ Custom view used for rendering the index page.