Skip to content

Commit

Permalink
Support read-only "installed" applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Aug 7, 2016
1 parent 3c4cce7 commit 4d9a9f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
17 changes: 15 additions & 2 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from six import iteritems
from update_checker import update_check
from prawcore import (ReadOnlyAuthorizer, Redirect, Requestor,
ScriptAuthorizer, TrustedAuthenticator, session)
from prawcore import (DeviceIDAuthorizer, ReadOnlyAuthorizer, Redirect,
Requestor, ScriptAuthorizer, TrustedAuthenticator,
UntrustedAuthenticator, session)

from .exceptions import ClientException
from .config import Config
Expand Down Expand Up @@ -146,6 +147,12 @@ def _prepare_objector(self):
def _prepare_prawcore(self):
requestor = Requestor(USER_AGENT_FORMAT.format(self.config.user_agent),
self.config.oauth_url, self.config.reddit_url)
if self.config.client_secret:
self._prepare_trusted_prawcore(requestor)
else:
self._prepare_untrusted_prawcore(requestor)

def _prepare_trusted_prawcore(self, requestor):
authenticator = TrustedAuthenticator(requestor, self.config.client_id,
self.config.client_secret)
read_only_authorizer = ReadOnlyAuthorizer(authenticator)
Expand All @@ -158,6 +165,12 @@ def _prepare_prawcore(self):
else:
self._core = self._read_only_core

def _prepare_untrusted_prawcore(self, requestor):
authenticator = UntrustedAuthenticator(requestor,
self.config.client_id)
read_only_authorizer = DeviceIDAuthorizer(authenticator)
self._core = self._read_only_core = session(read_only_authorizer)

def comment(self, id):
"""Return a lazy instance of :class:`~.Comment` for ``id``.
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/test_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ def test_read_only__with_script_authenticated_core(self):
reddit.read_only = False
assert not reddit.read_only

def test_read_only__without_authenticated_core(self):
def test_read_only__without_trusted_authenticated_core(self):
with Reddit(password=None, username=None,
**self.REQUIRED_DUMMY_SETTINGS) as reddit:
assert reddit.read_only
with pytest.raises(ClientException):
reddit.read_only = False
assert reddit.read_only
reddit.read_only = True
assert reddit.read_only

def test_read_only__without_untrusted_authenticated_core(self):
required_settings = self.REQUIRED_DUMMY_SETTINGS.copy()
required_settings['client_secret'] = None
with Reddit(password=None, username=None,
**self.REQUIRED_DUMMY_SETTINGS) as reddit:
assert reddit.read_only
Expand Down

0 comments on commit 4d9a9f4

Please sign in to comment.