File size: 2,332 Bytes
74c37a6
 
 
 
 
41d461d
 
74c37a6
41d461d
 
 
 
74c37a6
41d461d
 
 
74c37a6
41d461d
 
 
 
 
 
 
 
74c37a6
41d461d
 
 
74c37a6
41d461d
 
 
 
 
 
 
74c37a6
41d461d
 
 
 
 
 
 
 
74c37a6
41d461d
 
 
 
 
74c37a6
41d461d
 
 
74c37a6
41d461d
 
 
74c37a6
41d461d
 
 
 
 
 
 
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
import std/unittest
import std/strutils
import donkey/core/types
import donkey/core/security/dm_policy

proc runTests*() =
  suite "DM Security":

    test "DmPolicy has correct variants":
      check DmPolicyOpen == DmPolicyOpen
      check DmPolicyPairing == DmPolicyPairing
      check DmPolicyAllowlist == DmPolicyAllowlist

    test "isAllowedDm returns true for Open policy":
      let state = DmSecurityState(policy: DmPolicyOpen)
      check isAllowedDm(state, UserId("user123"))

    test "isAllowedDm checks allowlist":
      let state = DmSecurityState(
        policy: DmPolicyAllowlist,
        allowlist: @[UserId("allowed1"), UserId("allowed2")]
      )
      check isAllowedDm(state, UserId("allowed1")) == true
      check isAllowedDm(state, UserId("allowed2")) == true
      check isAllowedDm(state, UserId("blocked1")) == false

    test "isAllowedDm returns true for empty allowlist (legacy compat)":
      let state = DmSecurityState(policy: DmPolicyAllowlist)
      check isAllowedDm(state, UserId("anyone")) == true

    test "addToAllowlist adds user":
      var state = DmSecurityState(
        policy: DmPolicyAllowlist,
        allowlist: @[]
      )
      addToAllowlist(state, UserId("newuser"))
      check state.allowlist.contains(UserId("newuser"))

    test "removeFromAllowlist removes user":
      var state = DmSecurityState(
        policy: DmPolicyAllowlist,
        allowlist: @[UserId("user1"), UserId("user2")]
      )
      removeFromAllowlist(state, UserId("user1"))
      check state.allowlist.len == 1
      check state.allowlist.contains(UserId("user2"))

    test "newPairingState generates code":
      let state = newPairingState()
      check state.code.len == 6
      check strutils.allCharsInSet(state.code, {'0'..'9'})
      check isValid(state) == true

    test "validatePairingCode returns true for correct code":
      let pairing = newPairingState()
      check validatePairingCode(pairing, pairing.code) == true

    test "validatePairingCode returns false for wrong code":
      let pairing = newPairingState()
      check validatePairingCode(pairing, "000000") == false

    test "pairing expires after max attempts":
      var pairing = newPairingState()
      pairing.attempts = MaxPairingAttempts
      check isValid(pairing) == false

when isMainModule:
  runTests()