This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_mtc.py
125 lines (105 loc) · 3.06 KB
/
test_mtc.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
""" Test MessageTrustContext """
import pytest
from aries_staticagent.mtc import (
MessageTrustContext,
ContextsConflict,
NONE,
SIZE_OK,
DESERIALIZE_OK,
KEYS_OK,
CONFIDENTIALITY,
AUTHENTICATED_ORIGIN,
UNIQUENESS
)
@pytest.mark.parametrize(
'plus, minus, expected',
[
(SIZE_OK, NONE, 'mtc: +size_ok'),
(
SIZE_OK | DESERIALIZE_OK,
NONE,
'mtc: +size_ok +deserialize_ok'
),
(
KEYS_OK | SIZE_OK | DESERIALIZE_OK,
NONE,
'mtc: +size_ok +deserialize_ok +keys_ok'
),
(
SIZE_OK,
DESERIALIZE_OK,
'mtc: +size_ok -deserialize_ok'
),
(
KEYS_OK | SIZE_OK | DESERIALIZE_OK,
CONFIDENTIALITY | UNIQUENESS,
'mtc: +size_ok +deserialize_ok +keys_ok '
'-confidentiality -uniqueness'
),
]
)
def test_str(plus, minus, expected):
""" Test stringifying """
mtc = MessageTrustContext(plus, minus)
assert str(mtc) == expected
@pytest.mark.parametrize(
'plus, minus',
[
(
SIZE_OK,
SIZE_OK
),
(
SIZE_OK | DESERIALIZE_OK,
SIZE_OK
),
(
SIZE_OK | DESERIALIZE_OK | CONFIDENTIALITY,
CONFIDENTIALITY
),
]
)
def test_overlapping_affirm_denied_fail(plus, minus):
""" Test overlapping affirm and denied flags raise error """
with pytest.raises(ContextsConflict):
MessageTrustContext(plus, minus)
def test_cannot_alter_attributes():
""" Test that affirmed and denied attributes can't be altered """
mtc = MessageTrustContext()
assert mtc.affirmed == NONE
assert mtc.denied == NONE
with pytest.raises(AttributeError):
mtc.affirmed = CONFIDENTIALITY
with pytest.raises(AttributeError):
mtc.denied = CONFIDENTIALITY
def test_get_set():
""" Test using __getitem__ and __setitem__ for retrieving and changing
contexts.
"""
mtc = MessageTrustContext()
mtc[CONFIDENTIALITY] = True
assert mtc.affirmed == CONFIDENTIALITY
assert mtc[CONFIDENTIALITY]
assert mtc.denied == NONE
mtc[SIZE_OK] = True
assert mtc.affirmed == CONFIDENTIALITY | SIZE_OK
assert mtc[CONFIDENTIALITY | SIZE_OK]
assert mtc.denied == NONE
mtc[SIZE_OK] = False
assert mtc.affirmed == CONFIDENTIALITY
assert mtc[CONFIDENTIALITY]
assert not mtc[CONFIDENTIALITY | SIZE_OK]
assert mtc.denied == SIZE_OK
mtc[AUTHENTICATED_ORIGIN] = True
assert mtc.affirmed == CONFIDENTIALITY | AUTHENTICATED_ORIGIN
assert mtc[CONFIDENTIALITY | AUTHENTICATED_ORIGIN]
assert mtc.denied == SIZE_OK
mtc[AUTHENTICATED_ORIGIN] = None
assert mtc.affirmed == CONFIDENTIALITY
assert mtc.denied == SIZE_OK
def test_bad_set():
""" Test that bad values for set raise error. """
with pytest.raises(TypeError):
MessageTrustContext()['asdf'] = True
with pytest.raises(TypeError):
MessageTrustContext()[CONFIDENTIALITY] = 10