comparison tests/unit/test_plugin_xep_0338.py @ 4065:34c8e7e4fa52

tests (units): tests for plugin XEP-0338: fix 440
author Goffi <goffi@goffi.org>
date Tue, 30 May 2023 22:23:37 +0200
parents
children 4b842c1fb686
comparison
equal deleted inserted replaced
4064:08ee0e623e7e 4065:34c8e7e4fa52
1 #!/usr/bin/env python3
2
3 # Libervia: an XMPP client
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from twisted.words.xish import domish
21
22 from sat.plugins.plugin_xep_0338 import NS_JINGLE_GROUPING, XEP_0338
23 from sat.tools.xml_tools import parse
24
25
26 class TestXEP0338:
27 def test_parse_sdp(self, host):
28 """'group' attribute in SDP is correctly parsed"""
29 xep_0338 = XEP_0338(host)
30
31 call_data = {}
32 metadata = {}
33 media_type = "video"
34 application_data = {}
35 transport_data = {}
36
37 # SDP: a=group:BUNDLE audio video
38 attribute = "group"
39 parts = ["BUNDLE", "audio", "video"]
40
41 xep_0338._parse_sdp_a_trigger(
42 attribute,
43 parts,
44 call_data,
45 metadata,
46 media_type,
47 application_data,
48 transport_data,
49 )
50
51 assert metadata == {"group": {"BUNDLE": ["audio", "video"]}}
52
53 def test_generate_sdp(self, host):
54 """'group' attribute in SDP is correctly generated"""
55 xep_0338 = XEP_0338(host)
56
57 session = {"metadata": {"group": {"BUNDLE": ["audio", "video"]}}}
58 sdp_lines = []
59 local = True
60
61 xep_0338._generate_sdp_session_trigger(session, local, sdp_lines)
62
63 assert sdp_lines == ["a=group:BUNDLE audio video"]
64
65 def test_group_building(self, host, client):
66 """<group> element are built from session in session init trigger"""
67 xep_0338 = XEP_0338(host)
68
69 session = {
70 "jingle_elt": domish.Element((None, "jingle")),
71 "metadata": {"group": {"BUNDLE": ["audio", "video"]}},
72 }
73 content_name = "audio"
74 media = "audio"
75 media_data = {}
76 desc_elt = domish.Element((None, "description"))
77
78 xep_0338._jingle_session_init_trigger(
79 client, session, content_name, media, media_data, desc_elt
80 )
81
82 group_elts = list(session["jingle_elt"].elements(NS_JINGLE_GROUPING, "group"))
83 assert len(group_elts) == 1
84 group_elt = group_elts[0]
85 assert group_elt["semantics"] == "BUNDLE"
86 content_names = [
87 content_elt["name"]
88 for content_elt in group_elt.elements(NS_JINGLE_GROUPING, "content")
89 ]
90 assert content_names == ["audio", "video"]
91
92 def test_group_parsing(self, host, client):
93 """<group> elements are correctly parsed in jingle_handler trigger"""
94 xep_0338 = XEP_0338(host)
95
96 action = xep_0338._j.A_SESSION_INITIATE
97 session = {
98 "contents": ["audio", "video"],
99 "metadata": {},
100 }
101
102 raw_xml = """
103 <jingle xmlns='urn:xmpp:jingle:1'
104 action='session-initiate'
105 initiator='user@example.org/orchard'
106 sid='a73sjjvkla37jfea'>
107 <group xmlns='urn:xmpp:jingle:apps:grouping:0' semantics='BUNDLE'>
108 <content name='audio'/>
109 <content name='video'/>
110 </group>
111 <content creator='initiator' name='audio'>
112 <description xmlns='urn:xmpp:jingle:apps:rtp:1' media='audio'/>
113 </content>
114 <content creator='initiator' name='video'>
115 <description xmlns='urn:xmpp:jingle:apps:rtp:1' media='video'/>
116 </content>
117 </jingle>
118 """
119 session["jingle_elt"] = parse(raw_xml)
120
121 for content_elt in session["jingle_elt"].elements("urn:xmpp:jingle:1", "content"):
122 content_name = content_elt["name"]
123 desc_elt = next(
124 content_elt.elements("urn:xmpp:jingle:apps:rtp:1", "description")
125 )
126
127 xep_0338._jingle_handler_trigger(
128 client, action, session, content_name, desc_elt
129 )
130
131 group_elts = list(session["jingle_elt"].elements(NS_JINGLE_GROUPING, "group"))
132 assert len(group_elts) == 1
133 group_elt = group_elts[0]
134 assert group_elt["semantics"] == "BUNDLE"
135 content_names = [
136 content_elt["name"]
137 for content_elt in group_elt.elements(NS_JINGLE_GROUPING, "content")
138 ]
139 assert content_names == ["audio", "video"]