authentik.policies.reputation.tests

test reputation signals and policy

  1"""test reputation signals and policy"""
  2
  3from django.test import RequestFactory, TestCase
  4
  5from authentik.admin.models import DEFAULT_REPUTATION_LOWER_LIMIT, DEFAULT_REPUTATION_UPPER_LIMIT
  6from authentik.core.models import User
  7from authentik.lib.generators import generate_id
  8from authentik.policies.reputation.api import ReputationPolicySerializer
  9from authentik.policies.reputation.models import Reputation, ReputationPolicy
 10from authentik.policies.reputation.signals import (
 11    handle_successful_login,
 12    update_score,
 13    update_score_on_login,
 14)
 15from authentik.policies.types import PolicyRequest
 16from authentik.stages.password import BACKEND_INBUILT
 17from authentik.stages.password.models import PasswordDevice
 18from authentik.stages.password.stage import authenticate
 19
 20
 21class TestReputationPolicy(TestCase):
 22    """test reputation signals and policy"""
 23
 24    def setUp(self):
 25        self.request_factory = RequestFactory()
 26        self.request = self.request_factory.get("/")
 27        self.ip = "127.0.0.1"
 28        self.username = "username"
 29        self.password = generate_id()
 30        # We need a user for the one-to-one in userreputation
 31        self.user = User.objects.create(username=self.username)
 32        PasswordDevice.set_password(self.user, self.password)
 33        self.backends = [BACKEND_INBUILT]
 34
 35    def test_ip_reputation(self):
 36        """test IP reputation"""
 37        # Trigger negative reputation
 38        authenticate(self.request, self.backends, username=self.username, password=self.username)
 39        self.assertEqual(Reputation.objects.get(ip=self.ip).score, -1)
 40
 41    def test_user_reputation(self):
 42        """test User reputation"""
 43        # Trigger negative reputation
 44        authenticate(self.request, self.backends, username=self.username, password=self.username)
 45        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)
 46
 47    def test_update_reputation(self):
 48        """test reputation update"""
 49        Reputation.objects.create(identifier=self.username, ip=self.ip, score=4)
 50        # Trigger negative reputation
 51        authenticate(self.request, self.backends, username=self.username, password=self.username)
 52        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 3)
 53
 54    def test_reputation_lower_limit(self):
 55        """test reputation lower limit"""
 56        Reputation.objects.create(identifier=self.username, ip=self.ip)
 57        update_score(self.request, identifier=self.username, amount=-1000)
 58        self.assertEqual(
 59            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_LOWER_LIMIT
 60        )
 61
 62    def test_reputation_upper_limit(self):
 63        """test reputation upper limit"""
 64        Reputation.objects.create(identifier=self.username, ip=self.ip)
 65        update_score(self.request, identifier=self.username, amount=1000)
 66        self.assertEqual(
 67            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
 68        )
 69
 70    def test_login_reputation_negative_resets(self):
 71        """test that a successful login resets a negative score to 0"""
 72        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
 73        update_score_on_login(self.request, identifier=self.username)
 74        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
 75
 76    def test_login_reputation_zero_increments(self):
 77        """test that a successful login raises a zero score by 1"""
 78        Reputation.objects.create(identifier=self.username, ip=self.ip, score=0)
 79        update_score_on_login(self.request, identifier=self.username)
 80        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)
 81
 82    def test_login_reputation_positive_increments(self):
 83        """test that a successful login raises a positive score by 1"""
 84        Reputation.objects.create(identifier=self.username, ip=self.ip, score=3)
 85        update_score_on_login(self.request, identifier=self.username)
 86        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 4)
 87
 88    def test_login_reputation_creates_entry(self):
 89        """test that a successful login with no existing entry creates one at 1"""
 90        update_score_on_login(self.request, identifier=self.username)
 91        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)
 92
 93    def test_login_reputation_upper_limit(self):
 94        """test that the increment is still bound by the upper limit"""
 95        Reputation.objects.create(
 96            identifier=self.username, ip=self.ip, score=DEFAULT_REPUTATION_UPPER_LIMIT
 97        )
 98        update_score_on_login(self.request, identifier=self.username)
 99        self.assertEqual(
100            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
101        )
102
103    def test_login_reputation_other_identifier(self):
104        """test that only the matching identifier is touched for a shared IP"""
105        other_username = generate_id()
106        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
107        Reputation.objects.create(identifier=other_username, ip=self.ip, score=-3)
108        update_score_on_login(self.request, identifier=self.username)
109        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
110        self.assertEqual(Reputation.objects.get(identifier=other_username).score, -3)
111
112    def test_login_reputation_other_ip(self):
113        """test that only the matching IP is touched for the same identifier"""
114        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
115        Reputation.objects.create(identifier=self.username, ip="10.0.0.1", score=-3)
116        update_score_on_login(self.request, identifier=self.username)
117        self.assertEqual(Reputation.objects.get(identifier=self.username, ip=self.ip).score, 0)
118        self.assertEqual(Reputation.objects.get(identifier=self.username, ip="10.0.0.1").score, -3)
119
120    def test_login_reputation_signal_handler(self):
121        """test that a failed login followed by a successful one ends at 0"""
122        authenticate(self.request, self.backends, username=self.username, password=self.username)
123        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)
124        handle_successful_login(None, self.request, self.user)
125        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
126
127    def test_policy(self):
128        """Test Policy"""
129        request = PolicyRequest(user=self.user)
130        policy: ReputationPolicy = ReputationPolicy.objects.create(
131            name="reputation-test", threshold=0
132        )
133        self.assertTrue(policy.passes(request).passing)
134
135    def test_api(self):
136        """Test API Validation"""
137        no_toggle = ReputationPolicySerializer(data={"name": generate_id(), "threshold": -5})
138        self.assertFalse(no_toggle.is_valid())
class TestReputationPolicy(django.test.testcases.TestCase):
 22class TestReputationPolicy(TestCase):
 23    """test reputation signals and policy"""
 24
 25    def setUp(self):
 26        self.request_factory = RequestFactory()
 27        self.request = self.request_factory.get("/")
 28        self.ip = "127.0.0.1"
 29        self.username = "username"
 30        self.password = generate_id()
 31        # We need a user for the one-to-one in userreputation
 32        self.user = User.objects.create(username=self.username)
 33        PasswordDevice.set_password(self.user, self.password)
 34        self.backends = [BACKEND_INBUILT]
 35
 36    def test_ip_reputation(self):
 37        """test IP reputation"""
 38        # Trigger negative reputation
 39        authenticate(self.request, self.backends, username=self.username, password=self.username)
 40        self.assertEqual(Reputation.objects.get(ip=self.ip).score, -1)
 41
 42    def test_user_reputation(self):
 43        """test User reputation"""
 44        # Trigger negative reputation
 45        authenticate(self.request, self.backends, username=self.username, password=self.username)
 46        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)
 47
 48    def test_update_reputation(self):
 49        """test reputation update"""
 50        Reputation.objects.create(identifier=self.username, ip=self.ip, score=4)
 51        # Trigger negative reputation
 52        authenticate(self.request, self.backends, username=self.username, password=self.username)
 53        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 3)
 54
 55    def test_reputation_lower_limit(self):
 56        """test reputation lower limit"""
 57        Reputation.objects.create(identifier=self.username, ip=self.ip)
 58        update_score(self.request, identifier=self.username, amount=-1000)
 59        self.assertEqual(
 60            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_LOWER_LIMIT
 61        )
 62
 63    def test_reputation_upper_limit(self):
 64        """test reputation upper limit"""
 65        Reputation.objects.create(identifier=self.username, ip=self.ip)
 66        update_score(self.request, identifier=self.username, amount=1000)
 67        self.assertEqual(
 68            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
 69        )
 70
 71    def test_login_reputation_negative_resets(self):
 72        """test that a successful login resets a negative score to 0"""
 73        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
 74        update_score_on_login(self.request, identifier=self.username)
 75        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
 76
 77    def test_login_reputation_zero_increments(self):
 78        """test that a successful login raises a zero score by 1"""
 79        Reputation.objects.create(identifier=self.username, ip=self.ip, score=0)
 80        update_score_on_login(self.request, identifier=self.username)
 81        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)
 82
 83    def test_login_reputation_positive_increments(self):
 84        """test that a successful login raises a positive score by 1"""
 85        Reputation.objects.create(identifier=self.username, ip=self.ip, score=3)
 86        update_score_on_login(self.request, identifier=self.username)
 87        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 4)
 88
 89    def test_login_reputation_creates_entry(self):
 90        """test that a successful login with no existing entry creates one at 1"""
 91        update_score_on_login(self.request, identifier=self.username)
 92        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)
 93
 94    def test_login_reputation_upper_limit(self):
 95        """test that the increment is still bound by the upper limit"""
 96        Reputation.objects.create(
 97            identifier=self.username, ip=self.ip, score=DEFAULT_REPUTATION_UPPER_LIMIT
 98        )
 99        update_score_on_login(self.request, identifier=self.username)
100        self.assertEqual(
101            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
102        )
103
104    def test_login_reputation_other_identifier(self):
105        """test that only the matching identifier is touched for a shared IP"""
106        other_username = generate_id()
107        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
108        Reputation.objects.create(identifier=other_username, ip=self.ip, score=-3)
109        update_score_on_login(self.request, identifier=self.username)
110        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
111        self.assertEqual(Reputation.objects.get(identifier=other_username).score, -3)
112
113    def test_login_reputation_other_ip(self):
114        """test that only the matching IP is touched for the same identifier"""
115        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
116        Reputation.objects.create(identifier=self.username, ip="10.0.0.1", score=-3)
117        update_score_on_login(self.request, identifier=self.username)
118        self.assertEqual(Reputation.objects.get(identifier=self.username, ip=self.ip).score, 0)
119        self.assertEqual(Reputation.objects.get(identifier=self.username, ip="10.0.0.1").score, -3)
120
121    def test_login_reputation_signal_handler(self):
122        """test that a failed login followed by a successful one ends at 0"""
123        authenticate(self.request, self.backends, username=self.username, password=self.username)
124        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)
125        handle_successful_login(None, self.request, self.user)
126        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
127
128    def test_policy(self):
129        """Test Policy"""
130        request = PolicyRequest(user=self.user)
131        policy: ReputationPolicy = ReputationPolicy.objects.create(
132            name="reputation-test", threshold=0
133        )
134        self.assertTrue(policy.passes(request).passing)
135
136    def test_api(self):
137        """Test API Validation"""
138        no_toggle = ReputationPolicySerializer(data={"name": generate_id(), "threshold": -5})
139        self.assertFalse(no_toggle.is_valid())

test reputation signals and policy

def setUp(self):
25    def setUp(self):
26        self.request_factory = RequestFactory()
27        self.request = self.request_factory.get("/")
28        self.ip = "127.0.0.1"
29        self.username = "username"
30        self.password = generate_id()
31        # We need a user for the one-to-one in userreputation
32        self.user = User.objects.create(username=self.username)
33        PasswordDevice.set_password(self.user, self.password)
34        self.backends = [BACKEND_INBUILT]

Hook method for setting up the test fixture before exercising it.

def test_ip_reputation(self):
36    def test_ip_reputation(self):
37        """test IP reputation"""
38        # Trigger negative reputation
39        authenticate(self.request, self.backends, username=self.username, password=self.username)
40        self.assertEqual(Reputation.objects.get(ip=self.ip).score, -1)

test IP reputation

def test_user_reputation(self):
42    def test_user_reputation(self):
43        """test User reputation"""
44        # Trigger negative reputation
45        authenticate(self.request, self.backends, username=self.username, password=self.username)
46        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)

test User reputation

def test_update_reputation(self):
48    def test_update_reputation(self):
49        """test reputation update"""
50        Reputation.objects.create(identifier=self.username, ip=self.ip, score=4)
51        # Trigger negative reputation
52        authenticate(self.request, self.backends, username=self.username, password=self.username)
53        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 3)

test reputation update

def test_reputation_lower_limit(self):
55    def test_reputation_lower_limit(self):
56        """test reputation lower limit"""
57        Reputation.objects.create(identifier=self.username, ip=self.ip)
58        update_score(self.request, identifier=self.username, amount=-1000)
59        self.assertEqual(
60            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_LOWER_LIMIT
61        )

test reputation lower limit

def test_reputation_upper_limit(self):
63    def test_reputation_upper_limit(self):
64        """test reputation upper limit"""
65        Reputation.objects.create(identifier=self.username, ip=self.ip)
66        update_score(self.request, identifier=self.username, amount=1000)
67        self.assertEqual(
68            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
69        )

test reputation upper limit

def test_login_reputation_negative_resets(self):
71    def test_login_reputation_negative_resets(self):
72        """test that a successful login resets a negative score to 0"""
73        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
74        update_score_on_login(self.request, identifier=self.username)
75        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)

test that a successful login resets a negative score to 0

def test_login_reputation_zero_increments(self):
77    def test_login_reputation_zero_increments(self):
78        """test that a successful login raises a zero score by 1"""
79        Reputation.objects.create(identifier=self.username, ip=self.ip, score=0)
80        update_score_on_login(self.request, identifier=self.username)
81        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)

test that a successful login raises a zero score by 1

def test_login_reputation_positive_increments(self):
83    def test_login_reputation_positive_increments(self):
84        """test that a successful login raises a positive score by 1"""
85        Reputation.objects.create(identifier=self.username, ip=self.ip, score=3)
86        update_score_on_login(self.request, identifier=self.username)
87        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 4)

test that a successful login raises a positive score by 1

def test_login_reputation_creates_entry(self):
89    def test_login_reputation_creates_entry(self):
90        """test that a successful login with no existing entry creates one at 1"""
91        update_score_on_login(self.request, identifier=self.username)
92        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 1)

test that a successful login with no existing entry creates one at 1

def test_login_reputation_upper_limit(self):
 94    def test_login_reputation_upper_limit(self):
 95        """test that the increment is still bound by the upper limit"""
 96        Reputation.objects.create(
 97            identifier=self.username, ip=self.ip, score=DEFAULT_REPUTATION_UPPER_LIMIT
 98        )
 99        update_score_on_login(self.request, identifier=self.username)
100        self.assertEqual(
101            Reputation.objects.get(identifier=self.username).score, DEFAULT_REPUTATION_UPPER_LIMIT
102        )

test that the increment is still bound by the upper limit

def test_login_reputation_other_identifier(self):
104    def test_login_reputation_other_identifier(self):
105        """test that only the matching identifier is touched for a shared IP"""
106        other_username = generate_id()
107        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
108        Reputation.objects.create(identifier=other_username, ip=self.ip, score=-3)
109        update_score_on_login(self.request, identifier=self.username)
110        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)
111        self.assertEqual(Reputation.objects.get(identifier=other_username).score, -3)

test that only the matching identifier is touched for a shared IP

def test_login_reputation_other_ip(self):
113    def test_login_reputation_other_ip(self):
114        """test that only the matching IP is touched for the same identifier"""
115        Reputation.objects.create(identifier=self.username, ip=self.ip, score=-3)
116        Reputation.objects.create(identifier=self.username, ip="10.0.0.1", score=-3)
117        update_score_on_login(self.request, identifier=self.username)
118        self.assertEqual(Reputation.objects.get(identifier=self.username, ip=self.ip).score, 0)
119        self.assertEqual(Reputation.objects.get(identifier=self.username, ip="10.0.0.1").score, -3)

test that only the matching IP is touched for the same identifier

def test_login_reputation_signal_handler(self):
121    def test_login_reputation_signal_handler(self):
122        """test that a failed login followed by a successful one ends at 0"""
123        authenticate(self.request, self.backends, username=self.username, password=self.username)
124        self.assertEqual(Reputation.objects.get(identifier=self.username).score, -1)
125        handle_successful_login(None, self.request, self.user)
126        self.assertEqual(Reputation.objects.get(identifier=self.username).score, 0)

test that a failed login followed by a successful one ends at 0

def test_policy(self):
128    def test_policy(self):
129        """Test Policy"""
130        request = PolicyRequest(user=self.user)
131        policy: ReputationPolicy = ReputationPolicy.objects.create(
132            name="reputation-test", threshold=0
133        )
134        self.assertTrue(policy.passes(request).passing)

Test Policy

def test_api(self):
136    def test_api(self):
137        """Test API Validation"""
138        no_toggle = ReputationPolicySerializer(data={"name": generate_id(), "threshold": -5})
139        self.assertFalse(no_toggle.is_valid())

Test API Validation