From 5f2c71756ef90e71848aaecbd4e8f6005b510acb Mon Sep 17 00:00:00 2001 From: moyosore Date: Mon, 12 Sep 2022 08:34:29 +0100 Subject: [PATCH] add basic realtime auth --- ably/__init__.py | 1 + ably/realtime/__init__.py | 0 ably/realtime/realtime.py | 34 ++++++++++++++++++++++++++++++++++ test/ably/realtimeauthtest.py | 21 +++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 ably/realtime/__init__.py create mode 100644 ably/realtime/realtime.py create mode 100644 test/ably/realtimeauthtest.py diff --git a/ably/__init__.py b/ably/__init__.py index 578a1537..128e3d08 100644 --- a/ably/__init__.py +++ b/ably/__init__.py @@ -1,4 +1,5 @@ from ably.rest.rest import AblyRest +from ably.realtime.realtime import AblyRealtime from ably.rest.auth import Auth from ably.rest.push import Push from ably.types.capability import Capability diff --git a/ably/realtime/__init__.py b/ably/realtime/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ably/realtime/realtime.py b/ably/realtime/realtime.py new file mode 100644 index 00000000..d9baff7c --- /dev/null +++ b/ably/realtime/realtime.py @@ -0,0 +1,34 @@ +import logging +from ably.rest.auth import Auth +from ably.types.options import Options + + +log = logging.getLogger(__name__) + +class AblyRealtime: + """Ably Realtime Client""" + + def __init__(self, key=None, **kwargs): + """Create an AblyRealtime instance. + + :Parameters: + **Credentials** + - `key`: a valid ably key string + """ + + if key is not None: + options = Options(key=key, **kwargs) + else: + options = Options(**kwargs) + + self.__auth = Auth(self, options) + + self.__options = options + + @property + def auth(self): + return self.__auth + + @property + def options(self): + return self.__options diff --git a/test/ably/realtimeauthtest.py b/test/ably/realtimeauthtest.py new file mode 100644 index 00000000..2c759481 --- /dev/null +++ b/test/ably/realtimeauthtest.py @@ -0,0 +1,21 @@ +import pytest +from ably import Auth, AblyRealtime +from ably.util.exceptions import AblyAuthException +from test.ably.utils import BaseAsyncTestCase + + +class TestRealtimeAuth(BaseAsyncTestCase): + async def setUp(self): + self.invalid_key = "some key" + self.valid_key_format = "Vjhddw.owt:R97sjjbdERJdjwer" + + def test_auth_with_correct_key_format(self): + key = self.valid_key_format.split(":") + ably = AblyRealtime(self.valid_key_format) + assert Auth.Method.BASIC == ably.auth.auth_mechanism, "Unexpected Auth method mismatch" + assert ably.auth.auth_options.key_name == key[0] + assert ably.auth.auth_options.key_secret == key[1] + + def test_auth_incorrect_key_format(self): + with pytest.raises(AblyAuthException): + ably = AblyRealtime(self.invalid_key) \ No newline at end of file