Skip to content

Commit

Permalink
Add test for Authorizer.refresh with invalid token.
Browse files Browse the repository at this point in the history
  • Loading branch information
bboe committed Feb 7, 2016
1 parent dc045a7 commit eb13263
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
2 changes: 1 addition & 1 deletion prawcore/__init__.py
Expand Up @@ -3,7 +3,7 @@
import logging
from .auth import Authenticator, Authorizer # noqa
from .const import __version__ # noqa
from .exceptions import InvalidInvocation, PrawcoreException # noqa
from .exceptions import * # noqa
from .sessions import Session, session # noqa


Expand Down
23 changes: 13 additions & 10 deletions prawcore/auth.py
Expand Up @@ -3,7 +3,8 @@
import requests
import time
from . import const
from .exceptions import InvalidInvocation
from .exceptions import InvalidInvocation, RequestException
from requests.status_codes import codes


class Authenticator(object):
Expand Down Expand Up @@ -47,12 +48,14 @@ def refresh(self):
auth = (self.authenticator.client_id, self.authenticator.client_secret)
data = {'grant_type': 'refresh_token',
'refresh_token': self.refresh_token}
try:
data = self._session.post(const.ACCESS_TOKEN_URL, auth=auth,
data=data).json()
except:
raise

self.access_token = data['access_token']
self.expiration = time.time() + data['expires_in']
self.scopes = set(data['scope'].split(' '))

response = self._session.post(const.ACCESS_TOKEN_URL, auth=auth,
data=data)
if response.status_code != codes['ok']:
raise RequestException(response)

payload = response.json()

self.access_token = payload['access_token']
self.expiration = time.time() + payload['expires_in']
self.scopes = set(payload['scope'].split(' '))
14 changes: 14 additions & 0 deletions prawcore/exceptions.py
Expand Up @@ -7,3 +7,17 @@ class PrawcoreException(Exception):

class InvalidInvocation(PrawcoreException):
"""Indicate that the code to execute cannot be completed."""


class RequestException(PrawcoreException):
"""Indicate that there was an error with the HTTP request."""

def __init__(self, response):
"""RequestException instances contain the failing response.
:param response: A requests.response instance.
"""
self.response = response
super(RequestException, self).__init__('error processing request ({})'
.format(response.status_code))
52 changes: 52 additions & 0 deletions tests/cassettes/Authorizer_refresh__with_invalid_token.json
@@ -0,0 +1,52 @@
{
"http_interactions": [
{
"recorded_at": "2016-02-07T01:02:52",
"request": {
"body": {
"encoding": "utf-8",
"string": "grant_type=refresh_token&refresh_token=INVALID_TOKEN"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Basic <BASIC_AUTH>",
"Connection": "keep-alive",
"Content-Length": "52",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "prawcore/0.0.1a1"
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": "{\"error\": 400}"
},
"headers": {
"CF-RAY": "270b2bfaced23982-PHX",
"Connection": "keep-alive",
"Content-Length": "14",
"Content-Type": "application/json; charset=UTF-8",
"Date": "Sun, 07 Feb 2016 01:02:52 GMT",
"Server": "cloudflare-nginx",
"Set-Cookie": "__cfduid=dd555eb868b1bcfd517d08fcb174c3afc1454806972; expires=Mon, 06-Feb-17 01:02:52 GMT; path=/; domain=.reddit.com; HttpOnly",
"Strict-Transport-Security": "max-age=15552000; includeSubDomains; preload",
"X-Moose": "majestic",
"cache-control": "max-age=0, must-revalidate",
"x-content-type-options": "nosniff",
"x-frame-options": "SAMEORIGIN",
"x-ua-compatible": "IE=edge",
"x-xss-protection": "1; mode=block"
},
"status": {
"code": 400,
"message": "Bad Request"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
}
],
"recorded_with": "betamax/0.5.1"
}
6 changes: 6 additions & 0 deletions tests/test_authorizer.py
Expand Up @@ -24,6 +24,12 @@ def test_refresh(self):
self.assertIsInstance(authorizer.scopes, set)
self.assertTrue(len(authorizer.scopes) > 0)

def test_refresh__with_invalid_token(self):
authorizer = prawcore.Authorizer(self.authentication, 'INVALID_TOKEN')
with Betamax(authorizer._session).use_cassette(
'Authorizer_refresh__with_invalid_token'):
self.assertRaises(prawcore.RequestException, authorizer.refresh)

def test_refresh__without_refresh_token(self):
authorizer = prawcore.Authorizer(self.authentication)
self.assertRaises(prawcore.InvalidInvocation, authorizer.refresh)

0 comments on commit eb13263

Please sign in to comment.