Bladeren bron

add more tests for jitsi-meet

Guénaël Muller 6 jaren geleden
bovenliggende
commit
09da73c2a2

+ 128 - 0
tracim/tracim/tests/library/test_jitsi_meet_room.py Bestand weergeven

@@ -0,0 +1,128 @@
1
+from tracim.lib.jitsi_meet.room import JitsiMeetRoom
2
+from tracim.lib.jitsi_meet.room import JitsiMeetTokenNotActivated
3
+from tracim.lib.jitsi_meet.room import JitsiMeetNoTokenGenerator
4
+from tracim.lib.jitsi_meet.token import JitsiMeetUser
5
+from tracim.model.data import User
6
+from tracim.model.data import Workspace
7
+from nose.tools import raises
8
+from mock import patch, Mock
9
+
10
+
11
+class TestJitsiMeetRoom(object):
12
+
13
+    ROOM = 'room'
14
+    DOMAIN = 'tracim'
15
+    TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MTYyNzA3NzksImlzcyI6InRlc3QiLCJhdWQiOiIqIiwibmJmIjoxNTE2MjcwNzE5LCJyb29tIjoicm9vbSIsImlhdCI6MTUxNjI3MDcxOX0.zqFBUcPGjlCfXTjuFP7brqalY8TKlgcg6DUE72KhCx0'  # nopep8
16
+
17
+    def test_unit__generate_url_token_no_token(self) -> None:
18
+        with patch.object(JitsiMeetRoom,
19
+                          "__init__",
20
+                          lambda item, receivers: None):
21
+            jmr = JitsiMeetRoom(
22
+                receivers=Workspace()
23
+            )
24
+            jmr.domain = TestJitsiMeetRoom.DOMAIN
25
+            jmr.room = TestJitsiMeetRoom.ROOM
26
+            url = jmr.generate_url()
27
+            assert url == 'https://{domain}/{room}'.format(
28
+                domain=self.DOMAIN,
29
+                room=self.ROOM,
30
+                token=self.TOKEN
31
+            )
32
+
33
+    def test_unit__generate_url_token(self) -> None:
34
+        with patch.object(JitsiMeetRoom,
35
+                          "__init__",
36
+                          lambda item, receivers: None):
37
+            jmr = JitsiMeetRoom(
38
+                receivers=Workspace()
39
+            )
40
+            jmr.domain = TestJitsiMeetRoom.DOMAIN
41
+            jmr.room = TestJitsiMeetRoom.ROOM
42
+            url = jmr.generate_url(token=self.TOKEN)
43
+            assert url == 'https://{domain}/{room}?jwt={token}'.format(
44
+                domain=self.DOMAIN,
45
+                room=self.ROOM,
46
+                token=self.TOKEN
47
+            )
48
+
49
+    @raises(JitsiMeetNoTokenGenerator)
50
+    def test_unit__set_token_params_no_token_generator(self) -> None:
51
+        with patch.object(JitsiMeetRoom,
52
+                          "__init__",
53
+                          lambda item, receivers: None):
54
+            jmr = JitsiMeetRoom(
55
+                receivers=Workspace()
56
+            )
57
+            jmr.tracim_cfg = Mock()
58
+            jmr.tracim_cfg.JITSI_MEET_TOKEN_GENERATOR = None
59
+            jmr._set_token_params()
60
+
61
+    @raises(JitsiMeetTokenNotActivated)
62
+    def test_unit__generate_token_not_activated(self) -> None:
63
+        with patch.object(JitsiMeetRoom,
64
+                          "__init__",
65
+                          lambda item, receivers: None):
66
+            jmr = JitsiMeetRoom(
67
+                receivers=Workspace()
68
+            )
69
+            jmr.use_token = False
70
+            jmr.generate_token()
71
+
72
+    def test_unit__set_context_return_always_jitsi_meet_user(self):
73
+        with patch.object(JitsiMeetRoom,
74
+                          "__init__",
75
+                          lambda item, receivers: None):
76
+            jmr = JitsiMeetRoom(
77
+                receivers=Workspace()
78
+            )
79
+            jmr._set_context(issuer=None, receivers=Workspace())
80
+            assert jmr.context
81
+            assert hasattr(jmr.context, 'user')
82
+            assert isinstance(jmr.context.user, JitsiMeetUser)
83
+
84
+            jmr2 = JitsiMeetRoom(
85
+                receivers=Workspace()
86
+            )
87
+            jmr2._set_context(issuer=JitsiMeetUser(jitsi_meet_user_id='user'),
88
+                              receivers=Workspace())
89
+            assert jmr2.context
90
+            assert hasattr(jmr2.context, 'user')
91
+            assert isinstance(jmr2.context.user, JitsiMeetUser)
92
+
93
+            jmr3 = JitsiMeetRoom(
94
+                receivers=Workspace()
95
+            )
96
+            jmr3._set_context(issuer=User(), receivers=Workspace())
97
+            assert jmr3.context
98
+            assert hasattr(jmr3.context, 'user')
99
+            assert isinstance(jmr3.context.user, JitsiMeetUser)
100
+
101
+    def test_unit__generate_room_name(self):
102
+        with patch.object(JitsiMeetRoom,
103
+                          "__init__",
104
+                          lambda item, receivers: None):
105
+            jmr = JitsiMeetRoom(
106
+                receivers=Workspace()
107
+            )
108
+            mock = Mock()
109
+            jmr.tracim_cfg = Mock()
110
+            jmr.tracim_cfg.TRACIM_INSTANCE_UUID = 'myuuid'
111
+            mock.workspace_id = 1
112
+            mock.label = 'myroom'
113
+            assert jmr._generate_room_name(mock) == 'myuuid1myroom'
114
+
115
+    def test_unit__generate_room_name_no_special_character(self):
116
+        with patch.object(JitsiMeetRoom,
117
+                          "__init__",
118
+                          lambda item, receivers: None):
119
+            jmr = JitsiMeetRoom(
120
+                receivers=Workspace()
121
+            )
122
+            mock = Mock()
123
+            jmr.tracim_cfg = Mock()
124
+            # TODO - G.M - 14-02-2018 - Be exhaustive about special char ?
125
+            jmr.tracim_cfg.TRACIM_INSTANCE_UUID = '*%hél\/ <>."{}|+&-@"~]=lo{à!ll;:'  # nopep8
126
+            mock.workspace_id = 1
127
+            mock.label = 'myroom'
128
+            assert jmr._generate_room_name(mock) == 'helloall1myroom'

+ 150 - 4
tracim/tracim/tests/library/test_jitsi_meet_token.py Bestand weergeven

@@ -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
+        }