Example #1
0
 def test_tasks(self):
     """Test Task API"""
     clean_expired_models.delay()
     response = self.client.get(reverse("authentik_api:admin_system_tasks-list"))
     self.assertEqual(response.status_code, 200)
     body = loads(response.content)
     self.assertTrue(any(task["task_name"] == "clean_expired_models" for task in body))
Example #2
0
 def test_tasks_retry(self):
     """Test Task API (retry)"""
     clean_expired_models.delay()
     response = self.client.post(
         reverse(
             "authentik_api:admin_system_tasks-retry",
             kwargs={"pk": "clean_expired_models"},
         ))
     self.assertEqual(response.status_code, 204)
Example #3
0
 def test_token_expire(self):
     """Test Token expire task"""
     token: Token = Token.objects.create(expires=now(),
                                         user=get_anonymous_user(),
                                         intent=TokenIntents.INTENT_API)
     key = token.key
     clean_expired_models.delay().get()
     token.refresh_from_db()
     self.assertNotEqual(key, token.key)
Example #4
0
 def test_tasks_single(self):
     """Test Task API (read single)"""
     clean_expired_models.delay()
     response = self.client.get(
         reverse(
             "authentik_api:admin_system_tasks-detail",
             kwargs={"pk": "clean_expired_models"},
         ))
     self.assertEqual(response.status_code, 200)
     body = loads(response.content)
     self.assertEqual(body["status"], TaskResultStatus.SUCCESSFUL.name)
     self.assertEqual(body["task_name"], "clean_expired_models")
     response = self.client.get(
         reverse("authentik_api:admin_system_tasks-detail",
                 kwargs={"pk": "qwerqwer"}))
     self.assertEqual(response.status_code, 404)
Example #5
0
    def test_expire(self):
        """Test expiring consent from user"""
        self.client.force_login(self.user)
        flow = Flow.objects.create(
            name="test-consent",
            slug="test-consent",
            designation=FlowDesignation.AUTHENTICATION,
        )
        stage = ConsentStage.objects.create(
            name="consent", mode=ConsentMode.EXPIRING, consent_expire_in="seconds=1"
        )
        FlowStageBinding.objects.create(target=flow, stage=stage, order=2)

        plan = FlowPlan(
            flow_pk=flow.pk.hex,
            stages=[stage],
            markers=[StageMarker()],
            context={PLAN_CONTEXT_APPLICATION: self.application},
        )
        session = self.client.session
        session[SESSION_KEY_PLAN] = plan
        session.save()
        response = self.client.post(
            reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}),
            {},
        )
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            force_str(response.content),
            {
                "component": "xak-flow-redirect",
                "to": reverse("authentik_core:root-redirect"),
                "type": ChallengeTypes.REDIRECT.value,
            },
        )
        self.assertTrue(
            UserConsent.objects.filter(
                user=self.user, application=self.application
            ).exists()
        )
        sleep(1)
        clean_expired_models.delay().get()
        self.assertFalse(
            UserConsent.objects.filter(
                user=self.user, application=self.application
            ).exists()
        )
Example #6
0
 def test_token_cleanup(self):
     """Test Token cleanup task"""
     Token.objects.create(expires=now(), user=get_anonymous_user())
     self.assertEqual(Token.objects.all().count(), 1)
     clean_expired_models.delay().get()
     self.assertEqual(Token.objects.all().count(), 0)