Bin
2025-12-17 2e6c955be321cefd7e0c4a3031eab805e0a5a303
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from users.serializers import HotkeysSerializer
 
 
class TestHotkeysSerializer:
    """Tests for the HotkeysSerializer"""
 
    def test_valid_data(self):
        """
        Test serializer accepts valid hotkeys data.
 
        Validates that the serializer correctly processes a dictionary of hotkeys
        with proper action key format (namespace:action) and valid key bindings.
        """
        valid_hotkeys = {
            'editor:save': {'key': 'ctrl+s', 'active': True},
            'editor:open': {'key': 'ctrl+o', 'active': False},
            'editor:cut': {'key': 'ctrl+x'},
            'navigation:home': {'key': 'alt+h', 'active': True},
        }
 
        serializer_data = {'custom_hotkeys': valid_hotkeys}
        serializer = HotkeysSerializer(data=serializer_data)
 
        assert serializer.is_valid()
 
    def test_invalid_format_not_dict(self):
        """
        Test serializer rejects non-dictionary custom_hotkeys.
 
        Validates that the serializer enforces the custom_hotkeys field
        to be a dictionary type and rejects other data types like lists.
        """
        data = {'custom_hotkeys': ['not a dictionary']}
        serializer = HotkeysSerializer(data=data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors
 
    def test_invalid_action_key_format(self):
        """
        Test serializer rejects action keys without proper format.
 
        Validates that action keys must follow the namespace:action format
        and rejects keys that don't contain the required colon separator.
        """
        invalid_data = {'custom_hotkeys': {'editorsave': {'key': 'ctrl+s'}}}  # Missing colon
        serializer = HotkeysSerializer(data=invalid_data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors
 
    def test_empty_action_key(self):
        """
        Test serializer rejects empty action keys.
 
        Validates that action keys cannot be empty strings and must
        contain meaningful namespace and action identifiers.
        """
        invalid_data = {'custom_hotkeys': {'': {'key': 'ctrl+s'}}}
        serializer = HotkeysSerializer(data=invalid_data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors
 
    def test_missing_key_in_hotkey_data(self):
        """
        Test serializer rejects hotkey data without required 'key' field.
 
        Validates that each hotkey configuration must include a 'key' field
        specifying the keyboard shortcut binding.
        """
        invalid_data = {'custom_hotkeys': {'editor:save': {'active': True}}}  # Missing 'key'
        serializer = HotkeysSerializer(data=invalid_data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors
 
    def test_invalid_key_value(self):
        """
        Test serializer rejects invalid key values.
 
        Validates that the 'key' field cannot be empty and must contain
        a valid keyboard shortcut string representation.
        """
        invalid_data = {'custom_hotkeys': {'editor:save': {'key': ''}}}  # Empty key
        serializer = HotkeysSerializer(data=invalid_data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors
 
    def test_invalid_active_flag(self):
        """
        Test serializer rejects non-boolean active flags.
 
        Validates that the optional 'active' field must be a boolean value
        when present, rejecting string or other non-boolean types.
        """
        invalid_data = {'custom_hotkeys': {'editor:save': {'key': 'ctrl+s', 'active': 'yes'}}}  # Should be boolean
        serializer = HotkeysSerializer(data=invalid_data)
 
        assert not serializer.is_valid()
        assert 'custom_hotkeys' in serializer.errors