예제 #1
0
    def test_serialization_fails_on_only_empty_name(self):
        """Test that an account is not saved if only name is empty."""
        mock_request = Mock()
        mock_request.user = util_helper.generate_test_user()
        context = {"request": mock_request}

        validated_data = {"account_arn": self.arn, "cloud_type": "aws"}

        with patch.object(
                aws, "verify_account_access") as mock_verify, patch.object(
                    aws.sts, "boto3") as mock_boto3:
            mock_assume_role = mock_boto3.client.return_value.assume_role
            mock_assume_role.return_value = self.role
            mock_verify.return_value = True, []

            serializer = CloudAccountSerializer(context=context,
                                                data=validated_data)
            serializer.is_valid()
            self.assertEquals("This field is required.",
                              str(serializer.errors["name"][0]))
예제 #2
0
    def test_serialization_fails_on_unsupported_cloud_type(self):
        """Test that account is not saved with unsupported cloud_type."""
        mock_request = Mock()
        mock_request.user = util_helper.generate_test_user()
        context = {"request": mock_request}

        bad_type = _faker.name()
        validated_data = {"cloud_type": bad_type, "name": _faker.name()}

        with patch.object(
                aws, "verify_account_access") as mock_verify, patch.object(
                    aws.sts, "boto3") as mock_boto3:
            mock_assume_role = mock_boto3.client.return_value.assume_role
            mock_assume_role.return_value = self.role
            mock_verify.return_value = True, []

            serializer = CloudAccountSerializer(context=context,
                                                data=validated_data)
            serializer.is_valid()
            self.assertEquals(
                f'"{bad_type}" is not a valid choice.',
                str(serializer.errors["cloud_type"][0]),
            )
예제 #3
0
    def test_serialization_fails_on_only_empty_account_arn(self):
        """Test that an AWS account is not saved if account_arn is empty."""
        mock_request = Mock()
        mock_request.user = util_helper.generate_test_user()
        context = {"request": mock_request}

        validated_data = {"cloud_type": "aws", "name": _faker.name()}

        with patch.object(
                aws, "verify_account_access") as mock_verify, patch.object(
                    aws.sts, "boto3") as mock_boto3:
            mock_assume_role = mock_boto3.client.return_value.assume_role
            mock_assume_role.return_value = self.role
            mock_verify.return_value = True, []

            serializer = CloudAccountSerializer(context=context,
                                                data=validated_data)
            serializer.is_valid()
            with self.assertRaises(ValidationError) as cm:
                serializer.create(validated_data)
            self.assertEquals(
                "This field is required.",
                str(cm.exception.detail["account_arn"]),
            )