Skip to content

Commit

Permalink
Merge 8cf93b6 into fdce2a2
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Feb 25, 2021
2 parents fdce2a2 + 8cf93b6 commit 6891d57
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 @@ -9,10 +9,12 @@
See ref:`using_refresh_tokens` for examples on how to leverage these classes.
"""
from abc import ABC, abstractmethod

import aiofiles


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 @@ -42,8 +45,8 @@ def post_refresh_callback(self, authorizer):
This callback can be used for saving the updated ``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 @@ -54,7 +57,6 @@ def pre_refresh_callback(self, authorizer):
``asyncprawcore.Authorizer`` instance, such as setting the ``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 6891d57

Please sign in to comment.