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