From c140c39d071dc40bbedfb00ebd3025111a0f6a39 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 10 May 2020 01:02:04 +0200 Subject: [PATCH] stages/login: add unittests --- passbook/stages/login/stage.py | 10 +++-- passbook/stages/login/tests.py | 77 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 passbook/stages/login/tests.py diff --git a/passbook/stages/login/stage.py b/passbook/stages/login/stage.py index 74bce930b..1e43c163f 100644 --- a/passbook/stages/login/stage.py +++ b/passbook/stages/login/stage.py @@ -17,12 +17,16 @@ class LoginStageView(AuthenticationStage): def get(self, request: HttpRequest) -> HttpResponse: if PLAN_CONTEXT_PENDING_USER not in self.executor.plan.context: - messages.error(request, _("No Pending user to login.")) + message = _("No Pending user to login.") + messages.error(request, message) + LOGGER.debug(message) return self.executor.stage_invalid() if PLAN_CONTEXT_AUTHENTICATION_BACKEND not in self.executor.plan.context: - messages.error(request, _("Pending user has no backend.")) + message = _("Pending user has no backend.") + messages.error(request, message) + LOGGER.debug(message) return self.executor.stage_invalid() - backend = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER].backend + backend = self.executor.plan.context[PLAN_CONTEXT_AUTHENTICATION_BACKEND] login( self.request, self.executor.plan.context[PLAN_CONTEXT_PENDING_USER], diff --git a/passbook/stages/login/tests.py b/passbook/stages/login/tests.py new file mode 100644 index 000000000..e15078567 --- /dev/null +++ b/passbook/stages/login/tests.py @@ -0,0 +1,77 @@ +"""login tests""" +from django.shortcuts import reverse +from django.test import Client, TestCase + +from passbook.core.models import User +from passbook.flows.models import Flow, FlowDesignation, FlowStageBinding +from passbook.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan +from passbook.flows.views import SESSION_KEY_PLAN +from passbook.stages.login.models import LoginStage +from passbook.stages.password.stage import PLAN_CONTEXT_AUTHENTICATION_BACKEND + + +class TestLoginStage(TestCase): + """Login tests""" + + def setUp(self): + super().setUp() + self.user = User.objects.create(username="unittest", email="test@beryju.org") + self.client = Client() + + self.flow = Flow.objects.create( + name="test-login", + slug="test-login", + designation=FlowDesignation.AUTHENTICATION, + ) + self.stage = LoginStage.objects.create(name="login") + FlowStageBinding.objects.create(flow=self.flow, stage=self.stage, order=2) + + def test_valid_password(self): + """Test with a valid pending user and backend""" + plan = FlowPlan(stages=[self.stage]) + plan.context[PLAN_CONTEXT_PENDING_USER] = self.user + plan.context[ + PLAN_CONTEXT_AUTHENTICATION_BACKEND + ] = "django.contrib.auth.backends.ModelBackend" + session = self.client.session + session[SESSION_KEY_PLAN] = plan + session.save() + + response = self.client.get( + reverse( + "passbook_flows:flow-executor", kwargs={"flow_slug": self.flow.slug} + ) + ) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse("passbook_core:overview")) + + def test_without_user(self): + """Test a plan without any pending user, resulting in a denied""" + plan = FlowPlan(stages=[self.stage]) + session = self.client.session + session[SESSION_KEY_PLAN] = plan + session.save() + + response = self.client.get( + reverse( + "passbook_flows:flow-executor", kwargs={"flow_slug": self.flow.slug} + ) + ) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse("passbook_flows:denied")) + + def test_without_backend(self): + """Test a plan with pending user, without backend, resulting in a denied""" + plan = FlowPlan(stages=[self.stage]) + plan.context[PLAN_CONTEXT_PENDING_USER] = self.user + session = self.client.session + session[SESSION_KEY_PLAN] = plan + session.save() + + response = self.client.get( + reverse( + "passbook_flows:flow-executor", kwargs={"flow_slug": self.flow.slug} + ) + ) + self.assertEqual(response.status_code, 302) + self.assertEqual(response.url, reverse("passbook_flows:denied"))