def test_time_without_key_or_token(self): ably = AblyRest(Options(host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"])) ably.time()
def test_enviroment(self): ably = AblyRest(token='token', environment='custom') with patch.object(Session, 'prepare_request', wraps=ably.http._Http__session.prepare_request) as get_mock: try: ably.time() except AblyException: pass request = get_mock.call_args_list[0][0][0] self.assertEquals(request.url, 'https://custom-rest.ably.io:443/time')
def test_enviroment(self): ably = AblyRest(token='token', environment='custom') with patch.object( Session, 'prepare_request', wraps=ably.http._Http__session.prepare_request) as get_mock: try: ably.time() except AblyException: pass request = get_mock.call_args_list[0][0][0] assert request.url == 'https://custom-rest.ably.io:443/time'
def test_time_accuracy(self): ably = AblyRest(Options.with_key(test_vars["keys"][0]["key_str"], host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"])) reported_time = ably.time() actual_time = time.time() * 1000.0 self.assertLess(abs(actual_time - reported_time), 2000, msg="Time is not within 2 seconds")
def test_time_without_key_or_token(self): ably = AblyRest(token='foo', rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], use_binary_protocol=self.use_binary_protocol) reported_time = ably.time() actual_time = time.time() * 1000.0 self.assertLess(abs(actual_time - reported_time), 2000, msg="Time is not within 2 seconds")
def test_time_accuracy(self): ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], use_binary_protocol=self.use_binary_protocol) reported_time = ably.time() actual_time = time.time() * 1000.0 self.assertLess(abs(actual_time - reported_time), 2000, msg="Time is not within 2 seconds")
class TestAuthAuthorize(BaseTestCase): def setUp(self): self.ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"]) def per_protocol_setup(self, use_binary_protocol): self.ably.options.use_binary_protocol = use_binary_protocol self.use_binary_protocol = use_binary_protocol def test_if_authorize_changes_auth_mechanism_to_token(self): self.assertEqual(Auth.Method.BASIC, self.ably.auth.auth_mechanism, msg="Unexpected Auth method mismatch") self.ably.auth.authorise() self.assertEqual(Auth.Method.TOKEN, self.ably.auth.auth_mechanism, msg="Authorise should change the Auth method") def test_authorize_shouldnt_create_token_if_not_expired(self): token = self.ably.auth.authorise() new_token = self.ably.auth.authorise() self.assertGreater(token.expires, time.time()*1000) self.assertIs(new_token, token) def test_authorize_should_create_new_token_if_forced(self): token = self.ably.auth.authorise() new_token = self.ably.auth.authorise(force=True) self.assertGreater(token.expires, time.time()*1000) self.assertIsNot(new_token, token) self.assertGreater(new_token.expires, token.expires) another_token = self.ably.auth.authorise(auth_options={'force': True}) self.assertIsNot(new_token, another_token) def test_authorize_create_new_token_if_expired(self): token = self.ably.auth.authorise() with mock.patch('ably.types.tokendetails.TokenDetails.is_expired', return_value=True): new_token = self.ably.auth.authorise() self.assertIsNot(token, new_token) def test_authorize_returns_a_token_details(self): token = self.ably.auth.authorise() self.assertIsInstance(token, TokenDetails) @dont_vary_protocol def test_authorize_adheres_to_request_token(self): token_params = {'ttl': 10, 'client_id': 'client_id'} auth_params = {'auth_url': 'somewhere.com', 'query_time': True} with mock.patch('ably.rest.auth.Auth.request_token') as request_mock: self.ably.auth.authorise(token_params, auth_params, force=True) token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], token_params) # Authorise may call request_token with some default auth_options. for arg, value in six.iteritems(auth_params): self.assertEqual(auth_called[arg], value, "%s called with wrong value: %s" % (arg, value)) def test_with_token_str_https(self): token = self.ably.auth.authorise() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=True, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_with_token_str_http(self): token = self.ably.auth.authorise() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=False, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_if_default_client_id_is_used(self): ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], client_id='my_client_id', use_binary_protocol=self.use_binary_protocol) token = ably.auth.authorise() self.assertEqual(token.client_id, 'my_client_id') def test_if_parameters_are_stored_and_used_as_defaults(self): self.ably.auth.authorise({'ttl': 555, 'client_id': 'new_id'}, {'auth_headers': {'a_headers': 'a_value'}}) with mock.patch('ably.rest.auth.Auth.request_token', wraps=self.ably.auth.request_token) as request_mock: self.ably.auth.authorise(force=True) token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], {'ttl': 555, 'client_id': 'new_id'}) self.assertEqual(auth_called['auth_headers'], {'a_headers': 'a_value'}) def test_force_and_timestamp_are_not_stored(self): # authorise once with arbitrary defaults token_1 = self.ably.auth.authorise( {'ttl': 60 * 1000, 'client_id': 'new_id'}, {'auth_headers': {'a_headers': 'a_value'}}) self.assertIsInstance(token_1, TokenDetails) # call authorise again with force and timestamp set timestamp = self.ably.time() with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: token_2 = self.ably.auth.authorise( {'ttl': 60 * 1000, 'client_id': 'new_id', 'timestamp': timestamp}, {'auth_headers': {'a_headers': 'a_value'}, 'force': True}) self.assertIsInstance(token_2, TokenDetails) self.assertNotEqual(token_1, token_2) self.assertEqual(tr_mock.call_args[1]['timestamp'], timestamp) # call authorise again with no params token_3 = self.ably.auth.authorise() self.assertIsInstance(token_3, TokenDetails) self.assertEqual(token_2, token_3) # call authorise again with force with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: token_4 = self.ably.auth.authorise(force=True) self.assertIsInstance(token_4, TokenDetails) self.assertNotEqual(token_2, token_4) self.assertNotEqual(tr_mock.call_args[1]['timestamp'], timestamp) def test_client_id_precedence(self): client_id = uuid.uuid4().hex overridden_client_id = uuid.uuid4().hex ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], use_binary_protocol=self.use_binary_protocol, client_id=client_id, default_token_params={'client_id': overridden_client_id}) token = ably.auth.authorise() self.assertEqual(token.client_id, client_id) self.assertEqual(ably.auth.client_id, client_id) channel = ably.channels[ self.protocol_channel_name('test_client_id_precedence')] channel.publish('test', 'data') self.assertEqual(channel.history().items[0].client_id, client_id)
class TestAuthAuthorize(BaseTestCase): def setUp(self): self.ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"]) def per_protocol_setup(self, use_binary_protocol): self.ably.options.use_binary_protocol = use_binary_protocol self.use_binary_protocol = use_binary_protocol def test_if_authorize_changes_auth_mechanism_to_token(self): self.assertEqual(Auth.Method.BASIC, self.ably.auth.auth_mechanism, msg="Unexpected Auth method mismatch") self.ably.auth.authorize() self.assertEqual(Auth.Method.TOKEN, self.ably.auth.auth_mechanism, msg="Authorise should change the Auth method") # RSA10a @dont_vary_protocol def test_authorize_always_creates_new_token(self): self.ably.auth.authorize({'capability': {'test': ['publish']}}) self.ably.channels.test.publish('event', 'data') self.ably.auth.authorize({'capability': {'test': ['subscribe']}}) with self.assertRaises(AblyAuthException): self.ably.channels.test.publish('event', 'data') def test_authorize_create_new_token_if_expired(self): token = self.ably.auth.authorize() with mock.patch('ably.types.tokendetails.TokenDetails.is_expired', return_value=True): new_token = self.ably.auth.authorize() self.assertIsNot(token, new_token) def test_authorize_returns_a_token_details(self): token = self.ably.auth.authorize() self.assertIsInstance(token, TokenDetails) @dont_vary_protocol def test_authorize_adheres_to_request_token(self): token_params = {'ttl': 10, 'client_id': 'client_id'} auth_params = {'auth_url': 'somewhere.com', 'query_time': True} with mock.patch('ably.rest.auth.Auth.request_token') as request_mock: self.ably.auth.authorize(token_params, auth_params) token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], token_params) # Authorise may call request_token with some default auth_options. for arg, value in six.iteritems(auth_params): self.assertEqual(auth_called[arg], value, "%s called with wrong value: %s" % (arg, value)) def test_with_token_str_https(self): token = self.ably.auth.authorize() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=True, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_with_token_str_http(self): token = self.ably.auth.authorize() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=False, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_if_default_client_id_is_used(self): ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], client_id='my_client_id', use_binary_protocol=self.use_binary_protocol) token = ably.auth.authorize() self.assertEqual(token.client_id, 'my_client_id') # RSA10j def test_if_parameters_are_stored_and_used_as_defaults(self): # Define some parameters auth_options = dict(self.ably.auth.auth_options.auth_options) auth_options['auth_headers'] = {'a_headers': 'a_value'} self.ably.auth.authorize({'ttl': 555}, auth_options) with mock.patch('ably.rest.auth.Auth.request_token', wraps=self.ably.auth.request_token) as request_mock: self.ably.auth.authorize() token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], {'ttl': 555}) self.assertEqual(auth_called['auth_headers'], {'a_headers': 'a_value'}) # Different parameters, should completely replace the first ones, not merge auth_options = dict(self.ably.auth.auth_options.auth_options) auth_options['auth_headers'] = None self.ably.auth.authorize({}, auth_options) with mock.patch('ably.rest.auth.Auth.request_token', wraps=self.ably.auth.request_token) as request_mock: self.ably.auth.authorize() token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], {}) self.assertEqual(auth_called['auth_headers'], None) # RSA10g def test_timestamp_is_not_stored(self): # authorize once with arbitrary defaults auth_options = dict(self.ably.auth.auth_options.auth_options) auth_options['auth_headers'] = {'a_headers': 'a_value'} token_1 = self.ably.auth.authorize( {'ttl': 60 * 1000, 'client_id': 'new_id'}, auth_options) self.assertIsInstance(token_1, TokenDetails) # call authorize again with timestamp set timestamp = self.ably.time() with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: auth_options = dict(self.ably.auth.auth_options.auth_options) auth_options['auth_headers'] = {'a_headers': 'a_value'} token_2 = self.ably.auth.authorize( {'ttl': 60 * 1000, 'client_id': 'new_id', 'timestamp': timestamp}, auth_options) self.assertIsInstance(token_2, TokenDetails) self.assertNotEqual(token_1, token_2) self.assertEqual(tr_mock.call_args[1]['timestamp'], timestamp) # call authorize again with no params with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: token_4 = self.ably.auth.authorize() self.assertIsInstance(token_4, TokenDetails) self.assertNotEqual(token_2, token_4) self.assertNotEqual(tr_mock.call_args[1]['timestamp'], timestamp) def test_client_id_precedence(self): client_id = uuid.uuid4().hex overridden_client_id = uuid.uuid4().hex ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], use_binary_protocol=self.use_binary_protocol, client_id=client_id, default_token_params={'client_id': overridden_client_id}) token = ably.auth.authorize() self.assertEqual(token.client_id, client_id) self.assertEqual(ably.auth.client_id, client_id) channel = ably.channels[ self.protocol_channel_name('test_client_id_precedence')] channel.publish('test', 'data') self.assertEqual(channel.history().items[0].client_id, client_id) # RSA10l @dont_vary_protocol def test_authorise(self): with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered warnings.simplefilter("always") token = self.ably.auth.authorise() self.assertIsInstance(token, TokenDetails) # Verify warning is raised self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
class TestAuthAuthorize(BaseTestCase): def setUp(self): self.ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"]) def per_protocol_setup(self, use_binary_protocol): self.ably.options.use_binary_protocol = use_binary_protocol self.use_binary_protocol = use_binary_protocol def test_if_authorize_changes_auth_mechanism_to_token(self): self.assertEqual(Auth.Method.BASIC, self.ably.auth.auth_mechanism, msg="Unexpected Auth method mismatch") self.ably.auth.authorise() self.assertEqual(Auth.Method.TOKEN, self.ably.auth.auth_mechanism, msg="Authorise should change the Auth method") def test_authorize_shouldnt_create_token_if_not_expired(self): token = self.ably.auth.authorise() new_token = self.ably.auth.authorise() self.assertGreater(token.expires, time.time()*1000) self.assertIs(new_token, token) def test_authorize_should_create_new_token_if_forced(self): token = self.ably.auth.authorise() new_token = self.ably.auth.authorise(force=True) self.assertGreater(token.expires, time.time()*1000) self.assertIsNot(new_token, token) self.assertGreater(new_token.expires, token.expires) another_token = self.ably.auth.authorise(auth_options={'force': True}) self.assertIsNot(new_token, another_token) def test_authorize_create_new_token_if_expired(self): token = self.ably.auth.authorise() with mock.patch('ably.types.tokendetails.TokenDetails.is_expired', return_value=True): new_token = self.ably.auth.authorise() self.assertIsNot(token, new_token) def test_authorize_returns_a_token_details(self): token = self.ably.auth.authorise() self.assertIsInstance(token, TokenDetails) @dont_vary_protocol def test_authorize_adheres_to_request_token(self): token_params = {'ttl': 10, 'client_id': 'client_id'} auth_params = {'auth_url': 'somewhere.com', 'query_time': True} with mock.patch('ably.rest.auth.Auth.request_token') as request_mock: self.ably.auth.authorise(token_params, auth_params, force=True) token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], token_params) # Authorise may call request_token with some default auth_options. for arg, value in six.iteritems(auth_params): self.assertEqual(auth_called[arg], value, "%s called with wrong value: %s" % (arg, value)) def test_with_token_str_https(self): token = self.ably.auth.authorise() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=True, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_with_token_str_http(self): token = self.ably.auth.authorise() token = token.token ably = AblyRest(token=token, rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=False, use_binary_protocol=self.use_binary_protocol) ably.channels.test_auth_with_token_str.publish('event', 'foo_bar') def test_if_default_client_id_is_used(self): ably = AblyRest(key=test_vars["keys"][0]["key_str"], rest_host=test_vars["host"], port=test_vars["port"], tls_port=test_vars["tls_port"], tls=test_vars["tls"], client_id='my_client_id', use_binary_protocol=self.use_binary_protocol) token = ably.auth.authorise() self.assertEqual(token.client_id, 'my_client_id') def test_if_parameters_are_stored_and_used_as_defaults(self): self.ably.auth.authorise({'ttl': 555, 'client_id': 'new_id'}, {'auth_headers': {'a_headers': 'a_value'}}) with mock.patch('ably.rest.auth.Auth.request_token', wraps=self.ably.auth.request_token) as request_mock: self.ably.auth.authorise(force=True) token_called, auth_called = request_mock.call_args self.assertEqual(token_called[0], {'ttl': 555, 'client_id': 'new_id'}) self.assertEqual(auth_called['auth_headers'], {'a_headers': 'a_value'}) def test_force_and_timestamp_are_not_stored(self): # authorise once with arbitrary defaults token_1 = self.ably.auth.authorise( {'ttl': 60 * 1000, 'client_id': 'new_id'}, {'auth_headers': {'a_headers': 'a_value'}}) self.assertIsInstance(token_1, TokenDetails) # call authorise again with force and timestamp set timestamp = self.ably.time() with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: token_2 = self.ably.auth.authorise( {'ttl': 60 * 1000, 'client_id': 'new_id', 'timestamp': timestamp}, {'auth_headers': {'a_headers': 'a_value'}, 'force': True}) self.assertIsInstance(token_2, TokenDetails) self.assertNotEqual(token_1, token_2) self.assertEqual(tr_mock.call_args[1]['timestamp'], timestamp) # call authorise again with no params token_3 = self.ably.auth.authorise() self.assertIsInstance(token_3, TokenDetails) self.assertEqual(token_2, token_3) # call authorise again with force with mock.patch('ably.rest.auth.TokenRequest', wraps=ably.types.tokenrequest.TokenRequest) as tr_mock: token_4 = self.ably.auth.authorise(force=True) self.assertIsInstance(token_4, TokenDetails) self.assertNotEqual(token_2, token_4) self.assertNotEqual(tr_mock.call_args[1]['timestamp'], timestamp)