<s> import json <EOL> import logging <EOL> try : <EOL> from urlparse import parse_qsl , urlparse <EOL> except ImportError : <EOL> from urllib . parse import parse_qsl , urlparse <EOL> from mock import patch <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . auth import get_user_model <EOL> from django . test . utils import override_settings <EOL> from rest_framework . test import APITestCase <EOL> from rest_framework . authtoken . models import Token <EOL> from rest_framework . permissions import DjangoModelPermissionsOrAnonReadOnly <EOL> from httpretty import HTTPretty <EOL> from social . utils import module_member , parse_qs <EOL> from social . backends . utils import load_backends <EOL> from social . tests . backends . test_facebook import FacebookOAuth2Test <EOL> from social . tests . backends . test_twitter import TwitterOAuth1Test <EOL> from rest_social_auth import views <EOL> from . utils import modify_settings <EOL> l = logging . getLogger ( __name__ ) <EOL> for attr in ( attr for attr in dir ( FacebookOAuth2Test ) if attr . startswith ( '<STR_LIT>' ) ) : <EOL> delattr ( FacebookOAuth2Test , attr ) <EOL> for attr in ( attr for attr in dir ( TwitterOAuth1Test ) if attr . startswith ( '<STR_LIT>' ) ) : <EOL> delattr ( TwitterOAuth1Test , attr ) <EOL> session_modify_settings = dict ( <EOL> INSTALLED_APPS = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> } , <EOL> ) <EOL> token_modify_settings = dict ( <EOL> INSTALLED_APPS = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' <EOL> ] <EOL> } , <EOL> MIDDLEWARE_CLASSES = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> } <EOL> ) <EOL> jwt_modify_settings = dict ( <EOL> INSTALLED_APPS = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> } , <EOL> MIDDLEWARE_CLASSES = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> } <EOL> ) <EOL> class RestSocialMixin ( object ) : <EOL> def setUp ( self ) : <EOL> HTTPretty . enable ( ) <EOL> Backend = module_member ( self . backend_path ) <EOL> self . strategy = views . load_strategy ( ) <EOL> self . backend = Backend ( self . strategy , redirect_uri = self . complete_url ) <EOL> self . name = self . backend . name . upper ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> self . complete_url = self . strategy . build_absolute_uri ( <EOL> self . raw_complete_url . format ( self . backend . name ) <EOL> ) <EOL> backends = ( self . backend_path , ) <EOL> load_backends ( backends , force_load = True ) <EOL> user_data_body = json . loads ( self . user_data_body ) <EOL> self . email = '<STR_LIT>' <EOL> user_data_body [ '<STR_LIT:email>' ] = self . email <EOL> self . user_data_body = json . dumps ( user_data_body ) <EOL> self . do_rest_login ( ) <EOL> def tearDown ( self ) : <EOL> HTTPretty . disable ( ) <EOL> HTTPretty . reset ( ) <EOL> self . backend = None <EOL> self . strategy = None <EOL> self . name = None <EOL> self . complete_url = None <EOL> class BaseFacebookAPITestCase ( RestSocialMixin , FacebookOAuth2Test ) : <EOL> def do_rest_login ( self ) : <EOL> start_url = self . backend . start ( ) . url <EOL> self . auth_handlers ( start_url ) <EOL> class BaseTwitterApiTestCase ( RestSocialMixin , TwitterOAuth1Test ) : <EOL> def do_rest_login ( self ) : <EOL> self . request_token_handler ( ) <EOL> start_url = self . backend . start ( ) . url <EOL> self . auth_handlers ( start_url ) <EOL> class TestSocialAuth1 ( APITestCase , BaseTwitterApiTestCase ) : <EOL> @ modify_settings ( ** session_modify_settings ) <EOL> def test_login_social_oauth1_session ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data , parse_qs ( self . request_token_body ) ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> def test_login_social_oauth1_token ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data , parse_qs ( self . request_token_body ) ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : [ '<STR_LIT>' , ] } ) <EOL> def test_login_social_oauth1_jwt ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import rest_framework_jwt <EOL> except ImportError : <EOL> return <EOL> assert rest_framework_jwt is not None <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data , parse_qs ( self . request_token_body ) ) <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> class TestSocialAuth2 ( APITestCase , BaseFacebookAPITestCase ) : <EOL> @ modify_settings ( ** session_modify_settings ) <EOL> def _check_login_social_session ( self , url , data ) : <EOL> resp = self . client . post ( url , data ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data [ '<STR_LIT:email>' ] , self . email ) <EOL> self . assertTrue ( '<STR_LIT>' in resp . cookies ) <EOL> self . assertTrue ( <EOL> get_user_model ( ) . objects . filter ( email = self . email ) . exists ( ) ) <EOL> @ modify_settings ( ** token_modify_settings ) <EOL> def _check_login_social_token_user ( self , url , data ) : <EOL> resp = self . client . post ( url , data ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data [ '<STR_LIT:email>' ] , self . email ) <EOL> token = Token . objects . get ( key = resp . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( token . user . email , self . email ) <EOL> @ modify_settings ( ** token_modify_settings ) <EOL> def _check_login_social_token_only ( self , url , data ) : <EOL> resp = self . client . post ( url , data ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> token = Token . objects . get ( key = resp . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( token . user . email , self . email ) <EOL> @ modify_settings ( ** jwt_modify_settings ) <EOL> def _check_login_social_jwt_only ( self , url , data ) : <EOL> try : <EOL> from rest_framework_jwt . settings import api_settings <EOL> except ImportError : <EOL> return <EOL> jwt_decode_handler = api_settings . JWT_DECODE_HANDLER <EOL> resp = self . client . post ( url , data ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> jwt_data = jwt_decode_handler ( resp . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( jwt_data [ '<STR_LIT:email>' ] , self . email ) <EOL> @ modify_settings ( ** jwt_modify_settings ) <EOL> def _check_login_social_jwt_user ( self , url , data ) : <EOL> try : <EOL> from rest_framework_jwt . settings import api_settings <EOL> except ImportError : <EOL> return <EOL> jwt_decode_handler = api_settings . JWT_DECODE_HANDLER <EOL> resp = self . client . post ( url , data ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( resp . data [ '<STR_LIT:email>' ] , self . email ) <EOL> jwt_data = jwt_decode_handler ( resp . data [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( jwt_data [ '<STR_LIT:email>' ] , self . email ) <EOL> def test_login_social_session ( self ) : <EOL> self . _check_login_social_session ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_session_provider_in_url ( self ) : <EOL> self . _check_login_social_session ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_token_user ( self ) : <EOL> self . _check_login_social_token_user ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_token_user_provider_in_url ( self ) : <EOL> self . _check_login_social_token_user ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_token_only ( self ) : <EOL> self . _check_login_social_token_only ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_token_only_provider_in_url ( self ) : <EOL> self . _check_login_social_token_only ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_jwt_only ( self ) : <EOL> self . _check_login_social_jwt_only ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_jwt_only_provider_in_url ( self ) : <EOL> self . _check_login_social_jwt_only ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_jwt_user ( self ) : <EOL> self . _check_login_social_jwt_user ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_login_social_jwt_user_provider_in_url ( self ) : <EOL> self . _check_login_social_jwt_user ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> data = { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> def test_no_provider_session ( self ) : <EOL> resp = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> def test_unknown_provider_session ( self ) : <EOL> resp = self . client . post ( <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> { '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> def test_login_social_http_origin ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } , <EOL> HTTP_ORIGIN = "<STR_LIT>" ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> url_params = dict ( parse_qsl ( urlparse ( HTTPretty . latest_requests [ <NUM_LIT:0> ] . path ) . query ) ) <EOL> self . assertEqual ( url_params [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> @ override_settings ( REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI = '<STR_LIT>' ) <EOL> def test_login_absolute_redirect ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> url_params = dict ( parse_qsl ( urlparse ( HTTPretty . latest_requests [ <NUM_LIT:0> ] . path ) . query ) ) <EOL> self . assertEqual ( '<STR_LIT>' , url_params [ '<STR_LIT>' ] ) <EOL> @ override_settings ( REST_SOCIAL_OAUTH_ABSOLUTE_REDIRECT_URI = '<STR_LIT>' ) <EOL> def test_login_manual_redirect ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> url_params = dict ( parse_qsl ( urlparse ( HTTPretty . latest_requests [ <NUM_LIT:0> ] . path ) . query ) ) <EOL> self . assertEqual ( '<STR_LIT>' , url_params [ '<STR_LIT>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_login_social_session_model_permission ( self , m_permission_classes ) : <EOL> setattr ( m_permission_classes , '<STR_LIT>' , lambda * args , ** kwargs : ( DjangoModelPermissionsOrAnonReadOnly , ) ) <EOL> self . _check_login_social_session ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> class TestSocialAuth2Error ( APITestCase , BaseFacebookAPITestCase ) : <EOL> access_token_status = <NUM_LIT> <EOL> def test_login_oauth_provider_error ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> class TestSocialAuth2HTTPError ( APITestCase , BaseFacebookAPITestCase ) : <EOL> access_token_status = <NUM_LIT> <EOL> def test_login_oauth_provider_http_error ( self ) : <EOL> resp = self . client . post ( reverse ( '<STR_LIT>' ) , <EOL> data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:code>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) </s>
