|
@@ -1,4 +1,7 @@
|
1
|
1
|
import jwt
|
|
2
|
+import datetime
|
|
3
|
+import calendar
|
|
4
|
+
|
2
|
5
|
from tracim.lib.jitsi_meet.token import JitsiMeetToken
|
3
|
6
|
from tracim.lib.jitsi_meet.token import JitsiMeetContext
|
4
|
7
|
from tracim.lib.jitsi_meet.token import JitsiMeetUser
|
|
@@ -10,12 +13,19 @@ class TestJitsiMeetToken(object):
|
10
|
13
|
TOKEN_SECRET = 'secret'
|
11
|
14
|
TOKEN_ALG = 'HS256'
|
12
|
15
|
TOKEN_DURATION = 60
|
13
|
|
- TOKEN_USER = JitsiMeetUser(name='john', email='john@doe', jitsi_meet_user_id='0')
|
|
16
|
+ TOKEN_USER = JitsiMeetUser(name='john',
|
|
17
|
+ email='john@doe',
|
|
18
|
+ jitsi_meet_user_id='0')
|
14
|
19
|
TOKEN_CONTEXT = JitsiMeetContext(user=TOKEN_USER, group='test')
|
15
|
20
|
ROOM = 'room'
|
16
|
21
|
DOMAIN = 'tracim'
|
|
22
|
+ ISSUE_DATE = datetime.datetime.utcfromtimestamp(0)
|
17
|
23
|
|
18
|
|
- def test_unit__generate_token(self) -> None:
|
|
24
|
+ def test_unit__generate_token_check_value(self) -> None:
|
|
25
|
+ """
|
|
26
|
+ Test all value without checking jwt validity (timestamp 0 as issue date)
|
|
27
|
+ :return: Nothing
|
|
28
|
+ """
|
19
|
29
|
|
20
|
30
|
token = JitsiMeetToken(
|
21
|
31
|
domain=self.DOMAIN,
|
|
@@ -26,14 +36,150 @@ class TestJitsiMeetToken(object):
|
26
|
36
|
duration=self.TOKEN_DURATION,
|
27
|
37
|
context=self.TOKEN_CONTEXT,
|
28
|
38
|
)
|
29
|
|
- str_token = token.generate()
|
|
39
|
+
|
|
40
|
+ str_token = token.generate(issue_date=self.ISSUE_DATE)
|
30
|
41
|
decoded_token = jwt.decode(str_token,
|
31
|
42
|
key=self.TOKEN_SECRET,
|
32
|
43
|
algorithm=self.TOKEN_ALG,
|
33
|
44
|
audience='*',
|
34
|
45
|
issuer=self.TOKEN_APP_ID,
|
|
46
|
+ verify=False
|
35
|
47
|
)
|
|
48
|
+
|
|
49
|
+ assert decoded_token.get('iss') == self.TOKEN_APP_ID
|
36
|
50
|
assert decoded_token.get('room') == self.ROOM
|
37
|
|
- assert decoded_token.get('iat')
|
|
51
|
+ assert decoded_token.get('aud') == "*"
|
|
52
|
+ assert decoded_token.get('context')
|
|
53
|
+ assert decoded_token['context'].get('user')
|
|
54
|
+ assert decoded_token['context']['user'].get('name') == 'john'
|
|
55
|
+ assert decoded_token['context']['user'].get('email') == 'john@doe'
|
|
56
|
+ assert decoded_token['context']['user'].get('id') == '0'
|
|
57
|
+ assert decoded_token['context'].get('group') == 'test'
|
|
58
|
+ assert decoded_token.get('iat') == calendar.timegm(
|
|
59
|
+ self.ISSUE_DATE.utctimetuple()
|
|
60
|
+ )
|
38
|
61
|
assert decoded_token.get('iat') == decoded_token.get('nbf')
|
39
|
62
|
assert decoded_token.get('exp') == decoded_token.get('iat') + self.TOKEN_DURATION # nopep8
|
|
63
|
+
|
|
64
|
+ def test_unit__generate_token_check_jwt(self) -> None:
|
|
65
|
+ """
|
|
66
|
+ Test only JWT validity and time params with default issue date
|
|
67
|
+ :return: Nothing
|
|
68
|
+ """
|
|
69
|
+ token = JitsiMeetToken(
|
|
70
|
+ domain=self.DOMAIN,
|
|
71
|
+ room=self.ROOM,
|
|
72
|
+ app_id=self.TOKEN_APP_ID,
|
|
73
|
+ secret=self.TOKEN_SECRET,
|
|
74
|
+ alg=self.TOKEN_ALG,
|
|
75
|
+ duration=self.TOKEN_DURATION,
|
|
76
|
+ context=self.TOKEN_CONTEXT,
|
|
77
|
+ )
|
|
78
|
+
|
|
79
|
+ str_token = token.generate()
|
|
80
|
+ decoded_token = jwt.decode(str_token,
|
|
81
|
+ key=self.TOKEN_SECRET,
|
|
82
|
+ algorithm=self.TOKEN_ALG,
|
|
83
|
+ audience='*',
|
|
84
|
+ issuer=self.TOKEN_APP_ID,
|
|
85
|
+ )
|
|
86
|
+
|
|
87
|
+ assert decoded_token.get('iat') == decoded_token.get('nbf')
|
|
88
|
+ assert decoded_token.get('exp') == decoded_token.get('iat') + self.TOKEN_DURATION # nopep8
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+class TestJitsiMeetUser(object):
|
|
92
|
+
|
|
93
|
+ def test_unit__as_dict(self):
|
|
94
|
+
|
|
95
|
+ user1 = JitsiMeetUser(jitsi_meet_user_id='1')
|
|
96
|
+ assert user1.as_dict() == {'id': '1'}
|
|
97
|
+
|
|
98
|
+ user2 = JitsiMeetUser(jitsi_meet_user_id='2', name='john')
|
|
99
|
+ assert user2.as_dict() == {'id': '2', 'name': 'john'}
|
|
100
|
+
|
|
101
|
+ user3 = JitsiMeetUser(jitsi_meet_user_id='3', name='john', email='a@b')
|
|
102
|
+ assert user3.as_dict() == {'id': '3', 'name': 'john', 'email': 'a@b'}
|
|
103
|
+
|
|
104
|
+ user4 = JitsiMeetUser(
|
|
105
|
+ jitsi_meet_user_id='4',
|
|
106
|
+ name='john',
|
|
107
|
+ email='a@b',
|
|
108
|
+ avatar_url='http://mysuperavatar/avatar.png'
|
|
109
|
+ )
|
|
110
|
+ assert user4.as_dict() == {
|
|
111
|
+ 'id': '4',
|
|
112
|
+ 'name': 'john',
|
|
113
|
+ 'email': 'a@b',
|
|
114
|
+ 'avatar': 'http://mysuperavatar/avatar.png',
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ user5 = JitsiMeetUser(
|
|
118
|
+ jitsi_meet_user_id='5',
|
|
119
|
+ name='john',
|
|
120
|
+ avatar_url='http://mysuperavatar/avatar.png'
|
|
121
|
+ )
|
|
122
|
+ assert user5.as_dict() == {
|
|
123
|
+ 'id': '5',
|
|
124
|
+ 'name': 'john',
|
|
125
|
+ 'avatar': 'http://mysuperavatar/avatar.png',
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ user6 = JitsiMeetUser(
|
|
129
|
+ jitsi_meet_user_id='6',
|
|
130
|
+ email='a@b',
|
|
131
|
+ avatar_url='http://mysuperavatar/avatar.png'
|
|
132
|
+ )
|
|
133
|
+ assert user6.as_dict() == {
|
|
134
|
+ 'id': '6',
|
|
135
|
+ 'email': 'a@b',
|
|
136
|
+ 'avatar': 'http://mysuperavatar/avatar.png',
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ user7 = JitsiMeetUser(
|
|
140
|
+ jitsi_meet_user_id='7',
|
|
141
|
+ email='a@b',
|
|
142
|
+ )
|
|
143
|
+ assert user7.as_dict() == {
|
|
144
|
+ 'id': '7',
|
|
145
|
+ 'email': 'a@b',
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ user8 = JitsiMeetUser(
|
|
149
|
+ jitsi_meet_user_id='8',
|
|
150
|
+ avatar_url='http://mysuperavatar/avatar.png'
|
|
151
|
+ )
|
|
152
|
+ assert user8.as_dict() == {
|
|
153
|
+ 'id': '8',
|
|
154
|
+ 'avatar': 'http://mysuperavatar/avatar.png',
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+class TestJitsiMeetContext(object):
|
|
159
|
+
|
|
160
|
+ def test_unit__as_dict(self):
|
|
161
|
+
|
|
162
|
+ context = JitsiMeetContext()
|
|
163
|
+ assert context.as_dict() == {
|
|
164
|
+ 'group': 'default'
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ context = JitsiMeetContext(group='group1')
|
|
168
|
+ assert context.as_dict() == {
|
|
169
|
+ 'group': 'group1'
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ user = JitsiMeetUser(jitsi_meet_user_id='user')
|
|
173
|
+ context = JitsiMeetContext(group='group1', user=user)
|
|
174
|
+ assert context.as_dict() == {
|
|
175
|
+ 'group': 'group1',
|
|
176
|
+ 'user': user.as_dict()
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ callee = JitsiMeetUser(jitsi_meet_user_id='callee')
|
|
180
|
+ context = JitsiMeetContext(group='group1', user=user, callee=callee)
|
|
181
|
+ assert context.as_dict() == {
|
|
182
|
+ 'group': 'group1',
|
|
183
|
+ 'user': user.as_dict(),
|
|
184
|
+ 'callee': callee.as_dict()
|
|
185
|
+ }
|