|
| 1 | +import unittest |
| 2 | +from six import text_type, u, b, PY2, PY3 |
| 3 | +from six.moves.urllib.parse import parse_qs |
| 4 | +from nose.tools import raises |
| 5 | +from sure import expect |
| 6 | +import httpretty |
| 7 | + |
| 8 | +from opentok import OpenTok, Session, OpenTokException, __version__ |
| 9 | + |
| 10 | +class OpenTokSessionCreationTest(unittest.TestCase): |
| 11 | + def setUp(self): |
| 12 | + self.api_key = u('123456') |
| 13 | + self.api_secret = u('1234567890abcdef1234567890abcdef1234567890') |
| 14 | + # self.api_key = u('854511') |
| 15 | + # self.api_secret = u('***REMOVED***') |
| 16 | + self.opentok = OpenTok(self.api_key, self.api_secret) |
| 17 | + |
| 18 | + @httpretty.activate |
| 19 | + def test_create_default_session(self): |
| 20 | + httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), |
| 21 | + body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), |
| 22 | + status=200, |
| 23 | + content_type=u('text/xml')) |
| 24 | + |
| 25 | + session = self.opentok.create_session() |
| 26 | + |
| 27 | + expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) |
| 28 | + expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) |
| 29 | + expect(session).to.be.a(Session) |
| 30 | + expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) |
| 31 | + expect(session).to.have.property(u('p2p')).being.equal(False) |
| 32 | + expect(session).to.have.property(u('location')).being.equal(None) |
| 33 | + |
| 34 | + @httpretty.activate |
| 35 | + def test_create_p2p_session(self): |
| 36 | + httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), |
| 37 | + body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), |
| 38 | + status=200, |
| 39 | + content_type=u('text/xml')) |
| 40 | + |
| 41 | + session = self.opentok.create_session(p2p=True) |
| 42 | + |
| 43 | + expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) |
| 44 | + expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) |
| 45 | + expect(httpretty.last_request().body).to.equal(b('p2p.preference=enabled')) |
| 46 | + expect(session).to.be.a(Session) |
| 47 | + expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) |
| 48 | + expect(session).to.have.property(u('p2p')).being.equal(True) |
| 49 | + expect(session).to.have.property(u('location')).being.equal(None) |
| 50 | + |
| 51 | + @httpretty.activate |
| 52 | + def test_create_session_with_location_hint(self): |
| 53 | + httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), |
| 54 | + body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), |
| 55 | + status=200, |
| 56 | + content_type=u('text/xml')) |
| 57 | + |
| 58 | + session = self.opentok.create_session(location='12.34.56.78') |
| 59 | + |
| 60 | + expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) |
| 61 | + expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) |
| 62 | + expect(httpretty.last_request().body).to.equal(b('location=12.34.56.78')) |
| 63 | + expect(session).to.be.a(Session) |
| 64 | + expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) |
| 65 | + expect(session).to.have.property(u('p2p')).being.equal(False) |
| 66 | + expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) |
| 67 | + |
| 68 | + @httpretty.activate |
| 69 | + def test_create_p2p_session_with_location_hint(self): |
| 70 | + httpretty.register_uri(httpretty.POST, u('https://api.opentok.com/session/create'), |
| 71 | + body=u('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sessions><Session><session_id>1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg</session_id><partner_id>123456</partner_id><create_dt>Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>'), |
| 72 | + status=200, |
| 73 | + content_type=u('text/xml')) |
| 74 | + |
| 75 | + session = self.opentok.create_session(location='12.34.56.78', p2p=True) |
| 76 | + |
| 77 | + expect(httpretty.last_request().headers[u('x-tb-partner-auth')]).to.equal(self.api_key+u(':')+self.api_secret) |
| 78 | + expect(httpretty.last_request().headers[u('user-agent')]).to.contain(u('OpenTok-Python-SDK/')+__version__) |
| 79 | + # ordering of keys is non-deterministic, must parse the body to see if it is correct |
| 80 | + body = parse_qs(httpretty.last_request().body) |
| 81 | + expect(body).to.have.key(b('location')).being.equal([b('12.34.56.78')]) |
| 82 | + expect(body).to.have.key(b('p2p.preference')).being.equal([b('enabled')]) |
| 83 | + expect(session).to.be.a(Session) |
| 84 | + expect(session).to.have.property(u('session_id')).being.equal(u('1_MX4xMjM0NTZ-fk1vbiBNYXIgMTcgMDA6NDE6MzEgUERUIDIwMTR-MC42ODM3ODk1MzQ0OTQyODA4fg')) |
| 85 | + expect(session).to.have.property(u('p2p')).being.equal(True) |
| 86 | + expect(session).to.have.property(u('location')).being.equal(u('12.34.56.78')) |
| 87 | + |
| 88 | + # TODO: all the cases that throw exceptions |
| 89 | + # TODO: custom api_url requests |
| 90 | + |
0 commit comments