|
| 1 | +from js2pysecrets.decorators import JsFunction, jsNeedless |
| 2 | +from js2pysecrets.wrapper import wrapper |
| 3 | + |
| 4 | +import js2pysecrets.node as node |
| 5 | +import pytest |
| 6 | +import warnings |
| 7 | + |
| 8 | + |
| 9 | +def not_supported(decorated_function): |
| 10 | + try: |
| 11 | + decorated_function() |
| 12 | + # If the function didn't raise an exception, return False |
| 13 | + return False |
| 14 | + except Exception as e: |
| 15 | + # If an exception was raised, ensure it matches the expected message |
| 16 | + expected_message = ( |
| 17 | + "Calling subsequent JavaScript functions are not supported. -or- " |
| 18 | + "The JavaScript function isn't necessary for the Python version." |
| 19 | + ) |
| 20 | + return str(e) == expected_message |
| 21 | + |
| 22 | + |
| 23 | +@JsFunction |
| 24 | +def invalidFunction(*args, **kwargs): |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +@jsNeedless |
| 29 | +def needless(*args, **kwargs): |
| 30 | + pass # pragma: no cover |
| 31 | + |
| 32 | + |
| 33 | +def test_JsFunction(): |
| 34 | + assert node.random(128) != node.random(32) |
| 35 | + |
| 36 | + |
| 37 | +def test_jsNeedless(): |
| 38 | + assert not_supported(lambda: needless(33, "blue")) |
| 39 | + |
| 40 | + |
| 41 | +def test_Distinct(): |
| 42 | + command = node.share("aabb", 6, 3, distinct=True) |
| 43 | + assert command == "share('aabb', 6, 3)" |
| 44 | + |
| 45 | + |
| 46 | +def test_Randomness(): |
| 47 | + count = 0 |
| 48 | + num_trials = 10 |
| 49 | + for _ in range(num_trials): |
| 50 | + # Simulate random behavior |
| 51 | + rand1 = node.random(16) |
| 52 | + rand2 = node.random(16) |
| 53 | + if int(rand1, 16) == int(rand2, 16): |
| 54 | + count += 1 |
| 55 | + assert count < num_trials, "Randomness test failed" |
| 56 | + |
| 57 | + |
| 58 | +def test_TestRNG(): |
| 59 | + count = 0 |
| 60 | + num_trials = 10 |
| 61 | + for _ in range(num_trials): |
| 62 | + # Simulate random behavior |
| 63 | + rand1 = node.random(16, test=True) |
| 64 | + rand2 = node.random(16, test=True) |
| 65 | + if int(rand1, 16) == int(rand2, 16): |
| 66 | + count += 1 |
| 67 | + assert count == num_trials, "Test RNG Failed" |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.filterwarnings("ignore:invalidFunction") |
| 71 | +def test_invalidFunction(): |
| 72 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 73 | + invalidFunction(1, 2, 3) |
| 74 | + # Check if any warnings were raised |
| 75 | + assert len(caught_warnings) == 1 |
| 76 | + assert issubclass(caught_warnings[0].category, Warning) |
| 77 | + assert "not a function" in str(caught_warnings[0].message) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.filterwarnings("ignore:node.share") |
| 81 | +def test_CalledProcessError(): |
| 82 | + with warnings.catch_warnings(record=True) as caught_warnings: |
| 83 | + node.share("hello world", 3, 3) |
| 84 | + # Check if any warnings were raised |
| 85 | + assert len(caught_warnings) == 1 |
| 86 | + assert issubclass(caught_warnings[0].category, Warning) |
| 87 | + assert "Invalid hex character" in str(caught_warnings[0].message) |
| 88 | + |
| 89 | + |
| 90 | +# @pytest.mark.filterwarnings("ignore:fail") |
| 91 | +# def test_JSONDecodeError(): |
| 92 | +# with warnings.catch_warnings(record=True) as caught_warnings: |
| 93 | +# node.fail() |
| 94 | +# # Check if any warnings were raised |
| 95 | +# assert len(caught_warnings) == 1 |
| 96 | +# assert issubclass(caught_warnings[0].category, Warning) |
| 97 | +# assert "error decoding JSON" in str(caught_warnings[0].message) |
| 98 | + |
| 99 | + |
| 100 | +# @pytest.mark.filterwarnings("ignore:wrapper") |
| 101 | +# def test_JSONDecodeError(): |
| 102 | +# with warnings.catch_warnings(record=True) as caught_warnings: |
| 103 | +# wrapper("hello world") |
| 104 | +# # Check if any warnings were raised |
| 105 | +# assert len(caught_warnings) == 1 |
| 106 | +# assert issubclass(caught_warnings[0].category, Warning) |
| 107 | +# assert "error decoding JSON" in str(caught_warnings[0].message) |
| 108 | +# # assert True |
0 commit comments