|
| 1 | +import unittest |
| 2 | +import textwrap |
| 3 | +import httpretty |
| 4 | +import json |
| 5 | +from sure import expect |
| 6 | + |
| 7 | +from six import u, PY2, PY3 |
| 8 | +from expects import * |
| 9 | +from opentok import Client, WebhookAudioConnection, __version__ |
| 10 | +from opentok.exceptions import InvalidWebsocketOptionsError, InvalidMediaModeError |
| 11 | +from .validate_jwt import validate_jwt_header |
| 12 | + |
| 13 | + |
| 14 | +class OpenTokAudioStreamerLiteTest(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + self.api_key = u("123456") |
| 17 | + self.api_secret = u("1234567890abcdef1234567890abcdef1234567890") |
| 18 | + self.opentok = Client(self.api_key, self.api_secret) |
| 19 | + self.session_id = u("2_MX4xMDBfjE0Mzc2NzY1NDgwMTJ-TjMzfn4") |
| 20 | + self.token = u("1234-5678-9012") |
| 21 | + self.response_body = textwrap.dedent( |
| 22 | + u( |
| 23 | + """ \ |
| 24 | + { |
| 25 | + "id": "b0a5a8c7-dc38-459f-a48d-a7f2008da853", |
| 26 | + "connectionId": "e9f8c166-6c67-440d-994a-04fb6dfed007" |
| 27 | + } |
| 28 | + """ |
| 29 | + ) |
| 30 | + ) |
| 31 | + |
| 32 | + @httpretty.activate |
| 33 | + def test_stream_audio_to_websocket(self): |
| 34 | + httpretty.register_uri( |
| 35 | + httpretty.POST, |
| 36 | + u(f"https://api.opentok.com/v2/project/{self.api_key}/connect"), |
| 37 | + body=self.response_body, |
| 38 | + status=200, |
| 39 | + content_type=u("application/json"), |
| 40 | + ) |
| 41 | + |
| 42 | + websocket_options = { |
| 43 | + "uri": "wss://service.com/ws-endpoint" |
| 44 | + } |
| 45 | + |
| 46 | + webhook_audio_connection = self.opentok.stream_audio_to_websocket(self.session_id, self.token, websocket_options) |
| 47 | + |
| 48 | + validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) |
| 49 | + expect(httpretty.last_request().headers[u("user-agent")]).to( |
| 50 | + contain(u("OpenTok-Python-SDK/") + __version__) |
| 51 | + ) |
| 52 | + expect(httpretty.last_request().headers[u("content-type")]).to( |
| 53 | + equal(u("application/json")) |
| 54 | + |
| 55 | + ) |
| 56 | + # non-deterministic json encoding. have to decode to test it properly |
| 57 | + if PY2: |
| 58 | + body = json.loads(httpretty.last_request().body) |
| 59 | + if PY3: |
| 60 | + body = json.loads(httpretty.last_request().body.decode("utf-8")) |
| 61 | + |
| 62 | + expect(body).to(have_key(u("token"))) |
| 63 | + expect(webhook_audio_connection).to(be_a(WebhookAudioConnection)) |
| 64 | + expect(webhook_audio_connection).to( |
| 65 | + have_property(u("id"), u("b0a5a8c7-dc38-459f-a48d-a7f2008da853")) |
| 66 | + ) |
| 67 | + expect(webhook_audio_connection).to( |
| 68 | + have_property(u("connectionId"), u("e9f8c166-6c67-440d-994a-04fb6dfed007")) |
| 69 | + ) |
| 70 | + |
| 71 | + @httpretty.activate |
| 72 | + def test_stream_audio_to_websocket_custom_options(self): |
| 73 | + httpretty.register_uri( |
| 74 | + httpretty.POST, |
| 75 | + u(f"https://api.opentok.com/v2/project/{self.api_key}/connect"), |
| 76 | + body=self.response_body, |
| 77 | + status=200, |
| 78 | + content_type=u("application/json"), |
| 79 | + ) |
| 80 | + |
| 81 | + websocket_options = { |
| 82 | + "uri": "wss://service.com/ws-endpoint", |
| 83 | + "streams": ["stream-id-1", "stream-id-2"], |
| 84 | + "headers": { |
| 85 | + "websocketHeader": "Sent via Audio Streamer API" |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + webhook_audio_connection = self.opentok.stream_audio_to_websocket(self.session_id, self.token, websocket_options) |
| 90 | + |
| 91 | + validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) |
| 92 | + expect(httpretty.last_request().headers[u("user-agent")]).to( |
| 93 | + contain(u("OpenTok-Python-SDK/") + __version__) |
| 94 | + ) |
| 95 | + expect(httpretty.last_request().headers[u("content-type")]).to( |
| 96 | + equal(u("application/json")) |
| 97 | + |
| 98 | + ) |
| 99 | + # non-deterministic json encoding. have to decode to test it properly |
| 100 | + if PY2: |
| 101 | + body = json.loads(httpretty.last_request().body) |
| 102 | + if PY3: |
| 103 | + body = json.loads(httpretty.last_request().body.decode("utf-8")) |
| 104 | + |
| 105 | + expect(body).to(have_key(u("token"))) |
| 106 | + expect(webhook_audio_connection).to(be_a(WebhookAudioConnection)) |
| 107 | + expect(webhook_audio_connection).to( |
| 108 | + have_property(u("id"), u("b0a5a8c7-dc38-459f-a48d-a7f2008da853")) |
| 109 | + ) |
| 110 | + expect(webhook_audio_connection).to( |
| 111 | + have_property(u("connectionId"), u("e9f8c166-6c67-440d-994a-04fb6dfed007")) |
| 112 | + ) |
| 113 | + |
| 114 | + @httpretty.activate |
| 115 | + def test_stream_audio_to_websocket_media_mode_error(self): |
| 116 | + httpretty.register_uri( |
| 117 | + httpretty.POST, |
| 118 | + u(f"https://api.opentok.com/v2/project/{self.api_key}/connect"), |
| 119 | + body={}, |
| 120 | + status=409, |
| 121 | + content_type=u("application/json"), |
| 122 | + ) |
| 123 | + |
| 124 | + websocket_options = { |
| 125 | + "uri": "wss://service.com/ws-endpoint" |
| 126 | + } |
| 127 | + |
| 128 | + session_id = "session-where-mediaMode=relayed-was-selected" |
| 129 | + |
| 130 | + with self.assertRaises(InvalidMediaModeError) as context: |
| 131 | + self.opentok.stream_audio_to_websocket(session_id, self.token, websocket_options) |
| 132 | + self.assertTrue("Only routed sessions are allowed to initiate Audio Streamer WebSocket connections." in str(context.exception)) |
| 133 | + |
| 134 | + validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")]) |
| 135 | + expect(httpretty.last_request().headers[u("user-agent")]).to( |
| 136 | + contain(u("OpenTok-Python-SDK/") + __version__) |
| 137 | + ) |
| 138 | + expect(httpretty.last_request().headers[u("content-type")]).to( |
| 139 | + equal(u("application/json")) |
| 140 | + ) |
| 141 | + |
| 142 | + def test_stream_audio_to_websocket_invalid_options_type_error(self): |
| 143 | + websocket_options = "wss://service.com/ws-endpoint" |
| 144 | + with self.assertRaises(InvalidWebsocketOptionsError) as context: |
| 145 | + self.opentok.stream_audio_to_websocket(self.session_id, self.token, websocket_options) |
| 146 | + self.assertTrue("Must pass websocket options as a dictionary." in str(context.exception)) |
| 147 | + |
| 148 | + def test_stream_audio_to_websocket_missing_uri_error(self): |
| 149 | + websocket_options = {} |
| 150 | + with self.assertRaises(InvalidWebsocketOptionsError) as context: |
| 151 | + self.opentok.stream_audio_to_websocket(self.session_id, self.token, websocket_options) |
| 152 | + self.assertTrue("Provide a webhook URI." in str(context.exception)) |
0 commit comments