<s> import factory <EOL> from mock import patch , call <EOL> from rest_framework import test <EOL> from nodeconductor . structure import SupportedServices <EOL> from nodeconductor . structure . tests import factories as structure_factories <EOL> from nodeconductor . structure . models import CustomerRole , ProjectRole <EOL> from nodeconductor . structure . utils import serialize_ssh_key , serialize_user <EOL> @ patch ( '<STR_LIT>' ) <EOL> class UserAndSshKeyPropagationTest ( test . APITransactionTestCase ) : <EOL> def setUp ( self ) : <EOL> self . owner = structure_factories . UserFactory ( ) <EOL> self . customer = structure_factories . CustomerFactory ( ) <EOL> self . customer . add_user ( self . owner , CustomerRole . OWNER ) <EOL> self . project = structure_factories . ProjectFactory ( customer = self . customer ) <EOL> self . links = [ self . create_service_project_link ( self . customer , self . project ) for i in range ( <NUM_LIT:3> ) ] <EOL> def create_service_project_link ( self , customer , project ) : <EOL> service_type = '<STR_LIT>' <EOL> models = SupportedServices . get_service_models ( ) [ service_type ] <EOL> class ServiceFactory ( factory . DjangoModelFactory ) : <EOL> class Meta ( object ) : <EOL> model = models [ '<STR_LIT>' ] <EOL> class ServiceProjectLinkFactory ( factory . DjangoModelFactory ) : <EOL> class Meta ( object ) : <EOL> model = models [ '<STR_LIT>' ] <EOL> settings = structure_factories . ServiceSettingsFactory ( customer = customer , type = service_type , shared = False ) <EOL> service = ServiceFactory ( customer = customer , settings = settings ) <EOL> return ServiceProjectLinkFactory ( service = service , project = project ) <EOL> def assert_task_called ( self , task , name , entity ) : <EOL> calls = [ call ( name , ( entity , link . to_string ( ) ) , { } , countdown = <NUM_LIT:2> ) for link in self . links ] <EOL> task . assert_has_calls ( calls , any_order = True ) <EOL> def test_create_and_delete_key ( self , mocked_task ) : <EOL> ssh_key = structure_factories . SshPublicKeyFactory ( user = self . owner ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> ssh_key . uuid . hex ) <EOL> self . client . force_authenticate ( self . owner ) <EOL> self . client . delete ( structure_factories . SshPublicKeyFactory . get_url ( ssh_key ) ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> serialize_ssh_key ( ssh_key ) ) <EOL> def test_delete_user ( self , mocked_task ) : <EOL> staff = structure_factories . UserFactory ( is_staff = True ) <EOL> self . client . force_authenticate ( staff ) <EOL> self . client . delete ( structure_factories . UserFactory . get_url ( self . owner ) ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> serialize_user ( self . owner ) ) <EOL> def test_grant_and_revoke_user_from_project ( self , mocked_task ) : <EOL> user = structure_factories . UserFactory ( ) <EOL> ssh_key = structure_factories . SshPublicKeyFactory ( user = user ) <EOL> self . project . add_user ( user , ProjectRole . ADMINISTRATOR ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> ssh_key . uuid . hex ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> user . uuid . hex ) <EOL> self . project . remove_user ( user ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> serialize_ssh_key ( ssh_key ) ) <EOL> self . assert_task_called ( mocked_task , <EOL> '<STR_LIT>' , <EOL> serialize_user ( user ) ) </s>
