Skip to content

Commit

Permalink
Use the ABC class to declare an abstract class
Browse files Browse the repository at this point in the history
This allows IDEs and other tools to recognize that the class is abstract and throws the appropriate warnings. It also causes an exception to be thrown before a token is consumed.

(cherry picked from commit 29ea3b0e2617fe88bc4488a900b1f12cc1ca68e5)
  • Loading branch information
PythonCoderAS authored and LilSpazJoekp committed Feb 25, 2021
1 parent afce60e commit 1c342d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
8 changes: 5 additions & 3 deletions asyncpraw/util/token_manager.py
Expand Up @@ -11,8 +11,10 @@
"""
import aiofiles

from abc import ABC, abstractmethod

class BaseTokenManager:

class BaseTokenManager(ABC):
"""An abstract class for all token managers."""

def __init__(self):
Expand All @@ -32,6 +34,7 @@ def reddit(self, value):
)
self._reddit = value

@abstractmethod
def post_refresh_callback(self, authorizer):
"""Handle callback that is invoked after a refresh token is used.
Expand All @@ -43,8 +46,8 @@ def post_refresh_callback(self, authorizer):
``refresh_token``.
"""
raise NotImplementedError("``post_refresh_callback`` must be extended.")

@abstractmethod
def pre_refresh_callback(self, authorizer):
"""Handle callback that is invoked before refreshing PRAW's authorization.
Expand All @@ -56,7 +59,6 @@ def pre_refresh_callback(self, authorizer):
``refresh_token``.
"""
raise NotImplementedError("``pre_refresh_callback`` must be extended.")


class FileTokenManager(BaseTokenManager):
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/test_reddit.py
Expand Up @@ -16,6 +16,14 @@
from . import UnitTest


class DummyTokenManager(BaseTokenManager):
def post_refresh_callback(self, authorizer):
pass

def pre_refresh_callback(self, authorizer):
pass


class TestReddit(UnitTest):
REQUIRED_DUMMY_SETTINGS = {
x: "dummy" for x in ["client_id", "client_secret", "user_agent"]
Expand Down Expand Up @@ -182,8 +190,8 @@ async def test_post_ratelimit(self, __, _):
async def test_read_only__with_authenticated_core(self):
async with Reddit(
password=None,
token_manager=BaseTokenManager(),
username=None,
token_manager=DummyTokenManager(),
**self.REQUIRED_DUMMY_SETTINGS,
) as reddit:
assert not reddit.read_only
Expand All @@ -210,8 +218,8 @@ async def test_read_only__with_authenticated_core__non_confidential(self):
client_id="dummy",
client_secret=None,
redirect_uri="dummy",
token_manager=BaseTokenManager(),
user_agent="dummy",
token_manager=DummyTokenManager(),
) as reddit:
assert not reddit.read_only
reddit.read_only = True
Expand Down
19 changes: 6 additions & 13 deletions tests/unit/util/test_token_manager.py
Expand Up @@ -6,6 +6,7 @@
from asyncpraw.util.token_manager import BaseTokenManager, FileTokenManager

from .. import UnitTest
from ..test_reddit import DummyTokenManager


class DummyAuthorizer:
Expand All @@ -14,25 +15,17 @@ def __init__(self, refresh_token):


class TestBaseTokenManager(UnitTest):
def test_post_refresh_token_callback__raises_not_implemented(self):
manager = BaseTokenManager()
with pytest.raises(NotImplementedError) as excinfo:
manager.post_refresh_callback(None)
assert str(excinfo.value) == "``post_refresh_callback`` must be extended."

def test_pre_refresh_token_callback__raises_not_implemented(self):
manager = BaseTokenManager()
with pytest.raises(NotImplementedError) as excinfo:
manager.pre_refresh_callback(None)
assert str(excinfo.value) == "``pre_refresh_callback`` must be extended."
def test_init_base_fail(self):
with pytest.raises(TypeError):
BaseTokenManager()

def test_reddit(self):
manager = BaseTokenManager()
manager = DummyTokenManager()
manager.reddit = "dummy"
assert manager.reddit == "dummy"

def test_reddit__must_only_be_set_once(self):
manager = BaseTokenManager()
manager = DummyTokenManager()
manager.reddit = "dummy"
with pytest.raises(RuntimeError) as excinfo:
manager.reddit = None
Expand Down

0 comments on commit 1c342d0

Please sign in to comment.