1
1
"""
2
2
The MIT License
3
-
3
+
4
4
Copyright (c) 2009 Vic Fryzel
5
-
5
+
6
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
of this software and associated documentation files (the "Software"), to deal
8
8
in the Software without restriction, including without limitation the rights
9
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
10
copies of the Software, and to permit persons to whom the Software is
11
11
furnished to do so, subject to the following conditions:
12
-
12
+
13
13
The above copyright notice and this permission notice shall be included in
14
14
all copies or substantial portions of the Software.
15
-
15
+
16
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -61,7 +61,7 @@ def test_build_auth_header(self):
61
61
self .assertEqual (header ['WWW-Authenticate' ], 'OAuth realm="%s"' %
62
62
realm )
63
63
self .assertEqual (len (header ), 1 )
64
-
64
+
65
65
def test_escape (self ):
66
66
string = 'http://whatever.com/~someuser/?test=test&other=other'
67
67
self .assert_ ('~' in oauth .escape (string ))
@@ -83,7 +83,7 @@ def test_gen_verifier(self):
83
83
def test_gen_timestamp (self ):
84
84
exp = int (time .time ())
85
85
now = oauth .generate_timestamp ()
86
- self .assertEqual (exp , now )
86
+ self .assertEqual (exp , now )
87
87
88
88
class TestConsumer (unittest .TestCase ):
89
89
def setUp (self ):
@@ -252,7 +252,7 @@ def test_url(self):
252
252
253
253
req = oauth .Request (method , url1 )
254
254
self .assertEquals (req .url , exp1 )
255
-
255
+
256
256
req = oauth .Request (method , url2 )
257
257
self .assertEquals (req .url , exp2 )
258
258
@@ -270,11 +270,10 @@ def test_get_nonoauth_parameters(self):
270
270
oauth_params = {
271
271
'oauth_consumer' : 'asdfasdfasdf'
272
272
}
273
-
273
+
274
274
other_params = {
275
275
'foo' : 'baz' ,
276
- 'bar' : 'foo' ,
277
- 'multi' : ['FOO' ,'BAR' ]
276
+ 'bar' : 'foo'
278
277
}
279
278
280
279
params = oauth_params
@@ -302,10 +301,10 @@ def test_to_header(self):
302
301
parts = value .split ('OAuth ' )
303
302
vars = parts [1 ].split (', ' )
304
303
self .assertTrue (len (vars ), (len (params ) + 1 ))
305
-
304
+
306
305
res = {}
307
306
for v in vars :
308
- var , val = v .split ('=' )
307
+ var , val = v .split ('=' )
309
308
res [var ] = urllib .unquote (val .strip ('"' ))
310
309
311
310
self .assertEquals (realm , res ['realm' ])
@@ -320,7 +319,6 @@ def test_to_postdata(self):
320
319
realm = "http://sp.example.com/"
321
320
322
321
params = {
323
- 'multi' : ['FOO' ,'BAR' ],
324
322
'oauth_version' : "1.0" ,
325
323
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
326
324
'oauth_timestamp' : "137131200" ,
@@ -331,11 +329,8 @@ def test_to_postdata(self):
331
329
}
332
330
333
331
req = oauth .Request ("GET" , realm , params )
334
-
335
- flat = [('multi' ,'FOO' ),('multi' ,'BAR' )]
336
- del params ['multi' ]
337
- flat .extend (params .items ())
338
- self .assertEquals (flat , parse_qsl (req .to_postdata ()))
332
+
333
+ self .assertEquals (params , dict (parse_qsl (req .to_postdata ())))
339
334
340
335
def test_to_url (self ):
341
336
url = "http://sp.example.com/"
@@ -353,13 +348,13 @@ def test_to_url(self):
353
348
req = oauth .Request ("GET" , url , params )
354
349
exp = urlparse .urlparse ("%s?%s" % (url , urllib .urlencode (params )))
355
350
res = urlparse .urlparse (req .to_url ())
356
- self .assertEquals (exp .scheme , res .scheme )
357
- self .assertEquals (exp .netloc , res .netloc )
358
- self .assertEquals (exp .path , res .path )
351
+ self .assertEquals (exp .scheme , res .scheme )
352
+ self .assertEquals (exp .netloc , res .netloc )
353
+ self .assertEquals (exp .path , res .path )
359
354
360
355
a = parse_qs (exp .query )
361
356
b = parse_qs (res .query )
362
- self .assertEquals (a , b )
357
+ self .assertEquals (a , b )
363
358
364
359
def test_get_normalized_parameters (self ):
365
360
url = "http://sp.example.com/"
@@ -431,7 +426,7 @@ def test_sign_request(self):
431
426
req = oauth .Request (method = "GET" , url = url , parameters = params )
432
427
433
428
methods = {
434
- 'TQ6vGQ5A6IZn8dmeGB4+/Jl3EMI=' : oauth .SignatureMethod_HMAC_SHA1 (),
429
+ 'TQ6vGQ5A6IZn8dmeGB4+/Jl3EMI=' : oauth .SignatureMethod_HMAC_SHA1 (),
435
430
'con-test-secret&tok-test-secret' : oauth .SignatureMethod_PLAINTEXT ()
436
431
}
437
432
@@ -452,17 +447,17 @@ def test_from_request(self):
452
447
'oauth_token' : "ad180jjd733klru7" ,
453
448
'oauth_signature' : "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D" ,
454
449
}
455
-
450
+
456
451
req = oauth .Request ("GET" , url , params )
457
452
headers = req .to_header ()
458
453
459
454
# Test from the headers
460
455
req = oauth .Request .from_request ("GET" , url , headers )
461
456
self .assertEquals (req .method , "GET" )
462
457
self .assertEquals (req .url , url )
463
-
458
+
464
459
self .assertEquals (params , req .copy ())
465
-
460
+
466
461
# Test with bad OAuth headers
467
462
bad_headers = {
468
463
'Authorization' : 'OAuth this is a bad header'
@@ -474,7 +469,7 @@ def test_from_request(self):
474
469
# Test getting from query string
475
470
qs = urllib .urlencode (params )
476
471
req = oauth .Request .from_request ("GET" , url , query_string = qs )
477
-
472
+
478
473
exp = parse_qs (qs , keep_blank_values = False )
479
474
for k , v in exp .iteritems ():
480
475
exp [k ] = urllib .unquote (v [0 ])
@@ -497,7 +492,7 @@ def test_from_token_and_callback(self):
497
492
'oauth_token' : "ad180jjd733klru7" ,
498
493
'oauth_signature' : "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D" ,
499
494
}
500
-
495
+
501
496
tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
502
497
req = oauth .Request .from_token_and_callback (tok )
503
498
self .assertFalse ('oauth_callback' in req )
@@ -512,7 +507,7 @@ def test_from_consumer_and_token(self):
512
507
513
508
tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
514
509
con = oauth .Consumer (key = "con-test-key" , secret = "con-test-secret" )
515
- req = oauth .Request .from_consumer_and_token (con , token = tok ,
510
+ req = oauth .Request .from_consumer_and_token (con , token = tok ,
516
511
http_method = "GET" , http_url = url )
517
512
518
513
self .assertEquals (req ['oauth_token' ], tok .key )
@@ -537,11 +532,10 @@ def setUp(self):
537
532
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
538
533
'oauth_timestamp' : int (time .time ()),
539
534
'bar' : 'blerg' ,
540
- 'multi' : ['FOO' ,'BAR' ],
541
535
'foo' : 59
542
536
}
543
537
544
- self .consumer = oauth .Consumer (key = "consumer-key" ,
538
+ self .consumer = oauth .Consumer (key = "consumer-key" ,
545
539
secret = "consumer-secret" )
546
540
self .token = oauth .Token (key = "token-key" , secret = "token-secret" )
547
541
@@ -555,7 +549,7 @@ def setUp(self):
555
549
def test_init (self ):
556
550
server = oauth .Server (signature_methods = {'HMAC-SHA1' : oauth .SignatureMethod_HMAC_SHA1 ()})
557
551
self .assertTrue ('HMAC-SHA1' in server .signature_methods )
558
- self .assertTrue (isinstance (server .signature_methods ['HMAC-SHA1' ],
552
+ self .assertTrue (isinstance (server .signature_methods ['HMAC-SHA1' ],
559
553
oauth .SignatureMethod_HMAC_SHA1 ))
560
554
561
555
server = oauth .Server ()
@@ -564,15 +558,15 @@ def test_init(self):
564
558
def test_add_signature_method (self ):
565
559
server = oauth .Server ()
566
560
res = server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
567
- self .assertTrue (len (res ) == 1 )
561
+ self .assertTrue (len (res ) == 1 )
568
562
self .assertTrue ('HMAC-SHA1' in res )
569
- self .assertTrue (isinstance (res ['HMAC-SHA1' ],
563
+ self .assertTrue (isinstance (res ['HMAC-SHA1' ],
570
564
oauth .SignatureMethod_HMAC_SHA1 ))
571
565
572
566
res = server .add_signature_method (oauth .SignatureMethod_PLAINTEXT ())
573
- self .assertTrue (len (res ) == 2 )
567
+ self .assertTrue (len (res ) == 2 )
574
568
self .assertTrue ('PLAINTEXT' in res )
575
- self .assertTrue (isinstance (res ['PLAINTEXT' ],
569
+ self .assertTrue (isinstance (res ['PLAINTEXT' ],
576
570
oauth .SignatureMethod_PLAINTEXT ))
577
571
578
572
def test_verify_request (self ):
@@ -584,10 +578,8 @@ def test_verify_request(self):
584
578
585
579
self .assertTrue ('bar' in parameters )
586
580
self .assertTrue ('foo' in parameters )
587
- self .assertTrue ('multi' in parameters )
588
581
self .assertEquals (parameters ['bar' ], 'blerg' )
589
582
self .assertEquals (parameters ['foo' ], 59 )
590
- self .assertEquals (parameters ['multi' ], ['FOO' ,'BAR' ])
591
583
592
584
def test_no_version (self ):
593
585
url = "http://sp.example.com/"
@@ -596,11 +588,10 @@ def test_no_version(self):
596
588
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
597
589
'oauth_timestamp' : int (time .time ()),
598
590
'bar' : 'blerg' ,
599
- 'multi' : ['FOO' ,'BAR' ],
600
591
'foo' : 59
601
592
}
602
593
603
- self .consumer = oauth .Consumer (key = "consumer-key" ,
594
+ self .consumer = oauth .Consumer (key = "consumer-key" ,
604
595
secret = "consumer-secret" )
605
596
self .token = oauth .Token (key = "token-key" , secret = "token-secret" )
606
597
@@ -625,11 +616,10 @@ def test_invalid_version(self):
625
616
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
626
617
'oauth_timestamp' : int (time .time ()),
627
618
'bar' : 'blerg' ,
628
- 'multi' : ['foo' ,'bar' ],
629
619
'foo' : 59
630
620
}
631
621
632
- consumer = oauth .Consumer (key = "consumer-key" ,
622
+ consumer = oauth .Consumer (key = "consumer-key" ,
633
623
secret = "consumer-secret" )
634
624
token = oauth .Token (key = "token-key" , secret = "token-secret" )
635
625
@@ -643,7 +633,7 @@ def test_invalid_version(self):
643
633
server = oauth .Server ()
644
634
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
645
635
646
- self .assertRaises (oauth .Error , server .verify_request , request ,
636
+ self .assertRaises (oauth .Error , server .verify_request , request ,
647
637
consumer , token )
648
638
649
639
def test_invalid_signature_method (self ):
@@ -654,11 +644,10 @@ def test_invalid_signature_method(self):
654
644
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
655
645
'oauth_timestamp' : int (time .time ()),
656
646
'bar' : 'blerg' ,
657
- 'multi' : ['FOO' ,'BAR' ],
658
647
'foo' : 59
659
648
}
660
649
661
- consumer = oauth .Consumer (key = "consumer-key" ,
650
+ consumer = oauth .Consumer (key = "consumer-key" ,
662
651
secret = "consumer-secret" )
663
652
token = oauth .Token (key = "token-key" , secret = "token-secret" )
664
653
@@ -672,7 +661,7 @@ def test_invalid_signature_method(self):
672
661
server = oauth .Server ()
673
662
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
674
663
675
- self .assertRaises (oauth .Error , server .verify_request , request ,
664
+ self .assertRaises (oauth .Error , server .verify_request , request ,
676
665
consumer , token )
677
666
678
667
def test_missing_signature (self ):
@@ -683,11 +672,10 @@ def test_missing_signature(self):
683
672
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
684
673
'oauth_timestamp' : int (time .time ()),
685
674
'bar' : 'blerg' ,
686
- 'multi' : ['FOO' ,'BAR' ],
687
675
'foo' : 59
688
676
}
689
677
690
- consumer = oauth .Consumer (key = "consumer-key" ,
678
+ consumer = oauth .Consumer (key = "consumer-key" ,
691
679
secret = "consumer-secret" )
692
680
token = oauth .Token (key = "token-key" , secret = "token-secret" )
693
681
@@ -702,15 +690,15 @@ def test_missing_signature(self):
702
690
server = oauth .Server ()
703
691
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
704
692
705
- self .assertRaises (oauth .MissingSignature , server .verify_request ,
693
+ self .assertRaises (oauth .MissingSignature , server .verify_request ,
706
694
request , consumer , token )
707
695
708
696
709
- # Request Token: http://oauth-sandbox.sevengoslings.net/request_token
710
- # Auth: http://oauth-sandbox.sevengoslings.net/authorize
711
- # Access Token: http://oauth-sandbox.sevengoslings.net/access_token
712
- # Two-legged: http://oauth-sandbox.sevengoslings.net/two_legged
713
- # Three-legged: http://oauth-sandbox.sevengoslings.net/three_legged
697
+ # Request Token: http://oauth-sandbox.sevengoslings.net/request_token
698
+ # Auth: http://oauth-sandbox.sevengoslings.net/authorize
699
+ # Access Token: http://oauth-sandbox.sevengoslings.net/access_token
700
+ # Two-legged: http://oauth-sandbox.sevengoslings.net/two_legged
701
+ # Three-legged: http://oauth-sandbox.sevengoslings.net/three_legged
714
702
# Key: bd37aed57e15df53
715
703
# Secret: 0e9e6413a9ef49510a4f68ed02cd
716
704
class TestClient (unittest .TestCase ):
@@ -726,19 +714,18 @@ class TestClient(unittest.TestCase):
726
714
'two_legged' : '/two_legged' ,
727
715
'three_legged' : '/three_legged'
728
716
}
729
-
717
+
730
718
consumer_key = 'bd37aed57e15df53'
731
719
consumer_secret = '0e9e6413a9ef49510a4f68ed02cd'
732
720
host = 'http://oauth-sandbox.sevengoslings.net'
733
721
734
722
def setUp (self ):
735
- self .consumer = oauth .Consumer (key = self .consumer_key ,
723
+ self .consumer = oauth .Consumer (key = self .consumer_key ,
736
724
secret = self .consumer_secret )
737
725
738
726
self .body = {
739
727
'foo' : 'bar' ,
740
728
'bar' : 'foo' ,
741
- 'multi' : ['FOO' ,'BAR' ],
742
729
'blah' : 599999
743
730
}
744
731
@@ -748,7 +735,7 @@ def _uri(self, type):
748
735
raise KeyError ("%s is not a valid OAuth URI type." % type )
749
736
750
737
return "%s%s" % (self .host , uri )
751
-
738
+
752
739
def test_access_token_get (self ):
753
740
"""Test getting an access token via GET."""
754
741
client = oauth .Client (self .consumer , None )
@@ -770,7 +757,7 @@ def test_access_token_post(self):
770
757
def _two_legged (self , method ):
771
758
client = oauth .Client (self .consumer , None )
772
759
773
- return client .request (self ._uri ('two_legged' ), method ,
760
+ return client .request (self ._uri ('two_legged' ), method ,
774
761
body = urllib .urlencode (self .body ))
775
762
776
763
def test_two_legged_post (self ):
0 commit comments