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_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 destroy(self, request, *args, **kwargs): """Delete a customer. @api {delete} /api/v1/customers/:uuid/ Delete a customer @apiName DeleteCustomers @apiGroup Customer @apiVersion 1.0.0 @apiDescription Delete a customer. @apiHeader {String} token Service Admin authorizaton token. @apiHeaderExample {json} Header-Example: { "Authorizaton": "Token 45138a913da44ab89532bab0352ef84b" } @apiParam {String} uuid Customer unique ID. @apiSuccessExample {json} Success-Response: HTTP/1.1 204 NO CONTENT """ user_savepoint = transaction.savepoint() customer_manager = None try: customer_manager = CustomerManager(kwargs['uuid']) except CustomerManagerDoesNotExist: LOG.error('Unable to find provider for uuid {}.'.format( kwargs['uuid'])) raise NotFound providers = ProviderManager.get_providers_queryset_for_customer( customer_manager.get_model()) try: customer_manager.remove_users(request.user) customer_manager.remove_tenant(request.user) except DatabaseError: transaction.savepoint_rollback(user_savepoint) LOG.error('Failed to remove assets for customer {}.'.format( customer_manager.get_name())) raise UserDeleteException try: for provider in providers: provider_manager = ProviderManager(provider.uuid) provider_manager.remove(request.user, customer_remove_context=True) except (DatabaseError, ProviderManagerError): transaction.savepoint_rollback(user_savepoint) LOG.error('{} failed to remove provider {}.'.format( request.user.username, provider_manager.get_name())) raise ProviderDeleteException http_response = super().destroy(request=request, args=args, kwargs=kwargs) if http_response.status_code is not 204: transaction.savepoint_rollback(user_savepoint) return http_response
def test_get_model(self): """Can the model object 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_model(), customer)