Skip to content

Commit

Permalink
add basic realtime auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mohyour committed Sep 12, 2022
1 parent fa2c7b3 commit 5f2c717
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions ably/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Empty file added ably/realtime/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions ably/realtime/realtime.py
Original file line number Diff line number Diff line change
@@ -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)

This comment has been minimized.

Copy link
@owenpearson

owenpearson Sep 13, 2022

Member

Should raise an error if key isn't provided since we only support basic auth at the moment


self.__auth = Auth(self, options)

self.__options = options

@property
def auth(self):
return self.__auth

@property
def options(self):
return self.__options
21 changes: 21 additions & 0 deletions test/ably/realtimeauthtest.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5f2c717

Please sign in to comment.