<s> import json <EOL> from django . contrib . auth . models import User <EOL> from django . test import Client <EOL> from django . test . utils import override_settings <EOL> from testcases import TestCaseWithFixture <EOL> class ViewsTestCase ( TestCaseWithFixture ) : <EOL> def test_gets ( self ) : <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:6> ) <EOL> self . assertEqual ( deserialized [ '<STR_LIT>' ] , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( deserialized [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( deserialized [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( [ obj [ '<STR_LIT:title>' ] for obj in deserialized [ '<STR_LIT>' ] ] , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:9> ) <EOL> self . assertEqual ( deserialized [ '<STR_LIT:title>' ] , u'<STR_LIT>' ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( deserialized [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( [ obj [ '<STR_LIT:title>' ] for obj in deserialized [ '<STR_LIT>' ] ] , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> def test_get_test_client_error ( self ) : <EOL> self . assertRaises ( Exception , self . client . get , '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_posts ( self ) : <EOL> post_data = b'<STR_LIT>' <EOL> resp = self . client . post ( '<STR_LIT>' , data = post_data , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( resp [ '<STR_LIT:location>' ] . endswith ( '<STR_LIT>' ) ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> obj = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( obj [ '<STR_LIT:content>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( obj [ '<STR_LIT>' ] , True ) <EOL> self . assertEqual ( obj [ '<STR_LIT:user>' ] , '<STR_LIT>' ) <EOL> def test_puts ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> resp = self . client . put ( '<STR_LIT>' , data = post_data , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> obj = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( obj [ '<STR_LIT:content>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( obj [ '<STR_LIT>' ] , True ) <EOL> self . assertEqual ( obj [ '<STR_LIT:user>' ] , '<STR_LIT>' ) <EOL> def test_api_field_error ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> resp = self . client . post ( '<STR_LIT>' , data = post_data , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> { <EOL> "<STR_LIT:error>" : "<STR_LIT>" <EOL> } <EOL> ) <EOL> def test_invalid_json_error ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> resp = self . client . post ( '<STR_LIT>' , data = post_data , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> { <EOL> "<STR_LIT:error>" : "<STR_LIT>" <EOL> } <EOL> ) <EOL> def test_options ( self ) : <EOL> resp = self . client . options ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> allows = '<STR_LIT>' <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , allows ) <EOL> self . assertEqual ( resp . content . decode ( '<STR_LIT:utf-8>' ) , allows ) <EOL> resp = self . client . options ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> allows = '<STR_LIT>' <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , allows ) <EOL> self . assertEqual ( resp . content . decode ( '<STR_LIT:utf-8>' ) , allows ) <EOL> resp = self . client . options ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> allows = '<STR_LIT:GET>' <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , allows ) <EOL> self . assertEqual ( resp . content . decode ( '<STR_LIT:utf-8>' ) , allows ) <EOL> resp = self . client . options ( '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> allows = '<STR_LIT:GET>' <EOL> self . assertEqual ( resp [ '<STR_LIT>' ] , allows ) <EOL> self . assertEqual ( resp . content . decode ( '<STR_LIT:utf-8>' ) , allows ) <EOL> def test_slugbased ( self ) : <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( deserialized [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( deserialized [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( [ obj [ '<STR_LIT:title>' ] for obj in deserialized [ '<STR_LIT>' ] ] , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( deserialized [ '<STR_LIT:title>' ] , u'<STR_LIT>' ) <EOL> resp = self . client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( deserialized [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( [ obj [ '<STR_LIT:title>' ] for obj in deserialized [ '<STR_LIT>' ] ] , [ u'<STR_LIT>' , u'<STR_LIT>' ] ) <EOL> def test_session_auth ( self ) : <EOL> csrf_client = Client ( enforce_csrf_checks = True ) <EOL> User . objects . create_superuser ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> resp = csrf_client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( csrf_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) ) <EOL> csrf_client . cookies [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> resp = csrf_client . get ( '<STR_LIT>' , data = { '<STR_LIT>' : '<STR_LIT>' } , HTTP_X_CSRFTOKEN = '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , <NUM_LIT:200> ) <EOL> deserialized = json . loads ( resp . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEqual ( len ( deserialized ) , <NUM_LIT:2> ) <EOL> @ override_settings ( DEBUG = True ) <EOL> class MoreViewsTestCase ( TestCaseWithFixture ) : <EOL> def test_get_apis_json ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> data = response . content . decode ( '<STR_LIT:utf-8>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( data , '<STR_LIT>' ) <EOL> def test_get_apis_invalid_accept ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> , "<STR_LIT>" ) <EOL> def test_get_resource_invalid_accept ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> , "<STR_LIT>" ) <EOL> def test_get_apis_xml ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT>' ) <EOL> data = response . content . decode ( '<STR_LIT:utf-8>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( data , '<STR_LIT>' ) <EOL> def test_get_list ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . content . decode ( '<STR_LIT:utf-8>' ) , '<STR_LIT>' ) <EOL> def test_post_object ( self ) : <EOL> post_data = '<STR_LIT>' <EOL> response = self . client . post ( '<STR_LIT>' , data = post_data , HTTP_ACCEPT = '<STR_LIT:application/json>' , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> location = response [ '<STR_LIT>' ] <EOL> self . assertTrue ( location . endswith ( '<STR_LIT>' ) ) <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> data = response . content . decode ( '<STR_LIT:utf-8>' ) <EOL> obj = json . loads ( data ) <EOL> self . assertEqual ( obj [ '<STR_LIT:content>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( obj [ '<STR_LIT>' ] , True ) <EOL> self . assertEqual ( obj [ '<STR_LIT:user>' ] , '<STR_LIT>' ) <EOL> def test_vary_accept ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> vary = response [ '<STR_LIT>' ] <EOL> vary_types = [ x . strip ( ) . lower ( ) for x in vary . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] <EOL> self . assertIn ( "<STR_LIT>" , vary_types ) <EOL> def test_cache_control ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> cache_control = set ( [ x . strip ( ) . lower ( ) for x in response [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] ) <EOL> self . assertEqual ( cache_control , set ( [ "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_cache_disabled_control ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> cache_control = set ( [ x . strip ( ) . lower ( ) for x in response [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] ) <EOL> self . assertEqual ( cache_control , set ( [ "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_public_cache_control ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> cache_control = set ( [ x . strip ( ) . lower ( ) for x in response [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] ) <EOL> self . assertEqual ( cache_control , set ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_private_cache_control ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> cache_control = set ( [ x . strip ( ) . lower ( ) for x in response [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) if x . strip ( ) ] ) <EOL> self . assertEqual ( cache_control , set ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> self . assertTrue ( '<STR_LIT>' in response . content . decode ( '<STR_LIT:utf-8>' ) ) </s>
