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,10 +270,11 @@ 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'
276
+ 'bar' : 'foo' ,
277
+ 'multi' : ['FOO' ,'BAR' ]
277
278
}
278
279
279
280
params = oauth_params
@@ -301,10 +302,10 @@ def test_to_header(self):
301
302
parts = value .split ('OAuth ' )
302
303
vars = parts [1 ].split (', ' )
303
304
self .assertTrue (len (vars ), (len (params ) + 1 ))
304
-
305
+
305
306
res = {}
306
307
for v in vars :
307
- var , val = v .split ('=' )
308
+ var , val = v .split ('=' )
308
309
res [var ] = urllib .unquote (val .strip ('"' ))
309
310
310
311
self .assertEquals (realm , res ['realm' ])
@@ -319,6 +320,7 @@ def test_to_postdata(self):
319
320
realm = "http://sp.example.com/"
320
321
321
322
params = {
323
+ 'multi' : ['FOO' ,'BAR' ],
322
324
'oauth_version' : "1.0" ,
323
325
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
324
326
'oauth_timestamp' : "137131200" ,
@@ -329,8 +331,12 @@ def test_to_postdata(self):
329
331
}
330
332
331
333
req = oauth .Request ("GET" , realm , params )
332
-
333
- self .assertEquals (params , dict (parse_qsl (req .to_postdata ())))
334
+
335
+ flat = [('multi' ,'FOO' ),('multi' ,'BAR' )]
336
+ del params ['multi' ]
337
+ flat .extend (params .items ())
338
+ kf = lambda x : x [0 ]
339
+ self .assertEquals (sorted (flat , key = kf ), sorted (parse_qsl (req .to_postdata ()), key = kf ))
334
340
335
341
def test_to_url (self ):
336
342
url = "http://sp.example.com/"
@@ -348,13 +354,13 @@ def test_to_url(self):
348
354
req = oauth .Request ("GET" , url , params )
349
355
exp = urlparse .urlparse ("%s?%s" % (url , urllib .urlencode (params )))
350
356
res = urlparse .urlparse (req .to_url ())
351
- self .assertEquals (exp .scheme , res .scheme )
352
- self .assertEquals (exp .netloc , res .netloc )
353
- self .assertEquals (exp .path , res .path )
357
+ self .assertEquals (exp .scheme , res .scheme )
358
+ self .assertEquals (exp .netloc , res .netloc )
359
+ self .assertEquals (exp .path , res .path )
354
360
355
361
a = parse_qs (exp .query )
356
362
b = parse_qs (res .query )
357
- self .assertEquals (a , b )
363
+ self .assertEquals (a , b )
358
364
359
365
def test_get_normalized_parameters (self ):
360
366
url = "http://sp.example.com/"
@@ -426,7 +432,7 @@ def test_sign_request(self):
426
432
req = oauth .Request (method = "GET" , url = url , parameters = params )
427
433
428
434
methods = {
429
- 'TQ6vGQ5A6IZn8dmeGB4+/Jl3EMI=' : oauth .SignatureMethod_HMAC_SHA1 (),
435
+ 'TQ6vGQ5A6IZn8dmeGB4+/Jl3EMI=' : oauth .SignatureMethod_HMAC_SHA1 (),
430
436
'con-test-secret&tok-test-secret' : oauth .SignatureMethod_PLAINTEXT ()
431
437
}
432
438
@@ -447,17 +453,17 @@ def test_from_request(self):
447
453
'oauth_token' : "ad180jjd733klru7" ,
448
454
'oauth_signature' : "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D" ,
449
455
}
450
-
456
+
451
457
req = oauth .Request ("GET" , url , params )
452
458
headers = req .to_header ()
453
459
454
460
# Test from the headers
455
461
req = oauth .Request .from_request ("GET" , url , headers )
456
462
self .assertEquals (req .method , "GET" )
457
463
self .assertEquals (req .url , url )
458
-
464
+
459
465
self .assertEquals (params , req .copy ())
460
-
466
+
461
467
# Test with bad OAuth headers
462
468
bad_headers = {
463
469
'Authorization' : 'OAuth this is a bad header'
@@ -469,7 +475,7 @@ def test_from_request(self):
469
475
# Test getting from query string
470
476
qs = urllib .urlencode (params )
471
477
req = oauth .Request .from_request ("GET" , url , query_string = qs )
472
-
478
+
473
479
exp = parse_qs (qs , keep_blank_values = False )
474
480
for k , v in exp .iteritems ():
475
481
exp [k ] = urllib .unquote (v [0 ])
@@ -492,7 +498,7 @@ def test_from_token_and_callback(self):
492
498
'oauth_token' : "ad180jjd733klru7" ,
493
499
'oauth_signature' : "wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D" ,
494
500
}
495
-
501
+
496
502
tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
497
503
req = oauth .Request .from_token_and_callback (tok )
498
504
self .assertFalse ('oauth_callback' in req )
@@ -507,7 +513,7 @@ def test_from_consumer_and_token(self):
507
513
508
514
tok = oauth .Token (key = "tok-test-key" , secret = "tok-test-secret" )
509
515
con = oauth .Consumer (key = "con-test-key" , secret = "con-test-secret" )
510
- req = oauth .Request .from_consumer_and_token (con , token = tok ,
516
+ req = oauth .Request .from_consumer_and_token (con , token = tok ,
511
517
http_method = "GET" , http_url = url )
512
518
513
519
self .assertEquals (req ['oauth_token' ], tok .key )
@@ -532,10 +538,11 @@ def setUp(self):
532
538
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
533
539
'oauth_timestamp' : int (time .time ()),
534
540
'bar' : 'blerg' ,
541
+ 'multi' : ['FOO' ,'BAR' ],
535
542
'foo' : 59
536
543
}
537
544
538
- self .consumer = oauth .Consumer (key = "consumer-key" ,
545
+ self .consumer = oauth .Consumer (key = "consumer-key" ,
539
546
secret = "consumer-secret" )
540
547
self .token = oauth .Token (key = "token-key" , secret = "token-secret" )
541
548
@@ -549,7 +556,7 @@ def setUp(self):
549
556
def test_init (self ):
550
557
server = oauth .Server (signature_methods = {'HMAC-SHA1' : oauth .SignatureMethod_HMAC_SHA1 ()})
551
558
self .assertTrue ('HMAC-SHA1' in server .signature_methods )
552
- self .assertTrue (isinstance (server .signature_methods ['HMAC-SHA1' ],
559
+ self .assertTrue (isinstance (server .signature_methods ['HMAC-SHA1' ],
553
560
oauth .SignatureMethod_HMAC_SHA1 ))
554
561
555
562
server = oauth .Server ()
@@ -558,15 +565,15 @@ def test_init(self):
558
565
def test_add_signature_method (self ):
559
566
server = oauth .Server ()
560
567
res = server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
561
- self .assertTrue (len (res ) == 1 )
568
+ self .assertTrue (len (res ) == 1 )
562
569
self .assertTrue ('HMAC-SHA1' in res )
563
- self .assertTrue (isinstance (res ['HMAC-SHA1' ],
570
+ self .assertTrue (isinstance (res ['HMAC-SHA1' ],
564
571
oauth .SignatureMethod_HMAC_SHA1 ))
565
572
566
573
res = server .add_signature_method (oauth .SignatureMethod_PLAINTEXT ())
567
- self .assertTrue (len (res ) == 2 )
574
+ self .assertTrue (len (res ) == 2 )
568
575
self .assertTrue ('PLAINTEXT' in res )
569
- self .assertTrue (isinstance (res ['PLAINTEXT' ],
576
+ self .assertTrue (isinstance (res ['PLAINTEXT' ],
570
577
oauth .SignatureMethod_PLAINTEXT ))
571
578
572
579
def test_verify_request (self ):
@@ -578,8 +585,10 @@ def test_verify_request(self):
578
585
579
586
self .assertTrue ('bar' in parameters )
580
587
self .assertTrue ('foo' in parameters )
588
+ self .assertTrue ('multi' in parameters )
581
589
self .assertEquals (parameters ['bar' ], 'blerg' )
582
590
self .assertEquals (parameters ['foo' ], 59 )
591
+ self .assertEquals (parameters ['multi' ], ['FOO' ,'BAR' ])
583
592
584
593
def test_no_version (self ):
585
594
url = "http://sp.example.com/"
@@ -588,10 +597,11 @@ def test_no_version(self):
588
597
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
589
598
'oauth_timestamp' : int (time .time ()),
590
599
'bar' : 'blerg' ,
600
+ 'multi' : ['FOO' ,'BAR' ],
591
601
'foo' : 59
592
602
}
593
603
594
- self .consumer = oauth .Consumer (key = "consumer-key" ,
604
+ self .consumer = oauth .Consumer (key = "consumer-key" ,
595
605
secret = "consumer-secret" )
596
606
self .token = oauth .Token (key = "token-key" , secret = "token-secret" )
597
607
@@ -616,10 +626,11 @@ def test_invalid_version(self):
616
626
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
617
627
'oauth_timestamp' : int (time .time ()),
618
628
'bar' : 'blerg' ,
629
+ 'multi' : ['foo' ,'bar' ],
619
630
'foo' : 59
620
631
}
621
632
622
- consumer = oauth .Consumer (key = "consumer-key" ,
633
+ consumer = oauth .Consumer (key = "consumer-key" ,
623
634
secret = "consumer-secret" )
624
635
token = oauth .Token (key = "token-key" , secret = "token-secret" )
625
636
@@ -633,7 +644,7 @@ def test_invalid_version(self):
633
644
server = oauth .Server ()
634
645
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
635
646
636
- self .assertRaises (oauth .Error , server .verify_request , request ,
647
+ self .assertRaises (oauth .Error , server .verify_request , request ,
637
648
consumer , token )
638
649
639
650
def test_invalid_signature_method (self ):
@@ -644,10 +655,11 @@ def test_invalid_signature_method(self):
644
655
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
645
656
'oauth_timestamp' : int (time .time ()),
646
657
'bar' : 'blerg' ,
658
+ 'multi' : ['FOO' ,'BAR' ],
647
659
'foo' : 59
648
660
}
649
661
650
- consumer = oauth .Consumer (key = "consumer-key" ,
662
+ consumer = oauth .Consumer (key = "consumer-key" ,
651
663
secret = "consumer-secret" )
652
664
token = oauth .Token (key = "token-key" , secret = "token-secret" )
653
665
@@ -661,7 +673,7 @@ def test_invalid_signature_method(self):
661
673
server = oauth .Server ()
662
674
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
663
675
664
- self .assertRaises (oauth .Error , server .verify_request , request ,
676
+ self .assertRaises (oauth .Error , server .verify_request , request ,
665
677
consumer , token )
666
678
667
679
def test_missing_signature (self ):
@@ -672,10 +684,11 @@ def test_missing_signature(self):
672
684
'oauth_nonce' : "4572616e48616d6d65724c61686176" ,
673
685
'oauth_timestamp' : int (time .time ()),
674
686
'bar' : 'blerg' ,
687
+ 'multi' : ['FOO' ,'BAR' ],
675
688
'foo' : 59
676
689
}
677
690
678
- consumer = oauth .Consumer (key = "consumer-key" ,
691
+ consumer = oauth .Consumer (key = "consumer-key" ,
679
692
secret = "consumer-secret" )
680
693
token = oauth .Token (key = "token-key" , secret = "token-secret" )
681
694
@@ -690,15 +703,15 @@ def test_missing_signature(self):
690
703
server = oauth .Server ()
691
704
server .add_signature_method (oauth .SignatureMethod_HMAC_SHA1 ())
692
705
693
- self .assertRaises (oauth .MissingSignature , server .verify_request ,
706
+ self .assertRaises (oauth .MissingSignature , server .verify_request ,
694
707
request , consumer , token )
695
708
696
709
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
710
+ # Request Token: http://oauth-sandbox.sevengoslings.net/request_token
711
+ # Auth: http://oauth-sandbox.sevengoslings.net/authorize
712
+ # Access Token: http://oauth-sandbox.sevengoslings.net/access_token
713
+ # Two-legged: http://oauth-sandbox.sevengoslings.net/two_legged
714
+ # Three-legged: http://oauth-sandbox.sevengoslings.net/three_legged
702
715
# Key: bd37aed57e15df53
703
716
# Secret: 0e9e6413a9ef49510a4f68ed02cd
704
717
class TestClient (unittest .TestCase ):
@@ -714,18 +727,19 @@ class TestClient(unittest.TestCase):
714
727
'two_legged' : '/two_legged' ,
715
728
'three_legged' : '/three_legged'
716
729
}
717
-
730
+
718
731
consumer_key = 'bd37aed57e15df53'
719
732
consumer_secret = '0e9e6413a9ef49510a4f68ed02cd'
720
733
host = 'http://oauth-sandbox.sevengoslings.net'
721
734
722
735
def setUp (self ):
723
- self .consumer = oauth .Consumer (key = self .consumer_key ,
736
+ self .consumer = oauth .Consumer (key = self .consumer_key ,
724
737
secret = self .consumer_secret )
725
738
726
739
self .body = {
727
740
'foo' : 'bar' ,
728
741
'bar' : 'foo' ,
742
+ 'multi' : ['FOO' ,'BAR' ],
729
743
'blah' : 599999
730
744
}
731
745
@@ -735,7 +749,7 @@ def _uri(self, type):
735
749
raise KeyError ("%s is not a valid OAuth URI type." % type )
736
750
737
751
return "%s%s" % (self .host , uri )
738
-
752
+
739
753
def test_access_token_get (self ):
740
754
"""Test getting an access token via GET."""
741
755
client = oauth .Client (self .consumer , None )
@@ -757,7 +771,7 @@ def test_access_token_post(self):
757
771
def _two_legged (self , method ):
758
772
client = oauth .Client (self .consumer , None )
759
773
760
- return client .request (self ._uri ('two_legged' ), method ,
774
+ return client .request (self ._uri ('two_legged' ), method ,
761
775
body = urllib .urlencode (self .body ))
762
776
763
777
def test_two_legged_post (self ):
0 commit comments