Beispiel #1
0
    def setUp(self):
        """Set up shared values for kafka creation tests."""
        self.account_number = str(_faker.pyint())
        self.username = _faker.user_name()
        self.authentication_id = _faker.pyint()
        self.application_id = _faker.pyint()
        self.source_id = _faker.pyint()
        self.cloudigrade_sources_app_id = _faker.pyint()
        (
            self.message,
            self.headers,
        ) = util_helper.generate_applicationauthentication_create_message_value(
            account_number=self.account_number,
            application_id=self.application_id,
            authentication_id=self.authentication_id,
        )

        self.arn = util_helper.generate_dummy_arn()

        self.auth_return_value = {
            "username": self.arn,
            "resource_type": settings.SOURCES_RESOURCE_TYPE,
            "resource_id": self.application_id,
            "id": self.authentication_id,
            "authtype": settings.SOURCES_CLOUDMETER_ARN_AUTHTYPE,
        }
Beispiel #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)
Beispiel #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()