Skip to content

Commit

Permalink
Added a bunch of new tests. 100% coverage has lapsed a bit. Fixing now.
Browse files Browse the repository at this point in the history
  • Loading branch information
joestump committed May 16, 2010
1 parent 82c7b7f commit 77eb526
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
3 changes: 3 additions & 0 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ class SignatureMethod_HMAC_SHA1(SignatureMethod):
name = 'HMAC-SHA1'

def signing_base(self, request, consumer, token):
if request.normalized_url is None:
raise ValueError("Base URL for request is not set.")

sig = (
escape(request.method),
escape(request.normalized_url),
Expand Down
83 changes: 80 additions & 3 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys, os
sys.path[0:0] = [os.path.join(os.path.dirname(__file__), ".."),]

import sys
import os
import unittest
import oauth2 as oauth
import random
Expand All @@ -41,6 +40,9 @@
from cgi import parse_qs, parse_qsl


sys.path[0:0] = [os.path.join(os.path.dirname(__file__), ".."),]


class TestError(unittest.TestCase):
def test_message(self):
try:
Expand Down Expand Up @@ -283,6 +285,38 @@ def test_url(self):
self.assertEquals(req.normalized_url, exp2)
self.assertEquals(req.url, url2)

def test_bad_url(self):
request = oauth.Request()
try:
request.url = "ftp://example.com"
self.fail("Invalid URL scheme was accepted.")
except ValueError:
pass

def test_unset_consumer_and_token(self):
consumer = oauth.Consumer('my_consumer_key', 'my_consumer_secret')
token = oauth.Token('my_key', 'my_secret')
request = oauth.Request("GET", "http://example.com/fetch.php")
request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer,
token)

self.assertEquals(consumer.key, request['oauth_consumer_key'])
self.assertEquals(token.key, request['oauth_token'])

def test_no_url_set(self):
consumer = oauth.Consumer('my_consumer_key', 'my_consumer_secret')
token = oauth.Token('my_key', 'my_secret')
request = oauth.Request()

try:
try:
request.sign_request(oauth.SignatureMethod_HMAC_SHA1(),
consumer, token)
except TypeError:
self.fail("Signature method didn't check for a normalized URL.")
except ValueError:
pass

def test_url_query(self):
url = "https://www.google.com/m8/feeds/contacts/default/full/?alt=json&max-contacts=10"
normalized_url = urlparse.urlunparse(urlparse.urlparse(url)[:3] + (None, None, None))
Expand Down Expand Up @@ -495,6 +529,23 @@ def test_get_normalized_parameters_ignores_auth_signature(self):
del foo["oauth_signature"]
self.assertEqual(urllib.urlencode(sorted(foo.items())), res)

def test_set_signature_method(self):
consumer = oauth.Consumer('key', 'secret')
client = oauth.Client(consumer)

class Blah:
pass

try:
client.set_signature_method(Blah())
self.fail("Client.set_signature_method() accepted invalid method.")
except ValueError:
pass

m = oauth.SignatureMethod_HMAC_SHA1()
client.set_signature_method(m)
self.assertEquals(m, client.method)

def test_get_normalized_string_escapes_spaces_properly(self):
url = "http://sp.example.com/"
params = {
Expand Down Expand Up @@ -604,12 +655,14 @@ def test_from_consumer_and_token(self):
url = "http://sp.example.com/"

tok = oauth.Token(key="tok-test-key", secret="tok-test-secret")
tok.set_verifier('this_is_a_test_verifier')
con = oauth.Consumer(key="con-test-key", secret="con-test-secret")
req = oauth.Request.from_consumer_and_token(con, token=tok,
http_method="GET", http_url=url)

self.assertEquals(req['oauth_token'], tok.key)
self.assertEquals(req['oauth_consumer_key'], con.key)
self.assertEquals(tok.verifier, req['oauth_verifier'])

class SignatureMethod_Bad(oauth.SignatureMethod):
name = "BAD"
Expand Down Expand Up @@ -682,6 +735,13 @@ def test_verify_request(self):
self.assertEquals(parameters['foo'], 59)
self.assertEquals(parameters['multi'], ['FOO','BAR'])

def test_build_authenticate_header(self):
server = oauth.Server()
headers = server.build_authenticate_header('example.com')
self.assertTrue('WWW-Authenticate' in headers)
self.assertEquals('OAuth realm="example.com"',
headers['WWW-Authenticate'])

def test_no_version(self):
url = "http://sp.example.com/"

Expand Down Expand Up @@ -861,6 +921,23 @@ def create_simple_multipart_data(self, data):
content_type = 'multipart/form-data; boundary=%s' % boundary
return content_type, crlf.join(items)

def test_init(self):
class Blah():
pass

try:
client = oauth.Client(Blah())
self.fail("Client.__init__() accepted invalid Consumer.")
except ValueError:
pass

consumer = oauth.Consumer('token', 'secret')
try:
client = oauth.Client(consumer, Blah())
self.fail("Client.__init__() accepted invalid Token.")
except ValueError:
pass

def test_access_token_get(self):
"""Test getting an access token via GET."""
client = oauth.Client(self.consumer, None)
Expand Down

0 comments on commit 77eb526

Please sign in to comment.