Skip to content

Instantly share code, notes, and snippets.

@eykd
Created July 27, 2011 21:59
Show Gist options
  • Select an option

  • Save eykd/1110475 to your computer and use it in GitHub Desktop.

Select an option

Save eykd/1110475 to your computer and use it in GitHub Desktop.
Failing unittests in django_oauth_plus_tests
import time
import re
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from oauth_provider.models import Resource, Consumer
from oauth_provider.models import Token
class OAuthTests(TestCase):
"""Based on behavior described at http://code.welldev.org/django-oauth-plus/wiki/Home
"""
def setUp(self):
username = self.username = 'jane'
password = self.password = 'toto'
email = self.email = 'jane@example.com'
jane = self.jane = User.objects.create_user(username, email, password)
resource = self.resource = Resource(name='photos', url='/oauth/photo/')
resource.save()
CONSUMER_KEY = self.CONSUMER_KEY = 'dpf43f3p2l4k3l03'
CONSUMER_SECRET = self.CONSUMER_SECRET = 'kd94hf93k423kf44'
consumer = self.consumer = Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET,
name='printer.example.com', user=jane)
consumer.save()
self.callback_token = self.callback = 'http://printer.example.com/request_token_ready'
self.callback_confirmed = True
self.request_token_parameters = {
'oauth_consumer_key': self.CONSUMER_KEY,
'oauth_signature_method': 'PLAINTEXT',
'oauth_signature': '%s&' % self.CONSUMER_SECRET,
'oauth_timestamp': str(int(time.time())),
'oauth_nonce': 'requestnonce',
'oauth_version': '1.0',
'oauth_callback': self.callback,
'scope': 'photos', # custom argument to specify Protected Resource
}
self.c = Client()
def test_Request_token_request_succeeds_with_valid_request_token_parameters(self):
# The Consumer sends the following HTTP POST request to the
# Service Provider:
response = self.c.get("/oauth/request_token/", self.request_token_parameters)
# The Service Provider checks the signature and replies with an unauthorized Request Token in the body of the HTTP response:
self.assertEqual(
response.status_code,
200
)
self.assert_(
re.match(r'oauth_token_secret=[^&]+&oauth_token=[^&]+&oauth_callback_confirmed=true',
response.content
))
token = self.request_token = list(Token.objects.all())[-1]
self.assert_(token.key in response.content)
self.assert_(token.secret in response.content)
self.assertEqual(token.callback,
self.callback_token)
self.assert_(not self.request_token.is_approved)
self.assertEqual(
token.callback_confirmed,
self.callback_confirmed)
def test_Requesting_user_authorization_fails_when_user_denies_authorization(self):
self.test_Request_token_request_succeeds_with_valid_request_token_parameters()
self.c.login(username=self.username, password=self.password)
parameters = self.authorization_parameters = {'oauth_token': self.request_token.key}
response = self.c.get("/oauth/authorize/", parameters)
self.assertEqual(
response.status_code,
200)
self.assertEqual(
response.content,
'Fake authorize view for printer.example.com with params: oauth_token=...')
# fake access not granted by the user (set session parameter again)
self.authorization_parameters['authorize_access'] = 0
response = self.c.post("/oauth/authorize/", self.authorization_parameters)
self.assertEqual(
response.status_code,
302)
self.assert_(
re.match(r'http://printer.example.com/request_token_ready\?oauth_verifier=[^&]+&error=Access+not+granted+by+user.',
response['Location']),
response['Location'])
self.c.logout()
Creating test database for alias 'default'...
.F
======================================================================
FAIL: test_Requesting_user_authorization_fails_when_user_denies_authorization (host.tests.OAuthTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "django_oauth_plus_tests.py", line 92, in test_Requesting_user_authorization_fails_when_user_denies_authorization
response['Location'])
AssertionError: None is not True : http://printer.example.com/request_token_ready&error=Access+not+granted+by+user.
----------------------------------------------------------------------
Ran 2 tests in 0.106s
FAILED (failures=1)
Destroying test database for alias 'default'...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment