<s> import json <EOL> import time <EOL> from django . contrib . auth . models import User <EOL> from django . core import management <EOL> from django . test import TestCase , TransactionTestCase <EOL> from django . test . utils import override_settings <EOL> from restlib2 . http import codes <EOL> from avocado . history . models import Revision <EOL> from avocado . models import DataField , DataView , DataContext <EOL> from serrano . models import ApiToken <EOL> from serrano . resources import API_VERSION <EOL> class BaseTestCase ( TestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> management . call_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , quiet = True , <EOL> publish = False , concepts = False ) <EOL> DataField . objects . filter ( <EOL> model_name__in = [ '<STR_LIT>' , '<STR_LIT:title>' ] ) . update ( published = True ) <EOL> self . user = User . objects . create_user ( username = '<STR_LIT:root>' , <EOL> password = '<STR_LIT:password>' ) <EOL> self . user . is_superuser = True <EOL> self . user . save ( ) <EOL> class TransactionBaseTestCase ( TransactionTestCase ) : <EOL> fixtures = [ '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> management . call_command ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , quiet = True , <EOL> publish = False , concepts = False ) <EOL> DataField . objects . filter ( <EOL> model_name__in = [ '<STR_LIT>' , '<STR_LIT:title>' ] ) . update ( published = True ) <EOL> self . user = User . objects . create_user ( username = '<STR_LIT:root>' , <EOL> password = '<STR_LIT:password>' ) <EOL> self . user . is_superuser = True <EOL> self . user . save ( ) <EOL> class AuthenticatedBaseTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( AuthenticatedBaseTestCase , self ) . setUp ( ) <EOL> self . user = User . objects . create_user ( username = '<STR_LIT:test>' , password = '<STR_LIT:test>' ) <EOL> self . client . login ( username = '<STR_LIT:test>' , password = '<STR_LIT:test>' ) <EOL> class RootResourceTestCase ( TestCase ) : <EOL> def test_get ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertEqual ( response [ '<STR_LIT:Content-Type>' ] , '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> self . assertEqual ( json . loads ( response . content ) , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : API_VERSION , <EOL> } ) <EOL> @ override_settings ( SERRANO_AUTH_REQUIRED = True ) <EOL> def test_post ( self ) : <EOL> User . objects . create_user ( username = '<STR_LIT:root>' , password = '<STR_LIT:password>' ) <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . unauthorized ) <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> json . dumps ( { '<STR_LIT:username>' : '<STR_LIT:root>' , '<STR_LIT:password>' : '<STR_LIT:password>' } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( '<STR_LIT>' in json . loads ( response . content ) ) <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> json . dumps ( { '<STR_LIT:username>' : '<STR_LIT:root>' , '<STR_LIT:password>' : '<STR_LIT>' } ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . unauthorized ) <EOL> @ override_settings ( SERRANO_AUTH_REQUIRED = True ) <EOL> def test_api_token_access ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . unauthorized ) <EOL> user = User . objects . create_user ( username = '<STR_LIT:root>' , password = '<STR_LIT:password>' ) <EOL> api_token = ApiToken . objects . create ( user = user ) <EOL> self . assertTrue ( api_token . token ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> data = { '<STR_LIT>' : api_token . token } , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> @ override_settings ( SERRANO_RATE_LIMIT_COUNT = None ) <EOL> class ThrottledResourceTestCase ( BaseTestCase ) : <EOL> def test_too_many_auth_requests ( self ) : <EOL> f = DataField . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . client . login ( username = '<STR_LIT:root>' , password = '<STR_LIT:password>' ) <EOL> time . sleep ( <NUM_LIT:7> ) <EOL> for _ in range ( <NUM_LIT:20> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> for _ in range ( <NUM_LIT:20> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . too_many_requests ) <EOL> time . sleep ( <NUM_LIT:6> ) <EOL> for _ in range ( <NUM_LIT:5> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> def test_too_many_requests ( self ) : <EOL> f = DataField . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . user = None <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . too_many_requests ) <EOL> time . sleep ( <NUM_LIT:4> ) <EOL> for _ in range ( <NUM_LIT:5> ) : <EOL> response = self . client . get ( '<STR_LIT>' . format ( f . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> class RevisionResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_no_object_model ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> self . assertTrue ( Revision . objects . filter ( user = self . user ) . exists ( ) ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertEqual ( len ( json . loads ( response . content ) ) , <NUM_LIT:0> ) <EOL> def test_custom_template ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertEqual ( len ( json . loads ( response . content ) ) , <NUM_LIT:1> ) <EOL> revision = json . loads ( response . content ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( revision [ '<STR_LIT>' ] , view . pk ) <EOL> self . assertTrue ( response [ '<STR_LIT>' ] ) <EOL> self . assertFalse ( '<STR_LIT>' in revision ) <EOL> class ObjectRevisionResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_bad_urls ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> target_revision_id = Revision . objects . all ( ) . count ( ) <EOL> url = '<STR_LIT>' . format ( target_revision_id ) <EOL> response = self . client . get ( url , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) <EOL> url = '<STR_LIT>' . format ( view . id ) <EOL> response = self . client . get ( url , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) <EOL> class PingResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> @ override_settings ( SERRANO_AUTH_REQUIRED = True ) <EOL> def test ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( json . loads ( response . content ) [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . client . logout ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' , <EOL> HTTP_REFERER = '<STR_LIT>' ) <EOL> data = json . loads ( response . content ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( data [ '<STR_LIT:location>' ] , <EOL> '<STR_LIT>' ) <EOL> @ override_settings ( SERRANO_AUTH_REQUIRED = True , LOGIN_REDIRECT_URL = '<STR_LIT:/>' ) <EOL> def test_nonsafe_referer ( self ) : <EOL> self . client . logout ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' , <EOL> HTTP_REFERER = '<STR_LIT>' ) <EOL> data = json . loads ( response . content ) <EOL> self . assertEqual ( data [ '<STR_LIT:status>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( data [ '<STR_LIT:location>' ] , <EOL> '<STR_LIT>' ) <EOL> class MultipleObjectsTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_multiple_contexts ( self ) : <EOL> cxt1 = DataContext ( session = True , user = self . user ) <EOL> cxt1 . save ( ) <EOL> cxt2 = DataContext ( user = self . user , session = True ) <EOL> cxt2 . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> def test_multiple_views ( self ) : <EOL> dv1 = DataView ( session = True , user = self . user ) <EOL> dv1 . save ( ) <EOL> dv2 = DataView ( session = True , user = self . user ) <EOL> dv2 . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertTrue ( response . content ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> def test_multiple_context_and_view ( self ) : <EOL> cxt3 = DataContext ( session = True , user = self . user ) <EOL> cxt3 . save ( ) <EOL> cxt4 = DataContext ( user = self . user , session = True ) <EOL> cxt4 . save ( ) <EOL> dv3 = DataView ( session = True , user = self . user ) <EOL> dv3 . save ( ) <EOL> dv4 = DataView ( session = True , user = self . user ) <EOL> dv4 . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertTrue ( response . content ) <EOL> self . assertEqual ( response . status_code , codes . ok ) </s>
