|
| 1 | +import unittest |
| 2 | +from six import u |
| 3 | +from expects import * |
| 4 | +import httpretty |
| 5 | + |
| 6 | +from opentok import OpenTok, __version__, AuthError, ForceDisconnectError |
| 7 | +from .validate_jwt import validate_jwt_header |
| 8 | + |
| 9 | +class OpenTokForceDisconnectTest(unittest.TestCase): |
| 10 | + """" Class that contains test for force disconnect functionality """ |
| 11 | + |
| 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 | + self.session_id = u('SESSIONID') |
| 17 | + self.connection_id = u('CONNECTIONID') |
| 18 | + |
| 19 | + @httpretty.activate |
| 20 | + def test_force_disconnect(self): |
| 21 | + """ Method to test force disconnect functionality using an OpenTok instance """ |
| 22 | + |
| 23 | + httpretty.register_uri( |
| 24 | + httpretty.DELETE, |
| 25 | + u('https://api.opentok.com/v2/project/{0}/session/{1}/connection/{2}').format( |
| 26 | + self.api_key, |
| 27 | + self.session_id, |
| 28 | + self.connection_id |
| 29 | + ), |
| 30 | + status=204, |
| 31 | + content_type=u('application/json') |
| 32 | + ) |
| 33 | + |
| 34 | + self.opentok.force_disconnect(self.session_id, self.connection_id) |
| 35 | + |
| 36 | + validate_jwt_header(self, httpretty.last_request().headers[u('x-opentok-auth')]) |
| 37 | + expect(httpretty.last_request().headers[u('user-agent')]).to(contain(u('OpenTok-Python-SDK/')+__version__)) |
| 38 | + expect(httpretty.last_request().headers[u('content-type')]).to(equal(u('application/json'))) |
| 39 | + |
| 40 | + @httpretty.activate |
| 41 | + def test_throws_force_disconnect_exception(self): |
| 42 | + """ This method should throw a ForceDisconnectError """ |
| 43 | + |
| 44 | + httpretty.register_uri( |
| 45 | + httpretty.DELETE, |
| 46 | + u('https://api.opentok.com/v2/project/{0}/session/{1}/connection/{2}').format( |
| 47 | + self.api_key, |
| 48 | + self.session_id, |
| 49 | + self.connection_id |
| 50 | + ), |
| 51 | + status=400, |
| 52 | + content_type=u('application/json') |
| 53 | + ) |
| 54 | + |
| 55 | + self.assertRaises( |
| 56 | + ForceDisconnectError, |
| 57 | + self.opentok.force_disconnect, |
| 58 | + self.session_id, |
| 59 | + self.connection_id |
| 60 | + ) |
| 61 | + |
| 62 | + @httpretty.activate |
| 63 | + def test_throws_auth_exception(self): |
| 64 | + """ This method should throw an AuthError """ |
| 65 | + |
| 66 | + httpretty.register_uri( |
| 67 | + httpretty.DELETE, |
| 68 | + u('https://api.opentok.com/v2/project/{0}/session/{1}/connection/{2}').format( |
| 69 | + self.api_key, |
| 70 | + self.session_id, |
| 71 | + self.connection_id |
| 72 | + ), |
| 73 | + status=403, |
| 74 | + content_type=u('application/json') |
| 75 | + ) |
| 76 | + |
| 77 | + self.assertRaises( |
| 78 | + AuthError, |
| 79 | + self.opentok.force_disconnect, |
| 80 | + self.session_id, |
| 81 | + self.connection_id |
| 82 | + ) |
0 commit comments