<s> import json <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . test import Client <EOL> from restlib2 . http import codes <EOL> from avocado . history . models import Revision <EOL> from avocado . models import DataView <EOL> from . base import AuthenticatedBaseTestCase <EOL> class ViewResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_get_all ( self ) : <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertFalse ( json . loads ( response . content ) ) <EOL> self . assertEqual ( <EOL> response [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_get_all_default ( self ) : <EOL> view = DataView ( template = True , default = True , json = [ ] ) <EOL> view . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( len ( json . loads ( response . content ) ) , <NUM_LIT:1> ) <EOL> def test_get ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( view . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( response . content ) <EOL> self . assertLess ( view . accessed , <EOL> DataView . objects . get ( pk = view . pk ) . accessed ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) <EOL> def test_get_session ( self ) : <EOL> view = DataView ( user = self . user , name = '<STR_LIT>' , session = True ) <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 . assertTrue ( response . content ) <EOL> view . session = False <EOL> view . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) <EOL> def test_post ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , <EOL> data = u'<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . created ) <EOL> data = json . loads ( response . content ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( data [ '<STR_LIT:id>' ] ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( response . content ) <EOL> self . assertEqual ( json . loads ( response . content ) [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> response = self . client . post ( '<STR_LIT>' , <EOL> data = u'<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . unprocessable_entity ) <EOL> def test_put ( self ) : <EOL> view = DataView ( user = self . user , name = '<STR_LIT>' ) <EOL> view . save ( ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( view . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( response . content ) <EOL> response = self . client . put ( '<STR_LIT>' . format ( view . pk ) , <EOL> data = u'<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( view . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( response . content ) <EOL> self . assertEqual ( json . loads ( response . content ) [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> response = self . client . put ( '<STR_LIT>' . format ( view . pk ) , <EOL> data = u'<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . unprocessable_entity ) <EOL> def test_delete ( self ) : <EOL> view1 = DataView ( user = self . user , name = '<STR_LIT>' ) <EOL> view1 . save ( ) <EOL> view2 = DataView ( user = self . user , name = '<STR_LIT>' ) <EOL> view2 . save ( ) <EOL> view3 = DataView ( user = self . user , name = '<STR_LIT>' , session = True ) <EOL> view3 . 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:3> ) <EOL> response = self . client . delete ( '<STR_LIT>' . format ( view1 . pk ) ) <EOL> self . assertEqual ( response . status_code , codes . no_content ) <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:2> ) <EOL> response = self . client . delete ( '<STR_LIT>' . format ( view3 . pk ) ) <EOL> self . assertEqual ( response . status_code , codes . bad_request ) <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:2> ) <EOL> class ViewsRevisionsResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_get ( 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> response = self . client . get ( '<STR_LIT>' , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) <EOL> def test_user ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> user2 = User . objects . create_user ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> view2 = DataView ( user = user2 ) <EOL> view2 . save ( ) <EOL> self . assertEqual ( Revision . objects . filter ( <EOL> content_type = ContentType . objects . get_for_model ( DataView ) ) . count ( ) , <EOL> <NUM_LIT:2> ) <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> def test_session ( self ) : <EOL> self . client = Client ( ) <EOL> from django . conf import settings <EOL> from django . utils . importlib import import_module <EOL> engine = import_module ( settings . SESSION_ENGINE ) <EOL> store = engine . SessionStore ( ) <EOL> store . save ( ) <EOL> session_key = store . session_key <EOL> self . client . cookies [ settings . SESSION_COOKIE_NAME ] = session_key <EOL> view = DataView ( session_key = self . client . session . session_key ) <EOL> view . save ( ) <EOL> view2 = DataView ( session_key = '<STR_LIT>' ) <EOL> view2 . save ( ) <EOL> self . assertEqual ( Revision . objects . filter ( <EOL> content_type = ContentType . objects . get_for_model ( DataView ) ) . count ( ) , <EOL> <NUM_LIT:2> ) <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> def test_no_identifier ( self ) : <EOL> view = DataView ( ) <EOL> view . save ( ) <EOL> self . assertEqual ( Revision . objects . filter ( <EOL> content_type = ContentType . objects . get_for_model ( DataView ) , <EOL> user = None , session_key = None ) . count ( ) , <NUM_LIT:1> ) <EOL> self . client . logout ( ) <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_embedded ( self ) : <EOL> view = DataView ( user = self . user , name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <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> no_embed_revision = json . loads ( response . content ) [ <NUM_LIT:0> ] <EOL> self . assertFalse ( '<STR_LIT:object>' in no_embed_revision ) <EOL> response = self . client . get ( '<STR_LIT>' , { '<STR_LIT>' : True } , <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> embed_revision = json . loads ( response . content ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( '<STR_LIT:object>' in embed_revision ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( view . pk ) , <EOL> HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> revision_view = json . loads ( response . content ) <EOL> self . assertEqual ( revision_view , embed_revision [ '<STR_LIT:object>' ] ) <EOL> class ViewRevisionsResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_get ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> view . name = "<STR_LIT>" <EOL> view . save ( ) <EOL> view . description = "<STR_LIT>" <EOL> view . save ( ) <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 . ok ) <EOL> self . assertEqual ( len ( json . loads ( response . content ) ) , <NUM_LIT:3> ) <EOL> class ViewRevisionResourceTestCase ( AuthenticatedBaseTestCase ) : <EOL> def test_get ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> view . name = "<STR_LIT>" <EOL> view . save ( ) <EOL> target_revision_id = Revision . objects . latest ( ) . pk <EOL> view . description = "<STR_LIT>" <EOL> view . save ( ) <EOL> url = '<STR_LIT>' . format ( <EOL> view . id , target_revision_id ) <EOL> response = self . client . get ( url , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . ok ) <EOL> self . assertTrue ( response . content ) <EOL> revision = json . loads ( response . content ) <EOL> self . assertEqual ( revision [ '<STR_LIT>' ] , { <EOL> '<STR_LIT:name>' : { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } ) <EOL> self . assertFalse ( "<STR_LIT:description>" in revision [ '<STR_LIT>' ] ) <EOL> def test_non_existent_object ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> view . name = "<STR_LIT>" <EOL> view . save ( ) <EOL> target_revision_id = Revision . objects . latest ( ) . pk <EOL> url = '<STR_LIT>' . format ( <EOL> <NUM_LIT> , 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> def test_non_existent_revision ( self ) : <EOL> view = DataView ( user = self . user ) <EOL> view . save ( ) <EOL> view . name = "<STR_LIT>" <EOL> view . save ( ) <EOL> url = '<STR_LIT>' . format ( view . id , <NUM_LIT> ) <EOL> response = self . client . get ( url , HTTP_ACCEPT = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , codes . not_found ) </s>
