ringcentral.test.testcase

  1import unittest
  2import requests_mock
  3import json
  4import re
  5from time import time
  6from datetime import date
  7from .. import SDK
  8
  9requests_mock.Mocker.TEST_PREFIX = 'test'
 10
 11
 12class TestCase(unittest.TestCase):
 13    def __init__(self, method_name=None):
 14        unittest.TestCase.__init__(self, method_name)
 15
 16    def get_sdk(self, mock):
 17        sdk = SDK('whatever', 'whatever', 'https://whatever', redirect_uri='https://whatever-redirect')
 18        self.authentication_mock(mock)
 19        sdk.platform().login(jwt='jwt-token')
 20        return sdk
 21
 22    def add(self, mock, method, url, body, status=200):
 23        mock.register_uri(
 24            method=method,
 25            url='https://whatever' + url,
 26            text= '' if body is None else json.dumps(body),
 27            headers={'Content-Type': 'application/json'},
 28            status_code=status
 29        )
 30
 31    def authentication_mock(self, mock):
 32        return self.add(mock, 'POST', '/restapi/oauth/token', {
 33            'access_token': 'ACCESS_TOKEN',
 34            'token_type': 'bearer',
 35            'expires_in': 3600,
 36            'refresh_token': 'REFRESH_TOKEN',
 37            'refresh_token_expires_in': 60480,
 38            'scope': 'SMS RCM Foo Boo',
 39            'expireTime': time() + 3600,
 40            'owner_id': 'foo'
 41        })
 42
 43    def logout_mock(self, mock):
 44        return self.add(mock, 'POST', '/restapi/oauth/revoke', {})
 45
 46    def presence_subscription_mock(self, mock, id='1', detailed=True):
 47        detailed = '?detailedTelephonyState=true' if detailed else ''
 48        expires_in = 15 * 60 * 60
 49
 50        return self.add(mock, 'POST', '/restapi/v1.0/subscription', {
 51            'eventFilters': ['/restapi/v1.0/account/~/extension/' + id + '/presence' + detailed],
 52            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
 53            'expiresIn': expires_in,
 54            'deliveryMode': {
 55                'transportType': 'WebSocket',
 56                'encryption': True,
 57                'address': '123_foo',
 58                'subscriberKey': 'sub-c-foo',
 59                'secretKey': 'sec-c-bar',
 60                'encryptionAlgorithm': 'AES',
 61                'encryptionKey': 'e0bMTqmumPfFUbwzppkSbA=='
 62            },
 63            'creationTime': date.today().isoformat(),
 64            'id': 'foo-bar-baz',
 65            'status': 'Active',
 66            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
 67        })
 68
 69    def refresh_mock(self, mock, failure=False, expires_in=3600):
 70        status = 200
 71        body = {
 72            'access_token': 'ACCESS_TOKEN_FROM_REFRESH',
 73            'token_type': 'bearer',
 74            'expires_in': expires_in,
 75            'refresh_token': 'REFRESH_TOKEN_FROM_REFRESH',
 76            'refresh_token_expires_in': 60480,
 77            'scope': 'SMS RCM Foo Boo',
 78            'expireTime': time() + expires_in,
 79            'owner_id': 'foo'
 80        }
 81
 82        if failure:
 83            status = 400
 84            body = {'message': 'Wrong token (mock)'}
 85
 86        return self.add(mock, 'POST', '/restapi/oauth/token', body, status)
 87
 88    def subscription_mock(self, mock, expires_in=54000, filters=None, id=None):
 89        if filters is None:
 90            filters = ['/restapi/v1.0/account/~/extension/1/presence']
 91
 92        return self.add(mock, 'POST' if not id else 'PUT', '/restapi/v1.0/subscription' + ('/' + id if id else ''), {
 93            'eventFilters': filters,
 94            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
 95            'expiresIn': expires_in,
 96            'deliveryMode': {
 97                'transportType': 'WebSocket',
 98                'encryption': False,
 99                'address': '123_foo',
100                'subscriberKey': 'sub-c-foo',
101                'secretKey': 'sec-c-bar'
102            },
103            'id': id if id else 'foo-bar-baz',
104            'creationTime': date.today().isoformat(),
105            'status': 'Active',
106            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
107        })
108
109    def delete_mock_with_body(self,mock):
110        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', {"keepAssetsInInventory": True,"records": [{"id": "123"}]})
111
112    def delete_mock_without_body(self,mock):
113        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', body=None)
class TestCase(unittest.case.TestCase):
 13class TestCase(unittest.TestCase):
 14    def __init__(self, method_name=None):
 15        unittest.TestCase.__init__(self, method_name)
 16
 17    def get_sdk(self, mock):
 18        sdk = SDK('whatever', 'whatever', 'https://whatever', redirect_uri='https://whatever-redirect')
 19        self.authentication_mock(mock)
 20        sdk.platform().login(jwt='jwt-token')
 21        return sdk
 22
 23    def add(self, mock, method, url, body, status=200):
 24        mock.register_uri(
 25            method=method,
 26            url='https://whatever' + url,
 27            text= '' if body is None else json.dumps(body),
 28            headers={'Content-Type': 'application/json'},
 29            status_code=status
 30        )
 31
 32    def authentication_mock(self, mock):
 33        return self.add(mock, 'POST', '/restapi/oauth/token', {
 34            'access_token': 'ACCESS_TOKEN',
 35            'token_type': 'bearer',
 36            'expires_in': 3600,
 37            'refresh_token': 'REFRESH_TOKEN',
 38            'refresh_token_expires_in': 60480,
 39            'scope': 'SMS RCM Foo Boo',
 40            'expireTime': time() + 3600,
 41            'owner_id': 'foo'
 42        })
 43
 44    def logout_mock(self, mock):
 45        return self.add(mock, 'POST', '/restapi/oauth/revoke', {})
 46
 47    def presence_subscription_mock(self, mock, id='1', detailed=True):
 48        detailed = '?detailedTelephonyState=true' if detailed else ''
 49        expires_in = 15 * 60 * 60
 50
 51        return self.add(mock, 'POST', '/restapi/v1.0/subscription', {
 52            'eventFilters': ['/restapi/v1.0/account/~/extension/' + id + '/presence' + detailed],
 53            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
 54            'expiresIn': expires_in,
 55            'deliveryMode': {
 56                'transportType': 'WebSocket',
 57                'encryption': True,
 58                'address': '123_foo',
 59                'subscriberKey': 'sub-c-foo',
 60                'secretKey': 'sec-c-bar',
 61                'encryptionAlgorithm': 'AES',
 62                'encryptionKey': 'e0bMTqmumPfFUbwzppkSbA=='
 63            },
 64            'creationTime': date.today().isoformat(),
 65            'id': 'foo-bar-baz',
 66            'status': 'Active',
 67            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
 68        })
 69
 70    def refresh_mock(self, mock, failure=False, expires_in=3600):
 71        status = 200
 72        body = {
 73            'access_token': 'ACCESS_TOKEN_FROM_REFRESH',
 74            'token_type': 'bearer',
 75            'expires_in': expires_in,
 76            'refresh_token': 'REFRESH_TOKEN_FROM_REFRESH',
 77            'refresh_token_expires_in': 60480,
 78            'scope': 'SMS RCM Foo Boo',
 79            'expireTime': time() + expires_in,
 80            'owner_id': 'foo'
 81        }
 82
 83        if failure:
 84            status = 400
 85            body = {'message': 'Wrong token (mock)'}
 86
 87        return self.add(mock, 'POST', '/restapi/oauth/token', body, status)
 88
 89    def subscription_mock(self, mock, expires_in=54000, filters=None, id=None):
 90        if filters is None:
 91            filters = ['/restapi/v1.0/account/~/extension/1/presence']
 92
 93        return self.add(mock, 'POST' if not id else 'PUT', '/restapi/v1.0/subscription' + ('/' + id if id else ''), {
 94            'eventFilters': filters,
 95            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
 96            'expiresIn': expires_in,
 97            'deliveryMode': {
 98                'transportType': 'WebSocket',
 99                'encryption': False,
100                'address': '123_foo',
101                'subscriberKey': 'sub-c-foo',
102                'secretKey': 'sec-c-bar'
103            },
104            'id': id if id else 'foo-bar-baz',
105            'creationTime': date.today().isoformat(),
106            'status': 'Active',
107            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
108        })
109
110    def delete_mock_with_body(self,mock):
111        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', {"keepAssetsInInventory": True,"records": [{"id": "123"}]})
112
113    def delete_mock_without_body(self,mock):
114        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', body=None)

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes:

  • failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
  • longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
  • maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
TestCase(method_name=None)
14    def __init__(self, method_name=None):
15        unittest.TestCase.__init__(self, method_name)

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

def get_sdk(self, mock):
17    def get_sdk(self, mock):
18        sdk = SDK('whatever', 'whatever', 'https://whatever', redirect_uri='https://whatever-redirect')
19        self.authentication_mock(mock)
20        sdk.platform().login(jwt='jwt-token')
21        return sdk
def add(self, mock, method, url, body, status=200):
23    def add(self, mock, method, url, body, status=200):
24        mock.register_uri(
25            method=method,
26            url='https://whatever' + url,
27            text= '' if body is None else json.dumps(body),
28            headers={'Content-Type': 'application/json'},
29            status_code=status
30        )
def authentication_mock(self, mock):
32    def authentication_mock(self, mock):
33        return self.add(mock, 'POST', '/restapi/oauth/token', {
34            'access_token': 'ACCESS_TOKEN',
35            'token_type': 'bearer',
36            'expires_in': 3600,
37            'refresh_token': 'REFRESH_TOKEN',
38            'refresh_token_expires_in': 60480,
39            'scope': 'SMS RCM Foo Boo',
40            'expireTime': time() + 3600,
41            'owner_id': 'foo'
42        })
def logout_mock(self, mock):
44    def logout_mock(self, mock):
45        return self.add(mock, 'POST', '/restapi/oauth/revoke', {})
def presence_subscription_mock(self, mock, id='1', detailed=True):
47    def presence_subscription_mock(self, mock, id='1', detailed=True):
48        detailed = '?detailedTelephonyState=true' if detailed else ''
49        expires_in = 15 * 60 * 60
50
51        return self.add(mock, 'POST', '/restapi/v1.0/subscription', {
52            'eventFilters': ['/restapi/v1.0/account/~/extension/' + id + '/presence' + detailed],
53            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
54            'expiresIn': expires_in,
55            'deliveryMode': {
56                'transportType': 'WebSocket',
57                'encryption': True,
58                'address': '123_foo',
59                'subscriberKey': 'sub-c-foo',
60                'secretKey': 'sec-c-bar',
61                'encryptionAlgorithm': 'AES',
62                'encryptionKey': 'e0bMTqmumPfFUbwzppkSbA=='
63            },
64            'creationTime': date.today().isoformat(),
65            'id': 'foo-bar-baz',
66            'status': 'Active',
67            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
68        })
def refresh_mock(self, mock, failure=False, expires_in=3600):
70    def refresh_mock(self, mock, failure=False, expires_in=3600):
71        status = 200
72        body = {
73            'access_token': 'ACCESS_TOKEN_FROM_REFRESH',
74            'token_type': 'bearer',
75            'expires_in': expires_in,
76            'refresh_token': 'REFRESH_TOKEN_FROM_REFRESH',
77            'refresh_token_expires_in': 60480,
78            'scope': 'SMS RCM Foo Boo',
79            'expireTime': time() + expires_in,
80            'owner_id': 'foo'
81        }
82
83        if failure:
84            status = 400
85            body = {'message': 'Wrong token (mock)'}
86
87        return self.add(mock, 'POST', '/restapi/oauth/token', body, status)
def subscription_mock(self, mock, expires_in=54000, filters=None, id=None):
 89    def subscription_mock(self, mock, expires_in=54000, filters=None, id=None):
 90        if filters is None:
 91            filters = ['/restapi/v1.0/account/~/extension/1/presence']
 92
 93        return self.add(mock, 'POST' if not id else 'PUT', '/restapi/v1.0/subscription' + ('/' + id if id else ''), {
 94            'eventFilters': filters,
 95            'expirationTime': date.fromtimestamp(time() + expires_in).isoformat(),
 96            'expiresIn': expires_in,
 97            'deliveryMode': {
 98                'transportType': 'WebSocket',
 99                'encryption': False,
100                'address': '123_foo',
101                'subscriberKey': 'sub-c-foo',
102                'secretKey': 'sec-c-bar'
103            },
104            'id': id if id else 'foo-bar-baz',
105            'creationTime': date.today().isoformat(),
106            'status': 'Active',
107            'uri': 'https://platform.ringcentral.com/restapi/v1.0/subscription/foo-bar-baz'
108        })
def delete_mock_with_body(self, mock):
110    def delete_mock_with_body(self,mock):
111        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', {"keepAssetsInInventory": True,"records": [{"id": "123"}]})
def delete_mock_without_body(self, mock):
113    def delete_mock_without_body(self,mock):
114        return self.add(mock, 'DELETE', '/restapi/v2/accounts/~/extensions', body=None)
Inherited Members
unittest.case.TestCase
failureException
longMessage
maxDiff
addTypeEqualityFunc
addCleanup
enterContext
addClassCleanup
enterClassContext
setUp
tearDown
setUpClass
tearDownClass
countTestCases
defaultTestResult
shortDescription
id
subTest
run
doCleanups
doClassCleanups
debug
skipTest
fail
assertFalse
assertTrue
assertRaises
assertWarns
assertLogs
assertNoLogs
assertEqual
assertNotEqual
assertAlmostEqual
assertNotAlmostEqual
assertSequenceEqual
assertListEqual
assertTupleEqual
assertSetEqual
assertIn
assertNotIn
assertIs
assertIsNot
assertDictEqual
assertDictContainsSubset
assertCountEqual
assertMultiLineEqual
assertLess
assertLessEqual
assertGreater
assertGreaterEqual
assertIsNone
assertIsNotNone
assertIsInstance
assertNotIsInstance
assertRaisesRegex
assertWarnsRegex
assertRegex
assertNotRegex
failUnlessRaises
failIf
assertRaisesRegexp
assertRegexpMatches
assertNotRegexpMatches
failUnlessEqual
assertEquals
failIfEqual
assertNotEquals
failUnlessAlmostEqual
assertAlmostEquals
failIfAlmostEqual
assertNotAlmostEquals
failUnless
assert_