Esempio n. 1
0
    def test_delete_from_sources_kafka_message_fail_missing_message_data(self):
        """Assert delete_from_sources_kafka_message fails from missing data."""
        self.assertEqual(CloudAccount.objects.count(), 1)

        message = {}
        headers = []
        tasks.delete_from_sources_kafka_message(message, headers)

        # Delete should not have been called.
        self.assertEqual(CloudAccount.objects.count(), 1)
Esempio n. 2
0
    def test_delete_from_sources_kafka_message_fail_wrong_account_number(self):
        """Assert delete fails from mismatched data."""
        self.assertEqual(CloudAccount.objects.count(), 1)
        account_number = _faker.user_name()
        application_id = _faker.pyint()
        authentication_id = _faker.pyint()
        application_authentication_id = _faker.pyint()
        (
            message,
            headers,
        ) = util_helper.generate_applicationauthentication_create_message_value(
            account_number,
            platform_id=application_authentication_id,
            application_id=application_id,
            authentication_id=authentication_id,
        )

        tasks.delete_from_sources_kafka_message(message, headers)

        # Delete should not have been called.
        self.assertEqual(CloudAccount.objects.count(), 1)
Esempio n. 3
0
    def test_delete_from_sources_kafka_message_application_authentication_success(
            self, mock_notify_sources):
        """Assert removing CloudAccount via ApplicationAuthentication.destroy."""
        self.assertEqual(CloudAccount.objects.count(), 1)
        self.assertEqual(aws_models.AwsCloudAccount.objects.count(), 1)

        account_number = str(self.user.username)
        (
            message,
            headers,
        ) = util_helper.generate_applicationauthentication_create_message_value(
            account_number,
            platform_id=self.application_authentication_id,
            application_id=self.application_id,
            authentication_id=self.authentication_id,
        )

        expected_logger_infos = [
            "INFO:api.tasks:delete_from_sources_kafka_message for account_number "
            f"{account_number}, platform_id {self.application_authentication_id}",
            "INFO:api.tasks:Deleting CloudAccounts using filter "
            f"(AND: ('platform_application_id', {self.application_id}), "
            f"('platform_authentication_id', {self.authentication_id}))",
        ]

        with patch.object(sts, "boto3") as mock_boto3, patch.object(
                aws_models, "_delete_cloudtrail"), self.assertLogs(
                    "api.tasks", level="INFO") as logging_watcher:
            role = util_helper.generate_dummy_role()
            mock_assume_role = mock_boto3.client.return_value.assume_role
            mock_assume_role.return_value = role

            tasks.delete_from_sources_kafka_message(message, headers)
            for index, expected_logger_info in enumerate(
                    expected_logger_infos):
                self.assertEqual(expected_logger_info,
                                 logging_watcher.output[index])
        self.assertEqual(CloudAccount.objects.count(), 0)
        self.assertEqual(aws_models.AwsCloudAccount.objects.count(), 0)
        mock_notify_sources.delay.assert_called()
Esempio n. 4
0
from django.conf import settings
from api.models import CloudAccount
from util.tests import helper as util_helper
from api.tasks import delete_from_sources_kafka_message

CloudAccount.objects.count()

CloudAccount.objects.first()

account_number = "1701"
user = util_helper.get_test_user(account_number)

auth_id = 10

message, headers = util_helper.generate_authentication_create_message_value(
    account_number, account_number, platform_id=auth_id)
delete_from_sources_kafka_message(message, headers,
                                  settings.AUTHENTICATION_DESTROY_EVENT)