import json
import unittest

from app.terraform.list import obfuscator


class TestObfuscatingEncoder(unittest.TestCase):
    def test_obfuscating_string(self):
        data = "hello"
        obfs = obfuscator(data)
        print(f"Obfuscated string: {obfs}")
        j = json.dumps(obfs).replace("!AAA!", "\\u")
        loaded = json.loads(j)
        self.assertEqual(data, loaded)

    def test_obfuscating_simple_dict(self):
        data = {"hello": "world"}
        obfs = obfuscator(data)
        print(f"Obfuscated string: {obfs}")
        j = json.dumps(obfs).replace("!AAA!", "\\u")
        self.assertNotIn("hello", obfs)
        self.assertNotIn("world", obfs)
        loaded = json.loads(j)
        self.assertEqual(data, loaded)

    def test_obfuscating_simple_list(self):
        data = ["hello", "world"]
        obfs = obfuscator(data)
        print(f"Obfuscated string: {obfs}")
        j = json.dumps(obfs).replace("!AAA!", "\\u")
        self.assertNotIn("hello", obfs)
        self.assertNotIn("world", obfs)
        loaded = json.loads(j)
        self.assertEqual(data, loaded)

    def test_obfuscating_for_real(self):
        data = json.load(open("tests/list/mirrorSites.json"))
        obfs = obfuscator(data)
        j = json.dumps(obfs).replace("!AAA!", "\\u")
        print(f"Obfuscated string: {obfs}")
        for a in range(17, 27):
            self.assertNotIn(chr(a), j)
        loaded = json.loads(j)
        self.assertEqual(data, loaded)
