<s> """<STR_LIT>""" <EOL> from django . contrib . auth . models import Permission <EOL> from django . core . files import File <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from model_mommy import mommy <EOL> from open_connect . media . tests import get_in_memory_image_file <EOL> from open_connect . connect_core . utils . basetests import ConnectTestMixin <EOL> from open_connect . resources . models import generate_random_string , Resource <EOL> class TestGenerateRandomString ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_random_string_no_args ( self ) : <EOL> """<STR_LIT>""" <EOL> result = generate_random_string ( ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:10> ) <EOL> def test_random_string_with_length ( self ) : <EOL> """<STR_LIT>""" <EOL> result = generate_random_string ( length = <NUM_LIT:3> ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:3> ) <EOL> class TestResource ( ConnectTestMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_unicode ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = mommy . make ( '<STR_LIT>' ) <EOL> self . assertEqual ( unicode ( resource ) , resource . name ) <EOL> def test_get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = mommy . make ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> resource . get_absolute_url ( ) , <EOL> reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : resource . slug } ) <EOL> ) <EOL> def test_save_new_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> with File ( get_in_memory_image_file ( ) ) as tempfile : <EOL> user = self . create_user ( ) <EOL> resource = Resource ( <EOL> name = '<STR_LIT>' , <EOL> attachment = tempfile , <EOL> created_by = user <EOL> ) <EOL> resource . save ( ) <EOL> self . assertEqual ( resource . slug , '<STR_LIT>' ) <EOL> def test_save_existing_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = mommy . make ( '<STR_LIT>' ) <EOL> old_slug = resource . slug <EOL> resource . name = '<STR_LIT>' <EOL> resource . save ( ) <EOL> resource = Resource . objects . get ( pk = resource . pk ) <EOL> self . assertEqual ( resource . name , '<STR_LIT>' ) <EOL> self . assertEqual ( resource . slug , old_slug ) <EOL> def test_save_resource_with_same_name_as_another_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> with File ( get_in_memory_image_file ( ) ) as tempfile : <EOL> resource = Resource ( <EOL> name = '<STR_LIT>' , <EOL> attachment = tempfile , <EOL> created_by = user <EOL> ) <EOL> resource . save ( ) <EOL> with File ( get_in_memory_image_file ( ) ) as tempfile : <EOL> new_resource = Resource ( <EOL> name = '<STR_LIT>' , <EOL> attachment = tempfile , <EOL> created_by = user <EOL> ) <EOL> new_resource . save ( ) <EOL> self . assertTrue ( new_resource . slug . startswith ( '<STR_LIT>' ) ) <EOL> self . assertNotEqual ( new_resource . slug , resource . slug ) <EOL> def test_delete ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> with File ( get_in_memory_image_file ( ) ) as tempfile : <EOL> resource = Resource ( <EOL> name = '<STR_LIT>' , <EOL> attachment = tempfile , <EOL> created_by = user <EOL> ) <EOL> resource . save ( ) <EOL> self . assertEqual ( resource . status , '<STR_LIT>' ) <EOL> resource . delete ( ) <EOL> self . assertEqual ( resource . status , '<STR_LIT>' ) <EOL> def test_user_has_access_user_in_group ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' , private = True ) <EOL> resource = mommy . make ( Resource , groups = [ group ] ) <EOL> user = self . create_user ( ) <EOL> user . add_to_group ( group . pk ) <EOL> self . assertTrue ( resource . user_can_download ( user . pk ) ) <EOL> def test_user_has_access_group_is_public ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' , private = False ) <EOL> resource = mommy . make ( Resource , groups = [ group ] ) <EOL> user = self . create_user ( ) <EOL> self . assertTrue ( resource . user_can_download ( user . pk ) ) <EOL> def test_user_has_access_user_not_in_group_and_no_public_groups ( self ) : <EOL> """<STR_LIT>""" <EOL> group = mommy . make ( '<STR_LIT>' , private = True ) <EOL> resource = mommy . make ( Resource , groups = [ group ] ) <EOL> user = self . create_user ( ) <EOL> self . assertFalse ( resource . user_can_download ( user . pk ) ) <EOL> def test_user_can_edit_created_by_user ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> resource = mommy . make ( Resource , created_by = user ) <EOL> self . assertTrue ( resource . user_can_edit ( user . pk ) ) <EOL> def test_user_can_edit_user_has_anywhere_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> permission = Permission . objects . get_by_natural_key ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> user . user_permissions . add ( permission ) <EOL> resource = mommy . make ( Resource ) <EOL> self . assertTrue ( resource . user_can_edit ( user . pk ) ) <EOL> def test_user_can_edit_user_does_not_have_permission ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> resource = mommy . make ( Resource ) <EOL> self . assertFalse ( resource . user_can_edit ( user . pk ) ) <EOL> def test_user_can_delete_created_by_user ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> resource = mommy . make ( Resource , created_by = user ) <EOL> self . assertTrue ( resource . user_can_delete ( user . pk ) ) <EOL> def test_user_can_delete_user_has_anywhere_perm ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> permission = Permission . objects . get_by_natural_key ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> user . user_permissions . add ( permission ) <EOL> resource = mommy . make ( Resource ) <EOL> self . assertTrue ( resource . user_can_delete ( user . pk ) ) <EOL> def test_user_can_delete_user_does_not_have_permission ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . create_user ( ) <EOL> resource = mommy . make ( Resource ) <EOL> self . assertFalse ( resource . user_can_delete ( user . pk ) ) <EOL> def test_file_type ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = mommy . make ( Resource , content_type = '<STR_LIT>' ) <EOL> self . assertEqual ( resource . file_type , '<STR_LIT:image>' ) <EOL> def test_file_type_unrecognized ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = mommy . make ( Resource , content_type = '<STR_LIT>' ) <EOL> self . assertIsNone ( resource . file_type ) </s>
