|
| 1 | +import unittest |
| 2 | +import textwrap |
| 3 | +import httpretty |
| 4 | + |
| 5 | +from six import u |
| 6 | +from expects import * |
| 7 | +from opentok import OpenTok, SipCall, __version__ |
| 8 | + |
| 9 | +from .validate_jwt import validate_jwt_header |
| 10 | + |
| 11 | +class OpenTokSipCallTest(unittest.TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.api_key = u('123456') |
| 14 | + self.api_secret = u('1234567890abcdef1234567890abcdef1234567890') |
| 15 | + self.opentok = OpenTok(self.api_key, self.api_secret) |
| 16 | + |
| 17 | + self.session_id = u('SESSIONID') |
| 18 | + self.token = u('TOKEN') |
| 19 | + self. sip_uri = u( 'sip:[email protected];transport=tls') |
| 20 | + |
| 21 | + @httpretty.activate |
| 22 | + def test_sip_call_with_required_parameters(self): |
| 23 | + """ |
| 24 | + Test dial() method using just the required parameters: session_id, token, sip_uri |
| 25 | + """ |
| 26 | + sip_call = SipCall({ |
| 27 | + u('id'): u('b0a5a8c7-dc38-459f-a48d-a7f2008da853'), |
| 28 | + u('connectionId'): u('e9f8c166-6c67-440d-994a-04fb6dfed007'), |
| 29 | + u('streamId'): u('482bce73-f882-40fd-8ca5-cb74ff416036') |
| 30 | + }) |
| 31 | + |
| 32 | + httpretty.register_uri( |
| 33 | + httpretty.POST, |
| 34 | + u('https://api.opentok.com/v2/project/{0}/dial').format(self.api_key), |
| 35 | + body=textwrap.dedent(u("""\ |
| 36 | + { |
| 37 | + "id": "b0a5a8c7-dc38-459f-a48d-a7f2008da853", |
| 38 | + "connectionId": "e9f8c166-6c67-440d-994a-04fb6dfed007", |
| 39 | + "streamId": "482bce73-f882-40fd-8ca5-cb74ff416036" |
| 40 | + }""")), |
| 41 | + status=200, |
| 42 | + content_type=u('application/json') |
| 43 | + ) |
| 44 | + |
| 45 | + sip_call_response = self.opentok.dial(self.session_id, self.token, self.sip_uri) |
| 46 | + validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')]) |
| 47 | + expect(httpretty.last_request().headers[u('user-agent')]).to(contain( |
| 48 | + u('OpenTok-Python-SDK/')+__version__)) |
| 49 | + expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json'))) |
| 50 | + expect(sip_call_response).to(be_an(SipCall)) |
| 51 | + expect(sip_call_response).to(have_property(u('id'), sip_call.id)) |
| 52 | + expect(sip_call_response).to(have_property(u('connectionId'), sip_call.connectionId)) |
| 53 | + expect(sip_call_response).to(have_property(u('streamId'), sip_call.streamId)) |
| 54 | + |
| 55 | + @httpretty.activate |
| 56 | + def test_sip_call_with_aditional_options(self): |
| 57 | + """ |
| 58 | + Test dial() method with aditional options |
| 59 | + """ |
| 60 | + sip_call = SipCall({ |
| 61 | + u('id'): u('b0a5a8c7-dc38-459f-a48d-a7f2008da853'), |
| 62 | + u('connectionId'): u('e9f8c166-6c67-440d-994a-04fb6dfed007'), |
| 63 | + u('streamId'): u('482bce73-f882-40fd-8ca5-cb74ff416036') |
| 64 | + }) |
| 65 | + |
| 66 | + httpretty.register_uri( |
| 67 | + httpretty.POST, |
| 68 | + u('https://api.opentok.com/v2/project/{0}/dial').format(self.api_key), |
| 69 | + body=textwrap.dedent(u("""\ |
| 70 | + { |
| 71 | + "id": "b0a5a8c7-dc38-459f-a48d-a7f2008da853", |
| 72 | + "connectionId": "e9f8c166-6c67-440d-994a-04fb6dfed007", |
| 73 | + "streamId": "482bce73-f882-40fd-8ca5-cb74ff416036" |
| 74 | + }""")), |
| 75 | + status=200, |
| 76 | + content_type=u('application/json') |
| 77 | + ) |
| 78 | + |
| 79 | + # aditional options to establish the sip call |
| 80 | + options = { |
| 81 | + |
| 82 | + 'headers': { |
| 83 | + 'headerKey': 'headerValue' |
| 84 | + }, |
| 85 | + 'auth': { |
| 86 | + 'username': 'username', |
| 87 | + 'password': 'password' |
| 88 | + }, |
| 89 | + 'secure': True |
| 90 | + } |
| 91 | + |
| 92 | + sip_call_response = self.opentok.dial(self.session_id, self.token, self.sip_uri, options) |
| 93 | + validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')]) |
| 94 | + expect(httpretty.last_request().headers[u('user-agent')]).to(contain( |
| 95 | + u('OpenTok-Python-SDK/')+__version__)) |
| 96 | + expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json'))) |
| 97 | + expect(sip_call_response).to(be_an(SipCall)) |
| 98 | + expect(sip_call_response).to(have_property(u('id'), sip_call.id)) |
| 99 | + expect(sip_call_response).to(have_property(u('connectionId'), sip_call.connectionId)) |
| 100 | + expect(sip_call_response).to(have_property(u('streamId'), sip_call.streamId)) |
0 commit comments