/
test_stream.py
172 lines (154 loc) · 7.23 KB
/
test_stream.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import unittest
from six import text_type, u, b, PY2, PY3
from opentok import OpenTok, Stream, StreamList, __version__, SetStreamClassError
import httpretty
import json
import textwrap
from expects import *
from .validate_jwt import validate_jwt_header
class OpenTokStreamTest(unittest.TestCase):
def setUp(self):
self.api_key = u('123456')
self.api_secret = u('1234567890abcdef1234567890abcdef1234567890')
self.opentok = OpenTok(self.api_key, self.api_secret)
self.session_id = u('SESSIONID')
self.stream_id = u('8b732909-0a06-46a2-8ea8-074e64d43422')
@httpretty.activate
def test_get_stream(self):
stream = Stream({
u('id'): u('8b732909-0a06-46a2-8ea8-074e64d43422'),
u('videoType'): u('camera'),
u('name'): u(''),
u('layoutClassList'): ['full']
})
httpretty.register_uri(
httpretty.GET,
u('https://api.opentok.com/v2/project/{0}/session/{1}/stream/{2}').format(self.api_key, self.session_id, self.stream_id),
body=textwrap.dedent(u("""\
{
"id": "8b732909-0a06-46a2-8ea8-074e64d43422",
"videoType": "camera",
"name": "",
"layoutClassList": ["full"]
}""")),
status=200,
content_type=u('application/json')
)
stream_response = self.opentok.get_stream(self.session_id, self.stream_id)
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(u('OpenTok-Python-SDK/')+__version__))
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
expect(stream_response).to(be_an(Stream))
expect(stream_response).to(have_property(u('id'), stream.id))
expect(stream_response).to(have_property(u('videoType'), stream.videoType))
expect(stream_response).to(have_property(u('name'), stream.name))
expect(stream_response).to(have_property(u('layoutClassList'), stream.layoutClassList))
expect(list(stream_response.layoutClassList)).to(have_length(1))
@httpretty.activate
def test_get_stream_with_name(self):
stream = Stream({
u('id'): u('8b732909-0a06-46a2-8ea8-074e64d43422'),
u('videoType'): u('camera'),
u('name'): u('stream name'),
u('layoutClassList'): ['full']
})
httpretty.register_uri(
httpretty.GET,
u('https://api.opentok.com/v2/project/{0}/session/{1}/stream/{2}').format(self.api_key, self.session_id, self.stream_id),
body=textwrap.dedent(u("""\
{
"id": "8b732909-0a06-46a2-8ea8-074e64d43422",
"videoType": "camera",
"name": "stream name",
"layoutClassList": ["full"]
}""")),
status=200,
content_type=u('application/json')
)
stream_response = self.opentok.get_stream(self.session_id, self.stream_id)
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(u('OpenTok-Python-SDK/')+__version__))
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
expect(stream_response).to(be_an(Stream))
expect(stream_response).to(have_property(u('id'), stream.id))
expect(stream_response).to(have_property(u('videoType'), stream.videoType))
expect(stream_response).to(have_property(u('name'), stream.name))
expect(stream_response).to(have_property(u('layoutClassList'), stream.layoutClassList))
expect(list(stream_response.layoutClassList)).to(have_length(1))
@httpretty.activate
def test_get_stream_list(self):
httpretty.register_uri(
httpretty.GET,
u('https://api.opentok.com/v2/project/{0}/session/{1}/stream').format(self.api_key, self.session_id),
body=textwrap.dedent(u("""\
{
"count": 2,
"items": [
{
"id": "8b732909-0a06-46a2-8ea8-074e64d43422",
"videoType": "camera",
"name": "stream1",
"layoutClassList": ["full"]
},
{
"id": "7b732909-0a06-46a2-8ea8-074e64d43423",
"videoType": "camera",
"name": "stream2",
"layoutClassList": ["full"]
}
]
}""")),
status=200,
content_type=u('application/json')
)
stream_list = self.opentok.list_streams(self.session_id)
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(u('OpenTok-Python-SDK/')+__version__))
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
expect(stream_list).to(be_an(StreamList))
expect(stream_list).to(have_property(u('count'), 2))
expect(list(stream_list.items)).to(have_length(2))
@httpretty.activate
def test_set_stream_class_lists(self):
""" Test set stream class functionality """
payload = [
{'id': '7b09ec3c-26f9-43d7-8197-f608f13d4fb6', 'layoutClassList': ['focus']},
{'id': '567bc941-6ea0-4c69-97fc-70a740b68976', 'layoutClassList': ['top']},
{'id': '307dc941-0450-4c09-975c-705740d08970', 'layoutClassList': ['bottom']}
]
httpretty.register_uri(
httpretty.PUT,
u('https://api.opentok.com/v2/project/{0}/session/{1}/stream').format(
self.api_key,
self.session_id
),
status=200,
content_type=u('application/json')
)
self.opentok.set_stream_class_lists(self.session_id, payload)
validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')])
expect(httpretty.last_request().headers[u('user-agent')]).to(contain(
u('OpenTok-Python-SDK/')+__version__))
expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json')))
@httpretty.activate
def test_set_stream_class_lists_throws_exception(self):
""" Test invalid request in set stream class list """
#invalid payload
payload = [
{'id': '7b09ec3c-26f9-43d7-8197-f608f13d4fb6'}
]
httpretty.register_uri(
httpretty.PUT,
u('https://api.opentok.com/v2/project/{0}/session/{1}/stream').format(
self.api_key,
self.session_id
),
status=400,
content_type=u('application/json')
)
self.assertRaises(
SetStreamClassError,
self.opentok.set_stream_class_lists,
self.session_id,
payload
)