def test_provider_org_fail(self, check_org_access): """Test creating a provider with AWS org access failure.""" check_org_access.return_value = False iam_arn = 'arn:aws:s3:::my_s3_bucket' bucket_name = 'my_s3_bucket' access_key_id, secret_access_key, session_token = _get_sts_access( iam_arn) s3_resource = boto3.resource( 's3', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, aws_session_token=session_token, ) s3_resource.create_bucket(Bucket=bucket_name) provider = {'name': 'test_provider', 'type': Provider.PROVIDER_AWS, 'authentication': { 'provider_resource_name': iam_arn }, 'billing_source': { 'bucket': bucket_name }} new_cust = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): new_cust = serializer.save() request = Mock() request.user = new_cust.owner context = {'request': request} serializer = ProviderSerializer(data=provider, context=context) if serializer.is_valid(raise_exception=True): serializer.save() check_org_access.assert_called_once()
def test_create_provider(self): """Test creating a provider.""" iam_arn = 'arn:aws:s3:::my_s3_bucket' bucket_name = 'my_s3_bucket' provider = {'name': 'test_provider', 'type': Provider.PROVIDER_AWS, 'authentication': { 'provider_resource_name': iam_arn }, 'billing_source': { 'bucket': bucket_name }} new_cust = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): new_cust = serializer.save() request = Mock() request.user = new_cust.owner context = {'request': request} instance = None with patch.object(ProviderAccessor, 'cost_usage_source_ready', returns=True): serializer = ProviderSerializer(data=provider, context=context) if serializer.is_valid(raise_exception=True): instance = serializer.save() schema_name = serializer.data['customer'].get('schema_name') self.assertIsInstance(instance.uuid, uuid.UUID) self.assertIsNone(schema_name) self.assertFalse('schema_name' in serializer.data['customer'])
def test_provider_s3_fail(self, check_s3_access): """Test creating a provider with AWS s3 bucket doesn't exist.""" check_s3_access.return_value = False iam_arn = 'arn:aws:s3:::my_s3_bucket' bucket_name = 'my_s3_bucket' provider = {'name': 'test_provider', 'type': Provider.PROVIDER_AWS, 'authentication': { 'provider_resource_name': iam_arn }, 'billing_source': { 'bucket': bucket_name }} new_cust = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): new_cust = serializer.save() request = Mock() request.user = new_cust.owner context = {'request': request} serializer = ProviderSerializer(data=provider, context=context) if serializer.is_valid(raise_exception=True): with self.assertRaises(serializers.ValidationError): serializer.save() check_s3_access.assert_called_once(iam_arn)
def test_removing_tenant(self): """Remove the tenant object for a customer.""" # Create customer. customer_name = 'test_customer_tenant' customer_json = {'name': customer_name, 'owner': self.gen_user_data()} customer = None serializer = CustomerSerializer(data=customer_json) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid # Create tenant customer_obj = Customer.objects.filter(name=customer_name).get() tenant = Tenant(schema_name=customer_obj.schema_name) tenant.save() # Get manager manager = CustomerManager(customer_uuid) customer_obj = manager.get_model() customer_schema_name = customer_obj.schema_name # Verify tenant is returned from manager tenant_obj = Tenant.objects.filter(schema_name=customer_schema_name).get() self.assertEqual(manager.get_tenant(), tenant_obj) # Attempt to remove as customer owner with self.assertRaises(CustomerManagerPermissionError): manager.remove_tenant(customer_obj.owner) # Attempt to remove tenant as super user superuser = User.objects.filter(is_superuser=True).first() manager.remove_tenant(superuser)
def test_get_tenant(self): """Get the tenant object for a customer.""" # Create customer. customer_name = 'test_customer_tenant' customer_json = {'name': customer_name, 'owner': self.gen_user_data()} customer = None serializer = CustomerSerializer(data=customer_json) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid # Create tenant customer_obj = Customer.objects.filter(name=customer_name).get() tenant = Tenant(schema_name=customer_obj.schema_name) tenant.save() # Get manager manager = CustomerManager(customer_uuid) customer_obj = manager.get_model() customer_schema_name = customer_obj.schema_name # Verify tenant is returned from manager tenant_obj = Tenant.objects.filter(schema_name=customer_schema_name).get() self.assertEqual(manager.get_tenant(), tenant_obj)
def test_is_removable_by_user(self): """Can current user remove provider.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() # Create Provider provider = Provider.objects.create(name='providername', created_by=customer.owner, customer=customer) provider_uuid = provider.uuid # Create another user for negative tests # Add another user group = Group.objects.get(name=customer.name) new_user_dict = self.gen_user_data() user_serializer = UserSerializer(data=new_user_dict) new_user = None if user_serializer.is_valid(raise_exception=True): new_user = user_serializer.save() group.user_set.add(new_user) manager = ProviderManager(provider_uuid) self.assertTrue(manager.is_removable_by_user(customer.owner)) self.assertFalse(manager.is_removable_by_user(new_user)) superuser = User.objects.filter(is_superuser=True).first() self.assertFalse(manager.is_removable_by_user(superuser))
def test_get_providers_queryset_for_customer(self): """Verify all providers returned by a customer.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() # Verify no providers are returned self.assertFalse(ProviderManager.get_providers_queryset_for_customer(customer).exists()) # Create Providers provider_1 = Provider.objects.create(name='provider1', created_by=customer.owner, customer=customer) provider_2 = Provider.objects.create(name='provider2', created_by=customer.owner, customer=customer) providers = ProviderManager.get_providers_queryset_for_customer(customer) # Verify providers are returned provider_1_found = False provider_2_found = False for provider in providers: if provider.uuid == provider_1.uuid: provider_1_found = True elif provider.uuid == provider_2.uuid: provider_2_found = True self.assertTrue(provider_1_found) self.assertTrue(provider_2_found) self.assertEqual((len(providers)), 2)
def test_get_users_for_customer(self): """Can current user remove provider.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid # Add another user group = Group.objects.get(name=customer.name) new_user_dict = self.gen_user_data() user_serializer = UserSerializer(data=new_user_dict) new_user = None if user_serializer.is_valid(raise_exception=True): new_user = user_serializer.save() group.user_set.add(new_user) manager = CustomerManager(customer_uuid) users = manager.get_users_for_customer() self.assertEqual(len(users), 2) new_user_found = False owner_found = False for found_user in users: if found_user.username == new_user_dict['username']: new_user_found = True elif found_user.username == customer.owner.username: owner_found = True self.assertTrue(new_user_found) self.assertTrue(owner_found)
def test_remove(self): """Remove provider.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() # Create Provider provider = Provider.objects.create(name='providername', created_by=customer.owner, customer=customer) provider_uuid = provider.uuid # Create another user for negative tests # Add another user group = Group.objects.get(name=customer.name) new_user_dict = self.gen_user_data() user_serializer = UserSerializer(data=new_user_dict) other_user = None if user_serializer.is_valid(raise_exception=True): other_user = user_serializer.save() group.user_set.add(other_user) manager = ProviderManager(provider_uuid) with self.assertRaises(ProviderManagerError): self.assertFalse(manager.remove(other_user)) manager.remove(customer.owner) provider_query = Provider.objects.all().filter(uuid=provider_uuid) self.assertFalse(provider_query)
def test_remove_users(self): """Remove users associated with a customer.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid # Add another user group = Group.objects.get(name=customer.name) new_user_dict = self.gen_user_data() user_serializer = UserSerializer(data=new_user_dict) new_user = None if user_serializer.is_valid(raise_exception=True): new_user = user_serializer.save() group.user_set.add(new_user) manager = CustomerManager(customer_uuid) # Attempt to remove as customer owner with self.assertRaises(CustomerManagerPermissionError): manager.remove_users(customer.owner) # Attempt to remove as regular user with self.assertRaises(CustomerManagerPermissionError): manager.remove_users(new_user) # Attempt to remove as super user superuser = User.objects.filter(is_superuser=True).first() manager.remove_users(superuser) self.assertFalse(manager.get_users_for_customer())
def test_create_provider_with_exception(self): """Test creating a provider with a provider exception.""" iam_arn = 'arn:aws:s3:::my_s3_bucket' bucket_name = 'my_s3_bucket' provider = { 'name': 'test_provider', 'type': Provider.PROVIDER_AWS, 'authentication': { 'provider_resource_name': iam_arn }, 'billing_source': { 'bucket': bucket_name } } new_cust = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): new_cust = serializer.save() request = Mock() request.user = new_cust.owner context = {'request': request} with patch.object(ProviderAccessor, 'cost_usage_source_ready', side_effect=serializers.ValidationError): ProviderSerializer(data=provider, context=context)
def test_uuid_field(self): """Test that a uuid is generated.""" # create the customer instance = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): instance = serializer.save() self.assertIsInstance(instance.uuid, uuid.UUID)
def test_get_name(self): """Can the customer name be returned.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid manager = CustomerManager(customer_uuid) self.assertEqual(manager.get_name(), self.customer_data[0]['name'])
def test_create_customer(self): """Test creating a customer.""" # create the customers for customer in self.customer_data: instance = None serializer = CustomerSerializer(data=customer) if serializer.is_valid(raise_exception=True): instance = serializer.save() self.assertEqual(customer['name'], instance.name) self.assertTrue( ResetToken.objects.filter(user=instance.owner).exists)
def test_create_customer(self): """Test creating a customer.""" # create the customers customer = self._create_customer_data() instance = None serializer = CustomerSerializer(data=customer) if serializer.is_valid(raise_exception=True): instance = serializer.save() schema_name = serializer.data.get('schema_name') self.assertIsNone(schema_name) self.assertFalse('schema_name' in serializer.data) self.assertEqual(customer['account_id'], instance.account_id) self.assertIsInstance(instance.uuid, uuid.UUID)
def test_get_tenant_not_found(self): """Try to get a missing tenant object for a customer.""" # Create customer. customer_name = 'test_customer_tenant' customer_json = {'name': customer_name, 'owner': self.gen_user_data()} customer = None serializer = CustomerSerializer(data=customer_json) if serializer.is_valid(raise_exception=True): customer = serializer.save() customer_uuid = customer.uuid # Get manager manager = CustomerManager(customer_uuid) self.assertEqual(manager.get_tenant(), None)
def test_get_name(self): """Can the provider name be returned.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() # Create Provider provider_name = 'sample_provider' provider = Provider.objects.create(name=provider_name, created_by=customer.owner, customer=customer) # Get Provider UUID provider_uuid = provider.uuid # Get Provider Manager manager = ProviderManager(provider_uuid) self.assertEqual(manager.get_name(), provider_name)
def test_remove(self): """Remove provider.""" # Create Customer customer = None serializer = CustomerSerializer(data=self.customer_data[0]) if serializer.is_valid(raise_exception=True): customer = serializer.save() # Create Provider provider_authentication = ProviderAuthentication.objects.create(provider_resource_name='arn:aws:iam::2:role/mg') provider_billing = ProviderBillingSource.objects.create(bucket='my_s3_bucket') provider = Provider.objects.create(name='providername', created_by=customer.owner, customer=customer, authentication=provider_authentication, billing_source=provider_billing) provider_uuid = provider.uuid # Create another user for negative tests # Add another user group = Group.objects.get(name=customer.name) new_user_dict = self.gen_user_data() user_serializer = UserSerializer(data=new_user_dict) other_user = None if user_serializer.is_valid(raise_exception=True): other_user = user_serializer.save() group.user_set.add(other_user) manager = ProviderManager(provider_uuid) with self.assertRaises(ProviderManagerError): self.assertFalse(manager.remove(other_user)) manager.remove(customer.owner) provider_query = Provider.objects.all().filter(uuid=provider_uuid) self.assertFalse(provider_query)