11from __future__ import unicode_literals
22
3+ import base64
34import json
45
56from django .test import TestCase , RequestFactory
@@ -437,6 +438,51 @@ def test_basic_auth_bad_secret(self):
437438 response = self .client .post (reverse ('oauth2_provider:token' ), data = token_request_data , ** auth_headers )
438439 self .assertEqual (response .status_code , 400 )
439440
441+ def test_basic_auth_wrong_auth_type (self ):
442+ """
443+ Request an access token using basic authentication for client authentication
444+ """
445+ self .client .login (username = "test_user" , password = "123456" )
446+ authorization_code = self .get_auth ()
447+
448+ token_request_data = {
449+ 'grant_type' : 'authorization_code' ,
450+ 'code' : authorization_code ,
451+ 'redirect_uri' : 'http://example.it'
452+ }
453+
454+ user_pass = '{0}:{1}' .format (self .application .client_id , self .application .client_secret )
455+ auth_string = base64 .b64encode (user_pass .encode ('utf-8' ))
456+ auth_headers = {
457+ 'HTTP_AUTHORIZATION' : 'Wrong ' + auth_string .decode ("utf-8" ),
458+ }
459+
460+ response = self .client .post (reverse ('oauth2_provider:token' ), data = token_request_data , ** auth_headers )
461+ self .assertEqual (response .status_code , 400 )
462+
463+ def test_request_body_params (self ):
464+ """
465+ Request an access token using client_type: public
466+ """
467+ self .client .login (username = "test_user" , password = "123456" )
468+ authorization_code = self .get_auth ()
469+
470+ token_request_data = {
471+ 'grant_type' : 'authorization_code' ,
472+ 'code' : authorization_code ,
473+ 'redirect_uri' : 'http://example.it' ,
474+ 'client_id' : self .application .client_id ,
475+ 'client_secret' : self .application .client_secret ,
476+ }
477+
478+ response = self .client .post (reverse ('oauth2_provider:token' ), data = token_request_data )
479+ self .assertEqual (response .status_code , 200 )
480+
481+ content = json .loads (response .content .decode ("utf-8" ))
482+ self .assertEqual (content ['token_type' ], "Bearer" )
483+ self .assertEqual (content ['scope' ], "read write" )
484+ self .assertEqual (content ['expires_in' ], oauth2_settings .ACCESS_TOKEN_EXPIRE_SECONDS )
485+
440486 def test_public (self ):
441487 """
442488 Request an access token using client_type: public
@@ -451,8 +497,7 @@ def test_public(self):
451497 'grant_type' : 'authorization_code' ,
452498 'code' : authorization_code ,
453499 'redirect_uri' : 'http://example.it' ,
454- 'client_id' : self .application .client_id ,
455- 'client_secret' : self .application .client_secret ,
500+ 'client_id' : self .application .client_id
456501 }
457502
458503 response = self .client .post (reverse ('oauth2_provider:token' ), data = token_request_data )
0 commit comments