<s> from __future__ import unicode_literals <EOL> from rest_framework import status <EOL> from rest_framework import test <EOL> from nodeconductor . core . tests import helpers <EOL> from nodeconductor . openstack import models <EOL> from nodeconductor . openstack . tests import factories <EOL> from nodeconductor . structure import models as structure_models <EOL> from nodeconductor . structure . tests import factories as structure_factories <EOL> class BackupUsageTest ( test . APITransactionTestCase ) : <EOL> def setUp ( self ) : <EOL> self . user = structure_factories . UserFactory . create ( is_staff = True , is_superuser = True ) <EOL> self . client . force_authenticate ( user = self . user ) <EOL> def test_backup_manually_create ( self ) : <EOL> backupable = factories . InstanceFactory ( state = models . Instance . States . OFFLINE ) <EOL> backup_data = { <EOL> '<STR_LIT>' : factories . InstanceFactory . get_url ( backupable ) , <EOL> } <EOL> url = factories . BackupFactory . get_list_url ( ) <EOL> response = self . client . post ( url , data = backup_data ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> models . Backup . objects . get ( instance_id = backupable . id ) <EOL> backup_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> response = self . client . post ( url , data = backup_data ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertIn ( '<STR_LIT>' , response . content ) <EOL> def test_user_cannot_backup_unstable_instance ( self ) : <EOL> backupable = factories . InstanceFactory ( state = models . Instance . States . RESIZING ) <EOL> backup_data = { <EOL> '<STR_LIT>' : factories . InstanceFactory . get_url ( backupable ) , <EOL> } <EOL> url = factories . BackupFactory . get_list_url ( ) <EOL> response = self . client . post ( url , data = backup_data ) <EOL> self . assertEqual ( response . status_code , status . HTTP_409_CONFLICT ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_backup_restore ( self ) : <EOL> backup = factories . BackupFactory ( ) <EOL> url = factories . BackupFactory . get_url ( backup , action = '<STR_LIT>' ) <EOL> settings = backup . instance . service_project_link . service . settings <EOL> user_input = { <EOL> '<STR_LIT>' : factories . FlavorFactory . get_url ( factories . FlavorFactory ( settings = settings ) ) , <EOL> } <EOL> response = self . client . post ( url , data = user_input ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK , response . data ) <EOL> def test_backup_delete ( self ) : <EOL> backup = factories . BackupFactory ( ) <EOL> url = factories . BackupFactory . get_url ( backup , action = '<STR_LIT>' ) <EOL> response = self . client . post ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> class BackupListPermissionsTest ( helpers . ListPermissionsTest ) : <EOL> def get_url ( self ) : <EOL> return factories . BackupFactory . get_list_url ( ) <EOL> def get_users_and_expected_results ( self ) : <EOL> models . Backup . objects . all ( ) . delete ( ) <EOL> instance = factories . InstanceFactory ( ) <EOL> backup1 = factories . BackupFactory ( instance = instance ) <EOL> backup2 = factories . BackupFactory ( instance = instance ) <EOL> factories . BackupFactory ( instance = instance , state = models . Backup . States . DELETED ) <EOL> user_with_view_permission = structure_factories . UserFactory . create ( is_staff = True , is_superuser = True ) <EOL> user_without_view_permission = structure_factories . UserFactory . create ( ) <EOL> return [ <EOL> { <EOL> '<STR_LIT:user>' : user_with_view_permission , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:url>' : factories . BackupFactory . get_url ( backup1 ) } , <EOL> { '<STR_LIT:url>' : factories . BackupFactory . get_url ( backup2 ) } <EOL> ] <EOL> } , <EOL> { <EOL> '<STR_LIT:user>' : user_without_view_permission , <EOL> '<STR_LIT>' : [ ] <EOL> } , <EOL> ] <EOL> class BackupPermissionsTest ( helpers . PermissionsTest ) : <EOL> def setUp ( self ) : <EOL> super ( BackupPermissionsTest , self ) . setUp ( ) <EOL> self . customer = structure_factories . CustomerFactory ( ) <EOL> self . project = structure_factories . ProjectFactory ( customer = self . customer ) <EOL> self . project_group = structure_factories . ProjectGroupFactory ( customer = self . customer ) <EOL> self . project_group . projects . add ( self . project ) <EOL> self . service = factories . OpenStackServiceFactory ( customer = self . customer ) <EOL> self . spl = factories . OpenStackServiceProjectLinkFactory ( service = self . service , project = self . project ) <EOL> self . instance = factories . InstanceFactory ( service_project_link = self . spl ) <EOL> self . backup = factories . BackupFactory ( instance = self . instance ) <EOL> self . staff = structure_factories . UserFactory ( username = '<STR_LIT>' , is_staff = True ) <EOL> self . regular_user = structure_factories . UserFactory ( username = '<STR_LIT>' ) <EOL> self . project_admin = structure_factories . UserFactory ( username = '<STR_LIT>' ) <EOL> self . project . add_user ( self . project_admin , structure_models . ProjectRole . ADMINISTRATOR ) <EOL> self . customer_owner = structure_factories . UserFactory ( username = '<STR_LIT>' ) <EOL> self . customer . add_user ( self . customer_owner , structure_models . CustomerRole . OWNER ) <EOL> self . project_group_manager = structure_factories . UserFactory ( username = '<STR_LIT>' ) <EOL> self . project_group . add_user ( self . project_group_manager , structure_models . ProjectGroupRole . MANAGER ) <EOL> def get_users_with_permission ( self , url , method ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return [ self . staff , self . project_admin , self . project_group_manager ] <EOL> else : <EOL> return [ self . staff , self . project_admin , self . customer_owner ] <EOL> def get_users_without_permissions ( self , url , method ) : <EOL> if method == '<STR_LIT:GET>' : <EOL> return [ self . regular_user ] <EOL> else : <EOL> return [ self . project_group_manager ] <EOL> def get_urls_configs ( self ) : <EOL> yield { '<STR_LIT:url>' : factories . BackupFactory . get_url ( self . backup ) , '<STR_LIT>' : '<STR_LIT:GET>' } <EOL> yield { '<STR_LIT:url>' : factories . BackupFactory . get_url ( self . backup , action = '<STR_LIT>' ) , '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT:data>' : { <EOL> '<STR_LIT>' : factories . FlavorFactory . get_url ( factories . FlavorFactory ( settings = self . service . settings ) ) , <EOL> '<STR_LIT:image>' : factories . ImageFactory . get_url ( factories . ImageFactory ( settings = self . service . settings ) ) , <EOL> } } <EOL> yield { '<STR_LIT:url>' : factories . BackupFactory . get_list_url ( ) , '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : factories . InstanceFactory . get_url ( self . instance ) } } <EOL> yield { '<STR_LIT:url>' : factories . BackupFactory . get_url ( self . backup , action = '<STR_LIT>' ) , '<STR_LIT>' : '<STR_LIT:POST>' } <EOL> class BackupSourceFilterTest ( test . APITransactionTestCase ) : <EOL> def test_filter_backup_by_scope ( self ) : <EOL> user = structure_factories . UserFactory . create ( is_staff = True ) <EOL> instance1 = factories . InstanceFactory ( ) <EOL> factories . BackupFactory ( instance = instance1 ) <EOL> factories . BackupFactory ( instance = instance1 ) <EOL> instance2 = factories . InstanceFactory ( ) <EOL> factories . BackupFactory ( instance = instance2 ) <EOL> self . client . force_authenticate ( user = user ) <EOL> response = self . client . get ( factories . BackupFactory . get_list_url ( ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( response . data ) ) <EOL> response = self . client . get ( factories . BackupFactory . get_list_url ( ) , data = { <EOL> '<STR_LIT>' : instance1 . uuid . hex } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( response . data ) ) <EOL> self . assertEqual ( factories . InstanceFactory . get_url ( instance1 ) , response . data [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) </s>
