<s> import django <EOL> from django . core import mail <EOL> from django . contrib . auth . models import User <EOL> from django . conf import settings <EOL> from django . template import loader , TemplateDoesNotExist <EOL> from django . http import HttpRequest , HttpResponse <EOL> from django . utils import simplejson <EOL> from test import TestCase <EOL> from models import Consumer <EOL> from handler import BaseHandler <EOL> from utils import rc <EOL> from resource import Resource <EOL> class ConsumerTest ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . consumer = Consumer ( ) <EOL> self . consumer . name = "<STR_LIT>" <EOL> self . consumer . description = "<STR_LIT>" <EOL> self . consumer . user = User . objects . get ( pk = <NUM_LIT:3> ) <EOL> self . consumer . generate_random_codes ( ) <EOL> def _pre_test_email ( self ) : <EOL> template = "<STR_LIT>" % self . consumer . status <EOL> try : <EOL> loader . render_to_string ( template , { <EOL> '<STR_LIT>' : self . consumer , <EOL> '<STR_LIT:user>' : self . consumer . user <EOL> } ) <EOL> return True <EOL> except TemplateDoesNotExist : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def test_create_pending ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _pre_test_email ( ) : <EOL> return <EOL> if len ( settings . ADMINS ) : <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:2> ) <EOL> else : <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> expected = "<STR_LIT>" <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . subject , expected ) <EOL> def test_delete_consumer ( self ) : <EOL> """<STR_LIT>""" <EOL> mail . outbox = [ ] <EOL> self . consumer . delete ( ) <EOL> if not self . _pre_test_email ( ) : <EOL> return <EOL> self . assertEquals ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> expected = "<STR_LIT>" <EOL> self . assertEquals ( mail . outbox [ <NUM_LIT:0> ] . subject , expected ) <EOL> class CustomResponseWithStatusCodeTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_reponse_with_data_and_status_code ( self ) : <EOL> response_data = dict ( complex_response = dict ( something = '<STR_LIT>' , <EOL> something_else = '<STR_LIT>' ) ) <EOL> class MyHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = ( '<STR_LIT:POST>' , ) <EOL> def create ( self , request ) : <EOL> resp = rc . CREATED <EOL> resp . content = response_data <EOL> return resp <EOL> resource = Resource ( MyHandler ) <EOL> request = HttpRequest ( ) <EOL> request . method = '<STR_LIT:POST>' <EOL> response = resource ( request , emitter_format = '<STR_LIT>' ) <EOL> self . assertEquals ( <NUM_LIT> , response . status_code ) <EOL> is_string = ( not response . _base_content_is_iter ) if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:4> ) else response . _is_string <EOL> self . assert_ ( is_string , "<STR_LIT>" ) <EOL> self . assertEquals ( response_data , simplejson . loads ( response . content ) ) <EOL> class ErrorHandlerTest ( TestCase ) : <EOL> def test_customized_error_handler ( self ) : <EOL> """<STR_LIT>""" <EOL> class GoAwayError ( Exception ) : <EOL> def __init__ ( self , name , reason ) : <EOL> self . name = name <EOL> self . reason = reason <EOL> class MyHandler ( BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> allowed_methods = ( '<STR_LIT:GET>' , ) <EOL> def read ( self , request ) : <EOL> raise GoAwayError ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class MyResource ( Resource ) : <EOL> def error_handler ( self , error , request , meth , em_format ) : <EOL> if isinstance ( error , GoAwayError ) : <EOL> response = rc . FORBIDDEN <EOL> response . content = dict ( error = dict ( <EOL> name = error . name , <EOL> message = "<STR_LIT>" , <EOL> reason = error . reason <EOL> ) ) <EOL> return response <EOL> return super ( MyResource , self ) . error_handler ( error , request , meth ) <EOL> resource = MyResource ( MyHandler ) <EOL> request = HttpRequest ( ) <EOL> request . method = '<STR_LIT:GET>' <EOL> response = resource ( request , emitter_format = '<STR_LIT>' ) <EOL> self . assertEquals ( <NUM_LIT> , response . status_code ) <EOL> response_data = simplejson . loads ( response . content ) <EOL> self . assertTrue ( '<STR_LIT:error>' in response_data ) <EOL> self . assertTrue ( '<STR_LIT:name>' in response_data [ '<STR_LIT:error>' ] ) <EOL> self . assertTrue ( '<STR_LIT:message>' in response_data [ '<STR_LIT:error>' ] ) <EOL> self . assertTrue ( '<STR_LIT>' in response_data [ '<STR_LIT:error>' ] ) <EOL> def test_type_error ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyHandler ( BaseHandler ) : <EOL> def read ( self , request ) : <EOL> raise TypeError ( ) <EOL> request = HttpRequest ( ) <EOL> request . method = '<STR_LIT:GET>' <EOL> response = Resource ( MyHandler ) ( request ) <EOL> self . assertTrue ( isinstance ( response , HttpResponse ) , "<STR_LIT>" <EOL> % response ) <EOL> def test_other_error ( self ) : <EOL> """<STR_LIT>""" <EOL> class MyHandler ( BaseHandler ) : <EOL> def read ( self , request ) : <EOL> raise Exception ( ) <EOL> resource = Resource ( MyHandler ) <EOL> resource . display_errors = True <EOL> resource . email_errors = False <EOL> request = HttpRequest ( ) <EOL> request . method = '<STR_LIT:GET>' <EOL> response = resource ( request ) <EOL> self . assertTrue ( isinstance ( response , HttpResponse ) , "<STR_LIT>" <EOL> % response ) </s>
