<s> import tempfile <EOL> from django . conf import settings <EOL> from django . core . files . uploadedfile import InMemoryUploadedFile <EOL> from django . core . urlresolvers import reverse <EOL> from django . forms . widgets import HiddenInput <EOL> from django import http <EOL> from django . test . utils import override_settings <EOL> from mox3 . mox import IsA <EOL> from horizon import tables as horizon_tables <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . test import helpers as test <EOL> from openstack_dashboard . dashboards . project . images . images import forms <EOL> from openstack_dashboard . dashboards . project . images . images import tables <EOL> IMAGES_INDEX_URL = reverse ( '<STR_LIT>' ) <EOL> class CreateImageFormTests ( test . TestCase ) : <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_no_location_or_file ( self ) : <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( { } ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( { } ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> self . mox . ReplayAll ( ) <EOL> post = { <EOL> '<STR_LIT:name>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT:file>' , <EOL> '<STR_LIT:description>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1> } <EOL> files = { } <EOL> form = forms . CreateImageForm ( post , files ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> @ override_settings ( HORIZON_IMAGES_ALLOW_UPLOAD = False ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_upload_disabled ( self ) : <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( { } ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( { } ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> self . mox . ReplayAll ( ) <EOL> form = forms . CreateImageForm ( { } ) <EOL> self . assertEqual ( <EOL> isinstance ( form . fields [ '<STR_LIT>' ] . widget , HiddenInput ) , True ) <EOL> source_type_dict = dict ( form . fields [ '<STR_LIT>' ] . choices ) <EOL> self . assertNotIn ( '<STR_LIT:file>' , source_type_dict ) <EOL> def test_create_image_metadata_docker ( self ) : <EOL> form_data = { <EOL> '<STR_LIT:name>' : u'<STR_LIT>' , <EOL> '<STR_LIT:description>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT:url>' , <EOL> '<STR_LIT>' : u'<STR_LIT:/>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False <EOL> } <EOL> meta = forms . create_image_metadata ( form_data ) <EOL> self . assertEqual ( meta [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( meta [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , meta ) <EOL> self . assertNotIn ( '<STR_LIT:description>' , meta ) <EOL> self . assertNotIn ( '<STR_LIT>' , meta ) <EOL> self . assertEqual ( meta [ '<STR_LIT>' ] [ '<STR_LIT:description>' ] , <EOL> form_data [ '<STR_LIT:description>' ] ) <EOL> self . assertEqual ( meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> form_data [ '<STR_LIT>' ] ) <EOL> class UpdateImageFormTests ( test . TestCase ) : <EOL> def test_is_format_field_editable ( self ) : <EOL> form = forms . UpdateImageForm ( { } ) <EOL> disk_format = form . fields [ '<STR_LIT>' ] <EOL> self . assertFalse ( disk_format . widget . attrs . get ( '<STR_LIT>' , False ) ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_update ( self ) : <EOL> image = self . images . first ( ) <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( image ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) <EOL> res = self . client . get ( url ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertEqual ( res . context [ '<STR_LIT:image>' ] . disk_format , <EOL> image . disk_format ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , '<STR_LIT>' ) } ) <EOL> def test_image_update_post ( self ) : <EOL> image = self . images . first ( ) <EOL> data = { <EOL> '<STR_LIT:name>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : str ( image . id ) , <EOL> '<STR_LIT:description>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT:url>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( image ) <EOL> api . glance . image_update ( IsA ( http . HttpRequest ) , <EOL> image . id , <EOL> is_public = data [ '<STR_LIT>' ] , <EOL> protected = data [ '<STR_LIT>' ] , <EOL> disk_format = data [ '<STR_LIT>' ] , <EOL> container_format = "<STR_LIT>" , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> min_ram = data [ '<STR_LIT>' ] , <EOL> min_disk = data [ '<STR_LIT>' ] , <EOL> properties = { '<STR_LIT:description>' : data [ '<STR_LIT:description>' ] , <EOL> '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] } , <EOL> purge_props = False ) . AndReturn ( image ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) <EOL> res = self . client . post ( url , data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> class ImageViewTests ( test . TestCase ) : <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_create_get ( self ) : <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( http . HttpRequest ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( http . HttpRequest ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> res = self . client . get ( url ) <EOL> self . assertTemplateUsed ( res , <EOL> '<STR_LIT>' ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_create_post_copy_from ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:url>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' , <EOL> '<STR_LIT>' : True } <EOL> api_data = { '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> self . _test_image_create ( data , api_data ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_create_post_location ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:url>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' , <EOL> '<STR_LIT>' : False } <EOL> api_data = { '<STR_LIT:location>' : data [ '<STR_LIT>' ] } <EOL> self . _test_image_create ( data , api_data ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_create_post_upload ( self ) : <EOL> temp_file = tempfile . NamedTemporaryFile ( ) <EOL> temp_file . write ( b'<STR_LIT>' ) <EOL> temp_file . flush ( ) <EOL> temp_file . seek ( <NUM_LIT:0> ) <EOL> data = { '<STR_LIT>' : u'<STR_LIT:file>' , <EOL> '<STR_LIT>' : temp_file } <EOL> api_data = { '<STR_LIT:data>' : IsA ( InMemoryUploadedFile ) } <EOL> self . _test_image_create ( data , api_data ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_create_post_with_kernel_ramdisk ( self ) : <EOL> temp_file = tempfile . NamedTemporaryFile ( ) <EOL> temp_file . write ( b'<STR_LIT>' ) <EOL> temp_file . flush ( ) <EOL> temp_file . seek ( <NUM_LIT:0> ) <EOL> data = { <EOL> '<STR_LIT>' : u'<STR_LIT:file>' , <EOL> '<STR_LIT>' : temp_file , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> api_data = { '<STR_LIT:data>' : IsA ( InMemoryUploadedFile ) } <EOL> self . _test_image_create ( data , api_data ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def _test_image_create ( self , extra_form_data , extra_api_data ) : <EOL> data = { <EOL> '<STR_LIT:name>' : u'<STR_LIT>' , <EOL> '<STR_LIT:description>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:15> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> data . update ( extra_form_data ) <EOL> api_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:description>' : data [ '<STR_LIT:description>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } , <EOL> '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] } <EOL> api_data . update ( extra_api_data ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( http . HttpRequest ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> api . glance . image_list_detailed ( <EOL> IsA ( http . HttpRequest ) , filters = filters ) . AndReturn ( <EOL> [ self . images . list ( ) , False , False ] ) <EOL> api . glance . image_create ( <EOL> IsA ( http . HttpRequest ) , <EOL> ** api_data ) . AndReturn ( self . images . first ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> res = self . client . post ( url , data ) <EOL> self . assertNoFormErrors ( res ) <EOL> self . assertEqual ( res . status_code , <NUM_LIT> ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_detail_get ( self ) : <EOL> image = self . images . first ( ) <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( self . images . first ( ) ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) ) <EOL> self . assertTemplateUsed ( res , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( res . context [ '<STR_LIT:image>' ] . name , image . name ) <EOL> self . assertEqual ( res . context [ '<STR_LIT:image>' ] . protected , image . protected ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_detail_custom_props_get ( self ) : <EOL> image = self . images . list ( ) [ <NUM_LIT:8> ] <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( image ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) ) <EOL> image_props = res . context [ '<STR_LIT>' ] <EOL> image_keys = [ prop [ <NUM_LIT:0> ] for prop in image_props ] <EOL> self . assertNotIn ( ( '<STR_LIT:description>' ) , image_keys ) <EOL> self . assertEqual ( image_props [ <NUM_LIT:0> ] , ( '<STR_LIT:bar>' , '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( image_props [ <NUM_LIT:1> ] , ( '<STR_LIT:foo>' , '<STR_LIT:foo>' , '<STR_LIT>' ) ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> self . assertContains ( res , '<STR_LIT>' ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_protected_image_detail_get ( self ) : <EOL> image = self . images . list ( ) [ <NUM_LIT:2> ] <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( image ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( <EOL> reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) ) <EOL> self . assertTemplateUsed ( res , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( res . context [ '<STR_LIT:image>' ] . protected , image . protected ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_detail_get_with_exception ( self ) : <EOL> image = self . images . first ( ) <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndRaise ( self . exceptions . glance ) <EOL> self . mox . ReplayAll ( ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) <EOL> res = self . client . get ( url ) <EOL> self . assertRedirectsNoFollow ( res , IMAGES_INDEX_URL ) <EOL> @ test . create_stubs ( { api . glance : ( '<STR_LIT>' , ) } ) <EOL> def test_image_update_get ( self ) : <EOL> image = self . images . first ( ) <EOL> image . disk_format = "<STR_LIT>" <EOL> image . is_public = True <EOL> api . glance . image_get ( IsA ( http . HttpRequest ) , str ( image . id ) ) . AndReturn ( image ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . client . get ( <EOL> reverse ( '<STR_LIT>' , <EOL> args = [ image . id ] ) ) <EOL> self . assertTemplateUsed ( res , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( res . context [ '<STR_LIT:image>' ] . name , image . name ) <EOL> self . assertContains ( res , "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> html = True , <EOL> msg_prefix = "<STR_LIT>" ) <EOL> class OwnerFilterTests ( test . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( OwnerFilterTests , self ) . setUp ( ) <EOL> self . table = self . mox . CreateMock ( horizon_tables . DataTable ) <EOL> self . table . request = self . request <EOL> @ override_settings ( IMAGES_LIST_FILTER_TENANTS = [ { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] ) <EOL> def test_filter ( self ) : <EOL> self . mox . ReplayAll ( ) <EOL> all_images = self . images . list ( ) <EOL> table = self . table <EOL> self . filter_tenants = settings . IMAGES_LIST_FILTER_TENANTS <EOL> filter_ = tables . OwnerFilter ( ) <EOL> images = filter_ . filter ( table , all_images , '<STR_LIT>' ) <EOL> self . assertEqual ( images , self . _expected ( '<STR_LIT>' ) ) <EOL> images = filter_ . filter ( table , all_images , '<STR_LIT>' ) <EOL> self . assertEqual ( images , self . _expected ( '<STR_LIT>' ) ) <EOL> images = filter_ . filter ( table , all_images , '<STR_LIT>' ) <EOL> self . assertEqual ( images , self . _expected ( '<STR_LIT>' ) ) <EOL> images = filter_ . filter ( table , all_images , '<STR_LIT>' ) <EOL> self . assertEqual ( images , self . _expected ( '<STR_LIT>' ) ) <EOL> def _expected ( self , filter_string ) : <EOL> my_tenant_id = self . request . user . tenant_id <EOL> images = self . images . list ( ) <EOL> special = map ( lambda t : t [ '<STR_LIT>' ] , self . filter_tenants ) <EOL> if filter_string == '<STR_LIT>' : <EOL> return [ im for im in images if im . is_public ] <EOL> if filter_string == '<STR_LIT>' : <EOL> return [ im for im in images <EOL> if ( not im . is_public and <EOL> im . owner != my_tenant_id and <EOL> im . owner not in special ) ] <EOL> if filter_string == '<STR_LIT>' : <EOL> filter_string = my_tenant_id <EOL> return [ im for im in images if im . owner == filter_string ] </s>
