Merge pull request #406 from AlanCoding/variables_debt

Consolidation of variables parsing throughout codebase
This commit is contained in:
Alan Rominger
2017-10-13 15:40:36 -04:00
committed by GitHub
6 changed files with 102 additions and 95 deletions

View File

@@ -5,9 +5,13 @@
import os
import pytest
from uuid import uuid4
import json
import yaml
from django.core.cache import cache
from rest_framework.exceptions import ParseError
from awx.main.utils import common
from awx.main.models import (
@@ -38,6 +42,42 @@ def test_parse_yaml_or_json(input_, output):
assert common.parse_yaml_or_json(input_) == output
class TestParserExceptions:
@staticmethod
def json_error(data):
try:
json.loads(data)
return None
except Exception as e:
return str(e)
@staticmethod
def yaml_error(data):
try:
yaml.load(data)
return None
except Exception as e:
return str(e)
def test_invalid_JSON_and_YAML(self):
data = "{key:val"
with pytest.raises(ParseError) as exc:
common.parse_yaml_or_json(data, silent_failure=False)
message = str(exc.value)
assert "Cannot parse as" in message
assert self.json_error(data) in message
assert self.yaml_error(data) in message
def test_invalid_vars_type(self):
data = "[1, 2, 3]"
with pytest.raises(ParseError) as exc:
common.parse_yaml_or_json(data, silent_failure=False)
message = str(exc.value)
assert "Cannot parse as" in message
assert "Input type `list` is not a dictionary" in message
def test_set_environ():
key = str(uuid4())
old_environ = os.environ.copy()