6
6
from authentik .lib .generators import generate_id
7
7
from authentik .policies .reputation .api import ReputationPolicySerializer
8
8
from authentik .policies .reputation .models import Reputation , ReputationPolicy
9
+ from authentik .policies .reputation .signals import update_score
9
10
from authentik .policies .types import PolicyRequest
10
11
from authentik .stages .password import BACKEND_INBUILT
11
12
from authentik .stages .password .stage import authenticate
13
+ from authentik .tenants .models import DEFAULT_REPUTATION_LOWER_LIMIT , DEFAULT_REPUTATION_UPPER_LIMIT
12
14
13
15
14
16
class TestReputationPolicy (TestCase ):
@@ -17,36 +19,48 @@ class TestReputationPolicy(TestCase):
17
19
def setUp (self ):
18
20
self .request_factory = RequestFactory ()
19
21
self .request = self .request_factory .get ("/" )
20
- self .test_ip = "127.0.0.1"
21
- self .test_username = "test"
22
+ self .ip = "127.0.0.1"
23
+ self .username = "username"
24
+ self .password = generate_id ()
22
25
# We need a user for the one-to-one in userreputation
23
- self .user = User .objects .create (username = self .test_username )
26
+ self .user = User .objects .create (username = self .username )
27
+ self .user .set_password (self .password )
24
28
self .backends = [BACKEND_INBUILT ]
25
29
26
30
def test_ip_reputation (self ):
27
31
"""test IP reputation"""
28
32
# Trigger negative reputation
29
- authenticate (
30
- self .request , self .backends , username = self .test_username , password = self .test_username
31
- )
32
- self .assertEqual (Reputation .objects .get (ip = self .test_ip ).score , - 1 )
33
+ authenticate (self .request , self .backends , username = self .username , password = self .username )
34
+ self .assertEqual (Reputation .objects .get (ip = self .ip ).score , - 1 )
33
35
34
36
def test_user_reputation (self ):
35
37
"""test User reputation"""
36
38
# Trigger negative reputation
37
- authenticate (
38
- self .request , self .backends , username = self .test_username , password = self .test_username
39
- )
40
- self .assertEqual (Reputation .objects .get (identifier = self .test_username ).score , - 1 )
39
+ authenticate (self .request , self .backends , username = self .username , password = self .username )
40
+ self .assertEqual (Reputation .objects .get (identifier = self .username ).score , - 1 )
41
41
42
42
def test_update_reputation (self ):
43
43
"""test reputation update"""
44
- Reputation .objects .create (identifier = self .test_username , ip = self .test_ip , score = 43 )
44
+ Reputation .objects .create (identifier = self .username , ip = self .ip , score = 4 )
45
45
# Trigger negative reputation
46
- authenticate (
47
- self .request , self .backends , username = self .test_username , password = self .test_username
46
+ authenticate (self .request , self .backends , username = self .username , password = self .username )
47
+ self .assertEqual (Reputation .objects .get (identifier = self .username ).score , 3 )
48
+
49
+ def test_reputation_lower_limit (self ):
50
+ """test reputation lower limit"""
51
+ Reputation .objects .create (identifier = self .username , ip = self .ip )
52
+ update_score (self .request , identifier = self .username , amount = - 1000 )
53
+ self .assertEqual (
54
+ Reputation .objects .get (identifier = self .username ).score , DEFAULT_REPUTATION_LOWER_LIMIT
55
+ )
56
+
57
+ def test_reputation_upper_limit (self ):
58
+ """test reputation upper limit"""
59
+ Reputation .objects .create (identifier = self .username , ip = self .ip )
60
+ update_score (self .request , identifier = self .username , amount = 1000 )
61
+ self .assertEqual (
62
+ Reputation .objects .get (identifier = self .username ).score , DEFAULT_REPUTATION_UPPER_LIMIT
48
63
)
49
- self .assertEqual (Reputation .objects .get (identifier = self .test_username ).score , 42 )
50
64
51
65
def test_policy (self ):
52
66
"""Test Policy"""
0 commit comments